Playing 3 different audio samples using 3 different buttons on GUI?

I have 3 different convolved signals which I would like to play out using 3 different buttons using callbacks using UI control. I have following code for creating the GUI and playing out the signals. When I press the first button, pushbutton1_Callback needs to play sound 1 and print "is working". In the commnad window, "is working" is printed but I get the following error message:
Undefined function or variable 'player1'.
Error in playaudio/playaudio/pushbutton1_Callback (line 95)
play(player1);
player1 = audioplayer(sig_conv_WB,fs);
player2 = audioplayer(sig_conv_HybIR_125,fs);
player3 = audioplayer(sig_conv_HybIR_250,fs);
this.wavebased = uicontrol(hfig,'style','push',...
'position',[sl*0.35 sh*0.85 sl*0.3 sh*0.1],... %[sl*0.2 (sh/2)+(sh*0.2) sl*0.6 sh*0.1]
'string','Wave Based','fontsize',16,...
'callback',@pushbutton1_Callback);
this.hybrid125 = uicontrol(hfig,'style','push',...
'position',[sl*0.35 sh*0.70 sl*0.3 sh*0.1 ],... %[sl*0.2 (sh/2)+(sh*0.2) sl*0.6 sh*0.1]
'string','Hybrid 125','fontsize',16,...
'callback',@pushbutton2_Callback);
this.hybrid250 = uicontrol(hfig,'style','push',...
'position',[sl*0.35 sh*0.55 sl*0.3 sh*0.1 ],... %[sl*0.2 (sh/2)+(sh*0.2) sl*0.6 sh*0.1]
'string','Hybrid 250','fontsize',16,...
'callback',@pushbutton3_Callback);
function pushbutton1_Callback(this, ~, ~)
disp('is working')
play(player1);
end
function pushbutton2_Callback(this, ~, ~)
play(player2);
end
function pushbutton3_Callback(this, ~, ~)
play(player3);
end
Can someone help me solve this?

답변 (1개)

David Hill
David Hill 2022년 2월 14일
You need to pass player1, player2, and player3 to your functions. One way is to make the functions part of an overall function.
function Start()
player1 = audioplayer(sig_conv_WB,fs);
player2 = audioplayer(sig_conv_HybIR_125,fs);
player3 = audioplayer(sig_conv_HybIR_250,fs);
hfig=figure;
sl=500;sh=300;
this.wavebased = uicontrol(hfig,'style','push',...
'position',[sl*0.35 sh*0.85 sl*0.3 sh*0.1],... %[sl*0.2 (sh/2)+(sh*0.2) sl*0.6 sh*0.1]
'string','Wave Based','fontsize',16,...
'callback',@pushbutton1_Callback);
this.hybrid125 = uicontrol(hfig,'style','push',...
'position',[sl*0.35 sh*0.70 sl*0.3 sh*0.1 ],... %[sl*0.2 (sh/2)+(sh*0.2) sl*0.6 sh*0.1]
'string','Hybrid 125','fontsize',16,...
'callback',@pushbutton2_Callback);
this.hybrid250 = uicontrol(hfig,'style','push',...
'position',[sl*0.35 sh*0.55 sl*0.3 sh*0.1 ],... %[sl*0.2 (sh/2)+(sh*0.2) sl*0.6 sh*0.1]
'string','Hybrid 250','fontsize',16,...
'callback',@pushbutton3_Callback);
function pushbutton1_Callback(this, ~, ~)
display(player1);
end
function pushbutton2_Callback(this, ~, ~)
display(player2);
end
function pushbutton3_Callback(this, ~, ~)
display(player3);
end
end

댓글 수: 2

Hello. Thanks for the answer. But it doesnt work. I get the following error message.
Undefined function or variable 'sig_conv_WB'.
Error in Start (line 2)
player1conv = audioplayer(sig_conv_WB,fs);
Yep, need to pass your signals to the function
function Start(sig_conv_WB,sig_conv_HybIR_125,sig_conv_HybIR_250)

댓글을 달려면 로그인하십시오.

카테고리

질문:

2022년 2월 14일

댓글:

2022년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by