how can i play sounds in specific order with specific durations

24 views (last 30 days)
i need to let matlab play the happy birthday song. i already downloaded all the notes as wav files and i also put them in order in a list with the order of the duration.
duration=[0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 1, 3, 0.5, 0.5, 1, 1, 1, 2];
song=[si,si,do,si,mi,re,si,si,do,si,fa,mi,si,si,si,sol,mi,re,do,la,la,sol,mi,fa];
for exemple i need to play the note (si) first with a duration of (0.5 sec).
can anyone help?

Answers (1)

Spectro
Spectro on 30 Nov 2021
Edited: Spectro on 30 Nov 2021
Use a forcycle to run through all the notes with the appropriate durations using audioread. Also adjust each sound vector in dependence on the duration. See the idea below:
for i = 1:length(song)
% Load the note
[y, fs] = audioread('yourNote.wav');
% Example of y2 for halft the duration (0.5)
n = length(y);
y2 = y(1:fix(n*0.5), :);
% Play it
soundsc(y2, fs);
end
  3 Comments
Giovanni Azar
Giovanni Azar on 30 Nov 2021
the duration should change each time according to the note that is playing from the list
Spectro
Spectro on 1 Dec 2021
The code was just a clue to lead you on. In that loop, you have to load the specific note in each iteration and adjust its playing duration. It's playing with the indexes. You already have an example for 0.5.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!