flac/vorbiscomment.c : Fix typo.
[flac.git] / test / test_replaygain.sh
1 #!/bin/sh -e
2
3 #  FLAC - Free Lossless Audio Codec
4 #  Copyright (C) 2002-2009  Josh Coalson
5 #  Copyright (C) 2011-2014  Xiph.Org Foundation
6 #
7 #  This file is part the FLAC project.  FLAC is comprised of several
8 #  components distributed under different licenses.  The codec libraries
9 #  are distributed under Xiph.Org's BSD-like license (see the file
10 #  COPYING.Xiph in this distribution).  All other programs, libraries, and
11 #  plugins are distributed under the GPL (see COPYING.GPL).  The documentation
12 #  is distributed under the Gnu FDL (see COPYING.FDL).  Each file in the
13 #  FLAC distribution contains at the top the terms under which it may be
14 #  distributed.
15 #
16 #  Since this particular file is relevant to all components of FLAC,
17 #  it may be distributed under the Xiph.Org license, which is the least
18 #  restrictive of those mentioned above.  See the file COPYING.Xiph in this
19 #  distribution.
20
21 . ./common.sh
22
23 PATH=`pwd`/../src/flac:$PATH
24 PATH=`pwd`/../src/metaflac:$PATH
25 PATH=`pwd`/../objs/$BUILD/bin:$PATH
26
27 if echo a | (grep -E '(a|b)') >/dev/null 2>&1
28         then EGREP='grep -E'
29         else EGREP='egrep'
30 fi
31
32 testdir="metaflac-test-files"
33 flacfile="replaygain.flac"
34
35 run_flac ()
36 {
37         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
38                 echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 flac $*" >>test_replaygain.valgrind.log
39                 valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 flac --no-error-on-compression-fail $* 4>>test_replaygain.valgrind.log
40         else
41                 flac${EXE} --no-error-on-compression-fail $*
42         fi
43 }
44
45 run_metaflac ()
46 {
47         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
48                 echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_replaygain.valgrind.log
49                 valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac $* 4>>test_replaygain.valgrind.log
50         else
51                 metaflac${EXE} $*
52         fi
53 }
54
55 run_metaflac_silent ()
56 {
57         if [ -z "$SILENT" ] ; then
58                 run_metaflac $*
59         else
60                 if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
61                         echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=50 metaflac $*" >>test_replaygain.valgrind.log
62                         valgrind --leak-check=yes --show-reachable=yes --num-callers=50 --log-fd=4 metaflac $* 2>/dev/null 4>>test_replaygain.valgrind.log
63                 else
64                         metaflac${EXE} $* 2>/dev/null
65                 fi
66         fi
67 }
68
69 check_flac ()
70 {
71         run_flac --silent --test $flacfile || die "ERROR in $flacfile" 1>&2
72 }
73
74 echo "Generating stream..."
75 bytes=80000
76 if dd if=/dev/zero ibs=1 count=$bytes | flac${EXE} --force --verify -0 --input-size=$bytes --output-name=$flacfile --force-raw-format --endian=big --sign=signed --channels=1 --bps=8 --sample-rate=8000 - ; then
77         chmod +w $flacfile
78 else
79         die "ERROR during generation"
80 fi
81
82 check_flac
83
84
85 if mawk ; then
86         AWK=mawk
87 else
88         # Really hope awk is not gawk, because the following AWK script doesn't
89         # work correctly with gawk 4.0.1 but did with earlier versions.
90         AWK=awk
91         fi
92
93 # Replay gain tests - Test the rates which have specific filter table entries
94 # and verify that harmonics can be processed correctly.
95
96 tonegenerator ()
97 {
98         # When using GAWK, use --lint=posix to identify non-POSIX awk usages.
99     $AWK -- '
100     BEGIN {
101             samplerate = '$1';
102
103             tone = 1000;
104             duration = 1;
105             bitspersample = 24;
106
107             samplemidpoint = 1;
108                         for (sps = 0 ; sps < bitspersample - 1 ; sps++) {
109                                 samplemidpoint *= 2;
110                         }
111
112             samplerange = samplemidpoint - 1;
113
114             pi = 4 * atan2(1,1);
115
116             for (ix = 0; ix < duration * samplerate; ++ix) {
117                     sample = sin(2 * pi * tone * ix / samplerate);
118                     sample *= samplerange;
119                     sample += samplemidpoint;
120                     sample = int(sample);
121                     for (bx = 0; bx < bitspersample/8; ++bx) {
122                             byte[bx] = sample % 256;
123                             sample /= 256;
124                     }
125                     while (bx--) {
126                             printf("%c", byte[bx]);
127                     }
128             }
129
130     }' /dev/null |
131     flac${EXE} --silent --no-error-on-compression-fail --force-raw-format \
132         --endian=big --channels=1 --bps=24 --sample-rate=$1 --sign=unsigned -
133 }
134
135 REPLAYGAIN_FREQ=
136 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ  8000/-12.76"
137 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 11025/-12.93"
138 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 12000/-13.00"
139 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 16000/-13.29"
140 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 18900/-13.43"
141 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 22050/-13.77"
142 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 24000/-13.83"
143 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 28000/-14.06"
144 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 32000/-14.08"
145 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 36000/-14.12"
146 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 37800/-14.18"
147 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 44100/-14.17"
148 REPLAYGAIN_FREQ="$REPLAYGAIN_FREQ 48000/-14.16:1:2:4"
149
150 set -e
151
152 for ACTION in $REPLAYGAIN_FREQ ; do
153   if [ -n "${ACTION##*:*}" ] ; then
154     HARMONICS=1
155   else
156     HARMONICS="${ACTION#*:}"
157   fi
158   FREQ="${ACTION%%/*}"
159   GAIN="${ACTION#*/}"
160   GAIN="${GAIN%%:*}"
161   while [ -n "$HARMONICS" ] ; do
162     MULTIPLE="${HARMONICS%%:*}"
163     if [ x"$MULTIPLE" = x"$HARMONICS" ] ; then
164       HARMONICS=
165     else
166       HARMONICS="${HARMONICS#*:}"
167     fi
168     RATE=$(($MULTIPLE * FREQ))
169     [ $MULTIPLE -eq 1 -o -n "${REPLAYGAIN_FREQ##* $RATE/*}" ] || break
170     echo -n "Testing FLAC replaygain $RATE ($FREQ x $MULTIPLE) ... "
171     tonegenerator $RATE > $flacfile
172     run_metaflac --add-replay-gain $flacfile
173     run_metaflac --list $flacfile | grep REPLAYGAIN.*GAIN= |
174     while read -r REPLAYGAIN ; do
175       MEASUREDGAIN="${REPLAYGAIN##*=}"
176       MEASUREDGAIN="${MEASUREDGAIN%% *}"
177       if [ x"$MEASUREDGAIN" != x"$GAIN" ] ; then
178         die "ERROR, Expected $GAIN db instead of $REPLAYGAIN"
179       fi
180     done
181     echo OK
182   done
183 done
184
185
186 rm -f $testdir/out.flac $testdir/out.meta
187
188 exit 0