Dan Ellis : Resources : Matlab :

mp3read for Matlab

Introduction

This is an m-file I wrote to read mp3 audio files (i.e. files compressed using MPEG-Audio layer 3 encoding) into Matlab. The script started out little, but has now become somewhat larger. Rather than actually decoding the mp3 stream, it cheats by calling an external mp3 decoder program to convert the file to wav, then reading in that temporary file. But it does do some tricks like only decompressing the part of the file that is required rather than the whole thing; this can be useful for accessing very large mp3 files.

Although it's called mp3read, it will, in fact, read any MPEG Audio file recognized by the underlying Unix utilities.

The function actually uses two freely available Unix utilities. As such, it works principally on Unix versions of Matlab (e.g. Linux and Mac OS X). However, these utilities have been ported to Windows, so you should be able to get it to work there also (more details at the bottom of the page).

Specifically, you need the mpg123 decoder (note: mpg321 is not an adequate substitute, because it lacks certain options), and the mp3info scanner. You can find them here:

The syntax of mp3read() attempts to duplicate wavread() as closely as possible, including trying to duplicate the OPTS.fmt fields and the 'size' syntax etc. Because mpg123 supports on-the-fly downsampling by 2 or 4, and conversion to mono, these are supported as options beyond the first two arguments.

Here is the function: mp3read.m

Here are some examples of use:

Using under Windows

As mentioned above, the mp3read Matlab function depends on two external programs, mpg123 and mp3info, which were originally developed for Linux. To make the script work on a Windows system, you need versions of these programs that work on that architecture. Keansub Lee had made these available at:

http://www.columbia.edu/~kl2074/mp3codec/

To make them work, you also need to make small changes to the mp3read.m script. First, you must change the location that the script looks for binaries e.g. change the lines:

    %%%%%% Location of the binaries
    mpg123 = '/usr/bin/mpg123';
    mp3info = '/usr/bin/mp3info';

to point to the directory where you put the binaries above, e.g.

    mpg123 = 'C:\Matlab6p5\work\project\mpg123.exe';
    mp3info = 'C:\Matlab6p5\work\project\mp3info.exe';

Secondly, you must also change the directory where temporary files are written, as set up around line 119:

    % Temporary file to use
    tmpfile = ['/tmp/tmp',num2str(round(1000*rand(1))),'.wav'];

.. to be a suitable directory for temporary files on your system, e.g.

    tmpfile = ['C:\Matlab6p5\work\project\tmp',num2str(round(1000*rand(1))),'.wav'];

There is a third change required for operation on a plain Windows system. Around line 150, the code:

  % Delete tmp file
  mysystem(['rm ', tmpfile]);

becomes

  % Delete tmp file
  mysystem(['del ', tmpfile]);

Thanks to Keansub Lee and Meredith Morris for their help.


Last updated: $Date: 2005/04/27 13:58:20 $

Dan Ellis <dpwe@ee.columbia.edu>