Record and Play Audio
Record and play audio data for processing in MATLAB® from audio input and output devices on your system. Audio playback and recording in MATLAB Online™ and MATLAB Web App Server™ are supported in Google Chrome®.
Record Audio
Record data from an audio input device such as a microphone connected to your system:
Create an
audiorecorderobject.Call the
recordorrecordblockingmethod, where:recordreturns 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 thestopmethod. Optionally, call thepauseandresumemethods. The recording is performed asynchronously.recordblockingretains control until the recording is complete. Specify the length of the recording in seconds. The recording is performed synchronously.
Create a numeric array corresponding to the signal data using the
getaudiodatamethod.
The following examples show how to use the recordblocking and
record methods.
Record Microphone Input
This example shows how to record microphone input, play back the recording, and store the recorded audio signal in a numeric array. You must first connect a microphone to your system.
Create an audiorecorder object with default
properties named recObj for recording audio
input.
recObj = audiorecorder
recObj =
audiorecorder with properties:
SampleRate: 8000
BitsPerSample: 8
NumChannels: 1
DeviceID: -1
CurrentSample: 1
TotalSamples: 0
Running: 'off'
StartFcn: []
StopFcn: []
TimerFcn: []
TimerPeriod: 0.0500
Tag: ''
UserData: []
Type: 'audiorecorder'audiorecorder creates an 8000 Hz, 8-bit,
1-channel audiorecorder object.
Record your voice for 5 seconds.
recDuration = 5; disp("Begin speaking.") recordblocking(recObj,recDuration); disp("End of recording.")
Play the recording.
play(recObj);
Store data in double-precision array y.
y = getaudiodata(recObj);
Plot the audio samples.
plot(y);
Record Two Channels from Different Sound Cards
To record audio independently from two different sound cards, with a microphone connected to each:
Call
audiodevinfoto list the available sound cards. For example, this code returns a structure array containing all input and output audio devices on your system.Identify the sound cards you want to use by name, and note theirinfo = audiodevinfo;
IDvalues.Create two
audiorecorderobjects. For example, this code creates theaudiorecorderobjectrecorder1for recording a single channel from device 3 at 44.1 kHz and 16 bits per sample. The code then creates theaudiorecorderobjectrecorder2for recording a single channel from device 4 at 48 kHz.recorder1 = audiorecorder(44100,16,1,3); recorder2 = audiorecorder(48000,16,1,4);
Record each audio channel separately.
The recordings occur simultaneously as the first call torecord(recorder1); record(recorder2); pause(5);
recorddoes not block.Stop the recordings.
stop(recorder1); stop(recorder2);
Specify the Quality of the Recording
By default, an audiorecorder object uses a sample rate of
8000 Hz, a depth of 8 bits (8 bits per sample), and a single audio channel. With
these settings, the required amount of data storage is low. For higher quality
recordings, increase the sample rate or bit depth.
For example, compact disks use a sample rate of 44,100 Hz, a 16-bit depth, and
two audio channels. Create an audiorecorder object to record
with those settings.
myRecObj = audiorecorder(44100,16,2);
For more information on the available properties and values, see the audiorecorder reference
page.
Play Audio
After you import or record audio, MATLAB supports several ways to listen to the data:
For simple playback using a single function call, use
soundorsoundsc. For example, load a sample MAT-file that contains signal and sample rate data, and listen to the audio.load chirp.mat sound(y,Fs)
For more flexibility during playback, including the ability to pause, resume, or define callbacks, use the
audioplayerfunction. Create anaudioplayerobject, then call methods to play the audio. For example, listen to thegongsample file.load gong.mat gong = audioplayer(y,Fs); play(gong);
For an additional example, see Record or Play Audio within a Function.
If you do not specify the sample rate, sound plays back at
8192 Hz. For any playback, specify smaller sample rates to play back more slowly,
and larger sample rates to play back more quickly.
Note
Most sound cards support sample rates between approximately 5000 and 192,000 Hz. Specifying sample rates outside this range can produce unexpected results.
Record or Play Audio within a Function
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.
playFile("handel.mat")
At the recorded sample rate of 8192 samples per second, playing the 73,113 samples
in the file takes approximately 8.9 seconds. However, the
playFile function typically ends before playback completes,
and clears the audioplayer object obj.
For complete playback or recording, consider these options:
Use
playblockingorrecordblockinginstead ofplayorrecord. The blocking methods retain control until playing or recording completes. If you block control, you cannot issue any other commands or methods (such aspauseorresume) during the playback or recording.Create an output argument for your function that generates an object in the base workspace. For example, modify the
playFilefunction to include an output argument.function obj = playFile(myfile)
Call the function.
h = playFile("handel.mat");Because
hexists in the base workspace, you can pause playback from the command prompt.pause(h)
See Also
audioplayer | sound | soundsc | audiorecorder