Main Content

audioread

Read audio file

Description

example

[y,Fs] = audioread(filename)reads data from the file namedfilename, and returns sampled data,y, and a sample rate for that data,Fs.

example

[y,Fs] = audioread(filename,samples)reads the selected range of audio samples in the file, wheresamplesis a vector of the form[start,finish].

example

[y,Fs] = audioread(___,dataType)returns sampled data in the data range corresponding to thedataTypeof'native'or'double', and can include any of the input arguments in previous syntaxes.

Examples

collapse all

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

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

loadhandel.matfilename ='handel.wav'; audiowrite(filename,y,Fs); clearyFs

Read the data back into MATLAB usingaudioread.

[y,Fs] = audioread('handel.wav');

Play the audio.

sound(y,Fs);

Create a FLAC file from the example filehandel.mat, and then read only the first 2 seconds.

Create a FLAC (.flac) file in the current folder.

loadhandel.matfilename ='handel.flac'; audiowrite(filename,y,Fs);

只读第一个2秒。

samples = [1,2*Fs]; clearyFs[y,Fs] = audioread(filename,samples);

Play the samples.

sound(y,Fs);

Create a.flacfile, 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.

loadhandel.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 ofyisdouble.

samples = [1,2*Fs]; clearyFs[y,Fs] = audioread(filename,samples); whosy
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 datay. Note the new data type ofy.

[y,Fs] = audioread(filename,'native'); whosy
Name Size Bytes Class Attributes y 73113x1 146226 int16

Input Arguments

collapse all

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,filenamecan take on one of these forms.

Current folder

Specify the name of the file infilename.

Example:'sample_audio.wav'

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 infilename.

Example:'C:\myFolder\myFile.sample_audio.wav'

Example:'myFolder\sample_audio.mp3'

Internet URL

If the file is specified as an internet uniform resource locator (URL), thenfilenamemust contain the protocol type'http://'or'https://'.

Example:'http://hostname/path_to_file/sample_audio.mp3'

Remote Location

If the file is stored at a remote location, thenfilenamemust contain the full path of the file specified with the form:

scheme_name://path_to_file/my_file.ext

Based on the remote location,scheme_namecan be one of the values in this table.

Remote Location scheme_name
Amazon S3™ s3
Windows Azure®Blob Storage wasb,wasbs
HDFS™ hdfs

For more information, seeWork with Remote Data.

Example:'s3://bucketname/path_to_file/sample_audio.mp3'

Example:'myFile.mp3'

Example:'../myFile.mp3'

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

audioreadsupports 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,audioreaddoes not read WAVE files with MP3 encoded data.

On Windows 7 (or later) platforms,audioreadmight also read any files supported by Windows Media®Foundation.

On Linux platforms,audioreadmight also read any files supported by GStreamer.

audioreadcan extract audio from MPEG-4 (.mp4,.m4v) video files onWindows 7or 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

Audio samples to read, specified as a two-element vector of the form[start,finish], wherestartandfinishare the first and last samples to read, and are positive scalar integers.

  • startmust be less than or equal tofinish.

  • startandfinish必须小于the number of audio samples in the file,

  • You can useinfto indicate the last sample in the file.

Note

When reading a portion of some MP3 files onWindows 7platforms,audioreadmight 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,audioreadmight read a shifted range of samples. This is due to a limitation in the underlying GStreamer framework.

Example:[1,100]

Data Types:double

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 that do not store data in integer form,'native'defaults to'single'.

Data Types:char|string

Output Arguments

collapse all

Audio data in the file, returned as anm-by-nmatrix, wheremis the number of audio samples read andnis the number of audio channels in the file.

  • If you do not specifydataType, ordataTypeis'double', thenyis of typedouble, and matrix elements are normalized values between −1.0 and 1.0.

  • IfdataTypeis'native', thenycan be one of several MATLAB data types, depending on the file format and theBitsPerSamplevalue of the input file. Callaudioinfoto determine theBitsPerSamplevalue of the file.

    File Format BitsPerSample Data Type of y Data Range of y
    WAVE (.wav) 8 uint8 0 ≤y≤ 255
    16 int16 -32768 ≤y≤ +32767
    24 int32 -2^31 ≤y≤ 2^31–1
    32 int32 -2^31 ≤y≤ 2^31–1
    32 single -1.0 ≤y≤ +1.0
    64 double -1.0 ≤y≤ +1.0
    WAVE (.wav) (u-law) 8 int16 -32124 ≤y≤ +32124
    WAVE (.wav) (A-law) 8 int16 -32256 ≤y≤ +32256
    FLAC (.flac) 8 uint8 0 ≤y≤ 255
    16 int16 -32768 ≤y≤ +32767
    24 int32 -2^31 ≤y≤ 2^31–1
    MP3 (.mp3), MPEG-4 AAC (.m4a,.mp4), OGG (.ogg), OPUS (.opus), and certain compressed WAVE files N/A single -1.0 ≤y≤ +1.0

Note

Whereyissingleordoubleand theBitsPerSampleis 32 or 64, values inymight exceed −1.0 or +1.0.

Sample rate, in hertz, of audio datay, returned as a positive scalar.

Limitations

  • For MP3, MPEG-4 AAC, and AVI audio files onWindows 7or later and Linux platforms,audioreadmight read fewer samples than expected. OnWindows 7platforms, 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.

  • On Linux platforms,audioreadreads MPEG-4 AAC files that contain single-channel data as stereo data.

Extended Capabilities

Version History

Introduced in R2012b

expand all