audiowrite
Write audio file
Description
audiowrite(
writes
a matrix of audio data, filename
,y
,Fs
)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
.
audiowrite(
uses
additional options specified by one or more filename
,y
,Fs
,Name,Value
)Name,Value
pair
arguments.
Examples
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 by using the audioinfo
function.
info = audioinfo(filename) ;
The info
structure contains the following information fields: Filename
, CompressionMethod
, NumChannels
, SampleRate
, TotalSamples
, Duration
, Title
, Comment
, Artist
, and BitsPerSample
.
Input Arguments
filename
— Name of file to write
character vector | string scalar
Name of file to write, or the full path to the file, specified as a character vector or string scalar that includes the file extension.
Depending on the location you are writing to, filename
can take on one of these forms.
Location | Form | ||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder | To write to the current folder, specify the
name of the file in
Example: | ||||||||
Other folders | To write to a folder different from the current
folder, specify the full or relative path name in
Example:
Example:
| ||||||||
Remote Location | To write to a remote location,
Based on your
remote location,
For more information, see Work with Remote Data. Example:
|
audiowrite
supports the following file formats.
Platform Support | File Format | Abbreviation | File Extension |
---|---|---|---|
All platforms | Free Lossless Audio Codec | FLAC |
|
MPEG-1 Audio Layer III | MP3 |
| |
OGG Vorbis | OGG |
| |
OGG Opus | OPUS |
| |
Microsoft WAVE sound | WAV |
| |
Windows® and Mac | MPEG-4 Part 3 AAC | MP4 |
|
Example: 'myFile.m4a'
Example: '../myFile.m4a'
Example: 'C:\temp\myFile.m4a'
When writing MP4 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.
On Linux® platforms, these requirements apply:
To write MP3 files with
audiowrite
, you must havelibsndfile 1.1.0
or a later version installed. You must also have the LAME MP3 encoder and the mpg123 MP3 decoder installed.To write OPUS files with
audiowrite
, you must havelibsndfile 1.0.29
or a later version installed. You must also have the OPUS codec installed.
Data Types: char
| string
y
— Audio data to write
real matrix
Audio data to write, specified as an m
-by-n
real 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 Format | Maximum Number of Channels |
---|---|
WAV | 1024 |
OGG, OPUS | 255 |
FLAC | 8 |
MP3, MP4 | 2 |
The valid range for the data in y
depends
on the data type of y
.
Data Type of y | Valid Range for y |
---|---|
| 0 ≤ |
| -32768 ≤ |
| -2^31 ≤ |
| -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 rate
positive integer scalar
Sample rate, in hertz, of audio data y
, specified as a positive integer
scalar greater than 0.
When writing to MP3 files,
audiowrite
supports only sample rates of 48,000 Hz, 44,100 Hz, 32,000 Hz, 24,000 Hz, 22,050 Hz, 16,000 Hz, 12,000 Hz, 11,025 Hz, and 8,000 Hz.When writing to OPUS files,
audiowrite
supports only sample rates of 48,000 Hz, 24,000 Hz, 16,000 Hz, 12,000 Hz, and 8,000 Hz.When writing to MP4 files on Windows platforms,
audiowrite
supports only sample rates of 48,000 Hz and 44,100 Hz.
Example: 44100
Data Types: double
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
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 sample
16 (default) | 8 | 24 | 32 | 64
Number of output bits per sample, specified as the comma-separated pair consisting of
'BitsPerSample'
and an integer.
Only available for FLAC and WAV 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. On Windows 7 or later, the only valid values are 192, 160, 128, and
96.
In general, a larger BitRate
value results
in higher compression quality.
Only available for MP4 files.
Example: 'BitRate',96
Quality
— Quality setting for compression
75 (default) | value in the range [0, 100]
Quality setting for MP3, Vorbis, or OPUS compression, 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 MP3, OGG, and OPUS files.
Example: 'Quality',25
Title
— Title information
[]
(default) | character vector | string scalar
Title information, specified as the comma-separated pair consisting of
'Title'
and a character vector or string
scalar.
Data Types: char
| string
Artist
— Artist information
[]
(default) | character vector | string scalar
Artist information, specified as the comma-separated pair consisting of
'Artist'
and a character vector or string
scalar.
Data Types: char
| string
Comment
— Additional information
[]
(default) | character vector | string scalar
Additional information, specified as the comma-separated pair consisting of
'Comment'
and a character vector or string
scalar.
Data Types: char
| string
Note
On Mac platforms, audiowrite
does not write the
'Title'
, 'Artist'
, or
'Comment'
fields to MP4 files.
Algorithms
The output data type is determined by the file
format, the data type of y
, and the specified output BitsPerSample
.
File Formats | Data Type of | Output Bits Per Sample | Output Data Type |
---|---|---|---|
WAV |
| 8 |
|
16 |
| ||
24 |
| ||
| 32 |
| |
| 32 |
| |
| 64 |
| |
FLAC |
| 8 |
|
16 |
| ||
24 |
| ||
MP3, MP4, OGG, OPUS |
| N/A |
|
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced in R2012bR2023a: Write MP3 (.mp3
) audio files
You can write MP3 audio files.
R2022a: Write Opus (.opus
) audio files
You can write Ogg Opus audio files.
R2021b: Support for thread-based environment
You can run audiowrite
in the background using MATLAB®
backgroundPool
.
R2020b: Write audio files to a remote location
You can write audio files to remote locations, such as Amazon S3, Windows Azure Blob Storage, and HDFS.
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)