Main Content

dsp。AudioFileReader

Stream from audio file

Description

Thedsp。AudioFileReaderSystem object™ reads audio samples from an audio file.

To read audio samples from an audio file:

  1. Create thedsp。AudioFileReaderobject and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, seeWhat Are System Objects?

Creation

Description

afr= dsp.AudioFileReaderreturns an audio file reader System object,afrthat reads audio from an audio file.

example

afr= dsp.AudioFileReader(File name)returns an audio file reader object,afr, withFilenameproperty set toFile name.

example

afr= dsp.AudioFileReader(Name,Value)returns an audio file reader System object,afr, with each specified property set to the specified value. Enclose each property name in single quotes. Unspecified properties have default values.

Properties

expand all

Unless otherwise indicated, properties arenontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and thereleasefunction unlocks them.

If a property istunable, you can change its value at any time.

For more information on changing property values, seeSystem Design in MATLAB Using System Objects.

Specify the name of an audio file as a character vector or a string scalar. IfFilenameIsTunableInCodegenis set tofalseand the file is on MATLAB®path, then you do not need to specify the full name of the file. IfFilenameIsTunableInCodegenis set totrue, then the file name must either exist in the current directory, or you must specify the full file path. The file name can be an http web address like'http://audio.wgbh.org:8004/'. For an example, seeRead and Play Back Audio File from http Web Address.

TheFilenameproperty is tunable in generated code. That is, you can pass the name of the audio file as an input while running the code generated from this object. File attributes such as the audio format, the number of audio channels, the sample rate, and the bit rate are not tunable and must match the attributes of the prototype audio file you specify through theCodegenPrototypeFileproperty. The specified prototype audio file determines the attributes and the type of audio files that can be read by the generated code. For an example, seeTunable Audio File Name in Generated Code.

The following table lists the supported audio file formats.

Platforms File Name Extensions
Windows® .wav,.wma,.avi,.aif,.aifc,.aiff,.mp3,.au,.snd, .mp4, .m4a, .flac, .ogg, .mov
Non-Windows .avi, .mp3, .mp4, .m4a, .wav, .flac, .ogg, .aif, .aifc, .aiff, .au, .snd, .mov

Specify a positive integer as the number of times to play the file.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Specify the number of samples in an audio frame as a positive, scalar integer value.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Set the data type of the audio data output from the audio file reader object. Specify the data type as'double','single','int16', or'uint8'.

Set this property to true to enable tunability ofFilename在生成的C/C++ code. The file with the specifiedFilenamemust either exist in the current directory, or you must specify the full file path.

IfFilenameIsTunableInCodegenis set totrue, file attributes such as the audio format, the number of audio channels, the sample rate, and the bit rate are not tunable and must match the attributes of the prototype audio file you specify through theCodegenPrototypeFileproperty. The specified prototype audio file determines the attributes and the type of audio files that can be read by the generated code. For example, if the specified prototype file is a.wavfile, then the generated code can only read.wavfiles. If the specified prototype file sample rate is 44100 Hz, then the generated code can read files with a sample rate of 44100 Hz.

Data Types:logical

Specify the name of the prototype audio file used in code generation 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/'.

The attributes of the audio file specified inFilename(such as the audio format, the number of audio channels, the sample rate, and the bite rate) must match the attributes of the audio file specified inCodegenPrototypeFile. The specified prototype audio file determines the attributes and the type of audio files that can be read by the generated code. For example, if the specified prototype file is a.wavfile, then the generated code can only read.wavfiles. If the specified prototype file sample rate is 44100 Hz, then the generated code can read files with a sample rate of 44100 Hz.

The following table lists the supported audio file formats:

Platforms File Name Extensions
Windows .wav,.wma,.avi,.aif,.aifc,.aiff,.mp3,.au,.snd, .mp4, .m4a, .flac, .ogg, .mov
Non-Windows .avi, .mp3, .mp4, .m4a, .wav, .flac, .ogg, .aif, .aifc, .aiff, .au, .snd, .mov

Dependencies

This property applies only when theFilenameIsTunableInCodegenproperty is set totrue.

This property is read-only.

This property displays the sample rate, in Hz, of the audio file.

Data Types:double

Specify the sample range from which to read, as a vector in the form of [StartSampleEndSample], whereStartSampleis the sample at which file reading starts, andEndSampleis the sample at which file reading stops.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Usage

Description

