Stream from audio file
AudioFileReader object reads audio samples
from an audio file.
To read audio samples from an audio file:
Starting in R2016b, instead of using the
to perform the operation defined by the System
object™, you can
call the object with arguments, as if it were a function. For example,
= step(obj) and
y = obj() perform equivalent
afr = dsp.AudioFileReader returns
an audio file reader System
reads audio from an audio file.
afr = dsp.AudioFileReader(' returns
an audio file reader System
each specified property set to the specified value.
afr = dsp.AudioFileReader( returns
an audio file reader object,
FILENAME and other specified properties
set to the specified values.
Name of audio file from which to read
Specify the name of an audio file as a character vector. Specify
the full path for the file only if the file is not on the MATLAB® path.
The default is
Number of times to play file
Specify a positive integer as the number of times to play the
file. The default is
Sampling rate of the audio file
This read-only property displays the sampling rate, in Hz, of the audio file.
Number of samples in audio frame
Specify the number of samples in an audio frame as a positive,
scalar integer value. The default value is
Data type of output
Set the data type of the audio data output from the audio file
reader object. Specify the data type as
The following table lists the supported audio file formats:
|Platforms||File Name Extensions|
|info||Information about specific audio file|
|isDone||End-of-file status (logical)|
|reset||Reset internal states of audio file reader to read from beginning of file|
|step||Read audio samples from audio file|
This example shows how to read and play back an audio file using the standard audio output device.
Note: This example runs only in R2016b or later. If you are using an earlier release, replace each call to the function with the equivalent
step syntax. For
dsp.AudioFileReader System object™, myObject() becomes step(myObject). For all other objects, myObject(x) becomes step(myObject,x).
afr = dsp.AudioFileReader('speech_dft.mp3'); adw = audioDeviceWriter('SampleRate', afr.SampleRate); while ~isDone(afr) audio = afr(); adw(audio); end release(afr); release(adw);
This object implements the algorithm, inputs, and outputs described on the From Multimedia File block reference page. The object properties correspond to the block parameters, except:
The object has no corresponding property for the Inherit sample time from file block parameter. The object always inherits the sample time from the file.
The object has no corresponding property for the Output
end-of-file indicator parameter. The object always outputs
the last output.
The object has no corresponding property for the Multimedia Outputs parameter because audio is the only supported output.
The object has no corresponding property for the Image signal block parameter.
The object has no corresponding property for the Output color format parameter.
The object has no corresponding property for the Video output data type parameter.
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).
The executable generated from this System
on prebuilt dynamic library files (
included with MATLAB. Use the
to package the code generated from this object and all the relevant
files in a compressed zip file. Using this zip file, you can relocate,
unpack, and rebuild your project in another development environment
where MATLAB is not installed. For more details, see How To Run a Generated Executable Outside MATLAB.