Reputation: 120
I have ran into a small problem trying to read a signal in MATLAB.
The audioread
function converts a audio file into its individual samples.
The audioinfo
function examines an audio file and returns information relating to the signal.
A code snippet is as follows.
clear all;
%Read in the song
mySong = audioread('IowaFightSong.mp3');
%Get some information about the song
info = audioinfo('IowaFightSong.mp3')
I would expect both functions to have the same number of samples, but they differ slightly.
audioread
returns an array with length of 3,043,056
audioinfo
has the TotalSamples value of 3,043,582
These 2 values differ by 526 samples.
When I instead run the code with one of MATLAB's sample audio files
clear all
load handel.mat
audiowrite('handel.wav',y,Fs)
info = audioinfo('handel.wav')
y = audioread('handel.wav');
Both the audioread
and audioinfo
have the same total samples, 73,113.
I am wondering if anyone can explain the discrepancy in total samples between the 2 functions?
A simple workaround is
clear all;
%Read in the song
mySong = audioread('IowaFightSong.mp3');
%Get some information about the song
info = audioinfo('IowaFightSong.mp3')
info.TotalSamples = length(mySong)
info.Duration = info.TotalSamples/info.SampleRate
Upvotes: 1
Views: 459
Reputation: 70743
WAV (wave riff) files store a given number of audio samples, 1 to 1.
mp3 files do not contain the audio samples, but instead save a lossy compressed format, where the samples are not converted to compressed format individually, but in blocks or chunks of some number. So mp3 files often truncate or pad up the number of samples needed to fill each compressed block or chunk. What you get on playing an mp3 is the number in those blocks after decompression, thus a rounded up or down number. (and containing compression artifacts).
Upvotes: 1
Reputation: 125874
The limitations section for audioinfo
says this:
For MP3 and MPEG-4 AAC audio files on Windows 7 or later and Linux platforms,
audioinfo
might report fewer samples than expected. On Linux platforms, this is due to a limitation in the underlying GStreamer framework.
And the limitations section for audioread
says this:
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.
Basically, both suffer from inaccurate sample reporting for MP3 files, but not WAV files, which is why the MATLAB sample audio file has no issues. As suggested, you should probably use a different format if accurate sample counts are critical.
Upvotes: 1