how can i play sounds in specific order with specific durations
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
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
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.
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!