Making a GUI for playback of wav files

3 views (last 30 days)
I want to make a GUI were the listener is able to audition two separate sounds (wave files). Something like in the attached image. E.g. 1 = "reference", 2 = "comparison". When the listener hit pushbuton 1, he or she will hear the reference, when hitting pushbotton 2 he or she hear the comparison. Maybe with separate start and stop pusbuttons.
Is it best to you App designer for this?
  1 Comment
Marius Tøndel Eliassen
Marius Tøndel Eliassen on 6 Mar 2020
I have come so far with my code, but no sound yet. What's next?
%User interface for audition of reference and comparison intervals
f = figure;
fp1 = uipanel('parent',f,'Title','Reference','fontSize',18,'Position',[.170 .50 .200 .200]);
fp2 = uipanel('parent',f,'Title','Comparison', 'fontSize',18,'Position',[.620 .50 .200 .200]);
[y1,Fs] = audioread('Reference_iem_test_0.wav');
[y2,Fs] = audioread('Variable_iem_test_4_0.wav');
pushbutton1 = uicontrol('Style','pushbutton','String','Play','Position',[90 100 50 50]);
pushbutton2 = uicontrol('Style','pushbutton','String','Stop','Position',[170 100 50 50]);
pushbutton3 = uicontrol('Style','pushbutton','String','Play','Position',[340 100 50 50]);
pushbutton4 = uicontrol('Style','pushbutton','String','Stop','Position',[420 100 50 50]);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global file_name;
if(~ischar(file_name))
else
if handles.status==0
handles.status=1;
[y1,Fs]=audioread(file_name);
handles.r=audioplayer1(y1,Fs);
play(handles.r);
else
warndlg({'Warning: Play Is Already In Progress.';' To Begin A New Play You Must First Stop The Current Session.'})
end
guidata(hObject,handles);
end
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.status==1
handles.status=0;
[y1,Fs]=audioread(file_name);
handles.r=audioplayer1(y1,Fs);
stop(handles.r);
else
errordlg('Warning: To Stop, Play Must Be In Progress.')
end
guidata(hObject,handles);
end
% --- Executes on button press in pushbutton1.
function pushbutton3_Callback(hObject, eventdata, handles)
global file_name;
if(~ischar(file_name))
else
if handles.status==0
handles.status=1;
[y2,Fs]=audioread(file_name);
handles.r=audioplayer2(y2,Fs);
play(handles.r);
end
guidata(hObject,handles);
end
end
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.status==1
handles.status=0;
[y2,Fs]=audioread(file_name);
handles.r=audioplayer2(y2,Fs);
stop(handles.r);
else
errordlg('Warning: To Stop, Play Must Be In Progress.')
end
guidata(hObject,handles);
end

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Mar 2020
Marius - you seem to be programmatically creating your GUI and throwing in GUIDE-style callbacks. You need to assign the callbacks to the controls in order for them to fire. For example, to assign a callback to pushbutton1 you could do
pushbutton1 = uicontrol('Style','pushbutton','String','Play','Position',[90 100 50 50], 'Callback', @pushbutton1_Callback);
and then the signature of this callback would be
function pushbutton1_Callback(hObject, eventdata)
Note how handles has been removed since that is a GUIDE structure. It each callback seems to reference a global variable named file_name yet no where do I see this is defined. If you want to reference the audio files that you have already read (Reference_iem_test_0.wav and Variable_iem_test_4_0.wav), then create a main function that creates your GUI and nest the callbacks within it. For example, if you modify your code slightly to
function MyAudioPlayer
f = figure;
fp1 = uipanel('parent',f,'Title','Reference','fontSize',18,'Position',[.170 .50 .200 .200]);
fp2 = uipanel('parent',f,'Title','Comparison', 'fontSize',18,'Position',[.620 .50 .200 .200]);
[y1,Fs1] = audioread('Reference_iem_test_0.wav');
[y2,Fs2] = audioread('Variable_iem_test_4_0.wav');
player1 = audioplayer1(y1,Fs1);
player2 = audioplayer1(y2,Fs2);
uicontrol('Style','pushbutton','String','Play','Position',[90 100 50 50], 'Callback', @pushbutton1_Callback);
uicontrol('Style','pushbutton','String','Stop','Position',[170 100 50 50], 'Callback', @pushbutton2_Callback);
uicontrol('Style','pushbutton','String','Play','Position',[340 100 50 50], 'Callback', @pushbutton3_Callback);
uicontrol('Style','pushbutton','String','Stop','Position',[420 100 50 50], 'Callback', @pushbutton4_Callback);
function pushbutton1_Callback(hObject, eventdata)
% start player1
end
function pushbutton2_Callback(hObject, eventdata)
% stop player1
end
function pushbutton3_Callback(hObject, eventdata)
% start player2
end
function pushbutton4_Callback(hObject, eventdata)
% stop player2
end
end
and save to a file named MyAudioPlayer.m, then you should be able to start and stop the audio players as desired (I've left that code to you). Because the callbacks are nested within the main function, then the callbacks have access to the local variables declared in the main function (i.e. the player1 and player2 objects).
  3 Comments
Geoff Hayes
Geoff Hayes on 6 Mar 2020
Marius - did you update my code (replacing the comments like % start player1) with the commands to start and stop the audio players? Can you post your updated code?
Marius Tøndel Eliassen
Marius Tøndel Eliassen on 6 Mar 2020
Works now:) I have been working all day for a solution so, Geoff many thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!