music piece creation with matlab

27 views (last 30 days)
Giovanni Scamardo
Giovanni Scamardo on 5 Apr 2022
Answered: Image Analyst on 6 Apr 2022
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.

Answers (3)

Star Strider
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.
.

Jan
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
Giovanni Scamardo
Giovanni Scamardo on 5 Apr 2022
I have these notes to reproduce: F_A = 220; F_Asharp = 233.08; F_B = 246.94; F_C = 261.63; F_Csharp = 277.18; F_D = 293.66; F_Dsharp = 311.13; F_E = 329.63; F_F = 349.23; F_Fsharp = 369.99; F_G = 392; F_Gsharp = 415.3; how do i put rests between notes?
Jan
Jan on 6 Apr 2022
Rests can be implemente by a pause command or by a signal with 0 values.

Sign in to comment.


Image Analyst
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!