I need to create matlab code to play the Happy birthday tune, but at the moment it all plays together as a weird noise.

42 Ansichten (letzte 30 Tage)
I have it set up as an array of the notes, and then a function calls in a loop which is supposed to be played and generate the sound, but I'm not sure what's not working.
CODE:
Fs=1046; %Sampling rate
%frequency of note
a=[262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349];
%duration of note
b=[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];
%loop to play sound for all 25 notes, then end
for i=1:25
[x, t] = gen_cos(a(i), 1, b(i), 0, 1/Fs); %gencos function
soundsc(x, Fs);
i=i+1;
end
FUNCTION:
function [x,t] = gen_cos(f, A, d, p, s)
% GEN_COS Function to generate sinuisoidal signal
% of a given frequency, amplitude, phase and duration
% SYNTAX
% [x,t] = gen_cos(f, A, d, p, s)
%
% PARAMETERS
% f: frequency in Hertz
% A: amplitude
% d: duration in seconds
% p: phase in radian
% s: sampling step
%
% EXAMPLE
% Frequency = 10 Hz, Amplitude = 2, Phase = 0,
% Duration = 0.5s, Sampling step = 1/100
% [x,t] = gen_cos(10, 2, 0.5, 0, 1/100);
% plot(t,x);
t = 0:s:d; % time from 0 to d in step of s
x = A*cos(2*pi*f*t + p); % signal

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 4 Aug. 2016
Bearbeitet: Geoff Hayes am 4 Aug. 2016
Daniel - the problem with using soundsc (or sound) is that it doesn't block while playing. So as soon as you call this function (the next to last line in your for loop) then it does start to play the first note but then the next iteration of the for loop executes. And because there is no pause, the second note will start to play over the first. And the third over the second and first, etc.
So you could add a pause to your for loop to ensure that the next iteration doesn't start for that duration of the note
for i=1:25
[x, t] = gen_cos(a(i), 1, b(i), 0, 1/Fs);
soundsc(x, Fs);
pause(b(i));
% i=i+1; <-- this isn't necessary as the for loop handles the incrementing of i
end
While the above does work, the result is kind of "scratchy" and will not guarantee that one note won't overlap another.
An alternative is to build the array of sample data (for each note) and play it once outside of the for loop
Y = [];
for i=1:25
[y, t] = gen_cos(a(i), 1, b(i), 0, 1/Fs);
Y = [Y , y];
end
soundsc(Y,Fs);
Finally, an alternative to using soundsc (or sound) is to use the audioplayer. This is more versatile and allows you to play and play while blocking
Y = [];
for i=1:25
[y, t] = gen_cos(a(i), 1, b(i), 0, 1/Fs);
Y = [Y , y];
end
player = audioplayer(Y,Fs);
playblocking(player);

Weitere Antworten (1)

houssam chihane
houssam chihane am 1 Jan. 2022
Fs=1046; %Sampling rate
%frequency of note
a=[262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349];
%duration of note
b=[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];
%loop to play sound for all 25 notes, then end
for i=1:25
[x, t] = gen_cos(a(i), 1, b(i), 0, 1/Fs); %gencos function
soundsc(x, Fs);
i=i+1;
end
FUNCTION:
function [x,t] = gen_cos(f, A, d, p, s)
% GEN_COS Function to generate sinuisoidal signal
% of a given frequency, amplitude, phase and duration
% SYNTAX
% [x,t] = gen_cos(f, A, d, p, s)
%
% PARAMETERS
% f: frequency in Hertz
% A: amplitude
% d: duration in seconds
% p: phase in radian
% s: sampling step
%
% EXAMPLE
% Frequency = 10 Hz, Amplitude = 2, Phase = 0,
% Duration = 0.5s, Sampling step = 1/100
% [x,t] = gen_cos(10, 2, 0.5, 0, 1/100);
% plot(t,x);
t = 0:s:d; % time from 0 to d in step of s
x = A*cos(2*pi*f*t + p); % signal

Community Treasure Hunt

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

Start Hunting!

Translated by