Stream from audio file
System object™ reads audio samples from an audio file.
To read audio samples from an audio file:
dsp.AudioFileReader object and set its properties.
Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects?.
audio file reader System object,
afr = dsp.AudioFileReader
afr that reads audio from an audio file.
returns an audio file reader object,
afr = dsp.AudioFileReader(
Filename property set to
returns an audio file reader System object,
afr = dsp.AudioFileReader(
afr, with each specified property set to the
specified value. Enclose each property name in single quotes. Unspecified
properties have default values.
Unless otherwise indicated, properties are nontunable, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
release function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, see System Design in MATLAB Using System Objects.
Filename— Name of audio file from which to read
'speech_dft.mp3'(default) | character vector | string scalar
Specify the name of an audio file as a character vector or a string
scalar. Specify the full path for the file only if the file is not on the
MATLAB® path. The file name can be an http web address, like
'http://audio.wgbh.org:8004/'. For an example, see
Read and Play Back Audio File from http Web Address.
The following table lists the supported audio file formats:
|Platforms||File Name Extensions|
PlayCount— Number of times to play file
1(default) | positive integer
Specify a positive integer as the number of times to play the file.
SampleRate— Sampling rate of audio file
This property is read-only.
This property displays the sampling rate, in Hz, of the audio file.
SamplesPerFrame— Number of samples in audio frame
1024(default) | positive integer
Specify the number of samples in an audio frame as a positive, scalar integer value.
OutputDataType— Data type of output
Set the data type of the audio data output from the audio file reader
object. Specify the data type as
ReadRange— Range of samples to be read
[1 inf](default) | two-element row vector
Specify the sample range from which to read, as a vector in the form of [StartSample EndSample], where StartSample is the sample at which file reading starts, and EndSample is the sample at which file reading stops.
outputs one frame of
audio = afr()
audio. You can specify the number of times
to play the file using the
PlayCount property. After
playing the file for the number of times you specify,
audio— Audio samples
One frame of audio samples, returned as a column vector of length
equal to the value you specify in the
SamplesPerFrame property. The data type of the
audio output is specified in the
eof— End-of-file indicator
End-of-file indicator, returned as either a
1 or a
0. A value of
1 is output when
audio contains the last audio sample in the
To use an object function, specify the
System object as the first input argument. For
example, to release system resources of a System object named
Read and play back an audio file using the standard audio output device.
Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent
step syntax. For example,
You can choose to read the entire data or specify a range of data to read from using the
ReadRange property. By default,
ReadRange is set to [1 inf], indicating the file reader to read the entire data from the source. In this example, set
ReadRange to 3Fs, indicating the file reader to read the first 3 seconds of the data.
afr = dsp.AudioFileReader('speech_dft.mp3','ReadRange',[1 3*22050]); adw = audioDeviceWriter('SampleRate', afr.SampleRate); while ~isDone(afr) audio = afr(); adw(audio); end release(afr); release(adw);
Read audio data from an http web address using the
dsp.AudioFileReader System object™. Play back the data using the
audioDeviceWriter System object.
Create an audio file reader which reads data from http://audio.wgbh.org:8004/. Set the sample rate of the audio device writer to be the same as that of the audio file reader.
afr = dsp.AudioFileReader('http://audio.wgbh.org:8004/')
afr = dsp.AudioFileReader with properties: Filename: 'http://audio.wgbh.org:8004/' PlayCount: 1 SamplesPerFrame: 1024 OutputDataType: 'double' SampleRate: 44100 ReadRange: [1 Inf]
adw = audioDeviceWriter(afr.SampleRate)
adw = audioDeviceWriter with properties: Driver: 'DirectSound' Device: 'Default' SampleRate: 44100 Show all properties
Read and Play Back
Read a specific amount of data from the web address directly and play the data back using the audio device writer.
for i = 1:1000 audio = afr(); adw(audio); end
Close the input file and the audio output device.
For MP3, MPEG-4 AAC, and AVI audio files on Windows 7 or later and Linux® platforms,
dsp.AudioFileReader object can read fewer
samples than expected. On Windows platforms, this is due to a limitation in the
underlying Media Foundation framework. On Linux platforms, this is due to a limitation in the underlying GStreamer
framework. If you require sample-accurate reading, work with WAV or FLAC
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
EOF as 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 object relies on prebuilt dynamic library files
.dll files) included with MATLAB. Use the
packNGo function 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.