MATLAB: PushButton repeat previously generated audio sample (with other pushbutton)

audiorepeat

I will need to use the script below for a listening test. is still not as I need it and the deadline is approaching so I decided to come here and find out if someone can help. (Thanks in advance!)
Information about the script: I am using GUI; among other tools that are not relevant for this specific problem I have included 2 pushbuttons which are causing me some problems. One pushbutton is supposed to play one audio sample (randomly) out of 3 existing audio samples. The other pushbutton is supposed to play the sample again if the listener needs it.
My problems are due to this:
1- Each sample needs to be played randomly but the user needs to be able to listen to it again if he wants. I would like to use the "REPEAT" button for that.
2- I am using a different GUI for each of the three audio samples that will be evaluated by the listener (probably the worst idea ever as the GUIs are exactly the same, but it's how I managed to make it work so far). Each GUI has a "NEXT" button to close the old GUI and open the next one.
When the user changes from the first GUI to the second, the previously evaluated sample, must not be included in the random operation as they can't evaluate the same audio sample twice during the test.
——> I am not being able to solve these two issues and the deadline is almost here. PLEASE I need some help.
Thank you very much!
This is the code I am using for the pushbutton PLAY at the moment:
a = audioread('guitar.wav');
b = audioread('cinematic.wav');
c = audioread('epicorchestra.wav');
numsounds = 3;
sounds = { a; b; c;}
orders=randperm(numsounds,3); % random order
%pick one of the positions in "orders"
location = randi(length(orders));
% extract wave data from line identified by "locations" in column one.
wavData = sounds{location, 1};
% sampling frequency (same for all audio files)
sf = 48000
% play audio sample with that wavdata and corresponding sampling
% frequency
soundsc(wavData, sf);

Best Answer

Ana - since using GUIDE, you can Store or retrieve UI data with the handles structure (this structure is passed into each callback). First though, you may want to use this structure to manage the names of the wav file. In the GUI's OpeningFcn you could do something like
handles.myWavFiles = {'guitar.wav', 'cinematic.wav', 'epicorchestra.wav'};
guidata(hObject, handles);
The call to guidata is important as it will ensure that the handles object is updated with the array of wav files so that all subsequent callbacks have access to this updated object.
In the pushbutton callback to play one of the random assets, you could do
if ~isempty(handles.myWavFiles)
handles.wavFileIndex = randi(length(handles.myWavFiles),1,1); % a random file to play
guidata(hObject, handles); % save the index of the file that will be played
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex};
reader = audioread(wavFileToPlay);
% etc.
end
Note how we save the index of the file that we are going to play to the handles structure. This way, we have access to file in the repeat button callback.
An alternative to using sound is to use the audioplayer object which offers a little more control over for the audio playback.
In the repeat button callback, you would do
if isfield(handles, 'wavFileIndex')
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex};
reader = audioread(wavFileToPlay);
% play the sound
end
The above code should allow you to randomly choose a wav file (to play) and then repeat the last played file.
As for "moving to the next GUI", I would recommend a different approach. When the user presses the NEXT button, just update your list of played wav files by removing the one that has already been played. That way you don't have to worry about passing data from one GUI to the next. The NEXT pushbutton callback might have code like
if isfield(handles, 'wavFileIndex')
handles.myWavFiles(handles.wavFileIndex) = []; % we are removing this file from the list
guidata(hObject, handles);
end
Then, when the user presses the play button, your code will randomly select a file from that shorter list. (See how we have the if ~isempty(handles.myWavFiles) check in that callback to ensure that we don't try to play something from an empty list.)
Related Question