problem with play, not stopping @ 10 seconds

Hi, im having an issue with the play not stopping after 10 seconds.
I have created a GUI which has x2 play, pause, resume, stop. Named as A/B
here is a snippet of my code
Load A
% --- Executes on button press in load_sound.
function load_a_Callback(hObject, eventdata, handles)
global signal;
global FS;
% hObject handle to load_sound (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.wav','select a wave file to load');
if filename== 0
return;
end
set(handles.txt_sound_one,'String',[pathname filename]);
% check file is selected
[signal,FS] = wavread([pathname filename]);
signal = 0.99*signal/max(abs(signal));
% plot to sound_one axes
axes(handles.sound_one);
plot_sig(signal);
Play A
% --- Executes on button press in play_a.
function play_a_Callback(hObject, eventdata, handles)
% hObject handle to play_a (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal;
global FS;
global player;
player=audioplayer(signal,FS);
play(player);
pause(10);
Load B
% --- Executes on button press in load_b.
function load_b_Callback(hObject, eventdata, handles)
global signal1
global FS1;
% hObject handle to load_b (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.wav','select a wave file to load');
if filename== 0
return;
end
set(handles.txt_sound_two,'String',[pathname filename]);
% check file is selected
[file,FS1] = wavread([pathname filename]);
signal1 = 0.99*file/max(abs(file));
% plot to sound_one axes
axes(handles.sound_two);
plot_sig(signal1);
play B
% --- Executes on button press in play_b.
function play_b_Callback(hObject, eventdata, handles)
% hObject handle to play_b (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global signal1;
global FS1;
global player;
player=audioplayer(signal1,FS1);
play(player);
pause(10);

 Accepted Answer

James - the reason that the audio isn't stopping after ten seconds is because your player has been declared as a global variable. So even though the function exits after ten seconds, the audio continues.
You could try the replacing the play and pause with playblocking which will allow you to play a certain number of samples of audio before releasing control (and allow the processing to continue). For example, you could replace
player=audioplayer(signal,FS);
play(player);
pause(10);
with
player=audioplayer(signal,FS);
playblocking(player,[1 10*FS]);
Since there are FS samples per second, then we want to play (and block) from the first sample to 10*FS.
Of course, you may not need to declare the player as a global variable and so that would be another way to fix the problem. In fact, you don't have to use global variables at all, but make use of the handles structure which is also intended to manage user-defined data which you can use to store the signal and FS data (and signal1 and FS1) variables. In your load_a_Callback function, you could do
function load_a_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile('*.wav','select a wave file to load');
if filename== 0
return;
end
set(handles.txt_sound_one,'String',[pathname filename]);
% check file is selected
[signal,FS] = wavread([pathname filename]);
signal = 0.99*signal/max(abs(signal));
% plot to sound_one axes
axes(handles.sound_one);
plot_sig(signal);
% save the data to handles
handles.signal = signal;
handles.FS = FS;
guidata(hObject,handles);
The last three lines of code are important - we add the data to the two new fields within handles, and then call guidata to save the updated handles structure.
In the play_a function, we then access this data through handles as
function play_a_Callback(hObject, eventdata, handles)
if isfield(handles,'signal')
player=audioplayer(handles.signal,handles.FS);
playblocking(player,[1 10*handles.FS]);
end
Try the above and see what happens!

19 Comments

James' answer moved here
So I dont quite understand if im going the right direction for my other buttons(pause, resume, stop). As the complexity of the code above is a huge jump for me.
Im basically storing the data of signal and FS into a handles struct but to call do I use another isfield function?
As before I just used global variable e.g.
global player;
stop(player);
or
global player;
resume(player);
James - you could store the player in the handles structure too. In the (for example) pause button callback, you could do something like
if isfield(handles,'player')
if strcmpi(get(handles.player,'Running'),'on')
% player is running so pause it
pause(handles.player);
end
end
I have used your code but the pause dosent work. I look at a video about handles but still unsure what your snippet of code is doing.
this part is confusing part
if isfield(handles,'player')
if strcmpi(get(handles.player,'Running'),'on')
James - you will either have to post the code or describe what you mean by the pause doesn't work. Are you stepping through the code in the debugger and verifying that this code is not executing? Have you added the recorder to the handles structure in playa_Callback as
function play_a_Callback(hObject, eventdata, handles)
if isfield(handles,'signal')
handles.player=audioplayer(handles.signal,handles.FS);
guidata(handles,hObject);
playblocking(handles.player,[1 10*handles.FS]);
end
If you are going to retrieve the player from handles, then you have to set it too...
James Wayne
James Wayne on 28 Nov 2014
Edited: James Wayne on 28 Nov 2014
what I meant was when I click on button function pause_a_Callback it dosent pause the audio.
I have not stepped through the debugger to verify the code is executing or not, however when I click on Pause A no error shows up in the command window. Also I have not added a recorder to my handle as I dont know where to start.
just tried the debugged - I set my debugger to start @ line 105 which is
if isfield(handles,'signal')
in the compiler it displays
105 if isfield(handles,'signal')
and dosent go any further if I click step.
Also I have not added a recorder to my handle as I dont know where to start.
Add the recorder to the handle at the point where you have instantiated/created the recorder which will be the point where the audio starts/plays. That would be in the play_a_Callback and the play_b_Callback.
That being said, if you only play ten seconds of audio which is play blocking, how do you expect to pause it as the audio will already be finished? Are you wanting to pause it during the ten seconds or at some other time?
Right, what I doing before I got into this was playing the music x amount of seconds using the global variable and can pause/resume where it was left off, which worked, but this in depth coding has got me scratching my head. As you said I can increase the duration e.g. 1 40*handles.FS]), but I doubt that will help. What I really want is to play/pause/resume within the x seconds.
Then try replacing the
playblocking(player,[1 10*handles.FS]);
with just
play(player,[1 10*handles.FS]);
Since you are no longer blocking, then any other "event" such as pressing the pause button can occur and should allow the player to be paused.
The idea with the playblocking was to prevent any other action from occurring i.e. all other push button events or whatever would have had to wait until the audio had finished.
ive removed playblocking and used the play(player,[1 10*handles.FS]); however when I click play the sound dosent play, I ran it through the debugger
if isfield(handles,'signal')
player=audioplayer(handles.signal,handles.FS);
play(player,[1 10*handles.FS]);
end
at line 93 - if isfield(handles,'signal') is shown on the debugger but it skips line 94 and 95, and when the debugger hits end the music plays for 10 second but not when im actually pressing the button on the GUI.
Are you sure that you have saved your changes to the GUI before putting the breakpoint and pressing run? It isn't clear to me why the debugger would skip the lines and then play the audio. Can you attach your code?
James Wayne
James Wayne on 28 Nov 2014
Edited: James Wayne on 28 Nov 2014
I have fixed the issue and the play,pause,resume and stop works on A/B buttons , thanks for your feedback. The only issue I have now is joining the 2 audio files together.
I created something like this:
handles.signal_mix = [handles.signal;handles.signal1]; guidata(hObject,handles);
**
in here I dont know what to type.
**
In my previous attempt i got an audio to play but it only played A.
So you have joined the two audio signals together. Have you verified that they have the same sample rate? If so, then you should be able to just play this combined audio signal in a manner similar to what you have done for A or B. But if the two signals don't have the same sample rate, then you will have to resample. If you have access to the Signal Processing Toolbox, you could use its resample function.
James's Answers moved here:
Thanks for the help, I was thinking about moving the global variables away but werent too sure which option to take. This has help me a lot, Thank you.
So I dont quite understand if im going the right direction for my other buttons(pause, resume, stop). As the complexity of the code above is a huge jump for me.
Im basically storing the data of signal and FS into a handles struct but to call do I use another isfield function?
As before I just used global variable e.g.
global player;
stop(player);
or
global player;
resume(player);
Forgot to reply back, ive fixed my issue.
I'm trying to incorporate the audio duration onto a static text box, I've tried this code but it dosent show up the duration.
nSamples = signal();
fileTime = nSamples / FS;
% compute file time (minutes, seconds, dseconds):
fileHours = floor(fileTime/3600);
fileMinutes = floor(fileTime/60);
fileSeconds = mod(fileTime, 60);
fileDecSeconds = round(10*(fileSeconds - floor(fileSeconds)));
fileSeconds = floor(fileSeconds);
set(handles.info2, 'String', sprintf('%.2d:%.2d:%.2d.%d',fileHours,fileMinutes, fileSeconds, fileDecSeconds));
I have got the FS to show up in a static box but have no idea how to get the duration to work properly
James - put a breakpoint at the line
fileTime = nSamples / FS;
and then run your GUI and follow whatever steps are needed to get to this point where the above code will fire (not sure which callback this is from). Then when the debugger pauses at the line with the breakpoint, start stepping through the code and use the Command Window to type in these variables
nSamples
FS
fileTime
fileHours
etc.
Do you observe values for each? When you get to the line that sets the string, paste the line
sprintf('%.2d:%.2d:%.2d.%d',fileHours,fileMinutes, fileSeconds, fileDecSeconds)
in the Command Window. What do you see? With dummy data, I have verified that the above works and produces a string, so there must be something going wrong in the code that prevents the set from happening.
I fixed my issue with this:
End=length(signal);
nSamples = End;
fileTime = nSamples / FS;
% compute file time (minutes, seconds, dseconds):
fileMinutes = floor(fileTime/60);
fileSeconds = mod(fileTime, 60);
fileDecSeconds = round(10*(fileSeconds - floor(fileSeconds)));
fileSeconds = floor(fileSeconds);
%display file info:
set(handles.info1, 'String', sprintf('%d Hz',FS));
set(handles.info2, 'String', sprintf('%.2d:%.2d.%d',fileMinutes, fileSeconds, fileDecSeconds));
James - what do you mean by my next issue is trying to get the audio input seconds to work? You need to describe the problem that you are facing in more detail. Have you tried stepping through the code in the debugger?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!