PushButton repeat previously generated audio sample (with other pushbutton)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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);
2 Comments
Geoff Hayes
on 24 Jun 2019
Ana - you haven't indicated how you have created your GUIs: with GUIDE, App Designer, or programmatically. This is important because there will be slightly different ways to do what you want...
Ana Campos
on 24 Jun 2019
Sorry! I am using GUIDE:) cheers
Accepted Answer
Geoff Hayes
on 24 Jun 2019
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.)
23 Comments
Ana Campos
on 24 Jun 2019
Thank you SO SO much for your answer and help :) I will do what you told me to and hopefully I won’t need to nag you again with it. Thanks again!
Ana Campos
on 26 Jun 2019
Hello, something strange is happening with the code. Audioread is reading the right file (one of those three) according to what i can see on workspace. But, when i play it, the sound does not correspond to none of my samples. I don't even know what that audio is as Ive never heard it before from my laptop. Any thoughts on what might be happening please? cheers
Geoff Hayes
on 26 Jun 2019
Ana - I would have to see the code that you are using to play the audio file. Are you using sound or an audioplayer? Perhaps you are not using the correct sampling rate when calling either of these functions.
Ana Campos
on 26 Jun 2019
That was it Geoff!! the sampling rate!!! thanks! such a legend! Thank you very much for your massive help!
Geoff Hayes
on 26 Jun 2019
glad to have been able to help! :)
Ana Campos
on 26 Jun 2019
Edited: Ana Campos
on 26 Jun 2019
It's me again. sorry. I was using a pop-up menu to "register" the answers from the subjects of the experiment and it was working. But i had to change the popup menu for a slider and I am not used to deal with it in matlab so I am having some troubles.
I need to allow the user to select a position of the slider that corresponds to a rating scale. the scale has the following values: 1 2 3 4 5
I need a code that allows them to rate an audio sample using the slider with those rating scale positions and then store their answers on a .txt file.
The thing is, the .txt file is storing the position of the slider every time it is being moved.
I could use some help to fix this. Thanks again :)
This is the code I am using now:
% --- Executes on slider movement.
function slider5_Callback(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
sliderMin = 1
sliderMax = 5
sliderStep = [1, 1]/(sliderMax - sliderMin);
set(handles.slider5, 'Min', sliderMin);
set(handles.slider5, 'Max',sliderMax);
set(handles.slider5, 'SliderStep' , sliderStep);
subjectSliderStep1 = get(handles.slider5, 'Value');
filename = 'listeningtest_data1.txt';
attribute = 1.1
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',subjectSliderStep1,'-append')
Geoff Hayes
on 26 Jun 2019
Ana - the code to set the min and max for the slider should occur in the OpeningFcn of your GUI (since you won't want to do this when the slider callback fires). So just move the code that function.
As for the txt file was storing the position of the slider every time it was being moved, what should happen? Doesn't the text file get updated only when the user stops scrolling? Or is it being continually updated as the user moves the slider back and forth?
Ana Campos
on 26 Jun 2019
Edited: Ana Campos
on 27 Jun 2019
Thanks again! Regarding the txt:
at the moment, if the user moves the slider 10times, the txt is storing all the 10 positions and thats not what i need.
I need the txt to store only one value for each of the 3 sliders. That value should correspond to the position where the user left it for the last time he moved each of the sliders. i only need one rating per slider for each audio sample.
I will present them with 3 sliders. Each of those three sliders is supposed to rate each audio sample according to a different characteristic but the rating scale is exactly the same for all 3( if that is useful for anything at all that the code might require).
so: they listem to one sample, they rate the sample according to 3 characeristics (each one rated with its own slider) ; then they mive on to next sample amd the process repeats until the end of the test
Cheers
Geoff Hayes
on 27 Jun 2019
Are you still using three different GUIs or just one (for all three audio samples)?
Ana Campos
on 27 Jun 2019
Just one as you told me to do :) way more pro now ?
Geoff Hayes
on 27 Jun 2019
So if you know which slider corresponds to which audio file (or attribute?) then you could just store all of that data in the GUI until you close the GUI. At that point, when the GUI shuts down, you could update the text file with the attribute values for all three audio files (rather than whenever the slider changes). For example, in your slider callback you could do
function slider5_Callback(hObject, eventdata, handles)
handles.subjectSliderStep1 = get(hObject, 'Value');
guidata(hObject, handles);
In the above, we create (or update) a field called subjectSliderStep1 with the slider value. Then we save the updated handles structure via guidata.
Now you need to add a CloseRequestFcn to your GUI (see https://www.mathworks.com/matlabcentral/answers/139854-how-to-change-the-gui-close-function-whithout-changing-the-close-function-of-figures for an example of how to do this). The code for this function might look like
function YourGuiName_CloseRequestFcn(hObject, eventdata, handles)
if isfield(handles, 'subjectSliderStep1')
filename = 'listeningtest_data1.txt';
attribute = 1.1
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep1,'-append')
end
And you could add similar code for your other slider values.
Ana Campos
on 27 Jun 2019
Could you tell me what am I doing wrong here please? Thanks
% --- Executes on slider movement.
function slider5attribute_Callback(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.subjectSliderStep1 = get(hObject, 'Value');
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function slider5attribute_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider6attribute_Callback(hObject, eventdata, handles)
% hObject handle to slider6articulation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.subjectSliderStep2 = get(hObject, 'Value');
guidata(hObject, handles);
%items = get(handles.slider5,'String')
%selectedItem = items{subjectSliderStep1};
% --- Executes during object creation, after setting all properties.
function slider6attribute_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider6articulation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider7attribute_Callback(hObject, eventdata, handles)
% hObject handle to slider7bassenergy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.subjectSliderStep3 = get(hObject, 'Value');
guidata(hObject, handles);
%items = get(handles.slider5,'String')
%selectedItem = items{subjectSliderStep1};
% --- Executes during object creation, after setting all properties.
function slider7attribute_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider7bassenergy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'subjectSliderStep1')
filename = 'listeningtest_data1.txt';
attribute = 1.1
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep1,'-append')
end
if isfield(handles, 'subjectSliderStep2')
filename = 'listeningtest_data1.txt';
attribute = 1.2
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep2,'-append')
end
if isfield(handles, 'subjectSliderStep3')
filename = 'listeningtest_data1.txt';
attribute = 1.3
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep3,'-append')
end
% Hint: delete(hObject) closes the figure
delete(hObject);
Geoff Hayes
on 28 Jun 2019
Ana - what is happening with the above code? Is the file being updated with incorrect values? Or not updated at all? Is your GUI called figure1 so that the close function callback makse sense? Please clarify.
Ana Campos
on 28 Jun 2019
Edited: Ana Campos
on 29 Jun 2019
The .txt is not being updated. My GUI is called “dissertationgui” but every time I click on the gui to generate the close function it always appears as figure1.
But it worked before and was updating.But after I deleted one of the sliders’ “create function” by mistake, and pasted it again from a previous version of that code, it stopped working.
Geoff Hayes
on 28 Jun 2019
Ana - can you post the figure file for your GUI? Or confirm (using the GUIDE editor) that the close function callback is assigned correctly?
Ana Campos
on 29 Jun 2019
Edited: Ana Campos
on 29 Jun 2019
I tried to do it by myself before nagging you again. ahah :)
It is (kinda) working again. But the following is happening:
- I play the first sample and I rate the sample using three attributes(sliders)
- I play the second sample and I rate the sample using three attributes(sliders)
- I play the third sample and I rate the sample using three attributes(sliders)
- When I check the .txt file, the only answers it stored are the ones for the last sample
- If i run the code X times, the .txt file will always only store the answers for the last audio sample. In the end I'll only know what the subjects thought of the last sample each one of them listened to.
cheers
% The rest of the code is still the same as before
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'subjectSliderStep1')
filename = 'listeningtest_data1.txt';
attribute = 1.1
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dl
mwrite('listeningtest_data1.txt',handles.subjectSliderStep1,'-append')
isfield(handles, 'subjectSliderStep2')
filename = 'listeningtest_data1.txt';
attribute = 1.2
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep2,'-append')
isfield(handles, 'subjectSliderStep3')
filename = 'listeningtest_data1.txt';
attribute = 1.3
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep3,'-append')
end
% Hint: delete(hObject) closes the figure
delete(hObject);
Geoff Hayes
on 29 Jun 2019
Edited: Geoff Hayes
on 1 Jul 2019
Ana - you can use an array to store the data (slider values) for each slider. In the OpeningFcn of your GUI, initialize the these arrays as
handles.subjectSliderStep1 = zeros(3,1);
handles.subjectSliderStep2 = zeros(3,1);
handles.subjectSliderStep3 = zeros(3,1);
handles.atIteration = 1; % use this to know which iteration/audio file is being updated
guidata(hObject, handles);
Now I'm assuming that you know which iteration (1, 2 or 3) you are on, so when the slider callback fires, you can update thea appropriate element in the arrays. For example,
function slider7attribute_Callback(hObject, eventdata, handles)
% hObject handle to slider7bassenergy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.subjectSliderStep3(handles.atIteration) = get(hObject, 'Value');
guidata(hObject, handles);
Then when you exit the GUI, you will be writing an array of values to the file.
Ana Campos
on 29 Jun 2019
Edited: Ana Campos
on 29 Jun 2019
I've tried that. Unfortunately it is still not working. :)
The .txt file keeps showing only the rating given by each slider for the last audio sample.
I cannot get the ratings for the previous two sounds. only the last one.
It looks like it's overwriting the values and in the end i'm only left with the three ratings corresponding to the evaluation of the last audio sample.
I need to have 3 ratings (one per slider) for each sound. Cheers
Geoff Hayes
on 29 Jun 2019
Ana - please attach your m-code file so that I can see what you have written.
Ana Campos
on 30 Jun 2019
Edited: Ana Campos
on 30 Jun 2019
On the close request function I don't have the name of my GUI "dissertationgui". I don't know how to do that. ive tried right clicking on an area of the GUI panel with no objects and adding a close req function but it always shows up as "figure 1".
The following code does what i told you before: it only saves the ratings given to the last played sample
Last thing: do you know how I can store the identification (name) of the audio samples along with the rating they’ve been given? (because i need to know how people rate the different audio samples. so i need to know to which sample theyve given a certain rating)
cheers
function varargout = dissertationgui(varargin)
% DISSERTATIONGUI MATLAB code for dissertationgui.fig
% DISSERTATIONGUI, by itself, creates a new DISSERTATIONGUI or raises the existing
% singleton*.
%
% H = DISSERTATIONGUI returns the handle to a new DISSERTATIONGUI or the handle to
% the existing singleton*.
%
% DISSERTATIONGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DISSERTATIONGUI.M with the given input arguments.
%
% DISSERTATIONGUI('Property','Value',...) creates a new DISSERTATIONGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before dissertationgui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to dissertationgui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help dissertationgui
% Last Modified by GUIDE v2.5 27-Jun-2019 19:45:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @dissertationgui_OpeningFcn, ...
'gui_OutputFcn', @dissertationgui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before dissertationgui is made visible.
function dissertationgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to dissertationgui (see VARARGIN)
% Choose default command line output for dissertationgui
handles.output = hObject;
% create an axes that spans the whole gui
ah = axes('unit', 'normalized', 'position', [0 0 1 1]);
% import the background image and show it on the axes
bg = imread('black1.jpg'); imagesc(bg);
% prevent plotting over the background and turn the axis off
set(ah,'handlevisibility','off','visible','off')
% making sure the background is behind all the other uicontrols
uistack(ah, 'bottom');
% Update handles structure
handles.myWavFiles = {'piano.wav', 'harpsi.wav', 'Latin.wav'};
guidata(hObject, handles);
sliderMin = 1
sliderMax = 5
sliderStep = [1, 1]/(sliderMax - sliderMin);
set(handles.slider7attribute3, 'Min', sliderMin);
set(handles.slider7attribute3, 'Max',sliderMax);
set(handles.slider7attribute3, 'SliderStep' , sliderStep);
set(handles.slider6attribute2, 'Min', sliderMin);
set(handles.slider6attribute2, 'Max',sliderMax);
set(handles.slider6attribute2, 'SliderStep' , sliderStep);
set(handles.slider5, 'Min', sliderMin);
set(handles.slider5, 'Max',sliderMax);
set(handles.slider5, 'SliderStep' , sliderStep);
% UIWAIT makes dissertationgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = dissertationgui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in play.
function play_Callback(hObject, eventdata, handles)
% hObject handle to play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isempty(handles.myWavFiles)
handles.wavFileIndex = randi(length(handles.myWavFiles),1,1);
guidata(hObject, handles);
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex};
x = audioread(wavFileToPlay);
sound (x,48000)
end
% --- Executes on button press in next.
function next_Callback(hObject, eventdata, handles)
% hObject handle to next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%uicontrol('style','PushButton','String','Next','callback',@func_close, 'backgroundcolor','r','FontSize',14);
if isfield(handles, 'wavFileIndex')
handles.myWavFiles(handles.wavFileIndex) = [];
guidata(hObject, handles);
end
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear sound;
% --- Executes on button press in again.
function again_Callback(hObject, eventdata, handles)
% hObject handle to again (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'wavFileIndex')
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex};
x = audioread(wavFileToPlay);
sound (x,48000)
end
% SLIDERS----------------------------------------------------------------
% --- Executes on slider movement.
function slider5_Callback(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
handles.subjectSliderStep1 = get(hObject, 'Value');
guidata(hObject, handles);
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider5_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider6attribute2_Callback(hObject, eventdata, handles)
% hObject handle to slider6attribute2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
handles.subjectSliderStep2 = get(hObject, 'Value');
guidata(hObject, handles);
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider6attribute2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider6attribute2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider7attribute3_Callback(hObject, eventdata, handles)
% hObject handle to slider7attribute3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
handles.subjectSliderStep3 = get(hObject, 'Value');
guidata(hObject, handles);
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider7attribute3_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider7attribute3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% CLOSE REQUEST FUNCTION-----------------------------------------------
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles, 'subjectSliderStep1')
filename = 'listeningtest_data1.txt';
attribute = 1.1
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep1,'-append')
isfield(handles, 'subjectSliderStep2')
filename = 'listeningtest_data1.txt';
attribute = 1.2
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep2,'-append')
isfield(handles, 'subjectSliderStep3')
filename = 'listeningtest_data1.txt';
attribute = 1.3
dlmwrite('listeningtest_data1.txt',attribute,'-append')
dlmwrite('listeningtest_data1.txt',handles.subjectSliderStep3,'-append')
end
% Hint: delete(hObject) closes the figure
delete(hObject);
%POPUP MENUS -------------------------------------------------------------
% --- Executes on selection change in popupmenuattribute1.
function popupmenuattribute1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenuattribute1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenuattribute1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenuattribute1
% --- Executes during object creation, after setting all properties.
function popupmenuattribute1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenuattribute1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenuattribute2.
function popupmenuattribute2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenuattribute2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenuattribute2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenuattribute2
% --- Executes during object creation, after setting all properties.
function popupmenuattribute2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenuattribute2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenuattribute3.
function popupmenuattribute3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenuattribute3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenuattribute3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenuattribute3
% --- Executes during object creation, after setting all properties.
function popupmenuattribute3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenuattribute3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
ltr
Geoff Hayes
on 1 Jul 2019
Ana - in your slider callbacks, you need to save the slider value to an array. Something like
function slider7attribute_Callback(hObject, eventdata, handles)
% hObject handle to slider7bassenergy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.subjectSliderStep3(handles.atIteration) = get(hObject, 'Value');
guidata(hObject, handles);
where atIteration indicates which wav file you are considering (1, 2, or 3). You may want to set this in the play callback.
As for the CloseRequest function, I don't know why figure1 is being used. You would need to attach your fig file so that I can see why (it could be that your original GUI was named figure1 then you changed it at some point but there is still a reference to the old name).
For the other attributes, identication and rating, I don't know where they are coming from (the wav file?) so can't comment on how they can be used.
Ana Campos
on 1 Jul 2019
Edited: Ana Campos
on 1 Jul 2019
Ok I've tried again and i just can't make it work like that.
Here are my files. If you could explain me how to do it I would appreciate it because I can only make it work if my .txt keeps storing every movement of the sliders and that is not useful at all by the time I'll need to analise the data.
I'll explain what I need again just in case I wasn't very clear last time.
I have X audio samples (in this example only 3) to be rated
Each audio sample will be evaluated using 3 sliders /the sliders will allow the user to rate the samples according to 3 different characteristics
I need to store all of their ratings for each audio sample (3 ratings per sample always) on a .txt or xlsx file (at the moment im using a .txt)
I need to know the sample they were listening to, when they were giving the rating.
Do you know how to do it? Because I'm completely stuck here now.
Cheers
Geoff Hayes
on 1 Jul 2019
The figure1 may be coming from the tag value that you have assinged to your GUI. I guess if the CloseRequestFcn is firing then you don't need to worry about that.
For the updating of the array of ratings, I would consider initializing the atIteration to zero (in the OpeningFcn)
handles.atIteration = 0;
and then increment it by one when the play button is pressed
function play_Callback(hObject, eventdata, handles)
% hObject handle to play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isempty(handles.myWavFiles)
handles.atIteration = handles.atIteration + 1;
handles.wavFileIndex = randi(length(handles.myWavFiles),1,1); % one random file to play
guidata(hObject, handles); % save the index of the file that will be played
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex};
x = audioread(wavFileToPlay);
% NOW PLAY THE AUDIO SAMPLE
sound (x,48000)
end
that way when the ratings slider callbacks fire, you will update the appropriate element in your arrays.
As for knowing which sample the user was listening to when they applied the rating, there are probably several ways to do this. One way might be to record the time at which the user pressed the play button and then the time that the slider callback fired. The difference between the two - in seconds - will give you an idea of how long the audio asset has been playing when the slider callback fired. Knowing this duration and knowing the sample rate of the audio asset, you can estimate the sample
(sliderCallbackFireTimeSec - audioAssetStartTimeSec) * Fs
function play_Callback(hObject, eventdata, handles)
% hObject handle to play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isempty(handles.myWavFiles)
handles.atIteration = handles.atIteration + 1;
handles.wavFileIndex = randi(length(handles.myWavFiles),1,1); % one random file to play
guidata(hObject, handles); % save the index of the file that will be played
wavFileToPlay = handles.myWavFiles{handles.wavFileIndex};
x = audioread(wavFileToPlay);
% NOW PLAY THE AUDIO SAMPLE
sound (x,48000)
tic % <------- starts the stopwatch timer
end
And then in your slider callbacks, you would call toc
function slider5_Callback(hObject, eventdata, handles)
% hObject handle to slider5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
elapsedTimeSec = toc;
handles.subjectSliderSample1(handles.atIteration) = elapsedTimeSec * 48000;
handles.subjectSliderStep1(handles.atIteration) = get(hObject, 'Value');
guidata(hObject, handles);
Note how there is a new array subjectSliderSample1 for the sample that was being listened to. I'm assuming a sample rate of 48000 since you have hard-coded that elsehwere in your code. This should be the Fs (sampling rate) that is returned when reading the audio asset with audioread.
More Answers (0)
Categories
Find more on Audio and Video Data in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)