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

6 views (last 30 days)
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

Geoff Hayes
Geoff Hayes on 29 Sep 2014
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
John Bitzios
John Bitzios on 30 Sep 2014
Actually I mean save the data to a variable "data" for further processing, for the fft for example
Geoff Hayes
Geoff Hayes on 30 Sep 2014
Edited: Geoff Hayes on 30 Sep 2014
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 Graphics Objects 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!