Read audio file
collapse all in page
Syntax
[y,Fs] =audioread(filename)
[y,Fs] =audioread(filename,samples)
[y,Fs] =audioread(___,dataType)
Description
example
[y,Fs] =audioread(filename)
reads data fromthe file named filename
, and returns sampled data, y
,and a sample rate for that data, Fs
.
example
[y,Fs] =audioread(filename,samples)
readsthe selected range of audio samples in the file, where samples
isa vector of the form [start,finish]
.
example
[y,Fs] =audioread(___,dataType)
returnssampled data in the data range corresponding to the dataType
of 'native'
or 'double'
,and can include any of the input arguments in previous syntaxes.
Examples
collapse all
Read Complete Audio File
Open Script
Create a WAVE file from the example file handel.mat
, and read the file back into MATLAB®.
Create a WAVE (.wav
) file in the current folder.
load handel.matfilename = 'handel.wav';audiowrite(filename,y,Fs);clear y Fs
Read the data back into MATLAB using audioread
.
[y,Fs] = audioread('handel.wav');
Play the audio.
sound(y,Fs);
Read Portion of Audio File
Open Script
Create a FLAC file from the example file handel.mat
, and then read only the first 2 seconds.
Create a FLAC (.flac
) file in the current folder.
load handel.matfilename = 'handel.flac';audiowrite(filename,y,Fs);
Read only the first 2 seconds.
samples = [1,2*Fs];clear y Fs[y,Fs] = audioread(filename,samples);
Play the samples.
sound(y,Fs);
Return Audio in Native Integer Format
Open Script
Create a .flac
file, read the first 2 seconds of the file and then return audio in the native integer format.
Create a FLAC (.flac
) file in the current folder.
load handel.matfilename = 'handel.flac';audiowrite(filename,y,Fs);
Read only the first 2 seconds and specify the data and view the datatype of the sampled data y. The data type of y
is double
.
samples = [1,2*Fs];clear y Fs[y,Fs] = audioread(filename,samples);whos y
Name Size Bytes Class Attributes y 16384x1 131072 double
Request audio data in the native format of the file, and then view the data type of the sampled data y
. Note the new data type of y
.
[y,Fs] = audioread(filename,'native');whos y
Name Size Bytes Class Attributes y 73113x1 146226 int16
Input Arguments
collapse all
filename
— Name of file to read
character vector | string scalar
Name of file to read, specified as a character vector or string scalar that includes the file extension.
Depending on the location of your file, filename
can take on one of these forms.
Current folder | Specify the name of the file in Example: | ||||||||
Other folders | If the file is not in the current folder or in a folder on the MATLAB® path, then specify the full or relative path name in Example: Example: | ||||||||
Internet URL | If the file is specified as an internet uniform resource locator (URL), then Example: | ||||||||
Remote Location | If the file is stored at a remote location, then
Based on the remote location,
For more information, see Work with Remote Data. Example: |
Example: 'myFile.mp3'
Example: '../myFile.mp3'
Example: 'C:\temp\myFile.mp3'
audioread
supports the following file formats.
Platform Support | File Format |
---|---|
All platforms | AIFC (.aifc ) |
AIFF (.aiff , .aif ) | |
AU (.au ) | |
FLAC (.flac ) | |
OGG (.ogg ) | |
OPUS (.opus ) | |
WAVE (.wav ) | |
Windows® 7 (or later), Macintosh, and Linux® | MP3 (.mp3 ) |
MPEG-4 AAC (.m4a , .mp4 ) |
On Windows platforms prior to Windows 7, audioread
does not read WAVE files with MP3 encoded data.
On Windows 7 (or later) platforms, audioread
might also read any files supported by Windows Media® Foundation.
On Linux platforms, audioread
might also read any files supported by GStreamer.
audioread
can extract audio from MPEG-4 (.mp4
, .m4v
) video files on Windows 7 or later, Macintosh, and Linux, and from Windows Media Video (.wmv
) and AVI (.avi
) files on Windows 7 (or later) and Linux platforms.
Data Types: char
| string
samples
— Audio samples to read
[1,inf]
(default) | two-element vector of positive scalar integers
Audio samples to read, specified as a two-element vector ofthe form [start,finish]
, where start
and finish
arethe first and last samples to read, and are positive scalar integers.
start
must be less than or equaltofinish
.start
andfinish
mustbe less than the number of audio samples in the file,You can use
inf
to indicate thelast sample in the file.
Note
When reading a portion of some MP3 files on Windows 7 or Windows 10 platforms, audioread
might read a shifted range of samples. This is due to a limitation in the underlying Windows Media Foundation framework.
When reading a portion of MP3 and M4A files on Linux platforms, audioread
might read a shifted range of samples. This is due to a limitation in the underlying GStreamer framework.
Example: [1,100]
Data Types: double
dataType
— Data format of audio data, y
'double'
(default) | 'native'
Data format of audio data, y
, specified as one of the following:
'double' | Double-precision normalized samples. |
'native' | Samples in the native format found in the file. |
For compressed audio formats, such as MP3 and MPEG-4 AAC thatdo not store data in integer form, 'native'
defaultsto 'single'
.
Data Types: char
| string
Output Arguments
collapse all
y
— Audio data
matrix
Audio data in the file, returned as an m
-by-n
matrix,where m
is the number of audio samples read and n
isthe number of audio channels in the file.
If you do not specify
dataType
,ordataType
is'double'
, theny
isof typedouble
, and matrix elements are normalizedvalues between −1.0 and 1.0.If
dataType
is'native'
,theny
can be one of several MATLAB data types,depending on the file format and theBitsPerSample
valueof the input file. Callaudioinfo
to determinetheBitsPerSample
value of the file.File Format BitsPerSample Data Type of y DataRange of y WAVE ( .wav
)8 uint8
0 ≤ y
≤ 25516 int16
-32768 ≤ y
≤ +3276724 int32
-2^31 ≤ y
≤ 2^31–132 int32
-2^31 ≤ y
≤ 2^31–132 single
-1.0 ≤ y
≤ +1.064 double
-1.0 ≤ y
≤ +1.0WAVE ( .wav
) (u-law)8 int16
-32124 ≤ y
≤ +32124WAVE ( .wav
) (A-law)8 int16
-32256 ≤ y
≤ +32256FLAC ( .flac
)8 uint8
0 ≤ y
≤ 25516 int16
-32768 ≤ y
≤ +3276724 int32
-2^31 ≤ y
≤ 2^31–1MP3 ( .mp3
), MPEG-4 AAC (.m4a
,.mp4
), OGG (.ogg
), OPUS (.opus
), and certain compressed WAVE filesN/A single
-1.0 ≤ y
≤ +1.0
Note
Where y
is single
or double
andthe BitsPerSample
is 32 or 64, values in y
mightexceed −1.0 or +1.0.
Fs
— Sample rate
positive scalar
Sample rate, in hertz, of audio data y
, returnedas a positive scalar.
Limitations
For MP3, MPEG-4 AAC, and AVI audio files on Windows 7 orlater and Linux platforms,
audioread
mightread fewer samples than expected. On Windows 7 platforms, thisis due to a limitation in the underlying Media Foundation framework.On Linux platforms, this is due to a limitation in the underlyingGStreamer framework. If you require sample-accurate reading, workwith WAV or FLAC files.On Linux platforms,
audioread
readsMPEG-4 AAC files that contain single-channel data as stereo data.
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 R2012b
expand all
R2022a: Read Opus (.opus
) audio files
You can read Ogg Opus audio files.
R2021b: Support for thread-based environment
You can run audioread
in the background using MATLAB backgroundPool
.
R2021a: Read audio files from an HTTP or HTTPS URL
You can read audio files from an internet URL by specifying filename
as a string or character vector that contains the protocol type 'http://'
or 'https://'
.
R2020b: Read audio files from a remote location
You can read audio files stored in remote locations, such as Amazon S3, Windows Azure Blob Storage, and HDFS.
See Also
audiowrite | audioinfo | Import Data
Topics
- Read and Write Audio Files
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office