dsp.AudioFileWriter - Stream to audio file - MATLAB (original) (raw)

Description

The dsp.AudioFileWriter System object™ writes audio samples to an audio file.

To write audio samples to an audio file:

  1. Create the dsp.AudioFileWriter object and set its properties.
  2. Call the object with arguments, as if it were a function.

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

Creation

Syntax

Description

`afw` = dsp.AudioFileWriter returns an audio file writer System object, afw. This object writes audio samples to an audio file.

`afw` = dsp.AudioFileWriter(`Filename`) returns an audio file writer System object, afw. This object has theFilename property set toFilename.

`afw` = dsp.AudioFileWriter(`Name=Value`) sets properties using one or more name-value arguments. For example, to set the quality setting for compression as 50, set Quality to 50.

example

Properties

expand all

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

If a property is tunable, 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 the audio file as a character vector or a string scalar.

The Filename property 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. For an example, see Tunable Audio File Name in Generated Code.

Specify which audio file format the object writes. On Microsoft® platforms, select one of"AVI", "WAV","FLAC", "OGG","MPEG4","OPUS", "WMA", or "MP3". On Linux® platforms, select one of"AVI", "WAV","FLAC","OPUS", "OGG", or"MP3". On macOS platforms, select one of "AVI","WAV", "FLAC","OGG", "OPUS","MPEG4", or"MP3". These abbreviations correspond to the following file formats:

The default is "WAV".

Since R2024a

Quality setting for variable bitrate compression, specified as a scalar in the range [0,100], where 0 is lower quality and higher compression, and 100 is higher quality and lower compression.

MATLAB® encodes "OGG","OPUS", and"MP3" files with variable bitrate (VBR) codecs. These codecs select the appropriate bit rate for each encoded frame depending on the perceptual complexity of the signal and the value you specify for the Quality property.

Dependencies

To enable this property, setFileFormat to"OGG","OPUS", or"MP3".

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

Since R2024a

Bit rate of compressed audio files in kilobits per second (kb/s), specified as 64, 96, 128, 160, 192, 256, or 320. On a Windows® system, the only valid values are 192, 160,128, or 96.

In general, a larger BitRate value results in higher perceived audio quality.

Dependencies

To enable this property, setFileFormat to"MPEG4".

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

Specify the sample rate of the input audio data as a positive, numeric scalar value.

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

Specify the type of compression algorithm the audio file writer uses to compress the audio data. Compression reduces the size of the audio file. Select"None (uncompressed)" to save uncompressed audio data to the file. The other options available reflect the audio compression algorithms installed on your system. You can use tab completion to query valid Compressor options for your computer by typingH.Compressor = ' and then pressing the tab key.

Dependencies

This property applies when writing WAV or AVI files on Windows platforms.

Specify the type of uncompressed audio data written to the file as "int16","double","single","inherit","int24","int32", or"uint8".

Dependencies

This property only applies when writing uncompressed WAV files.

Usage

Syntax

Description

afw([audio](#d126e243722)) writes one frame of audio samples, audio, to the output file specified byFilename. audio is either a vector for mono audio input or an _M_-by-N matrix for _N_-channel audio input respectively.

example

Input Arguments

expand all

One frame of audio samples, returned as a column vector or a matrix. A column vector input indicates a mono audio input. An_M_-by-N matrix indicates an_N_-channel audio input.

If the input is fixed-point, the input must be a signed fixed-point input with power-of-two slope and zero bias.

Data Types: single | double | int16 | int32 | uint8 | fi

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 named obj, use this syntax:

expand all

step Run System object algorithm
release Release resources and allow changes to System object property values and input characteristics
reset Reset internal states of System object

Examples

collapse all

Decimate an audio signal, and write it to disk as a WAV file.

afr = dsp.AudioFileReader("OutputDataType",... "double"); firdec = dsp.FIRDecimator; % decimate by 2 afw = dsp.AudioFileWriter... ("speech_dft.wav", ... SampleRate=afr.SampleRate/2);

while ~isDone(afr) audio = afr(); audiod = firdec(audio); afw(audiod); end

release(afr); release(afw);

Generate a MEX file from a function named writeAudio. This function reads an audio signal from the funky-stereo.wav file, decimates the signal by a factor of 2, and writes the decimated signal to a specified output file.

The dsp.AudioFileReader object reads the audio signal from funky-stereo.wav file. The funky-stereo.wav file has two channels, a sample rate of 44100 Hz, and a bit rate of 1411 kbps. The CodegenPrototypeFile property of the object is set to rock-stereo.wav file. The rock-stereo.wav file has the same file attributes, such as the number of audio channels, sample rate, and bit rate, as the funky-stereo.wav file. The dsp.FIRDecimator object decimates the input audio signal by a factor of 2. The dsp.AudioFileWriter object writes the decimated signal to the output file myoutput.wav. Due to the decimation process, the output file has a sample rate of 22050 Hz and a bit rate of 2822 kbps.

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 the codegen function.

codegen writeAudio -args {readfilename,writefilename}

Code generation successful.

writeAudio_mex('funky-stereo.wav','myoutput.wav');

Limitations

The following platform-specific restrictions apply when writing these files:

Versions Windows 7 and later

macOS

Linux

Algorithms

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

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced in R2012a

expand all

You can now specify signal quality and signal bitrate in thedsp.AudioFileWriter object for these compressed audio file extensions:

The dsp.AudioFileWriter object supports writing MP3 files (.mp3).

The dsp.AudioFileWriter object supports OPUS file format (.opus).