Playing examples of matlab

Posted by tamilmani on Mon, 07 Mar 2022 21:22:21 +0100

  • Welcome to WeChat's official account: Thirteen dream, discuss more interesting and interesting.

There is a rumor in the Jianghu that there is nothing he can't do except having children.

It's hard to say that. Sooner or later, you have to have a baby.

I don't want to listen to my favorite music with musical instruments today. Let matlab do it for me.

To be able to play, you must first have some knowledge of music theory.

1. Law of twelve averages

The law of twelve averages is the cornerstone of learning music theory. This kind of law is very popular, and now most music is compiled according to this law system.

The twelve average law is to divide the region from frequency f to double frequency 2f into twelve frequency bands, that is, there are thirteen frequency points. It should be noted that this division is not equal difference sequence division, but equal ratio sequence division.

The value of the nth frequency ratio is obtained according to the knowledge of the frequency sequence:

So the first frequency point

Second frequency point

Until the thirteenth frequency point

As soon as we looked at it, we evenly divided an octave, white key and black key on the piano keyboard.

More simply, we can directly find the frequency we need by comparing the pitch and frequency comparison table of the twelve average rhythm written by our predecessors.

Find your own reference audio rate (I chose Alto do, that is, c1)

So obviously, the waveform of midrange do can be expressed as: sin(2pi261.63 *t), t is the length.

2. Length and beat

The length setting mainly follows the whole note, half note, quarter note, eighth note and so on. (I chose to use the 16th minute note as the benchmark, and set some basic sound lengths accordingly, which can be added later if necessary)

So I was going to check the professional length theory, and suddenly found out. It has been more than ten years since I last read the staff.

Unexpectedly, after looking at the staff, I can only count the notes by breaking my fingers

I can't say I used to play the piano anymore

Direct up note:

This little tadpole is still a little familiar.

According to my setting (16 diaeresis as the benchmark t16), the sound length of other notes should also meet the following requirements:

So when the problem comes, t16 how can we determine it?

Let's first look at a short piece of Bach's music score, which is famous for its precise logic and strict dryness:

Seeing such a score, only three big words echoed in my mind: for cycle!

This neat degree, if Bach carries handwritten code, it is definitely a great God!!

Returning to the theme, the content in the red box on the left side of the score indicates that the beat of the music is four or four beats (denominator 4 indicates that one beat is based on a quarter note, numerator 4 indicates that four beats are output per section), and the red box on the top indicates that 112 beats are output per minute, so the duration of each beat should be 60 / 112 ≈ 0.54s.

In this way, the reference length can be set directly according to the standard score. However, many popular music scores do not mark the duration of each shot, so they can only be set by feeling.

This part of the code is attached:

fs = 44100;
dt = 1/fs;
T16 = 0.2;
t16 = [0:dt:T16];
[temp k] = size(t16);
t1 = linspace(0,16*T16,8*k); %whole note
t2 = linspace(0,8*T16,8*k); %2 Diaeresis
t3 = linspace(0,7*T16,7*k); %7/16 beat
t4 = linspace(0,4*T16,4*k); %4 Diaeresis
t6 = linspace(0,3*T16,3*k); %3/16 beat
t8 = linspace(0,2*T16,2*k); %8 Diaeresis

The sampling frequency fs is selected as standard, and the CD lattice is 44.1k.

3. Prepare output

Then we will find that there is basically music output at this time. However, the sound is not smooth, and the playback of each sound will usher in an abrupt sound change process.

It's hot!

Why!

Think about it, maybe it's because of the normal attenuation of sound wave vibration?

Well, it's ok to add an attenuation to the output!

One line of code, say add!

type1 = sin(pi*t1/t1(end));
type2 = sin(pi*t2/t2(end));
type3= sin(pi*t3/t3(end));
type4 = sin(pi*t4/t4(end));
type6 = sin(pi*t6/t6(end));
type8 = sin(pi*t8/t8(end));
type16 = sin(pi*t16/t16(end));

Add the set attenuation to the output:

do0l = type4.*sin(2*pi*Sound(21)*f0*t4);
re0l = type4.*sin(2*pi*Sound(22)*f0*t4);
mi0l = type4.*sin(2*pi*Sound(23)*f0*t4);
fa0l = type4.*sin(2*pi*Sound(1)*f0*t4);
so0l = type4.*sin(2*pi*Sound(2)*f0*t4);
la0l = type4.*sin(2*pi*Sound(3)*f0*t4);
si0l = type4.*sin(2*pi*Sound(4)*f0*t4);
do1l = type4.*sin(2*pi*Sound(5)*f0*t4);
blkl = zeros(1,i);

In this way, a reduced tone within an octave plus a homophonic pause is set~

f0 is the selected reference audio rate, and the setting of Sound is the equal scale factor in the twelve average law. (in fact, you can write directly according to the table, but I'm too lazy to check the Sound one by one. Let matlab calculate it by himself. Anyway, he can do everything except having children.)

The next step is the last step. It's perfect to output notes in turn according to the melody of Xiaoao Jianghu you want!

Why did you choose Xiaoao Jianghu? It's mainly Huang laozhan's song Taiji, which is simple.

Within two octaves, between the Gong Shang horn and the Zheng Yu, it is worthy of being a ghost.

Oh, the book is true. That's the output of music.

mymusic1 = [blkn...
la1l so1s mi1n re1n do1l blkl...
mi1l re1s do1n la0n so0l blkl...
so0l la0s so0n la0n do1l re1s mi1n so1n la1l so1s mi1s re1s do1n re1l blkl...
la1n la1s so1s mi1n re1n do1l blkl...
mi1n mi1s re1s do1n la0n so0l blkl...
so0l la0s so0n la0n do1l re1s mi1n so1n la1n la1s so1s mi1s re1s do1n do1l blkl...
];
​
s = mymusic1;
​
sound(s,fs);
audiowrite('mymusic1.wav',mymusic1,fs)

Among them, the choice of sound length depends on the feeling (well, in fact, I'm too lazy to find the score)

You can further improve according to your own needs. If you output performance, try to find a reliable score on the Internet. Come on, simple score is also OK

Also welcome to discuss more interesting things~

Scan code or search for attention ↓↓↓↓↓↓ view more

Topics: MATLAB