example

audio= afr()outputs one frame of audio samples,audio. You can specify the number of times to play the file using thePlayCountproperty. After playing the file for the number of times you specify,audiocontains silence.

[audio,eof] = afr()returns an end-of-file indicator,eof.eofis true each time the outputaudio包含了去年在音频样本e file.

Output Arguments

expand all

One frame of audio samples, returned as a column vector of length equal to the value you specify in theSamplesPerFrameproperty. The data type of the audio output is specified in theOutputDataTypeproperty.

Data Types:single|double|int16|uint8

End-of-file indicator, returned as either a1or a0. A value of1is output whenaudio包含了去年在音频样本e file.

Data Types:logical

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object namedobj, use this syntax:

release(obj)

expand all

info Information about specific audio file
isDone End-of-file status (logical)
step RunSystem objectalgorithm
release Release resources and allow changes toSystem objectproperty values and input characteristics
reset Reset internal states ofSystem object

Examples

collapse all

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 equivalentstepsyntax. For example,obj()becomesstep(obj).

You can choose to read the entire data or specify a range of data to read from using theReadRangeproperty. By default,ReadRangeis set to [1 inf], indicating the file reader to read the entire data from the source. In this example, setReadRangeto 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);endrelease(afr); release(adw);

Read audio data from an http web address using thedsp。AudioFileReaderSystem object™. Play back the data using theaudioDeviceWriterSystem object.

Initialization

Create an audio file reader which reads data fromhttp://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.

fori = 1:1000 audio = afr(); adw(audio);end

Close the input file and the audio output device.

release(afr) release(adw)

Generate a MEX file from a function namedwriteAudio. This function reads an audio signal from thefunky-stereo.wavfile, decimates the signal by a factor of 2, and writes the decimated signal to a specified output file.

Thedsp。AudioFileReaderobject reads the audio signal fromfunky-stereo.wavfile. Thefunky-stereo.wavfile has two channels, a sample rate of 44100 Hz, and a bit rate of 1411 kbps. TheCodegenPrototypeFileproperty of the object is set torock-stereo.wavfile. Therock-stereo.wavfile has the same file attributes, such as the number of audio channels, sample rate, and bit rate, as thefunky-stereo.wavfile. Thedsp。FIRDecimatorobject decimates the input audio signal by a factor of 2. Thedsp。AudioFileWriterobject writes the decimated signal to the output filemyoutput.wav. Due to the decimation process, the output file has a sample rate of 22050 Hz and a bit rate of 2822 kbps.

typewriteAudio.m
function writeAudio(readfile,writefile) afr = dsp.AudioFileReader('FilenameIsTunableInCodegen',true,... 'CodegenPrototypeFile','rock-stereo.wav'); afr.Filename = readfile; % Filename is funky-stereo.wav and CodegenPrototypeFile is % rock-stereo.wav. firdec = dsp.FIRDecimator(2,'auto'); % decimate by 2 afw = dsp.AudioFileWriter('SampleRate',22050); afw.Filename = writefile; while ~isDone(afr) audio = afr(); audiod = firdec(audio); afw(audiod); end release(afr); release(afw); end

For generating code, specify file names to be variable-length character vectors of maximum length 500.

readfilename = coder.typeof('a',[1 500],[0 1]); writefilename = coder.typeof('b',[1 500],[0 1]);

Generate a MEX file using thecodegenfunction.

codegenwriteAudio-args{readfilename,writefilename}
Code generation successful.
writeAudio_mex('funky-stereo.wav','myoutput.wav');

Limitations

For MP3, MPEG-4 AAC, and AVI audio files onWindows 7or later and Linux®platforms,dsp。AudioFileReaderobject 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 files.

Algorithms

This object implements the algorithm, inputs, and outputs described on theFrom Multimedia Fileblock reference page. The object properties correspond to the block parameters, except:

  • The object has no corresponding property for theInherit sample time from fileblock parameter. The object always inherits the sample time from the file.

  • The object has no corresponding property for theOutput end-of-file indicatorparameter. The object always outputsEOFas the last output.

  • The object has no corresponding property for theMultimedia Outputsparameter because audio is the only supported output.

  • The object has no corresponding property for theImage signalblock parameter.

  • The object has no corresponding property for theOutput color formatparameter.

  • The object has no corresponding property for theVideo output data typeparameter.

Extended Capabilities

Introduced in R2012a