comparison toolboxes/mp3readwrite/html/demo_mp3readwrite.html @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1
2 <!DOCTYPE html
3 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html><head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <!--
7 This HTML is auto-generated from an M-file.
8 To make changes, update the M-file and republish this document.
9 --><title>MP3 reading and writing</title><meta name="generator" content="MATLAB 7.10"><meta name="date" content="2010-04-09"><meta name="m-file" content="demo_mp3readwrite"><style type="text/css">
10
11 body {
12 background-color: white;
13 margin:10px;
14 }
15
16 h1 {
17 color: #990000;
18 font-size: x-large;
19 }
20
21 h2 {
22 color: #990000;
23 font-size: medium;
24 }
25
26 /* Make the text shrink to fit narrow windows, but not stretch too far in
27 wide windows. */
28 p,h1,h2,div.content div {
29 max-width: 600px;
30 /* Hack for IE6 */
31 width: auto !important; width: 600px;
32 }
33
34 pre.codeinput {
35 background: #EEEEEE;
36 padding: 10px;
37 }
38 @media print {
39 pre.codeinput {word-wrap:break-word; width:100%;}
40 }
41
42 span.keyword {color: #0000FF}
43 span.comment {color: #228B22}
44 span.string {color: #A020F0}
45 span.untermstring {color: #B20000}
46 span.syscmd {color: #B28C00}
47
48 pre.codeoutput {
49 color: #666666;
50 padding: 10px;
51 }
52
53 pre.error {
54 color: red;
55 }
56
57 p.footer {
58 text-align: right;
59 font-size: xx-small;
60 font-weight: lighter;
61 font-style: italic;
62 color: gray;
63 }
64
65 </style></head><body><div class="content"><h1>MP3 reading and writing</h1><!--introduction--><p>These function, mp3read and mp3write, aim to exactly duplicate the operation of wavread and wavwrite for accessing soundfiles, except the soundfiles are in Mpeg-Audio layer 3 (MP3) compressed format. All the hard work is done by external binaries written by others: mp3info to query the format of existing mp3 files, mpg123 to decode mp3 files, and lame to encode audio files. Binaries for these files are widely available (and may be included in this distribution).</p><p>These functions were originally developed for access to very large mp3 files (i.e. many hours long), and so avoid creating the entire uncompressed audio stream if possible. mp3read allows you to specify the range of frames you want to read (as a second argument), and mp3read will construct an mpg123 command that skips blocks to decode only the part of the file that is required. This can be much quicker (and require less memory/temporary disk) than decoding the whole file.</p><p>mpg123 also provides for "on the fly" downsampling at conversion to mono, which are supported as extra options in mp3read.</p><p>mpg123 can read MP3s across the network. This is supported if the FILE argument is a URL (e.g. beginning 'http://...').</p><p>mp3info sometimes gets the file size wrong (as returned by the mp3read(...'size') syntax). I'm not sure when this happens exactly, but it's probably a result of VBR files. In the worst case, figuring the number of samples in such a file requires scanning through the whole file, and mp3info doesn't usually do this.</p><p>For more information, including advice on handling MP4 files, see <a href="http://labrosa.ee.columbia.edu/matlab/mp3read.html">http://labrosa.ee.columbia.edu/matlab/mp3read.html</a></p><!--/introduction--><h2>Contents</h2><div><ul><li><a href="#1">Example usage</a></li><li><a href="#2">Delay, size, and alignment</a></li><li><a href="#3">External binaries</a></li><li><a href="#4">Installation</a></li></ul></div><h2>Example usage<a name="1"></a></h2><p>Here, we read a wav file in, then write it out as an MP3, then read the resulting MP3 back in, and compare it to the original file.</p><pre class="codeinput"><span class="comment">% Read an audio waveform</span>
66 [d,sr] = wavread(<span class="string">'piano.wav'</span>);
67 <span class="comment">% Save to mp3 (default settings)</span>
68 mp3write(d,sr,<span class="string">'piano.mp3'</span>);
69 <span class="comment">% Read it back again</span>
70 [d2,sr] = mp3read(<span class="string">'piano.mp3'</span>);
71 <span class="comment">% mp3 encoding involves some extra padding at each end; we attempt</span>
72 <span class="comment">% to cut it off at the start, but can't do that at the end, because</span>
73 <span class="comment">% mp3read doesn't know how long the original was. But we do, so..</span>
74 <span class="comment">% Chop it down to be the same length as the original</span>
75 d2 = d2(1:length(d),:);
76 <span class="comment">% What is the SNR (distortion)?</span>
77 ddiff = d - d2;
78 disp([<span class="string">'SNR is '</span>,num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),<span class="string">' dB'</span>]);
79 <span class="comment">% Do they look similar?</span>
80 subplot(211)
81 specgram(d(:,1),1024,sr);
82 subplot(212)
83 plot(1:5000,d(10000+(1:5000),1),1:5000,d2(10000+(1:5000)));
84 <span class="comment">% Yes, pretty close</span>
85 <span class="comment">%</span>
86 <span class="comment">% NB: lame followed by mpg123 causes a little attenuation; you</span>
87 <span class="comment">% can get a better match by scaling up the read-back waveform:</span>
88 ddiff = d - 1.052*d2;
89 disp([<span class="string">'SNR is '</span>,num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),<span class="string">' dB'</span>]);
90 </pre><pre class="codeoutput">Warning: popenw not available, writing temporary file
91 SNR is 22.632 dB
92 SNR is 24.8699 dB
93 </pre><img vspace="5" hspace="5" src="demo_mp3readwrite_01.png" alt=""> <h2>Delay, size, and alignment<a name="2"></a></h2><p>In mid-2006 I noticed that mp3read followed by mp3write followed by mp3read effectively delayed the waveform by 2257 samples (at 44 kHz). So I introduced code to discard the first 2257 samples to ensure that the waveforms remained time aligned. As best I could understand, mpg123 (v 0.5.9) was including the "warm-up" samples from the synthesis filterbank which are more properly discarded.</p><p>Then in late 2009 I noticed that some chord recognition code, which used mp3read to read files which were then segmented on the basis of some hand-marked timings, suddenly started getting much poorer results. It turned out that I had upgraded my version of mpg123 to v 1.9.0, and the warm-up samples had been fixed in this version. So my code was discarding 2257 <b>good</b> samples, and the data was skewed 51ms early relative to the hand labels.</p><p>Hence, the current version of mp3read does not discard any samples by default -- appropriate for the recent versions of mpg123 included here. But if you know you're running an old, v 0.5.9, mpg123, you should edit the mp3read.m source to set the flag MPG123059 = 1.</p><p>Note also that the 'size' function relies on the number of blocks reported by mp3info. However, many mp3 files include additional information about the size of the file in the so-called Xing header, embedded in the first frame, which can specify that a certain number of samples from start and end should additionally be dropped. mp3info doesn't read that, and there's no way for my code to probe it except by running mpg123. Hence, the results of mp3read(fn,'size') may sometimes overestimate the length of the actual vector you'll get if you read the whole file.</p><h2>External binaries<a name="3"></a></h2><p>The m files rely on three external binaries, each of which is available for Linux, Mac OS X, or Windows:</p><p><b>mpg123</b> is a high-performance mp3 decoder. Its home page is <a href="http://www.mpg123.de/">http://www.mpg123.de/</a> .</p><p><b>mp3info</b> is a utility to read technical information on an mp3 file. Its home page is <a href="http://www.ibiblio.org/mp3info/">http://www.ibiblio.org/mp3info/</a> .</p><p><b>lame</b> is an open-source MP3 encoder. Its homepage is <a href="http://lame.sourceforge.net/">http://lame.sourceforge.net/</a> .</p><p>The various authors of these packages are gratefully acknowledged for doing all the hard work to make these Matlab functions possible.</p><h2>Installation<a name="4"></a></h2><p>The two routines, mp3read.m and mp3write.m, will look for their binaries (mpg123 and mp3info for mp3read; lame for mp3write) in the same directory where they are installed. Binaries for different architectures are distinguished by their extension, which is the standard Matlab computer code e.g. ".mac" for Mac PPC OS X, ".glnx86" for i386-linux. The exception is Windows, where the binaries have the extension ".exe".</p><p>Temporary files will be written to (a) a directory taken from the environment variable TMPDIR (b) /tmp if it exists, or (c) the current directory. This can easily be changed by editing the m files.</p><pre class="codeinput"><span class="comment">% Last updated: $Date: 2009/03/15 18:29:58 $</span>
94 <span class="comment">% Dan Ellis &lt;dpwe@ee.columbia.edu&gt;</span>
95 </pre><p class="footer"><br>
96 Published with MATLAB&reg; 7.10<br></p></div><!--
97 ##### SOURCE BEGIN #####
98 %% MP3 reading and writing
99 %
100 % These function, mp3read and mp3write, aim to exactly duplicate
101 % the operation of wavread and wavwrite for accessing soundfiles,
102 % except the soundfiles are in Mpeg-Audio layer 3 (MP3) compressed
103 % format. All the hard work is done by external binaries written
104 % by others: mp3info to query the format of existing mp3 files,
105 % mpg123 to decode mp3 files, and lame to encode audio files.
106 % Binaries for these files are widely available (and may be
107 % included in this distribution).
108 %
109 % These functions were originally developed for access to very
110 % large mp3 files (i.e. many hours long), and so avoid creating
111 % the entire uncompressed audio stream if possible. mp3read
112 % allows you to specify the range of frames you want to read
113 % (as a second argument), and mp3read will construct an mpg123
114 % command that skips blocks to decode only the part of the file
115 % that is required. This can be much quicker (and require less
116 % memory/temporary disk) than decoding the whole file.
117 %
118 % mpg123 also provides for "on the fly" downsampling at conversion
119 % to mono, which are supported as extra options in mp3read.
120 %
121 % mpg123 can read MP3s across the network. This is supported
122 % if the FILE argument is a URL (e.g. beginning 'http://...').
123 %
124 % mp3info sometimes gets the file size wrong (as returned by the
125 % mp3read(...'size') syntax). I'm not sure when this happens
126 % exactly, but it's probably a result of VBR files. In the worst
127 % case, figuring the number of samples in such a file requires
128 % scanning through the whole file, and mp3info doesn't usually do
129 % this.
130 %
131 % For more information, including advice on handling MP4 files,
132 % see http://labrosa.ee.columbia.edu/matlab/mp3read.html
133
134 %% Example usage
135 % Here, we read a wav file in, then write it out as an MP3, then
136 % read the resulting MP3 back in, and compare it to the original
137 % file.
138
139 % Read an audio waveform
140 [d,sr] = wavread('piano.wav');
141 % Save to mp3 (default settings)
142 mp3write(d,sr,'piano.mp3');
143 % Read it back again
144 [d2,sr] = mp3read('piano.mp3');
145 % mp3 encoding involves some extra padding at each end; we attempt
146 % to cut it off at the start, but can't do that at the end, because
147 % mp3read doesn't know how long the original was. But we do, so..
148 % Chop it down to be the same length as the original
149 d2 = d2(1:length(d),:);
150 % What is the SNR (distortion)?
151 ddiff = d - d2;
152 disp(['SNR is ',num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),' dB']);
153 % Do they look similar?
154 subplot(211)
155 specgram(d(:,1),1024,sr);
156 subplot(212)
157 plot(1:5000,d(10000+(1:5000),1),1:5000,d2(10000+(1:5000)));
158 % Yes, pretty close
159 %
160 % NB: lame followed by mpg123 causes a little attenuation; you
161 % can get a better match by scaling up the read-back waveform:
162 ddiff = d - 1.052*d2;
163 disp(['SNR is ',num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),' dB']);
164
165 %% Delay, size, and alignment
166 %
167 % In mid-2006 I noticed that mp3read followed by mp3write followed by
168 % mp3read effectively delayed the waveform by 2257 samples (at 44
169 % kHz). So I introduced code to discard the first 2257 samples to ensure
170 % that the waveforms remained time aligned. As best I could understand,
171 % mpg123 (v 0.5.9) was including the "warm-up" samples from the
172 % synthesis filterbank which are more properly discarded.
173 %
174 % Then in late 2009 I noticed that some chord recognition code, which
175 % used mp3read to read files which were then segmented on the basis of
176 % some hand-marked timings, suddenly started getting much poorer
177 % results. It turned out that I had upgraded my version of mpg123 to v
178 % 1.9.0, and the warm-up samples had been fixed in this version. So my
179 % code was discarding 2257 *good* samples, and the data was skewed 51ms
180 % early relative to the hand labels.
181 %
182 % Hence, the current version of mp3read does not
183 % discard any samples by default REPLACE_WITH_DASH_DASH appropriate for the recent versions
184 % of mpg123 included here. But if you know you're running an old, v
185 % 0.5.9, mpg123, you should edit the mp3read.m source to set the flag
186 % MPG123059 = 1.
187 %
188 % Note also that the 'size' function relies on the number of
189 % blocks reported by mp3info. However, many mp3 files include
190 % additional information about the size of the file in the
191 % so-called Xing header, embedded in the first frame, which can
192 % specify that a certain number of samples from start and end
193 % should additionally be dropped. mp3info doesn't read that,
194 % and there's no way for my code to probe it except by running
195 % mpg123. Hence, the results of mp3read(fn,'size') may sometimes
196 % overestimate the length of the actual vector you'll get if
197 % you read the whole file.
198
199 %% External binaries
200 % The m files rely on three external binaries, each of which is
201 % available for Linux, Mac OS X, or Windows:
202 %
203 % *mpg123* is a high-performance mp3 decoder. Its home page is
204 % http://www.mpg123.de/ .
205 %
206 % *mp3info* is a utility to read technical information on an mp3
207 % file. Its home page is http://www.ibiblio.org/mp3info/ .
208 %
209 % *lame* is an open-source MP3 encoder. Its homepage is
210 % http://lame.sourceforge.net/ .
211 %
212 % The various authors of these packages are gratefully acknowledged
213 % for doing all the hard work to make these Matlab functions possible.
214
215 %% Installation
216 % The two routines, mp3read.m and mp3write.m, will look for their
217 % binaries (mpg123 and mp3info for mp3read; lame for mp3write) in
218 % the same directory where they are installed. Binaries for
219 % different architectures are distinguished by their extension,
220 % which is the standard Matlab computer code e.g. ".mac" for Mac
221 % PPC OS X, ".glnx86" for i386-linux. The exception is Windows,
222 % where the binaries have the extension ".exe".
223 %
224 % Temporary files
225 % will be written to (a) a directory taken from the environment
226 % variable TMPDIR (b) /tmp if it exists, or (c) the current
227 % directory. This can easily be changed by editing the m files.
228
229 % Last updated: $Date: 2009/03/15 18:29:58 $
230 % Dan Ellis <dpwe@ee.columbia.edu>
231
232 ##### SOURCE END #####
233 --></body></html>