Playing sound one after another, according to number inputed

12 次查看(过去 30 天)
if I have multiple sounds that already have specific tones and they're named: y0,y1,y2,y3,y4,y5,y6,y7,y8,y9
how can I make them play one after another without overlapping? the input is random so if the user types in:1,2,3,4 then the sounds y1,y2,y3,and y4 should be played one after the other. Thank you!
[Merged from duplicate question]
prompt = 'Enter atleast 10 numbers: ';
str='0';
while length(regexp(str,'\d'))~=10
str = input(prompt,'s');
end
N=length(str);
count=1;
for k=1:N
m=str2double(str(k));
if isnan(m)==0
str(count)=k;
count=count+1;
numbers = cell2mat(sscanf((str),'k'))
disp(m)
end
end
%sound 1
fs=8192;
T=.5;
t=0:(1/fs):T;
y1=cos(2*pi*1209*t)+cos(2*pi*697*t);
sound(y1,fs);
pause(.5);
%sound 2
fs=8192;
T=.5;
t=0:(1/fs):T;
y2=cos(2*pi*1336*t)+cos(2*pi*697*t);
sound(y2,fs);
pause(.5);
%sound 3
fs=8192;
T=.5;
t=0:(1/fs):T;
y3=cos(2*pi*1477*t)+cos(2*pi*697*t);
sound(y3,fs);
pause(.5);
%sound 4
fs=8192;
T=.5;
t=0:(1/fs):T;
y4=cos(2*pi*1209*t)+cos(2*pi*770*t);
sound(y4,fs);
pause(.5);
%sound 5
fs=8192;
T=.5;
t=0:(1/fs):T;
y5=cos(2*pi*1336*t)+cos(2*pi*770*t);
sound(y5,fs);
pause(.5);
%sound 6
fs=8192;
T=.5;
t=0:(1/fs):T;
y6=cos(2*pi*1477*t)+cos(2*pi*770*t);
sound(y6,fs);
pause(.5);
%sound 7
fs=8192;
T=.5;
t=0:(1/fs):T;
y7=cos(2*pi*1209*t)+cos(2*pi*852*t);
sound(y7,fs);
pause(.5);
%sound 8
fs=8192;
T=.5;
t=0:(1/fs):T;
y8=cos(2*pi*1336*t)+cos(2*pi*852*t);
sound(y8,fs);
pause(.5);
%sound 9
fs=8192;
T=.5;
t=0:(1/fs):T;
y9=cos(2*pi*1477*t)+cos(2*pi*852*t);
sound(y9,fs);
pause(.5);
%sound 0
fs=8192;
T=.5;
t=0:(1/fs):T;
y0=cos(2*pi*1336*t)+cos(2*pi*941*t);
sound(y0,fs);
pause(.5);
this is my whole code and I'm having trouble figuring out how I can get one sound to play after the other depending on what the user inputs so if the user inputs: 4560987321 then I want it to play the sounds in order (y4,y5,y6,y0,y9,y8,y7,y3,y2,y1) thank you!

回答(2 个)

Star Strider
Star Strider 2016-10-26
You need to use the playblocking function to prevent them from overlapping. This is part of the audioplayer functions, so you should have access to it. See the documentation for details.

Massimo Zanetti
Massimo Zanetti 2016-10-26
编辑:Massimo Zanetti 2016-10-26
Just compute the time T (secs) the file lasts by using sample rate information and number of samples. Then, pause the for loop for the number of seconds (plus 1 if you want delay time of 1 sec. in between). For randomness, arrange files in array and randomize on indeces:
s(1)=load('gong.mat','y','Fs');
s(2)=load('handel.mat','y','Fs');
randsel = randperm(numel(s));
for x=s(randsel)
sound(x.y,x.Fs);
T = numel(x.y)/x.Fs;
pause(T+1);
end
  2 个评论
Valeria Chacon
Valeria Chacon 2016-10-26
I understand but how could I get the sounds to play in consecutive order if I get an input like: 5910 and each number has it's own sound
Massimo Zanetti
Massimo Zanetti 2016-10-27
Are you asking how to transform number (say) 5910 into array [5,9,1,0]?

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by