audiorecorder

Object for recording audio

Description

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

Creation

Description

example

recorder = audiorecorder creates and returns an audiorecorder object with these properties:

  • Sampling frequency Fs = 8000 hertz

  • Bits per sample nBits = 8

  • Number of channels nChannels = 1

example

recorder = audiorecorder(Fs,nBits,NumChannels) sets the sample rate Fs (in hertz), the bits per sample nBits, and the number of channels nChannels.

example

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

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 as 8, 16, or 24.

Specify nBits only when the signal Y contains floating-point values. Valid values of nBits depend on the audio hardware. For example, depending on your audio hardware, nBits can be one of these values: 8, 16, or 24.

Number of channels, specified as 1 (mono) or 2 (stereo).

Device identifier, specified as an integer.

To obtain the ID of a device, use the audiodevinfo function.

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, CurrentSample is the next sample to record using the record or resume methods.

This property is read-only.

Audio device identifier, returned as an integer.

This property is read-only.

Audio recorder status, returned as on or off.

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

To set the SampleRate, use the Fs input argument when constructing the audiorecorder object.

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 the audiorecorder object and an event structure. For more information, see callback 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 the audiorecorder object and an event structure. For more information, see callback 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 the TimerPeriod property.

The first two inputs to your callback function must be the audiorecorder object and an event structure. For more information, see callback functions.

Timer period, specified as numeric scalar.

Timer period is the time in seconds between TimerFcn callbacks.

Object Functions

getQuery property values for audiorecorder object
getaudiodataStore recorded audio signal in numeric array
getplayerCreates associated audioplayer object
isrecordingDetermine if recording is in progress
pausePause playback or recording
playPlay audio from audiorecorder object
recordRecord audio to audiorecorder object
recordblockingRecord audio to audiorecorder object, hold control until recording completes
resume Resume playback or recording from paused state
setSet property values for audiorecorder object
stopStop playback or recording

Examples

collapse all

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

Create an audiorecorder object with default property values.

recObj = audiorecorder;

Alternatively, create an audiorecorder object 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 device 
recObj = 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