課 程 設 計 (1)
原文
Syntax
-
[y,Fs] = audioread(filename)
-
[y,Fs] = audioread(filename,samples)
Description
[y,Fs] = audioread(filename) reads data from the file named filename, and returns sampled data, y, and a sample rate for that data, Fs.
[y,Fs] = audioread(filename,samples) reads the selected range of audio samples in the file, where samples is a vector of the form [start,finish].
[y,Fs] = audioread(___,dataType) returns sampled 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
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.
Read the data back into MATLAB using audioread.
Play the audio.
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.
Read only the first 2 seconds.
Play the samples.
Create a FLAC file and read the first 2 seconds according to the previous Example. Then, view the data type of the sampled data y.
Name Size Bytes Class Attributes
y 16384x1 131072 double
The data type of y is double.
Request audio data in the native format of the file, and then view the data type of the sampled data y.
Name Size Bytes Class Attributes
y 16384x1 32768 int16
The data type of y is now int16.
Output Arguments
Audio data in the file, returned as an m-by-n matrix, where m is the number of audio samples read and n is the number of audio channels in the file.
-
If you do not specify dataType, or dataType is 'double', then y is of type double, and matrix elements are normalized values between −1.0 and 1.0.
-
If dataType is 'native', then y can be one of several MATLAB data types, depending on the file format and the BitsPerSample value of the input file. Call audioinfo to determine theBitsPerSample value 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^32 ≤ y ≤ 2^32–1 |
| 32 |
int32 |
-2^32 ≤ y ≤ 2^32–1 |
| 32 |
single |
-1.0 ≤ y ≤ +1.0 |
| 64 |
double |
-1.0 ≤ y ≤ +1.0 |
FLAC (.flac) |
8 |
uint8 |
0 ≤ y ≤ 255 |
| 16 |
int16 |
-32768 ≤ y ≤ +32767 |
| 24 |
int32 |
-2^32 ≤ y ≤ 2^32–1 |
MP3 (.mp3), MPEG-4 AAC (.m4a, .mp4), OGG (.ogg), and certain compressed WAVE files |
N/A |
single |
-1.0 ≤ y ≤ +1.0 |
| Note: Where y is single or double and the BitsPerSample is 32 or 64, values in y might exceed −1.0 or +1.0. |
Sample rate, in hertz, of audio data y, returned as a positive scalar.
Limitations
-
For MP3, MPEG-4 AAC, and AVI audio files on Windows 7 or later and Linux platforms, audioread might read fewer samples than expected. On Windows 7 platforms, 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, audioread reads MPEG-4 AAC files that contain single-channel data as stereo data.
翻譯
調用結構&描述
[y, Fs] =audioread(filename)
從以“filename”為文件名的文件中讀取數據,並返回抽樣數據y和此數據的抽樣率Fs 。
[y, Fs] =audioread(filename, samples)
以選定范圍從文件中讀取音頻樣本,其中 samples 是具有[start, finish]形式的向量。
[y, Fs] =audioread( ____, dataType)
返回數據域中與 dataType 相對應的采樣數據,dataType 可選“native(本地類型)”或“double(雙精度型)”。
例子
可以自己從本地音樂里面選擇音樂來測試。但是建議適當縮減音頻長度,不然計算量會比較大,有可能卡死電腦。剪輯音頻可以使用“格式工廠”,它也能用來轉換文件格式。
輸入 1、對於輸入文件,表中給的信息已經很詳細了,".wav"格式的音頻是一定支持的,通用的".mp3"格式在win7以上及Linux系統上都是可以讀取的。
2、 對於輸入參數 samples ,在第一種調用結構中其實是隱藏了 [1, inf] 的默認samples參數,意思是從文件頭讀到文件尾。在第二種調用格式里面我們可以指定讀取的參數范圍。要注意 start, finish 兩個參數都是正整數,而且他們的大小不能超出抽樣個數。這個很容易理解,超出了源文件的抽樣范圍就不可能有數據。在使用中,可以用 [start, inf] 表示從 start 讀到文件尾。
注意:當在win7平台上讀取MP3以及在Linux上讀取MP3&M4A格式文件時,可能會出現讀取范圍轉移的現象,這是由於底層Windows Media Foundation框架的局限性造成的。
3、dataType有兩種取值,native 或 double 。默認情況下,native 為單精度類型 single 。
輸出
y 為音頻數據,是一個mxn 的矩陣,m是讀取文件的抽樣個數,n是讀取文件的音頻信道個數。
· 在未明確 dataType 和 dataType 取 double 時,y 的類型也是 double。y 的元素為值介於-1.0和1.0之間的規范化值。
· 如果 dataType 取 native,y 可以為matlab所允許的幾種數據類型之一。至於為哪一種類型取決於輸入文件的類型,這種對應關系在上面給出的表中。
注意:當 y 為單精度或雙精度數, 且 BitPerSample 為32或64時,y 的值有可能超出 -1.0~1.0。
局限
· 對於 MP3, MPEG-4 AAC, AVI音頻文件,在win7及更高版本和Linux平台上,有可能會出現抽樣個數比預期要少的現象。
· 在Linux上讀取MPEG-4 AAC文件時,即使是單聲道數據文件,也會當成立體聲文件來讀。