|On this page…|
To record data from an audio input device (such as a microphone connected to your system) for processing in MATLAB®:
Create an audiorecorder object.
Call the record or recordblocking method, where:
record returns immediate control to the calling function or the command prompt even as recording proceeds. Specify the length of the recording in seconds, or end the recording with the stop method. Optionally, call the pause and resume methods.
recordblocking retains control until the recording is complete. Specify the length of the recording in seconds.
Create a numeric array corresponding to the signal data using the getaudiodata method.
For example, connect a microphone to your system and record your voice for 5 seconds. Capture the numeric signal data and create a plot:
% Record your voice for 5 seconds. recObj = audiorecorder; disp('Start speaking.') recordblocking(recObj, 5); disp('End of Recording.'); % Play back the recording. play(recObj); % Store data in double-precision array. myRecording = getaudiodata(recObj); % Plot the samples. plot(myRecording);
By default, an audiorecorder object uses a sample rate of 8000 hertz, a depth of 8 bits (8 bits per sample), and a single audio channel. These settings minimize the required amount of data storage. For higher quality recordings, increase the sample rate or bit depth.
For example, typical compact disks use a sample rate of 44,100 hertz and a 16-bit depth. Create an audiorecorder object to record in stereo (two channels) with those settings:
myRecObj = audiorecorder(44100, 16, 2);
For more information on the available properties and values, see the audiorecorder reference page.
After you import or record audio, MATLAB supports several ways to listen to the data:
load chirp.mat; sound(y, Fs);
For more flexibility during playback, including the ability to pause, resume, or define callbacks, use the audioplayer function. Create an audioplayer object, then call methods to play the audio. For example, listen to the gong sample file:
load gong.mat; gong = audioplayer(y, Fs); play(gong);
For an additional example, see Recording or Playing Audio within a Function.
If you do not specify the sample rate, sound plays back at 8192 hertz. For any playback, specify smaller sample rates to play back more slowly, and larger sample rates to play back more quickly.
If you create an audioplayer or audiorecorder object inside a function, the object exists only for the duration of the function. For example, create a player function called playFile and a simple callback function showSeconds:
function playFile(myfile) load(myfile); obj = audioplayer(y, Fs); obj.TimerFcn = 'showSeconds'; obj.TimerPeriod = 1; play(obj); end function showSeconds disp('tick') end
Call playFile from the command prompt to play the file handel.mat:
At the recorded sample rate of 8192 samples per second, playing the 73113 samples in the file takes approximately 8.9 seconds. However, the playFile function typically ends before playback completes, and clears the audioplayer object obj.
To ensure complete playback or recording, consider the following options:
Use playblocking or recordblocking instead of play or record. The blocking methods retain control until playing or recording completes. If you block control, you cannot issue any other commands or methods (such as pause or resume) during the playback or recording.
Create an output argument for your function that generates an object in the base workspace. For example, modify the playFile function to include an output argument:
function obj = playFile(myfile)
Call the function:
h = playFile('handel.mat');
Because h exists in the base workspace, you can pause playback from the command prompt: