Making a GUI for playback of wav files

조회 수: 3 (최근 30일)
Marius Tøndel Eliassen
Marius Tøndel Eliassen 2020년 3월 6일
댓글: Marius Tøndel Eliassen 2020년 3월 6일
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
Marius Tøndel Eliassen
Marius Tøndel Eliassen 2020년 3월 6일
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

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

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 3월 6일
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
Geoff Hayes
Geoff Hayes 2020년 3월 6일
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 2020년 3월 6일
Works now:) I have been working all day for a solution so, Geoff many thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by