how to trigger the microfone to record via GUI's pushbutton

Hi there! I want to plot the original signal taken from the laptops microphone on a GUI axis and then another axis which will show the fft plot of the same signal. My main problem is how to trigger the microfone through the pushbutton to start recording? Has this something to do with the pushbutton's "userData" on the pushbutton's function which is generated through the guide??

 Accepted Answer

John - no, the pushbutton's UserData property isn't what you will need to start the recording.
One method is to use the audio recorder object. You would start the recording when the user presses the "record" button on your GUI, and stop the recording when the user presses the "stop" button. You create an audio recorder timer function that fires periodically (say every one second) and collects the latest set of samples, display the data in one plot/axes, do the FFT on that same set of samples and display it in the other plot/axes.
The trick is to get the push button callbacks to have access to the audio recorder, and the timer function to have access to the plots.
Here is a brief outline of what you could do. Since you are using GUIDE, in its yourGuiName_OpeningFcn create the audio recorder and save it to the handles structure so that the record and stop (push) buttons have access to this object
function yourGuiName_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% set the sample rate (Hz)
handles.Fs = 8192;
% create the recorder
handles.recorder = audiorecorder(handles.Fs,8,1);
% assign a timer function to the recorder
set(handles.recorder,'TimerPeriod',1,'TimerFcn',{@audioTimer,hObject});
% save the handles structure
guidata(hObject, handles);
All of the above is pretty straightforward. When we create the TimerFcn, we are specifying the handle to the function that will fire every 1 second (that is why we prepend the function audiotimer with the ampersand), and we are passing an additional parameter to this function, hObject which is the handle to the figure/GUI.
Now, in the start push button callback, we would do something like
function recordbutton_ClickedCallback(hObject, eventdata, handles)
record(handles.recorder);
Since we can access the audio recorder through the recorder field of handles, we do so and start the recording. We would do something similar for the stop button, replacing record with stop.
Finally, for the audio timer function that fires every one second, it could be defined as
function audioTimer(hObject,varargin)
In the above, hObject refers to the audio recorder, and varargin the variable array of input arguments/parameters. The first couple of lines for the body of this function could be
% get the handle to the figure/GUI (this is the handle we passed in
% when creating the timer function in myGuiName_OpeningFcn)
hFigure = varargin{2};
% get the handles structure so we can access the plots/axes
handles = guidata(hFigure);
% get the audio samples
samples = getaudiodata(hObject);
% etc.
The above is just an outline of what you could do - there is a lot of code missing. For example, the getaudiodata(hObject) from above returns all the samples since the start of the recording, so you will have to keep track of the last sample that you extracted when the timer fired previously, so that you can extract the samples that followed that on the next firing of the timer. You can store this information to the handles structure by updating a field. Just remember to call guidata(hFigure,handles) to save this user-defined data.
The above will continue until you press the stop button. If you only want to record 15 seconds of data, then just do the following in the record button callback
record(handles.recorder,15);
You may want to start with this until you get things working as expected, and then remove the maximum number of seconds limit.

5 Comments

Thanks for replying Geoff! I want the program to do all this stuff real time without any stops involved unless I push the pushbutton which will actually be some kind of an on/off switch...thats why I mentioned the userData...I thought that userData is some kind of a switch to the pushbutton on GUI...don't know...
Is there a way to save into a variable all the data that comes into the program?? Because it will help through the debuggins session.If yes , then where should I put the code?? Under which function?
John - what do you mean by all the data that comes into the program? If you mean the samples that are recorded by the audio recorder, then they will still be present in the handles.recorder object until you restart the process.
If you want to save specific variables to a mat file, then you could do this in the stop button callback of your GUI. So after you have stopped recording, you could add the following
function stopbutton_ClickedCallback(hObject, eventdata, handles)
stop(handles.recorder);
% save the recorder to file
recorder = handles.recorder
filename = [datestr(now,'yyyy-mm-dd_HHMMSS') '.mat'];
save(filename,'recorder');
We need to create a local variable from the handles.recorder field in order to save it to file. The filename is unique based on the current date and time. If you wish to save more variables, then just pass in other local variables as
save(filename,'recorder','Fs','whatever');
See save workspace variables to file for more details.
Actually I mean save the data to a variable "data" for further processing, for the fft for example
Once you have extracted the samples to do the FFT (which will be in the timer function) just save it to a field in the handles structure as
handles.rawData = rawData; % data to do FFT on
guidata(hFigure,handles);
Try that and see if it works. If that doesn't, try using the setappdata function to save the data in the timer callback as
setappdata(hFigure,'rawData',rawData);
and then in the other callback (or whatever that does the FFT), use the getappdata as
rawData = getappdata(hFigure,'rawData');
Note that the same figure handle must be used in both cases - for the saving and getting of the data.

Sign in to comment.

More Answers (0)

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!