Some people say: MATLAB In addition to children, the other will.
Then use it to create music?
The answer is yes
First of all we need to know what we have heard DO RE MI, is how did it? What is the difference between that?
Music as " the wave of a" naturally can be superimposed with a sine wave to simulate different frequencies can issue notes of different frequencies, so we just use MATLAB to generate a series of sine function, coupled with the built-in MATLAB ** sound () ** function can be.
Music composed mainly by the tone and tempo, the music function requires two inputs, i.e., tone (Tone) and beat (Rythm)
are now given function modules:
Fs of the sampling frequency, general default 8192Hz, that is, a second sampling frequency;
linspace (a, b, c) a function for uniformly generating a to b and c array;
freqs array corresponding to a different tone frequency, only lists the common simplicity;
Note: frequency of play should be consistent with the sampling frequency! ! !
function y = gen_wave( tone, rythm )
%UNTITLED2 音乐函数 对应music2
% 音调 拍
Fs = 8192;
freqs = [523, 587, 659, 698, 783, 880, 988];
x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));
y = sin(freqs(tone) * x);
end
New code to perform the following main functions DO emitted sound.
Fs = 8192;
y=[];
y = gen_wave(1,1);
sound(y,Fs);
Execution is completed with ** plot () ** function can draw a sound image, as shown below
can be seen very dense, after enlarged is:
First, it can be seen, after amplification, the value is not continuous, then the sound has been a constant , which are not consistent and we actually, the piano sound when pressed should be just the largest, followed over time, the decline of the sound, so we multiplied by a function y in the back to make it decay, code show as below:
function y = gen_wave( tone, rythm )
% 音调 拍
Fs = 8192;
freqs = [523, 587, 659, 698, 783, 880, 988];
x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));
y = sin(freqs(tone) * x) .*(1- x/(rythm * 2 *pi));
end
Rerun DO tone, and draw an image obtained:
enlarged fragmentary
can be seen that the oscillation damping, of course, be multiplied by an exponential decay function, this will give a more realistic sound.
Then we can find a music full of music, such as " Little Star "
we can generate an array of music on behalf of this song according to the notation, the code is as follows:
Fs = 8192;
y=[];
music = [1,1,5,5,6,6,5,...
4,4,3,3,2,2,1,...
5,5,4,4,3,3,2,...
5,5,4,4,3,3,2,...
1,1,5,5,6,6,5,...
4,4,3,3,2,2,1];
music_length = length(music(:));
y1 = gen_wave(1,1);
y2 = gen_wave(2,1);
y3 = gen_wave(3,1);
y4 = gen_wave(4,1);
y5 = gen_wave(5,1);
y6 = gen_wave(6,1);
y7 = gen_wave(7,1);
for i = 1:1:music_length
if music(i) == 1
y=[y,y1];
elseif music(i) == 2
y=[y,y2];
elseif music(i) == 3
y=[y,y3];
elseif music(i) == 4
y=[y,y4];
elseif music(i) == 5
y=[y,y5];
elseif music(i) == 6
y=[y,y6];
elseif music(i) == 7
y=[y,y7];
end
end
sound(y, Fs);
Music by traversing the array, the subroutine, we can generate an array of its scale, and then sound () to play.
If you feel useful, like a point of it, O (∩ _ ∩) O haha ~