Does lame cut off source samples when resampling from 44.1kHz to 22.05kHz?
During hacking around with mpg123 to get gapless decoding done (ignore the correct number of samples at beginning and end), I noticed that lame seems to kill source material when using --resample.
I used this script on a raw source file with 40803 samples of 100Hz sine (download raw source):
#!/bin/bash
res=resampled
rat=rated
./lame --resample 22.05 -r -x source.raw $res.mp3
./lame -s 22.05 -r -x source.raw $rat.mp3
decoders=`seq 0 2`
decname=("newmpg123" "oldmpg123" "lame")
decflags=("-s --gapless" "-s" "--decode -t ")
styles=("p" "l" "l")
decredirect=(">" ">" "")
echo 'set xrange[0:2000]' > $res.begin.plot
echo 'set xrange[0:4000]' > $rat.begin.plot
echo 'set xrange[19000:22000]' > $res.end.plot
echo 'set xrange[38000:44000]' > $rat.end.plot
for o in $res $rat
do
echo 'plot \' > $o.plots.plot
echo 'set x2range[0:4000]' >> $o.begin.plot
echo 'set x2range[38000:44000]' >> $o.end.plot
for place in begin end
do
{
echo "set title \"lame resampling cutting off? - $o $place\""
echo 'set xlabel "output sample"'
echo 'set x2label "input sample"'
echo 'set ylabel "output level"'
echo 'set y2label "input level"'
echo 'set yrange[-20000:30000]'
echo 'set y2range[-40000:60000]'
echo 'set x2tics'
echo 'set y2tics'
echo 'set key box'
echo "load '$o.plots.plot'"
} >> $o.$place.plot
done
for i in $decoders
do
bash -c "./${decname[$i]} ${decflags[$i]} $o.mp3 ${decredirect[$i]} $o.${decname[$i]}.raw"
echo "'$o.${decname[$i]}.raw' binary format=\"%short%short\" using 1 axes x1y1 with ${styles[$i]} title \"${decname[$i]}\", \\" >> $o.plots.plot
done
echo "'source.raw' binary format=\"%short%short\" using 1 axes x2y2 with l title \"source\"" >> $o.plots.plot
#creating png plots using my li'l frontend and recent gnuplot
if [[ -x `which buntstift` ]]
then
for place in begin end
do
buntstift -t=png -o="size 800,600" $o.$place.plot
done
else
echo buntstift not found... go plotting yourself
fi
done
The important part are the lame encoding lines at the beginning (btw.: lame-3.96.1); the rest is the decoding with lame itself (mpglib), my new mpg123 and mpg123-0.59r-thor5 plus quite some lines for nice plotting.
ls -l *.raw -rw-r----- 1 thomas users 166076 2006-06-16 15:17 rated.lame.raw -rw-r----- 1 thomas users 163212 2006-06-16 15:17 rated.newmpg123.raw -rw-r----- 1 thomas users 172800 2006-06-16 15:17 rated.oldmpg123.raw -rw-r----- 1 thomas users 80828 2006-06-16 15:17 resampled.lame.raw -rw-r----- 1 thomas users 77500 2006-06-16 15:17 resampled.newmpg123.raw -rw-r----- 1 thomas users 87552 2006-06-16 15:17 resampled.oldmpg123.raw -rw-r----- 1 thomas users 163212 2006-06-16 14:45 source.rawSo... that means
| file | samples | diff to source (source/2 for resampled) |
|---|---|---|
| source.raw | 40803 | 0 |
| rated.lame.raw | 41519 | 716 |
| rated.newmpg123.raw | 40803 | 0 |
| rated.oldmpg123.raw | 43200 | 2397 |
| resampled.lame.raw | 20207 | -194.5 |
| resampled.newmpg123.raw | 19375 | -1026.5 |
| resampled.oldmpg123.raw | 21888 | 1486.5 |
(Hm, is it significant that the input sample number is not even?)
You see that the new mpg123 gets the correct number of samples in the non-resampling case and lame's decode just has 716 (= padding - mpg123 delay) samples more.
The decoding results from the resampled file are too short for both lame and new mpg123 decoding and still too long for old mpg123, but that's just becaus this version doesn't ignore lame's delay and padding and also adds a silence frame for the lame tag.
The new mpg123 removes even more samples that lame - that is because I started the whole thing to see if I have to change the code for different sampling rates. I probably have to, but also when removing nothing at the end (at the beginning it looks just fine...), there would still be sound missing.
So, I want to first make sure that lame does keep the signal before I try to keep it with mpg123...
enough of words... that's how the first channel it looks like:



