Documentation Center

  • Trial Software
  • Product Updates
라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

audiowrite

Write audio file

Syntax

  • audiowrite(filename,y,Fs) example
  • audiowrite(filename,y,Fs,Name,Value) example

Description

example

audiowrite(filename,y,Fs) writes a matrix of audio data, y, with sample rate Fs to a file called filename. The filename input also specifies the output file format. The output data type depends on the output file format and the data type of the audio data, y.

example

audiowrite(filename,y,Fs,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

Examples

expand all

Write an Audio File

Create a WAVE file from the example file handel.mat, and read the file back into MATLAB®.

Write a WAVE (.wav) file in the current folder.

load handel.mat

filename = 'handel.wav';
audiowrite(filename,y,Fs);
clear y Fs

Read the data back into MATLAB using audioread.

[y,Fs] = audioread(filename);

Listen to the audio.

sound(y,Fs);

Specify Bits Per Sample and Metadata

Create a FLAC file from the example file handel.mat and specify the number of output bits per sample and a comment.

load handel.mat

filename = 'handel.flac';
audiowrite(filename,y,Fs,'BitsPerSample',24,...
'Comment','This is my new audio file.');
clear y Fs

View information about the new FLAC file.

info = audioinfo(filename)
info = 

             Filename: 'S:\handel.flac'
    CompressionMethod: 'FLAC'
          NumChannels: 1
           SampleRate: 8192
         TotalSamples: 73113
             Duration: 8.9249
                Title: []
              Comment: 'This is my new audio file.'
               Artist: []
        BitsPerSample: 24

Input Arguments

expand all

filename — Name of file to writestring

Name of file to write, or the full path to the file, specified as a string that includes the file extension. If a path is specified, it can be absolute or relative. If you do not specify the path, then the destination directory is the current working directory.

audiowrite supports the following file formats.

Platform SupportFile Format
All platformsWAVE (.wav)
OGG (.ogg)
FLAC (.flac)
Windows® and MacMPEG-4 AAC (.m4a, .mp4)

Example: 'myFile.m4a'

Example: '../myFile.m4a'

Example: 'C:\temp\myFile.m4a'

When writing AAC files on Windows, audiowrite pads the front and back of the output signal with extra samples of silence. The Windows AAC encoder also places a very sharp fade-in and fade-out on the audio. This results in audio with an increased number of samples after being written to disk.

y — Audio data to writematrix

Audio data to write, specified as an m-by-n matrix, where m is the number of audio samples to write and n is the number of audio channels to write.

If either m or n is 1, then audiowrite assumes that this dimension specifies the number of audio channels, and the other dimension specifies the number of audio samples.

The maximum number of channels depends on the file format.

File FormatMaximum Number of Channels
WAVE (.wav)256
OGG (.ogg)255
FLAC (.flac)8
MPEG-4 AAC (.m4a, .mp4)2

The valid range for the data in y depends on the data type of y.

Data Type of yValid Range for y
uint80 ≤ y ≤ 255
int16-32768 ≤ y ≤ +32767
int32-2^32 ≤ y ≤ 2^32–1
single-1.0 ≤ y ≤ +1.0
double-1.0 ≤ y ≤ +1.0

Data beyond the valid range is clipped.

If y is single or double, then audio data in y should be normalized to values in the range −1.0 and 1.0, inclusive.

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

Fs — Sample ratepositive scalar

Sample rate, in hertz, of audio data y, specified as a positive scalar greater than 0. Values of Fs are truncated to integer boundaries. When writing to .m4a or .mp4 files on Windows platforms, audiowrite supports only samples rates of 44100 and 48000.

Example: 44100

Data Types: double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Title','Symphony No. 9','Artist','My Orchestra' instructs audiowrite to write an audio file with the title "Symphony No. 9" and the artist information "My Orchestra."

'BitsPerSample' — Number of output bits per sample16 (default) | 8 | 24 | 32 | 64

Number of output bits per sample, specified as the comma-separated pair consisting of 'BitsPerSample' and a number.

Only available for WAVE (.wav) and FLAC (.flac) files. For FLAC files, only 8, 16, or 24 bits per sample are supported.

Example: 'BitsPerSample',32

'BitRate' — Kilobits per second (kbit/s)128 (default) | 64 | 96 | 160 | 192 | 256 | 320

Number of kilobits per second (kbit/s) used for compressed audio files, specified as the comma-separated pair consisting of 'BitRate' and an integer. Noninteger values are truncated. On Windows 7 or later, the only valid values are 96, 128, 160, and 192.

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

Only available for MPEG-4 (.m4a, .mp4) files.

Example: 'BitRate',96

'Quality' — Quality setting for the Ogg Vorbis Compressor75 (default) | value in the range [0 100]

Quality setting for the Ogg Vorbis Compressor, specified as the comma-separated pair consisting of 'Quality' and a number in the range [0 100], where 0 is lower quality and higher compression, and 100 is higher quality and lower compression.

Only available for OGG (.ogg) files.

Example: 'Quality',25

'Title' — Title information[] (default) | string

Title information, specified as the comma-separated pair consisting of 'Title' and a string.

'Artist' — Artist information[] (default) | string

Artist information, specified as the comma-separated pair consisting of 'Artist' and a string.

'Comment' — Additional information[] (default) | string

Additional information, specified as the comma-separated pair consisting of 'Comment' and a string.

    Note:   On Mac platforms, audiowrite writes metadata to WAVE, OGG, and FLAC files only, and will not write the 'Title', 'Author', or 'Comment' fields to MPEG-4 AAC files.

More About

expand all

Algorithms

The output data type is determined by the file format, the data type of y, and the specified output BitsPerSample.

File FormatsData Type of yOutput BitsPerSampleOutput Data Type
WAVE (.wav), uint8, int16, int32, single, double8uint8
16int16
24int32
uint8, int16, int3232int32
single, double32single
single, double64double
FLAC (.flac)uint8, int16, int32, single, double8int8
16int16
24int32
MPEG-4 (.m4a, .mp4),
OGG (.ogg)
uint8, int16, int32, single, doubleN/Asingle

See Also

|

Was this topic helpful?