Main Content

audiorecorder

Object for recording audio

Description

Use anaudiorecorderobject to record audio data from an input device such as a microphone for processing in MATLAB®. Theaudiorecorderobject contains properties that enable additional flexibility during recording. For example, you can pause, resume, or define callbacks using theaudiorecorderobject functions.

Creation

Description

example

recorder= audiorecordercreates and returns anaudiorecorder与这些公关对象operties:

  • Sampling frequencyFs= 8000 hertz

  • Bits per samplenBits= 8

  • Number of channelsnChannels= 1

example

recorder= audiorecorder(Fs,nBits,NumChannels)sets the sample rateFs(in hertz), the bits per samplenBits, and the number of channelsnChannels.

example

recorder= audiorecorder(Fs,nBits,NumChannels,ID)sets the audio input device to the device specified byID.

Input Arguments

expand all

Sampling frequency in hertz (Hz), specified as a numeric scalar.

Valid values of the sampling rate depend on both the sample rates permitted by MATLAB and the specific audio hardware on your system. MATLAB has a hard restriction of 1000 Hz <=Fs<= 384000 Hz, although further hardware-dependent restrictions apply. Typical values supported by most sound cards are 8000, 11025, 22050, 44100, 48000, and 96000 hertz.

Data Types:single|double

Bits per sample, specified as8,16, or24.

SpecifynBitsonly when the signalYcontains floating-point values. Valid values ofnBitsdepend on the audio hardware. For example, depending on your audio hardware,nBitscan be one of these values:8,16, or24.

Number of channels, specified as1(mono) or2(stereo).

Device identifier, specified as an integer.

To obtain the ID of a device, use theaudiodevinfofunction.

Properties

expand all

This property is read-only.

Bits per sample, returned as a positive integer.

This property is read-only.

Sample currently recording on the audio input device, returned as a positive integer.

If the device is not recording,CurrentSampleis the next sample to record using therecordorresume方法。

This property is read-only.

Audio device identifier, returned as an integer.

This property is read-only.

Audio recorder status, returned asonoroff.

Sampling frequency in hertz (Hz), returned as a numeric scalar.

To set theSampleRate, use theFsinput argument when constructing theaudiorecorderobject.

This property is read-only.

Total length of the audio data in samples, returned as an integer.

Label, specified as a character vector.

This property is read-only.

Object class name, returned as'audiorecorder'.

User-defined data, specified as a value of any data type. Use this property to store any additional data with the object.

Function to execute at start of recording, specified as a character vector or string scalar containing the name of the function, or a function handle.

The first two inputs to your callback function must be theaudiorecorderobject and aneventstructure. For more information, seecallback functions.

Function to execute at end of recording, specified as a character vector or string scalar containing the name of the function, or a function handle.

The first two inputs to your callback function must be theaudiorecorderobject and aneventstructure. For more information, seecallback functions.

Function to execute repeatedly during recording, specified as a character vector or string scalar containing the name of the function, or a function handle. To specify time intervals for the repetitions, use theTimerPeriodproperty.

The first two inputs to your callback function must be theaudiorecorderobject and aneventstructure. For more information, seecallback functions.

Timer period, specified as numeric scalar.

Timer period is the time in seconds betweenTimerFcncallbacks.

Object Functions

get Query property values foraudiorecorderobject
getaudiodata Store recorded audio signal in numeric array
getplayer Creates associatedaudioplayerobject
isrecording Determine if recording is in progress
pause Pause playback or recording
play Play audio fromaudiorecorderobject
record Record audio toaudiorecorderobject
recordblocking Record audio toaudiorecorderobject, hold control until recording completes
resume Resume playback or recording from paused state
set Set property values foraudiorecorderobject
stop Stop playback or recording

Examples

collapse all

Record audio data from a microphone and then play the recorded audio.

Create anaudiorecorderobject with default property values.

recObj = audiorecorder;

Alternatively, create anaudiorecorderobject with the desired properties. For a CD-quality audio in stereo, define these properties: sampling frequency (Fs), number of bits per sample (nBits), the number of channels (nChannels), and input device identifier (ID).

Fs = 44100 ; nBits = 16 ; nChannels = 2 ; ID = -1;% default audio input devicerecObj = audiorecorder(Fs,nBits,nChannels,ID);

Collect a five second sample of your speech with your microphone.

disp('Start speaking.')
Start speaking.
recordblocking(recObj,5); disp('End of Recording.');
End of Recording.

Play back the recording.

play(recObj);
Introduced before R2006a