Make music with matlab

1. Some basics of music

1. Know numbered musical notation

The most important information in the numbered notation is the melody and beat, which are located in the upper left corner of the numbered notation.

1=G in the picture is based on the G tone as the reference frequency, that is, 1 corresponds to the G tone, and other common ones include 1=C, etc.

4/4 is four 1/4 beats in one section, and one section is separated by a short vertical line. There are also 3/4 rows, 1/2 beats, etc. similarly.

 The corresponding numbers are different audio frequencies with 1 as the base frequency

0 beat means placeholder, no sound

Numbers with # in the upper left corner are half-tones

The dots above and below the number represent an increase or decrease of one octave, and there are also two dots, which correspond to a decrease of two octaves.

In the 4/4 beat spectrum, a section consists of four 1/4 beats, each 1/4 beat is 1 second, a single number corresponds to a 1/4 beat, and an underline under the number indicates that this sound occupies 1/8 Beat, two means 1/16 beat, and so on... The sum of rows in a section is always equal to the number of beats corresponding to this spectrum. The above example is 4/4 beat, which is 4 seconds.

The dot on the right side of the number means that the sound is extended by 1/2 beat, such as 1· means 3/8 beat, and the - after the number means that it is extended by 1/4 beat.

The upper connection symbol between the numbers means that the transition is smooth and coherent, which generally needs to be considered when singing.

2. Basic tune frequency

According to the calculation of the twelve equal temperament, the corresponding frequencies of the following different tunes are obtained

 It means to vibrate at this frequency to make a sound, and the tone produced is the corresponding tone.

It is worth noting that an octave corresponds to an octave relationship. This is also due to the law of twelve average rate calculations.

According to this, the frequency corresponding to each number can be determined

Corresponding frequency on the basis of G key

g0 means placeholder, g1_ means falling tone, g2_5 means #2

g0=0;2
g1_=195.998;
g2_=220.000;
g3_=246.942;
g4_=261.626;
g5_=293.665;
g6_=329.628;
g7_=349.228;
g1=391.995;
g2=440;
g2_5=415.305;
g3=493.883;
g4=523.251;
g4_5=554.365;
g5=587.330;
g5_5=622.254;
g6=659.255;
g7=698.456;
g33=987.767;

2. Use matlab to generate sound

1. Music frequency, sampling frequency, beat

Use a sine wave with a frequency such as 220hz for a period of time

The frequency of this wave is the frequency of the tone, the frequency of each sampling point represents the sampling frequency, and the duration is the time of the beat

The sampling frequency is generally much higher than the pronunciation frequency to ensure no distortion.

 So we can create a matrix with a frequency of 220, a duration of 1s, and a sampling frequency of 8000

fs=8000;
f=220;
x=linspace(0,1,fs);
y=sin(2*pi*f*x);
plot(x,y);
sound(y,fs);

It can be seen that a wave with a frequency of 220hz, a duration of 1s, and a sampling frequency of 8000 is generated.

Just keep changing the frequency and beat (duration), and a simple music can be played.

2. Play a piece of music

f1=[g0 g0 g0 g5_];
t1=[1 1 1 1];
f2=[g3 g2_5 g3 g6];
t2=[1.5 0.25 0.75 1.5];
f3=[g5 g3 g6_];
t3=[0.75 2.25 1];
f45=[g1 g2 g3 g5 g4_5 g5 g5_5 g5];
t45=[2 0.75 1.75 0.5 0.5 0.5 1.5 0.5];
f67=[g6 g33 g0];
t67=[1.5 4.5 2];
f0=[f1 f2 f3 f45 f67];
t0=[t1 t2 t3 t45 t67];
for i=1:size(f0,2)
    x=linspace(0,t0(i),8000*t0(i));
    y=sin(2*pi*f0(i)*x);
    sound(y,8000);
    pause(t0(i));
end

 We use two one-dimensional matrices to store pitch and beat information, which requires a one-to-one correspondence! Otherwise, the playback matrix will not match.

The sound function plays, and the pause function delays for a period of time.

3. Add an envelope to the music

 It can be heard that there is always noise during the transition in this piece of music, which affects the quality of the music, so we are caused by phase discontinuity . Our solution is to add an envelope to the sound

Explanation on the envelope signal (10 messages) Using the envelope function to optimize the sound function of the sound function in MATLAB [matlab tuning_2]_Waibubabi's blog-CSDN blog_Piano envelope function

The expression form of the last x/exp(-kx/rhythm) type envelope function is wrong or incomplete.

modify here

 

 The above formula is a complete envelope function, where t represents the takt time.

The modified envelope function solves the problem of different volumes caused by different beat durations and k values

 its shape is

 

 Obviously, this makes the sound more mellow. Compared with the traditional exponential envelope, there is a process of rising first, which can ensure that the energy of the previous note disappears before the subsequent note is issued, which can effectively eliminate the slight noise when the notes alternate.

The value of k here determines the speed of the drop, and it can be adjusted according to different scores. Generally speaking, the larger the value of k, the clearer the sound

4. Complete code

clc;clear all
fs=8000;
k=10;
k0=5;
g0=0;
g1_=195.998;
g2_=220.000;
g3_=246.942;
g4_=261.626;
g5_=293.665;
g6_=329.628;
g7_=349.228;
g1=391.995;
g2=440;
g2_5=415.305;
g3=493.883;
g4=523.251;
g4_5=554.365;
g5=587.330;
g5_5=622.254;
g6=659.255;
g7=698.456;
g33=987.767;
%前奏
f1=[g0 g0 g0 g5_];
t1=[1 1 1 1];
f2=[g3 g2_5 g3 g6];
t2=[1.5 0.25 0.75 1.5];
f3=[g5 g3 g6_];
t3=[0.75 2.25 1];
f45=[g1 g2 g3 g5 g4_5 g5 g5_5 g5];
t45=[2 0.75 1.75 0.5 0.5 0.5 1.5 0.5];
f67=[g6 g33 g0];
t67=[1.5 4.5 2];

