Main Content

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

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

  • SampleRate: 8000

  • BitsPerSample: 8

  • NumChannels: 1

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

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

example

Input Arguments

expand all

Sample rate in hertz (Hz), specified as a positive scalar.

Valid values of the sample rate depend on the audio hardware of your system. Typical sample rates supported by most setups are 8000, 11,025, 22,050, 44,100, 48,000, 96,000, and 192,000 Hz.

This argument sets the SampleRate property.

Data Types: single | double

Bits per sample, specified as 8, 16, or 24. Specify nBits only when the signal contains floating-point values.

Valid values of nBits depend on the audio hardware of your system.

This argument sets the BitsPerSample property.

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

This argument sets the NumChannels property.

Audio device identifier, specified as an integer.

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

This argument sets the DeviceID property.

Properties

expand all

This property is read-only.

Sample rate in hertz (Hz), returned as a positive scalar.

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

This property is read-only.

Bits per sample, returned as 8, 16, or 24.

To set the BitsPerSample property, use the nBits input argument when creating the audiorecorder object.

This property is read-only.

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

To set the NumChannels property, use the nChannels input argument when creating the audiorecorder object.

This property is read-only.

Audio device identifier, returned as an integer.

To set the DeviceID property, use the ID input argument when creating the audiorecorder object.

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 method.

This property is read-only.

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

This property is read-only.

Audio recorder status, returned as 'off' or 'on'.

Function to execute at the start of recording, specified as a character vector 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 Timer Callback Functions.

Function to execute at the end of recording, specified as a character vector 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 Timer Callback Functions.

Function to execute repeatedly during recording, specified as a character vector 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 Timer Callback Functions.

Timer period, specified as a positive scalar.

TimerPeriod is the time in seconds between TimerFcn callbacks.

Label, specified as a string scalar or character vector.

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

This property is read-only.

Object class name, returned as 'audiorecorder'.

Object Functions

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

Examples

collapse all

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

Create an audiorecorder object with the specified properties. For CD-quality audio, define these properties: sample rate (Fs), bits per sample (nBits), 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);

Record a 5-second sample of your voice with your microphone.

disp("Begin speaking.")
Begin speaking.
recDuration = 5; % record for 5 seconds
recordblocking(recObj,recDuration);
disp("End of recording.")
End of recording.

Play the recording.

play(recObj);

Limitations

  • These limitations apply when using audiorecorder in MATLAB Online™ or MATLAB Web App Server™:

    • You cannot create an audioplayer object from an audiorecorder object.

    • audiorecorder ignores nBits. Instead, it records audio data using the default number of bits per sample of the input audio device.

    • Valid values of the sample rate Fs may not be the same as they are in MATLAB Desktop.

Tips

  • The audiorecorder function supports sound devices on all Windows® and most UNIX® platforms.

  • audiorecorder in MATLAB Online and MATLAB Web App Server is supported in Google Chrome®.

  • Note

    Security Considerations When Using MATLAB Online or MATLAB Web App Server: When using audiorecorder in MATLAB Online or MATLAB Web App Server, certain features and settings help you keep control of your privacy.

    To reduce the likelihood of unwanted applications using your microphone, turn off automatic access to your audio device. You can change these settings at any time. After granting permission to a website once, your browser may be able to access your microphone automatically for that site on future visits. Use the browser settings for Google Chrome to revoke access for specific sites after you have initially allowed access.

    You can also access the MATLAB Online website or MATLAB Web App Server using the private browsing mode in Google Chrome. When you do this, Chrome® automatically asks you for permission every time it tries to access your microphone, regardless of your browser settings.

Version History

Introduced before R2006a