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

2 Ansichten (letzte 30 Tage)
Tanmayee Pathre
Tanmayee Pathre am 14 Feb. 2022
Kommentiert: David Hill am 14 Feb. 2022
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?

Antworten (1)

David Hill
David Hill am 14 Feb. 2022
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 Kommentare
Tanmayee Pathre
Tanmayee Pathre am 14 Feb. 2022
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);
David Hill
David Hill am 14 Feb. 2022
Yep, need to pass your signals to the function
function Start(sig_conv_WB,sig_conv_HybIR_125,sig_conv_HybIR_250)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by