annotate toolboxes/mp3readwrite/demo_mp3readwrite.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 %% MP3 reading and writing
Daniel@0 2 %
Daniel@0 3 % These function, mp3read and mp3write, aim to exactly duplicate
Daniel@0 4 % the operation of wavread and wavwrite for accessing soundfiles,
Daniel@0 5 % except the soundfiles are in Mpeg-Audio layer 3 (MP3) compressed
Daniel@0 6 % format. All the hard work is done by external binaries written
Daniel@0 7 % by others: mp3info to query the format of existing mp3 files,
Daniel@0 8 % mpg123 to decode mp3 files, and lame to encode audio files.
Daniel@0 9 % Binaries for these files are widely available (and may be
Daniel@0 10 % included in this distribution).
Daniel@0 11 %
Daniel@0 12 % These functions were originally developed for access to very
Daniel@0 13 % large mp3 files (i.e. many hours long), and so avoid creating
Daniel@0 14 % the entire uncompressed audio stream if possible. mp3read
Daniel@0 15 % allows you to specify the range of frames you want to read
Daniel@0 16 % (as a second argument), and mp3read will construct an mpg123
Daniel@0 17 % command that skips blocks to decode only the part of the file
Daniel@0 18 % that is required. This can be much quicker (and require less
Daniel@0 19 % memory/temporary disk) than decoding the whole file.
Daniel@0 20 %
Daniel@0 21 % mpg123 also provides for "on the fly" downsampling at conversion
Daniel@0 22 % to mono, which are supported as extra options in mp3read.
Daniel@0 23 %
Daniel@0 24 % mpg123 can read MP3s across the network. This is supported
Daniel@0 25 % if the FILE argument is a URL (e.g. beginning 'http://...').
Daniel@0 26 %
Daniel@0 27 % mp3info sometimes gets the file size wrong (as returned by the
Daniel@0 28 % mp3read(...'size') syntax). I'm not sure when this happens
Daniel@0 29 % exactly, but it's probably a result of VBR files. In the worst
Daniel@0 30 % case, figuring the number of samples in such a file requires
Daniel@0 31 % scanning through the whole file, and mp3info doesn't usually do
Daniel@0 32 % this.
Daniel@0 33 %
Daniel@0 34 % For more information, including advice on handling MP4 files,
Daniel@0 35 % see http://labrosa.ee.columbia.edu/matlab/mp3read.html
Daniel@0 36
Daniel@0 37 %% Example usage
Daniel@0 38 % Here, we read a wav file in, then write it out as an MP3, then
Daniel@0 39 % read the resulting MP3 back in, and compare it to the original
Daniel@0 40 % file.
Daniel@0 41
Daniel@0 42 % Read an audio waveform
Daniel@0 43 [d,sr] = wavread('piano.wav');
Daniel@0 44 % Save to mp3 (default settings)
Daniel@0 45 mp3write(d,sr,'piano.mp3');
Daniel@0 46 % Read it back again
Daniel@0 47 [d2,sr] = mp3read('piano.mp3');
Daniel@0 48 % mp3 encoding involves some extra padding at each end; we attempt
Daniel@0 49 % to cut it off at the start, but can't do that at the end, because
Daniel@0 50 % mp3read doesn't know how long the original was. But we do, so..
Daniel@0 51 % Chop it down to be the same length as the original
Daniel@0 52 d2 = d2(1:length(d),:);
Daniel@0 53 % What is the SNR (distortion)?
Daniel@0 54 ddiff = d - d2;
Daniel@0 55 disp(['SNR is ',num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),' dB']);
Daniel@0 56 % Do they look similar?
Daniel@0 57 subplot(211)
Daniel@0 58 specgram(d(:,1),1024,sr);
Daniel@0 59 subplot(212)
Daniel@0 60 plot(1:5000,d(10000+(1:5000),1),1:5000,d2(10000+(1:5000)));
Daniel@0 61 % Yes, pretty close
Daniel@0 62 %
Daniel@0 63 % NB: lame followed by mpg123 causes a little attenuation; you
Daniel@0 64 % can get a better match by scaling up the read-back waveform:
Daniel@0 65 ddiff = d - 1.052*d2;
Daniel@0 66 disp(['SNR is ',num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),' dB']);
Daniel@0 67
Daniel@0 68 %% Delay, size, and alignment
Daniel@0 69 %
Daniel@0 70 % In mid-2006 I noticed that mp3read followed by mp3write followed by
Daniel@0 71 % mp3read effectively delayed the waveform by 2257 samples (at 44
Daniel@0 72 % kHz). So I introduced code to discard the first 2257 samples to ensure
Daniel@0 73 % that the waveforms remained time aligned. As best I could understand,
Daniel@0 74 % mpg123 (v 0.5.9) was including the "warm-up" samples from the
Daniel@0 75 % synthesis filterbank which are more properly discarded.
Daniel@0 76 %
Daniel@0 77 % Then in late 2009 I noticed that some chord recognition code, which
Daniel@0 78 % used mp3read to read files which were then segmented on the basis of
Daniel@0 79 % some hand-marked timings, suddenly started getting much poorer
Daniel@0 80 % results. It turned out that I had upgraded my version of mpg123 to v
Daniel@0 81 % 1.9.0, and the warm-up samples had been fixed in this version. So my
Daniel@0 82 % code was discarding 2257 *good* samples, and the data was skewed 51ms
Daniel@0 83 % early relative to the hand labels.
Daniel@0 84 %
Daniel@0 85 % Hence, the current version of mp3read does not
Daniel@0 86 % discard any samples by default -- appropriate for the recent versions
Daniel@0 87 % of mpg123 included here. But if you know you're running an old, v
Daniel@0 88 % 0.5.9, mpg123, you should edit the mp3read.m source to set the flag
Daniel@0 89 % MPG123059 = 1.
Daniel@0 90 %
Daniel@0 91 % Note also that the 'size' function relies on the number of
Daniel@0 92 % blocks reported by mp3info. However, many mp3 files include
Daniel@0 93 % additional information about the size of the file in the
Daniel@0 94 % so-called Xing header, embedded in the first frame, which can
Daniel@0 95 % specify that a certain number of samples from start and end
Daniel@0 96 % should additionally be dropped. mp3info doesn't read that,
Daniel@0 97 % and there's no way for my code to probe it except by running
Daniel@0 98 % mpg123. Hence, the results of mp3read(fn,'size') may sometimes
Daniel@0 99 % overestimate the length of the actual vector you'll get if
Daniel@0 100 % you read the whole file.
Daniel@0 101
Daniel@0 102 %% External binaries
Daniel@0 103 % The m files rely on three external binaries, each of which is
Daniel@0 104 % available for Linux, Mac OS X, or Windows:
Daniel@0 105 %
Daniel@0 106 % *mpg123* is a high-performance mp3 decoder. Its home page is
Daniel@0 107 % http://www.mpg123.de/ .
Daniel@0 108 %
Daniel@0 109 % *mp3info* is a utility to read technical information on an mp3
Daniel@0 110 % file. Its home page is http://www.ibiblio.org/mp3info/ .
Daniel@0 111 %
Daniel@0 112 % *lame* is an open-source MP3 encoder. Its homepage is
Daniel@0 113 % http://lame.sourceforge.net/ .
Daniel@0 114 %
Daniel@0 115 % The various authors of these packages are gratefully acknowledged
Daniel@0 116 % for doing all the hard work to make these Matlab functions possible.
Daniel@0 117
Daniel@0 118 %% Installation
Daniel@0 119 % The two routines, mp3read.m and mp3write.m, will look for their
Daniel@0 120 % binaries (mpg123 and mp3info for mp3read; lame for mp3write) in
Daniel@0 121 % the same directory where they are installed. Binaries for
Daniel@0 122 % different architectures are distinguished by their extension,
Daniel@0 123 % which is the standard Matlab computer code e.g. ".mac" for Mac
Daniel@0 124 % PPC OS X, ".glnx86" for i386-linux. The exception is Windows,
Daniel@0 125 % where the binaries have the extension ".exe".
Daniel@0 126 %
Daniel@0 127 % Temporary files
Daniel@0 128 % will be written to (a) a directory taken from the environment
Daniel@0 129 % variable TMPDIR (b) /tmp if it exists, or (c) the current
Daniel@0 130 % directory. This can easily be changed by editing the m files.
Daniel@0 131
Daniel@0 132 % Last updated: $Date: 2009/03/15 18:29:58 $
Daniel@0 133 % Dan Ellis <dpwe@ee.columbia.edu>