f0=[f1 f2 f3 f45 f67];
t0=[t1 t2 t3 t45 t67];
d=0.2;
for n=1:size(f0,2)
    x=linspace(0,t0(n),t0(n)*fs);
    if n==1
        m0=d*exp(1)*k0*x.*exp((-k0/t0(n))*x).*sin(2*pi*f0(n)*x);
        %m=k*x.*exp(-k*x).*sin(2*pi*f0(n)*x);
        %m0=exp(-k0*x).*sin(2*pi*f0(n)*x); 
    else 
        if n==size(f0,2)-1
            m0=[m0 d*exp(1)*2/t0(n)*x.*exp((-4/t0(n))*x).*sin(2*pi*f0(n)*x)]; 
        else
            m0=[m0 d*exp(1)*k0/t0(n)*x.*exp((-k0/t0(n))*x).*sin(2*pi*f0(n)*x)];
            %m=[m k*x.*exp(-k*x).*sin(2*pi*f0(n)*x)];
            %m0=[m0 exp(-k0*x).*sin(2*pi*f0(n)*x)];
        end
    end
end
sound(m0,fs*2)
plot(linspace(0,sum(t0),sum(t0)*fs),m0)
pause(sum(t0)/2-2)
%主调
f7=[g0 g0 g0 g5_ g6_ g1];
t7=[1 1 0.5 0.5 0.5 0.5];
f8=[g5 g3 g0 g5 g2 g1];
t8=[0.5 1.5 0.5 0.5 0.5 0.5];
f9=[g3 g2 g0 g5 g2 g1];
t9=[0.5 1.5 0.5 0.5 0.5 0.5];
f10=[g2 g2 g0 g3 g1 g6_];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3 g2 g3 g2];
t26=[0.5 0.25 0.25 0.5 0.5 0.5 0.25 0.25 0.5 0.5];
f27=[g3 g3 g2 g3 g2 g1 g0 g3 g4];
t27=[0.5 0.25 0.25 0.5 0.5 1 0.5 0.25 0.25];
f28=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t28=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f29=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t29=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f30=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t30=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f31=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t31=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f32=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t32=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f33=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t33=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f34=[g5 g5 g3 g5 g5 g5 g3 g5 g6 g5 g5];
t34=[0.5 0.25 0.25 0.5 0.5 0.25 0.25 0.25 0.25 0.5 0.5];
f35=[g3 g5 g3 g2 g1 g5_ g1 g1 g1 g2];
t35=[0.125 0.375 0.5 0.5 0.5 0.5 0.5 0.5 0.25 0.25];
f36=[g3 g3 g2 g3 g3 g2 g3 g3 g2 g3 g4 g3 g2];
t36=[0.5 0.25 0.25 0.5 0.25 0.25 0.5 0.25 0.25 0.25 0.25 0.25 0.25];
f37=[g1 g1 g2 g1 g7_ g6_ g0 g2 g3];
t37=[0.5 0.25 0.25 0.5 0.5 1 0.5 0.25 0.25];
f38=[g4 g4 g3 g4 g4 g3 g4 g4 g3 g4 g5 g4 g3];
t38=[0.5 0.25 0.25 0.5 0.25 0.25 0.5 0.25 0.25 0.25 0.25 0.25 0.25];
f39=[g2 g2 g1 g7_ g1 g2 g5_ g1 g2];
t39=[0.5 0.25 0.25 0.5 0.5 0.5 0.5 0.5 0.5];
f40=[g3 g3 g2 g3 g3 g2 g3 g3 g2 g3 g4 g3 g2];
t40=[0.5 0.25 0.25 0.5 0.25 0.25 0.5 0.25 0.25 0.25 0.25 0.25 0.25];
f41=[g1 g1 g2 g1 g7_ g6_ g0 g2 g3];
t41=[0.5 0.25 0.25 0.5 0.5 1 0.5 0.25 0.25];
f42=[g4 g4 g3 g4 g4 g3 g4 g4 g3 g4 g5 g4 g3];
t42=[0.5 0.25 0.25 0.5 0.25 0.25 0.5 0.25 0.25 0.25 0.25 0.25 0.25];
f43=[g5 g3 g2 g1 g5_ g1 g1];
t43=[0.5 0.5 0.5 0.5 0.5 0.5 1];

f=[f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 f21 f22 f23 f24 f25 f26 f27 f28 f29 f30 f31 f32 f33 f34 f35 f36 f37 f38 f39 f40 f41 f42 f43];
t=[t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24 t25 t26 t27 t28 t29 t30 t31 t32 t33 t34 t35 t36 t37 t38 t39 t40 t41 t42 t43];

for n=1:size(f,2)
    x=linspace(0,t(n),t(n)*fs);
    if n==1
        m=exp(1)*k/t(n)*x.*exp((-k/t(n))*x).*sin(2*pi*f(n)*x);
        %m=k*x.*exp(-k*x).*sin(2*pi*f(n)*x);
        %m=exp(-k*x).*sin(2*pi*f(n)*x);
    else
        m=[m exp(1)*k/t(n)*x.*exp((-k/t(n))*x).*sin(2*pi*f(n)*x)];
        %m=[m k*x.*exp(-k*x).*sin(2*pi*f(n)*x)];
        %m=[m exp(-k*x).*sin(2*pi*f(n)*x)];
    end
end
sound(m,fs*2)

 

Guess you like

Origin blog.csdn.net/npu_noj/article/details/128515299
Recommended