Create musical notes with pauses in between

Dear community,
I am interested in creating a simple alarm tone using MATLAB and would like to know how to put various lengths of pauses in between notes.
Currently i have the following. How can i add pauses between the notes 'C' 'd' 'g' 'C' 'd'?
notes={'C' 'c' 'd' 'e' 'f' 'g'}
freq=[523.251 261 294 330.00 349.00 392]
song={'C' 'd' 'g' 'C' 'd' }
dur=[0.125 0.125 0.125 0.06 0.5]

Answers (1)

10 Comments

Hi Cris, thanks for the note. However, I do not find relevant information on putting pauses between the notes. Could you please enlighten me on that?
Use a for loop to play the notes one at a time, and add a pause after each note has been played. The pause should be at least as long as the duration value, or the notes will play on top of each other.
Hi Cris, thanks for the response. I tried using pause() in loop but it seems to mess up with the original tune. Could you please share some insights?
notes={'C' 'c' 'd' 'e' 'f' 'g'}
freq=[523.251 261 294 330.00 349.00 392]
deltaT = 1 / (20*min(freq))
song={'C' 'd' 'g' 'C' 'd' } % your song
dur=[0.5 0.5 0.5 0.5 0.5]
a=[]
for k = 1 : numel(song)
fprintf('In iteration %d, looking for note %s in notes.\n', k, song{k});
index = find(ismember(notes, song{k}))
thisFrequency = freq(index)
numberOfSoundSamples = dur(k) / deltaT
t = linspace(0, dur(k), numberOfSoundSamples);
a=[a sin(2 * pi * thisFrequency * t)]; % sinusoidal
%soundsc(a)
%pause(1)
end
audiowrite('alarm.wav',a,8192)
I am now considering adding an "artificial note" with frequency outside of human hearing range as substitute for pause
Could you explain what you mean by "mess up with the original tune"?
I would modify your code by specifying a sample frequency instead of a period. Then, when playing the sound and when writing the audio, use the specified sample frequency to ensure the timing is preserved.
Here's how I might do it.
notes={'C' 'c' 'd' 'e' 'f' 'g'}
freq=[523.251 261 294 330.00 349.00 392]
Fs = 20*min(freq);
song={'C' 'd' 'g' 'C' 'd' } % your song
dur=[0.5 0.5 0.5 0.5 0.5]
a=[]
for k = 1 : numel(song)
index = find(ismember(notes, song{k}));
numberOfSoundSamples = dur(k) * Fs;
t = linspace(0, dur(k), numberOfSoundSamples);
a=[a sin(2 * pi * freq(index) * t)]; % sinusoidal
end
sound(a,Fs)
audiowrite('alarm.wav',a,Fs)
Thanks for your reply! The modification is helpful.
I intend to have a 1s pause between the sequences 'C' 'd' 'g' 'C' 'd' and 'C' 'd' 'g' 'C' 'd' (that is repeat the sequence twice with a pause in the middle). I have trouble creating that "pause".
When I use the pause(1) function in the loop as below, it does not produce the 'C' 'd' 'g' 'C' 'd' sequence and instead plays something confusing.... Hope you can shed some light on this!
for k = 1 : numel(song)
index = find(ismember(notes, song{k}));
numberOfSoundSamples = dur(k) * Fs;
t = linspace(0, dur(k), numberOfSoundSamples);
a=[a sin(2 * pi * freq(index) * t)]; % sinusoidal
sound(a)
pause(1)
end
The first issue is the placement of the pause, then. This pause is happening after every note. However, the code does not wait for sound(a) to finish playing before moving on to the pause. They are executed near simultaneously. Your pause should be equal to the duration of a plus one more second.
Also, pause pauses execution of the code. It does nothing to affect the values of a.
" the code does not wait for sound(a) to finish playing before moving on to the pause" Thanks so much! That is enlightening! I will try harder.
One more question though: is there a function that can determine the duration of a? If not I would try hardcode it
Duration is dependent on the sample frequency, which could be anything. You could try setting the sample frequency rather than defining it as the min of freq.
Try this.
Fs = 8000;
...
for k = 1 : numel(song)
index = ismember(notes, song{k});
t = 0:1/Fs:dur(k);
a=[sin(2 * pi * freq(index) * t)]; % sinusoidal
sound(a,Fs)
pause(dur(k))
end

Sign in to comment.

Tags

Asked:

on 5 Jan 2021

Edited:

on 5 Jan 2021

Community Treasure Hunt

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

Start Hunting!