Main Content

readAudio

Read collected audio data from mobile device

Since R2022b

Description

Add-On Required

To use this function for accessing mobile device sensors remotely from MATLAB® installed on a desktop or laptop computer, you must also install either MATLAB Support Package for Apple iOS Sensors or MATLAB Support Package for Android® Sensors. For more information, see Get and Manage Add-Ons.

audioData = readAudio(devObj) reads all available audio data from the collection buffer, and returns it to a double matrix of size NumSamples-by-NumChannels.

audioData = readAudio(devObj,span) reads the number of samples indicated by span, which you can specify as a numeric value for the number of samples, or as a duration, in which case the function reads the number of samples to fill the duration span, depending on the sample rate. You can set the sample rate on the device in the Sensors menu, or by setting the microphone object SampleRate value in MATLAB.

Data is read from the start of the buffer, that is, the earliest samples. All data is cleared from the buffer by this operation. The function blocks MATLAB until the requested number of samples is read, or until a timeout occurs.

audioData = readAudio(devObj,span,"latest") specifies to read the most recently collected samples from the audio buffer. The default is to read to the oldest samples. You can specify "oldest" to be explicit. Reading the latest data leaves the earlier samples intact in the collection buffer. You can clear the buffer with discardlogs.

audioData = readAudio(___,OutputFormat=outFmt) specifies the format of the returned data. The argument value outFmt is a string specified as "matrix" or "timetable". The default is "matrix".

[audioData,timeStamp] = readAudio(___) returns a matrix of audio data samples, and a corresponding timestamp for the first sample in the buffer. This two variable output requires the default OutputFormat="matrix", and is not allowed when OutputFormat="timetable".

example

Examples

collapse all

Using MATLAB Mobile™ on your phone or tablet, you must enable sensor access to the device microphone. Select Stream to > MATLAB.

In MATLAB, connect to your device and enable the microphone for 30 seconds of audio signal logging.

devObj = mobiledev;
devObj.MicrophoneEnabled = 1;
devObj.Logging = 1;
pause(30);
devObj.Logging = 0;

Alternatively, you can manually enable logging on your device.

Read the first (oldest) 1000 samples into a matrix.

audioData = readAudio(devObj,1000);

Read the most recent 10 seconds of buffered audio data into a timetable.

audioData = readAudio(devObj,seconds(10),"latest",OutputFormat="timetable");

For an example of a more complete workflow, see Acquire Audio Data from a Mobile Device Microphone.

Input Arguments

collapse all

Mobile device, specified as a mobiledev object. Use the mobiledev function to make this object and connect to the device.

Length of read operation, specified as a duration or double. If this is a duration type, it specifies the time duration of the acquired samples. If it is a double, it specifies the number of samples.

Example: seconds(5)

Data Types: double | duration

Output Arguments

collapse all

Audio samples captured by device microphone, returned as a matrix of size NumSamples-by-NumChannels or as a timetable, with data values normalized in the range [-1,1].

Time of first sample in the collection buffer, returned as a datetime. This argument is available only when OutputFormat is specified as "matrix".

Version History

Introduced in R2022b