problem with play, not stopping @ 10 seconds

7 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

Geoff Hayes
Geoff Hayes il 27 Nov 2014
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 Commenti
James Wayne
James Wayne il 11 Dic 2014
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));
Geoff Hayes
Geoff Hayes il 11 Dic 2014
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?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Measurements and Spatial Audio in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by