music piece creation with matlab
27 views (last 30 days)
Show older comments
I am carrying out a project where I should synthesize a piece of music where each musical note can be represented by a specific frequency.
where each note has a frequency equal to 2 ^ (1/12). assuming that the notes are in the octave which contains the frequencies in the range (220hz-440hz)
the question is how I can construct a discrete time signal of the notes of a piece of music. Use a sampling rate of 8000 samples per second and insert rests between notes.
and also how do I digitally shift up or down an octave, adjusting the duration of each note accordingly.
finally I should convert the volume of each note to a decrease over time, to make the music more realistic.
0 Comments
Answers (3)
Star Strider
on 5 Apr 2022
I provided an outline on how to do exactly that in my Answer to your previous question: music piece with matlab .
Just change to :
A3 = 220;
A4 = 440;
A5 = 880;
Notes = A3*2^(1/12).^linspace(0, 11, 12); % Note Frequencies
Fs = 8000; % Sampling Frequency
t = linspace(0, Fs-1, Fs)/Fs; % Time Vector
NotesMatrix = sin(2*pi*t(:)*Notes); % Matrix Of One-Second Notes
for k = 1:size(NotesMatrix,2)
soundsc( NotesMatrix(:,k), Fs)
pause(1.1)
end
The code works. You will need to make the appropriate changes to make it work with your project.
.
0 Comments
Jan
on 5 Apr 2022
Edited: Jan
on 5 Apr 2022
You create the signal using sin() or cos() commands. 220Hz at 8000 Hz sampling frequency:
F = 8000;
t = linspace(0, 1, F); % 1 second
f = 220;
A = 1.0; % Amplitude
y = A * sin(2 * pi * f * t);
sound(y, F)
Shifting up by an octave means multiplying f by a factor of 2.
Decreasing the volume over the time:
A = linspace(1.0, 0.5, F); % From full to half
y = A .* sin(2 * pi * f * t); % .* is elementwise multiplication
"insert rests between notes" - I'm sure you are able to implement this.
2 Comments
Image Analyst
on 6 Apr 2022
I attach a demo where I create a warbling sound. Adapt it as needed, or ask a question about it if you have any.
0 Comments
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!