0285da8f49ca8af61b86f8b69853009d3fc8fc23
[platform/upstream/flac.git] / test / test_flac.sh
1 #!/bin/sh
2
3 #  FLAC - Free Lossless Audio Codec
4 #  Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
5 #
6 #  This file is part the FLAC project.  FLAC is comprised of several
7 #  components distributed under difference licenses.  The codec libraries
8 #  are distributed under Xiph.Org's BSD-like license (see the file
9 #  COPYING.Xiph in this distribution).  All other programs, libraries, and
10 #  plugins are distributed under the GPL (see COPYING.GPL).  The documentation
11 #  is distributed under the Gnu FDL (see COPYING.FDL).  Each file in the
12 #  FLAC distribution contains at the top the terms under which it may be
13 #  distributed.
14 #
15 #  Since this particular file is relevant to all components of FLAC,
16 #  it may be distributed under the Xiph.Org license, which is the least
17 #  restrictive of those mentioned above.  See the file COPYING.Xiph in this
18 #  distribution.
19
20 die ()
21 {
22         echo $* 1>&2
23         exit 1
24 }
25
26 dddie="die ERROR: creating files with dd"
27
28 if [ x = x"$1" ] ; then
29         BUILD=debug
30 else
31         BUILD="$1"
32 fi
33
34 # change to 'false' to show flac output (useful for debugging)
35 if true ; then
36         SILENT='--silent'
37         TOTALLY_SILENT='--totally-silent'
38 else
39         SILENT=''
40         TOTALLY_SILENT=''
41 fi
42
43 LD_LIBRARY_PATH=`pwd`/../src/libFLAC/.libs:$LD_LIBRARY_PATH
44 LD_LIBRARY_PATH=`pwd`/../src/share/grabbag/.libs:$LD_LIBRARY_PATH
45 LD_LIBRARY_PATH=`pwd`/../src/share/getopt/.libs:$LD_LIBRARY_PATH
46 LD_LIBRARY_PATH=`pwd`/../src/share/replaygain_analysis/.libs:$LD_LIBRARY_PATH
47 LD_LIBRARY_PATH=`pwd`/../src/share/replaygain_synthesis/.libs:$LD_LIBRARY_PATH
48 LD_LIBRARY_PATH=`pwd`/../src/share/utf8/.libs:$LD_LIBRARY_PATH
49 LD_LIBRARY_PATH=`pwd`/../obj/$BUILD/lib:$LD_LIBRARY_PATH
50 export LD_LIBRARY_PATH
51 PATH=`pwd`/../src/flac:$PATH
52 PATH=`pwd`/../src/metaflac:$PATH
53 PATH=`pwd`/../src/test_streams:$PATH
54 PATH=`pwd`/../obj/$BUILD/bin:$PATH
55
56 flac --help 1>/dev/null 2>/dev/null || die "ERROR can't find flac executable"
57
58 run_flac ()
59 {
60         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
61                 valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --logfile-fd=4 flac $* 4>>test_flac.valgrind.log
62         else
63                 flac $*
64         fi
65 }
66
67 run_metaflac ()
68 {
69         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
70                 valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --logfile-fd=4 metaflac $* 4>>test_flac.valgrind.log
71         else
72                 metaflac $*
73         fi
74 }
75
76 md5cmp ()
77 {
78         #n=`( [ -f "$1" ] && [ -f "$2" ] && metaflac --show-md5sum --no-filename "$1" "$2" 2>/dev/null || die "ERROR: comparing FLAC files $1 and $2 by MD5 sum" ) | uniq | wc -l`
79         n=`( [ -f "$1" ] && [ -f "$2" ] && metaflac --show-md5sum --no-filename "$1" "$2" 2>/dev/null || exit 1 ) | uniq | wc -l`
80         [ "$n" != "" ] && [ $n = 1 ]
81 }
82
83 if [ `env | grep -ic '^comspec='` != 0 ] ; then
84         is_win=yes
85 else
86         is_win=no
87 fi
88
89 echo "Checking for --ogg support in flac..."
90 if flac --ogg $SILENT --force-raw-format --endian=little --sign=signed --channels=1 --bps=8 --sample-rate=44100 -c $0 1>/dev/null 2>&1 ; then
91         has_ogg=yes;
92         echo "flac --ogg works"
93 else
94         has_ogg=no;
95         echo "flac --ogg doesn't work"
96 fi
97
98 echo "Generating streams..."
99 if [ ! -f wacky1.wav ] ; then
100         test_streams || die "ERROR during test_streams"
101 fi
102
103 ############################################################################
104 # test that flac doesn't automatically overwrite files unless -f is used
105 ############################################################################
106
107 echo "Try encoding to a file that exists; should fail"
108 cp wacky1.wav exist.wav
109 touch exist.flac
110 if run_flac $TOTALLY_SILENT -0 exist.wav ; then
111         die "ERROR: it should have failed but didn't"
112 else
113         echo "OK, it failed as it should"
114 fi
115
116 echo "Try encoding with -f to a file that exists; should succeed"
117 if run_flac $TOTALLY_SILENT -0 --force exist.wav ; then
118         echo "OK, it succeeded as it should"
119 else
120         die "ERROR: it should have succeeded but didn't"
121 fi
122
123 echo "Try decoding to a file that exists; should fail"
124 if run_flac $TOTALLY_SILENT -d exist.flac ; then
125         die "ERROR: it should have failed but didn't"
126 else
127         echo "OK, it failed as it should"
128 fi
129
130 echo "Try decoding with -f to a file that exists; should succeed"
131 if run_flac $TOTALLY_SILENT -d -f exist.flac ; then
132         echo "OK, it succeeded as it should"
133 else
134         die "ERROR: it should have succeeded but didn't"
135 fi
136
137 rm -f exist.wav exist.flac
138
139 ############################################################################
140 # test fractional block sizes
141 ############################################################################
142
143 for samples in 254 255 256 257 258 510 511 512 513 514 ; do
144         dd if=noise.raw ibs=4 count=$samples of=pbs.raw 2>/dev/null || $dddie
145         echo -n "fractional block size test ($samples) encode... "
146         run_flac $SILENT --force --verify --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=16 --channels=2 --blocksize=256 -o pbs.flac pbs.raw || die "ERROR"
147         echo -n "decode... "
148         run_flac $SILENT --force --decode --force-raw-format --endian=little --sign=signed -o pbs.cmp pbs.flac || die "ERROR"
149         echo -n "compare... "
150         cmp pbs.raw pbs.cmp || die "ERROR: file mismatch"
151         echo "OK"
152         rm -f pbs.raw pbs.flac pbs.cmp
153 done
154
155 ############################################################################
156 # basic 'round-trip' tests of various kinds of streams
157 ############################################################################
158
159 rt_test_raw ()
160 {
161         f="$1"
162         channels=`echo $f | awk -F- '{print $2}'`
163         bps=`echo $f | awk -F- '{print $3}'`
164         echo -n "round-trip test ($f) encode... "
165         run_flac $SILENT --force --verify --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=$bps --channels=$channels --lax -o rt.flac $f || die "ERROR"
166         echo -n "decode... "
167         run_flac $SILENT --force --decode --force-raw-format --endian=little --sign=signed -o rt.raw rt.flac || die "ERROR"
168         echo -n "compare... "
169         cmp $f rt.raw || die "ERROR: file mismatch"
170         echo "OK"
171         rm -f rt.flac rt.raw
172 }
173
174 rt_test_wav ()
175 {
176         f="$1"
177         echo -n "round-trip test ($f) encode... "
178         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.flac $f || die "ERROR"
179         echo -n "decode... "
180         run_flac $SILENT --force --decode --channel-map=none -o rt.wav rt.flac || die "ERROR"
181         echo -n "compare... "
182         cmp $f rt.wav || die "ERROR: file mismatch"
183         echo "OK"
184         rm -f rt.flac rt.wav
185 }
186
187 rt_test_aiff ()
188 {
189         f="$1"
190         echo -n "round-trip test ($f) encode... "
191         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.flac $f || die "ERROR"
192         echo -n "decode... "
193         run_flac $SILENT --force --decode --channel-map=none -o rt.aiff rt.flac || die "ERROR"
194         echo -n "compare... "
195         cmp $f rt.aiff || die "ERROR: file mismatch"
196         echo "OK"
197         rm -f rt.flac rt.aiff
198 }
199
200 # assumes input file is WAVE; does not check the metadata-preserving features of flac-to-flac; that is checked later
201 rt_test_flac ()
202 {
203         f="$1"
204         echo -n "round-trip test ($f->flac->flac->wav) encode... "
205         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.flac $f || die "ERROR"
206         echo -n "re-encode... "
207         run_flac $SILENT --force --verify --lax -o rt2.flac rt.flac || die "ERROR"
208         echo -n "decode... "
209         run_flac $SILENT --force --decode --channel-map=none -o rt.wav rt2.flac || die "ERROR"
210         echo -n "compare... "
211         cmp $f rt.wav || die "ERROR: file mismatch"
212         echo "OK"
213         rm -f rt.wav rt.flac rt2.flac
214 }
215
216 # assumes input file is WAVE; does not check the metadata-preserving features of flac-to-flac; that is checked later
217 rt_test_ogg_flac ()
218 {
219         f="$1"
220         echo -n "round-trip test ($f->oggflac->oggflac->wav) encode... "
221         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.ogg --ogg $f || die "ERROR"
222         echo -n "re-encode... "
223         run_flac $SILENT --force --verify --lax -o rt2.ogg --ogg rt.ogg || die "ERROR"
224         echo -n "decode... "
225         run_flac $SILENT --force --decode --channel-map=none -o rt.wav rt2.ogg || die "ERROR"
226         echo -n "compare... "
227         cmp $f rt.wav || die "ERROR: file mismatch"
228         echo "OK"
229         rm -f rt.wav rt.ogg rt2.ogg
230 }
231
232 for f in rt-*.raw ; do
233         rt_test_raw $f
234 done
235 for f in rt-*.wav ; do
236         rt_test_wav $f
237 done
238 for f in rt-*.aiff ; do
239         rt_test_aiff $f
240 done
241 for f in rt-*.wav ; do
242         rt_test_flac $f
243 done
244 if [ $has_ogg = yes ] ; then
245         for f in rt-*.wav ; do
246                 rt_test_ogg_flac $f
247         done
248 fi
249
250 ############################################################################
251 # test --skip and --until
252 ############################################################################
253
254 #
255 # first make some chopped-up raw files
256 #
257 echo "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMN" > master.raw
258 dd if=master.raw ibs=1 count=50 of=50c.raw 2>/dev/null || $dddie
259 dd if=master.raw ibs=1 skip=10 count=40 of=50c.skip10.raw 2>/dev/null || $dddie
260 dd if=master.raw ibs=1 skip=11 count=39 of=50c.skip11.raw 2>/dev/null || $dddie
261 dd if=master.raw ibs=1 skip=20 count=30 of=50c.skip20.raw 2>/dev/null || $dddie
262 dd if=master.raw ibs=1 skip=30 count=20 of=50c.skip30.raw 2>/dev/null || $dddie
263 dd if=master.raw ibs=1 skip=40 count=10 of=50c.skip40.raw 2>/dev/null || $dddie
264 dd if=master.raw ibs=1 count=10 of=50c.until10.raw 2>/dev/null || $dddie
265 dd if=master.raw ibs=1 count=20 of=50c.until20.raw 2>/dev/null || $dddie
266 dd if=master.raw ibs=1 count=30 of=50c.until30.raw 2>/dev/null || $dddie
267 dd if=master.raw ibs=1 count=39 of=50c.until39.raw 2>/dev/null || $dddie
268 dd if=master.raw ibs=1 count=40 of=50c.until40.raw 2>/dev/null || $dddie
269 dd if=master.raw ibs=1 skip=10 count=20 of=50c.skip10.until30.raw 2>/dev/null || $dddie
270 dd if=master.raw ibs=1 skip=10 count=29 of=50c.skip10.until39.raw 2>/dev/null || $dddie
271 dd if=master.raw ibs=1 skip=10 count=30 of=50c.skip10.until40.raw 2>/dev/null || $dddie
272 dd if=master.raw ibs=1 skip=20 count=10 of=50c.skip20.until30.raw 2>/dev/null || $dddie
273 dd if=master.raw ibs=1 skip=20 count=20 of=50c.skip20.until40.raw 2>/dev/null || $dddie
274
275 wav_eopt="$SILENT --force --verify --lax"
276 wav_dopt="$SILENT --force --decode"
277
278 raw_eopt="$wav_eopt --force-raw-format --endian=big --sign=signed --sample-rate=10 --bps=8 --channels=1"
279 raw_dopt="$wav_dopt --force-raw-format --endian=big --sign=signed"
280
281 #
282 # convert them to WAVE/AIFF/Ogg FLAC files
283 #
284 convert_to_wav ()
285 {
286         run_flac "$2" $1.raw || die "ERROR converting $1.raw to WAVE"
287         run_flac "$3" $1.flac || die "ERROR converting $1.raw to WAVE"
288 }
289 convert_to_wav 50c "$raw_eopt" "$wav_dopt"
290 convert_to_wav 50c.skip10 "$raw_eopt" "$wav_dopt"
291 convert_to_wav 50c.skip11 "$raw_eopt" "$wav_dopt"
292 convert_to_wav 50c.skip20 "$raw_eopt" "$wav_dopt"
293 convert_to_wav 50c.skip30 "$raw_eopt" "$wav_dopt"
294 convert_to_wav 50c.skip40 "$raw_eopt" "$wav_dopt"
295 convert_to_wav 50c.until10 "$raw_eopt" "$wav_dopt"
296 convert_to_wav 50c.until20 "$raw_eopt" "$wav_dopt"
297 convert_to_wav 50c.until30 "$raw_eopt" "$wav_dopt"
298 convert_to_wav 50c.until39 "$raw_eopt" "$wav_dopt"
299 convert_to_wav 50c.until40 "$raw_eopt" "$wav_dopt"
300 convert_to_wav 50c.skip10.until30 "$raw_eopt" "$wav_dopt"
301 convert_to_wav 50c.skip10.until39 "$raw_eopt" "$wav_dopt"
302 convert_to_wav 50c.skip10.until40 "$raw_eopt" "$wav_dopt"
303 convert_to_wav 50c.skip20.until30 "$raw_eopt" "$wav_dopt"
304 convert_to_wav 50c.skip20.until40 "$raw_eopt" "$wav_dopt"
305
306 convert_to_aiff ()
307 {
308         run_flac "$2" $1.raw || die "ERROR converting $1.raw to AIFF"
309         run_flac "$3" $1.flac -o $1.aiff || die "ERROR converting $1.raw to AIFF"
310 }
311 convert_to_aiff 50c "$raw_eopt" "$wav_dopt"
312 convert_to_aiff 50c.skip10 "$raw_eopt" "$wav_dopt"
313 convert_to_aiff 50c.skip11 "$raw_eopt" "$wav_dopt"
314 convert_to_aiff 50c.skip20 "$raw_eopt" "$wav_dopt"
315 convert_to_aiff 50c.skip30 "$raw_eopt" "$wav_dopt"
316 convert_to_aiff 50c.skip40 "$raw_eopt" "$wav_dopt"
317 convert_to_aiff 50c.until10 "$raw_eopt" "$wav_dopt"
318 convert_to_aiff 50c.until20 "$raw_eopt" "$wav_dopt"
319 convert_to_aiff 50c.until30 "$raw_eopt" "$wav_dopt"
320 convert_to_aiff 50c.until39 "$raw_eopt" "$wav_dopt"
321 convert_to_aiff 50c.until40 "$raw_eopt" "$wav_dopt"
322 convert_to_aiff 50c.skip10.until30 "$raw_eopt" "$wav_dopt"
323 convert_to_aiff 50c.skip10.until39 "$raw_eopt" "$wav_dopt"
324 convert_to_aiff 50c.skip10.until40 "$raw_eopt" "$wav_dopt"
325 convert_to_aiff 50c.skip20.until30 "$raw_eopt" "$wav_dopt"
326 convert_to_aiff 50c.skip20.until40 "$raw_eopt" "$wav_dopt"
327
328 convert_to_ogg ()
329 {
330         run_flac "$wav_eopt" --ogg $1.wav || die "ERROR converting $1.raw to Ogg FLAC"
331 }
332 if [ $has_ogg = yes ] ; then
333         convert_to_ogg 50c
334         convert_to_ogg 50c.skip10
335         convert_to_ogg 50c.skip11
336         convert_to_ogg 50c.skip20
337         convert_to_ogg 50c.skip30
338         convert_to_ogg 50c.skip40
339         convert_to_ogg 50c.until10
340         convert_to_ogg 50c.until20
341         convert_to_ogg 50c.until30
342         convert_to_ogg 50c.until39
343         convert_to_ogg 50c.until40
344         convert_to_ogg 50c.skip10.until30
345         convert_to_ogg 50c.skip10.until39
346         convert_to_ogg 50c.skip10.until40
347         convert_to_ogg 50c.skip20.until30
348         convert_to_ogg 50c.skip20.until40
349 fi
350
351 test_skip_until ()
352 {
353         in_fmt=$1
354         out_fmt=$2
355
356         [ "$in_fmt" = wav ] || [ "$in_fmt" = aiff ] || [ "$in_fmt" = raw ] || [ "$in_fmt" = flac ] || [ "$in_fmt" = ogg ] || die "ERROR: internal error, bad 'in' format '$in_fmt'"
357
358         [ "$out_fmt" = flac ] || [ "$out_fmt" = ogg ] || die "ERROR: internal error, bad 'out' format '$out_fmt'"
359
360         if [ $in_fmt = raw ] ; then
361                 eopt="$raw_eopt"
362                 dopt="$raw_dopt"
363         else
364                 eopt="$wav_eopt"
365                 dopt="$wav_dopt"
366         fi
367
368         if ( [ $in_fmt = flac ] || [ $in_fmt = ogg ] ) && ( [ $out_fmt = flac ] || [ $out_fmt = ogg ] ) ; then
369                 CMP=md5cmp
370         else
371                 CMP=cmp
372         fi
373
374         if [ $out_fmt = ogg ] ; then
375                 eopt="--ogg $eopt"
376         fi
377
378         #
379         # test --skip when encoding
380         #
381
382         desc="($in_fmt<->$out_fmt)"
383
384         echo -n "testing --skip=# (encode) $desc... "
385         run_flac $eopt --skip=10 -o z50c.skip10.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
386         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.$in_fmt z50c.skip10.$out_fmt || die "ERROR decoding FLAC file $desc"
387         $CMP 50c.skip10.$in_fmt z50c.skip10.$in_fmt || die "ERROR: file mismatch for --skip=10 (encode) $desc"
388         rm -f z50c.skip10.$out_fmt z50c.skip10.$in_fmt
389         echo OK
390
391         echo -n "testing --skip=mm:ss (encode) $desc... "
392         run_flac $eopt --skip=0:01 -o z50c.skip0:01.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
393         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip0:01.$in_fmt z50c.skip0:01.$out_fmt || die "ERROR decoding FLAC file $desc"
394         $CMP 50c.skip10.$in_fmt z50c.skip0:01.$in_fmt || die "ERROR: file mismatch for --skip=0:01 (encode) $desc"
395         rm -f z50c.skip0:01.$out_fmt z50c.skip0:01.$in_fmt
396         echo OK
397
398         echo -n "testing --skip=mm:ss.sss (encode) $desc... "
399         run_flac $eopt --skip=0:01.1001 -o z50c.skip0:01.1001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
400         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip0:01.1001.$in_fmt z50c.skip0:01.1001.$out_fmt || die "ERROR decoding FLAC file $desc"
401         $CMP 50c.skip11.$in_fmt z50c.skip0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=0:01.1001 (encode) $desc"
402         rm -f z50c.skip0:01.1001.$out_fmt z50c.skip0:01.1001.$in_fmt
403         echo OK
404
405         #
406         # test --skip when decoding
407         #
408
409         if [ $in_fmt != $out_fmt ] ; then run_flac $eopt -o z50c.$out_fmt 50c.$in_fmt ; else cp -f 50c.$in_fmt z50c.$out_fmt ; fi || die "ERROR generating FLAC file $desc"
410
411         echo -n "testing --skip=# (decode) $desc... "
412         run_flac $dopt --skip=10 -o z50c.skip10.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
413         $CMP 50c.skip10.$in_fmt z50c.skip10.$in_fmt || die "ERROR: file mismatch for --skip=10 (decode) $desc"
414         rm -f z50c.skip10.$in_fmt
415         echo OK
416
417         echo -n "testing --skip=mm:ss (decode) $desc... "
418         run_flac $dopt --skip=0:01 -o z50c.skip0:01.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
419         $CMP 50c.skip10.$in_fmt z50c.skip0:01.$in_fmt || die "ERROR: file mismatch for --skip=0:01 (decode) $desc"
420         rm -f z50c.skip0:01.$in_fmt
421         echo OK
422
423         echo -n "testing --skip=mm:ss.sss (decode) $desc... "
424         run_flac $dopt --skip=0:01.1001 -o z50c.skip0:01.1001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
425         $CMP 50c.skip11.$in_fmt z50c.skip0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=0:01.1001 (decode) $desc"
426         rm -f z50c.skip0:01.1001.$in_fmt
427         echo OK
428
429         rm -f z50c.$out_fmt
430
431         #
432         # test --until when encoding
433         #
434
435         echo -n "testing --until=# (encode) $desc... "
436         run_flac $eopt --until=40 -o z50c.until40.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
437         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until40.$in_fmt z50c.until40.$out_fmt || die "ERROR decoding FLAC file $desc"
438         $CMP 50c.until40.$in_fmt z50c.until40.$in_fmt || die "ERROR: file mismatch for --until=40 (encode) $desc"
439         rm -f z50c.until40.$out_fmt z50c.until40.$in_fmt
440         echo OK
441
442         echo -n "testing --until=mm:ss (encode) $desc... "
443         run_flac $eopt --until=0:04 -o z50c.until0:04.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
444         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until0:04.$in_fmt z50c.until0:04.$out_fmt || die "ERROR decoding FLAC file $desc"
445         $CMP 50c.until40.$in_fmt z50c.until0:04.$in_fmt || die "ERROR: file mismatch for --until=0:04 (encode) $desc"
446         rm -f z50c.until0:04.$out_fmt z50c.until0:04.$in_fmt
447         echo OK
448
449         echo -n "testing --until=mm:ss.sss (encode) $desc... "
450         run_flac $eopt --until=0:03.9001 -o z50c.until0:03.9001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
451         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until0:03.9001.$in_fmt z50c.until0:03.9001.$out_fmt || die "ERROR decoding FLAC file $desc"
452         $CMP 50c.until39.$in_fmt z50c.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --until=0:03.9001 (encode) $desc"
453         rm -f z50c.until0:03.9001.$out_fmt z50c.until0:03.9001.$in_fmt
454         echo OK
455
456         echo -n "testing --until=-# (encode) $desc... "
457         run_flac $eopt --until=-10 -o z50c.until-10.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
458         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until-10.$in_fmt z50c.until-10.$out_fmt || die "ERROR decoding FLAC file $desc"
459         $CMP 50c.until40.$in_fmt z50c.until-10.$in_fmt || die "ERROR: file mismatch for --until=-10 (encode) $desc"
460         rm -f z50c.until-10.$out_fmt z50c.until-10.$in_fmt
461         echo OK
462
463         echo -n "testing --until=-mm:ss (encode) $desc... "
464         run_flac $eopt --until=-0:01 -o z50c.until-0:01.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
465         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until-0:01.$in_fmt z50c.until-0:01.$out_fmt || die "ERROR decoding FLAC file $desc"
466         $CMP 50c.until40.$in_fmt z50c.until-0:01.$in_fmt || die "ERROR: file mismatch for --until=-0:01 (encode) $desc"
467         rm -f z50c.until-0:01.$out_fmt z50c.until-0:01.$in_fmt
468         echo OK
469
470         echo -n "testing --until=-mm:ss.sss (encode) $desc... "
471         run_flac $eopt --until=-0:01.1001 -o z50c.until-0:01.1001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
472         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until-0:01.1001.$in_fmt z50c.until-0:01.1001.$out_fmt || die "ERROR decoding FLAC file $desc"
473         $CMP 50c.until39.$in_fmt z50c.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --until=-0:01.1001 (encode) $desc"
474         rm -f z50c.until-0:01.1001.$out_fmt z50c.until-0:01.1001.$in_fmt
475         echo OK
476
477         #
478         # test --until when decoding
479         #
480
481         if [ $in_fmt != $out_fmt ] ; then run_flac $eopt -o z50c.$out_fmt 50c.$in_fmt ; else cp -f 50c.$in_fmt z50c.$out_fmt ; fi || die "ERROR generating FLAC file $desc"
482
483         echo -n "testing --until=# (decode) $desc... "
484         run_flac $dopt --until=40 -o z50c.until40.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
485         $CMP 50c.until40.$in_fmt z50c.until40.$in_fmt || die "ERROR: file mismatch for --until=40 (decode) $desc"
486         rm -f z50c.until40.$in_fmt
487         echo OK
488
489         echo -n "testing --until=mm:ss (decode) $desc... "
490         run_flac $dopt --until=0:04 -o z50c.until0:04.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
491         $CMP 50c.until40.$in_fmt z50c.until0:04.$in_fmt || die "ERROR: file mismatch for --until=0:04 (decode) $desc"
492         rm -f z50c.until0:04.$in_fmt
493         echo OK
494
495         echo -n "testing --until=mm:ss.sss (decode) $desc... "
496         run_flac $dopt --until=0:03.9001 -o z50c.until0:03.9001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
497         $CMP 50c.until39.$in_fmt z50c.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --until=0:03.9001 (decode) $desc"
498         rm -f z50c.until0:03.9001.$in_fmt
499         echo OK
500
501         echo -n "testing --until=-# (decode) $desc... "
502         run_flac $dopt --until=-10 -o z50c.until-10.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
503         $CMP 50c.until40.$in_fmt z50c.until-10.$in_fmt || die "ERROR: file mismatch for --until=-10 (decode) $desc"
504         rm -f z50c.until-10.$in_fmt
505         echo OK
506
507         echo -n "testing --until=-mm:ss (decode) $desc... "
508         run_flac $dopt --until=-0:01 -o z50c.until-0:01.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
509         $CMP 50c.until40.$in_fmt z50c.until-0:01.$in_fmt || die "ERROR: file mismatch for --until=-0:01 (decode) $desc"
510         rm -f z50c.until-0:01.$in_fmt
511         echo OK
512
513         echo -n "testing --until=-mm:ss.sss (decode) $desc... "
514         run_flac $dopt --until=-0:01.1001 -o z50c.until-0:01.1001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
515         $CMP 50c.until39.$in_fmt z50c.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --until=-0:01.1001 (decode) $desc"
516         rm -f z50c.until-0:01.1001.$in_fmt
517         echo OK
518
519         rm -f z50c.$out_fmt
520
521         #
522         # test --skip and --until when encoding
523         #
524
525         echo -n "testing --skip=10 --until=# (encode) $desc... "
526         run_flac $eopt --skip=10 --until=40 -o z50c.skip10.until40.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
527         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until40.$in_fmt z50c.skip10.until40.$out_fmt || die "ERROR decoding FLAC file $desc"
528         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until40.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=40 (encode) $desc"
529         rm -f z50c.skip10.until40.$out_fmt z50c.skip10.until40.$in_fmt
530         echo OK
531
532         echo -n "testing --skip=10 --until=mm:ss (encode) $desc... "
533         run_flac $eopt --skip=10 --until=0:04 -o z50c.skip10.until0:04.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
534         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until0:04.$in_fmt z50c.skip10.until0:04.$out_fmt || die "ERROR decoding FLAC file $desc"
535         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until0:04.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:04 (encode) $desc"
536         rm -f z50c.skip10.until0:04.$out_fmt z50c.skip10.until0:04.$in_fmt
537         echo OK
538
539         echo -n "testing --skip=10 --until=mm:ss.sss (encode) $desc... "
540         run_flac $eopt --skip=10 --until=0:03.9001 -o z50c.skip10.until0:03.9001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
541         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until0:03.9001.$in_fmt z50c.skip10.until0:03.9001.$out_fmt || die "ERROR decoding FLAC file $desc"
542         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:03.9001 (encode) $desc"
543         rm -f z50c.skip10.until0:03.9001.$out_fmt z50c.skip10.until0:03.9001.$in_fmt
544         echo OK
545
546         echo -n "testing --skip=10 --until=+# (encode) $desc... "
547         run_flac $eopt --skip=10 --until=+30 -o z50c.skip10.until+30.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
548         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until+30.$in_fmt z50c.skip10.until+30.$out_fmt || die "ERROR decoding FLAC file $desc"
549         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until+30.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=+30 (encode) $desc"
550         rm -f z50c.skip10.until+30.$out_fmt z50c.skip10.until+30.$in_fmt
551         echo OK
552
553         echo -n "testing --skip=10 --until=+mm:ss (encode) $desc... "
554         run_flac $eopt --skip=10 --until=+0:03 -o z50c.skip10.until+0:03.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
555         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until+0:03.$in_fmt z50c.skip10.until+0:03.$out_fmt || die "ERROR decoding FLAC file $desc"
556         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until+0:03.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=+0:03 (encode) $desc"
557         rm -f z50c.skip10.until+0:03.$out_fmt z50c.skip10.until+0:03.$in_fmt
558         echo OK
559
560         echo -n "testing --skip=10 --until=+mm:ss.sss (encode) $desc... "
561         run_flac $eopt --skip=10 --until=+0:02.9001 -o z50c.skip10.until+0:02.9001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
562         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until+0:02.9001.$in_fmt z50c.skip10.until+0:02.9001.$out_fmt || die "ERROR decoding FLAC file $desc"
563         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until+0:02.9001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=+0:02.9001 (encode) $desc"
564         rm -f z50c.skip10.until+0:02.9001.$out_fmt z50c.skip10.until+0:02.9001.$in_fmt
565         echo OK
566
567         echo -n "testing --skip=10 --until=-# (encode) $desc... "
568         run_flac $eopt --skip=10 --until=-10 -o z50c.skip10.until-10.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
569         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until-10.$in_fmt z50c.skip10.until-10.$out_fmt || die "ERROR decoding FLAC file $desc"
570         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-10.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-10 (encode) $desc"
571         rm -f z50c.skip10.until-10.$out_fmt z50c.skip10.until-10.$in_fmt
572         echo OK
573
574         echo -n "testing --skip=10 --until=-mm:ss (encode) $desc... "
575         run_flac $eopt --skip=10 --until=-0:01 -o z50c.skip10.until-0:01.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
576         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until-0:01.$in_fmt z50c.skip10.until-0:01.$out_fmt || die "ERROR decoding FLAC file $desc"
577         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-0:01.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01 (encode) $desc"
578         rm -f z50c.skip10.until-0:01.$out_fmt z50c.skip10.until-0:01.$in_fmt
579         echo OK
580
581         echo -n "testing --skip=10 --until=-mm:ss.sss (encode) $desc... "
582         run_flac $eopt --skip=10 --until=-0:01.1001 -o z50c.skip10.until-0:01.1001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
583         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until-0:01.1001.$in_fmt z50c.skip10.until-0:01.1001.$out_fmt || die "ERROR decoding FLAC file $desc"
584         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01.1001 (encode) $desc"
585         rm -f z50c.skip10.until-0:01.1001.$out_fmt z50c.skip10.until-0:01.1001.$in_fmt
586         echo OK
587
588         #
589         # test --skip and --until when decoding
590         #
591
592         if [ $in_fmt != $out_fmt ] ; then run_flac $eopt -o z50c.$out_fmt 50c.$in_fmt ; else cp -f 50c.$in_fmt z50c.$out_fmt ; fi || die "ERROR generating FLAC file $desc"
593
594
595         echo -n "testing --skip=10 --until=# (decode) $desc... "
596         run_flac $dopt --skip=10 --until=40 -o z50c.skip10.until40.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
597         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until40.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=40 (decode) $desc"
598         rm -f z50c.skip10.until40.$in_fmt
599         echo OK
600
601         echo -n "testing --skip=10 --until=mm:ss (decode) $desc... "
602         run_flac $dopt --skip=10 --until=0:04 -o z50c.skip10.until0:04.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
603         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until0:04.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:04 (decode) $desc"
604         rm -f z50c.skip10.until0:04.$in_fmt
605         echo OK
606
607         echo -n "testing --skip=10 --until=mm:ss.sss (decode) $desc... "
608         run_flac $dopt --skip=10 --until=0:03.9001 -o z50c.skip10.until0:03.9001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
609         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:03.9001 (decode) $desc"
610         rm -f z50c.skip10.until0:03.9001.$in_fmt
611         echo OK
612
613         echo -n "testing --skip=10 --until=-# (decode) $desc... "
614         run_flac $dopt --skip=10 --until=-10 -o z50c.skip10.until-10.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
615         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-10.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-10 (decode) $desc"
616         rm -f z50c.skip10.until-10.$in_fmt
617         echo OK
618
619         echo -n "testing --skip=10 --until=-mm:ss (decode) $desc... "
620         run_flac $dopt --skip=10 --until=-0:01 -o z50c.skip10.until-0:01.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
621         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-0:01.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01 (decode) $desc"
622         rm -f z50c.skip10.until-0:01.$in_fmt
623         echo OK
624
625         echo -n "testing --skip=10 --until=-mm:ss.sss (decode) $desc... "
626         run_flac $dopt --skip=10 --until=-0:01.1001 -o z50c.skip10.until-0:01.1001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
627         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01.1001 (decode) $desc"
628         rm -f z50c.skip10.until-0:01.1001.$in_fmt
629         echo OK
630
631         rm -f z50c.$out_fmt
632 }
633
634 test_skip_until raw flac
635 test_skip_until wav flac
636 test_skip_until aiff flac
637 test_skip_until flac flac
638 #@@@if [ $has_ogg = yes ] ; then
639 #@@@    #@@@ doesn't work yet because md5cmp doesn't work because metaflac doesn't work on ogg flac yet
640 #@@@    test_skip_until ogg flac
641 #@@@fi
642
643 if [ $has_ogg = yes ] ; then
644         test_skip_until raw ogg
645         test_skip_until wav ogg
646         test_skip_until aiff ogg
647         #@@@ doesn't work yet because md5cmp doesn't work because metaflac doesn't work on ogg flac yet
648         #@@@test_skip_until flac ogg
649         #@@@test_skip_until ogg ogg
650 fi
651
652 echo "testing seek extremes:"
653
654 run_flac --verify --force $SILENT --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=16 --channels=2 --blocksize=576 noise.raw || die "ERROR generating FLAC file"
655
656 if [ $is_win = no ] ; then
657         total_noise_cdda_samples=`run_metaflac --show-total-samples noise.flac`
658         [ $? = 0 ] || die "ERROR getting total sample count from noise.flac"
659 else
660         # some flavors of cygwin don't seem to treat the \x0d as a word
661         # separator, so we hard code it.  we'll just have to fix it later
662         # if we change the way noise.flac is made.
663         total_noise_cdda_samples=393216
664 fi
665
666 echo -n "testing --skip=0... "
667 run_flac $wav_dopt --skip=0 -o z.wav noise.flac || die "ERROR decoding FLAC file noise.flac"
668 echo OK
669
670 for delta in 2 1 ; do
671         n=`expr $total_noise_cdda_samples - $delta`
672         echo -n "testing --skip=$n... "
673         run_flac $wav_dopt --skip=$n -o z.wav noise.flac || die "ERROR decoding FLAC file noise.flac"
674         echo OK
675 done
676
677 rm noise.flac z.wav
678
679
680 ############################################################################
681 # test --input-size
682 ############################################################################
683
684 #@@@ cat will not work on old cygwin, need to fix
685 if [ $is_win = no ] ; then
686         echo -n "testing --input-size=50 --skip=10... "
687         cat 50c.raw | run_flac $raw_eopt --input-size=50 --skip=10 -o z50c.skip10.flac - || die "ERROR generating FLAC file"
688         run_flac $raw_dopt -o z50c.skip10.raw z50c.skip10.flac || die "ERROR decoding FLAC file"
689         cmp 50c.skip10.raw z50c.skip10.raw || die "ERROR: file mismatch for --input-size=50 --skip=10"
690         rm -f z50c.skip10.raw z50c.skip10.flac
691         echo OK
692 fi
693
694
695 ############################################################################
696 # test --cue
697 ############################################################################
698
699 #
700 # create the cue sheet
701 #
702 cuesheet=cuetest.cue
703 cat > $cuesheet << EOF
704 CATALOG 1234567890123
705 FILE "blah" WAVE
706   TRACK 01 AUDIO
707     INDEX 01 0
708     INDEX 02 10
709     INDEX 03 20
710   TRACK 02 AUDIO
711     INDEX 01 30
712   TRACK 04 AUDIO
713     INDEX 01 40
714 EOF
715
716 test_cue ()
717 {
718         in_fmt=$1
719         out_fmt=$2
720
721         [ "$in_fmt" = wav ] || [ "$in_fmt" = aiff ] || [ "$in_fmt" = raw ] || [ "$in_fmt" = flac ] || [ "$in_fmt" = ogg ] || die "ERROR: internal error, bad 'in' format '$in_fmt'"
722
723         [ "$out_fmt" = flac ] || [ "$out_fmt" = ogg ] || die "ERROR: internal error, bad 'out' format '$out_fmt'"
724
725         if [ $in_fmt = raw ] ; then
726                 eopt="$raw_eopt"
727                 dopt="$raw_dopt"
728         else
729                 eopt="$wav_eopt"
730                 dopt="$wav_dopt"
731         fi
732
733         if ( [ $in_fmt = flac ] || [ $in_fmt = ogg ] ) && ( [ $out_fmt = flac ] || [ $out_fmt = ogg ] ) ; then
734                 CMP=md5cmp
735         else
736                 CMP=cmp
737         fi
738
739         if [ $out_fmt = ogg ] ; then
740                 eopt="--ogg $eopt"
741         fi
742
743         desc="($in_fmt<->$out_fmt)"
744
745         #
746         # for this we need just need just one FLAC file; --cue only works while decoding
747         #
748         run_flac $eopt --cuesheet=$cuesheet -o z50c.cue.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
749
750         # To make it easy to translate from cue point to sample numbers, the
751         # file has a sample rate of 10 Hz and a cuesheet like so:
752         #
753         # TRACK 01, INDEX 01 : 0:00.00 -> sample 0
754         # TRACK 01, INDEX 02 : 0:01.00 -> sample 10
755         # TRACK 01, INDEX 03 : 0:02.00 -> sample 20
756         # TRACK 02, INDEX 01 : 0:03.00 -> sample 30
757         # TRACK 04, INDEX 01 : 0:04.00 -> sample 40
758         #
759         echo -n "testing --cue=- $desc... "
760         run_flac $dopt -o z50c.cued.$in_fmt --cue=- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
761         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=- $desc"
762         rm -f z50c.cued.$in_fmt
763         echo OK
764
765         echo -n "testing --cue=1.0 $desc... "
766         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
767         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.0 $desc"
768         rm -f z50c.cued.$in_fmt
769         echo OK
770
771         echo -n "testing --cue=1.0- $desc... "
772         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.0- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
773         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.0- $desc"
774         rm -f z50c.cued.$in_fmt
775         echo OK
776
777         echo -n "testing --cue=1.1 $desc... "
778         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
779         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.1 $desc"
780         rm -f z50c.cued.$in_fmt
781         echo OK
782
783         echo -n "testing --cue=1.1- $desc... "
784         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.1- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
785         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.1- $desc"
786         rm -f z50c.cued.$in_fmt
787         echo OK
788
789         echo -n "testing --cue=1.2 $desc... "
790         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.2 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
791         $CMP 50c.skip10.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.2 $desc"
792         rm -f z50c.cued.$in_fmt
793         echo OK
794
795         echo -n "testing --cue=1.2- $desc... "
796         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.2- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
797         $CMP 50c.skip10.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.2- $desc"
798         rm -f z50c.cued.$in_fmt
799         echo OK
800
801         echo -n "testing --cue=1.4 $desc... "
802         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.4 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
803         $CMP 50c.skip20.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.4 $desc"
804         rm -f z50c.cued.$in_fmt
805         echo OK
806
807         echo -n "testing --cue=1.4- $desc... "
808         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.4- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
809         $CMP 50c.skip20.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.4- $desc"
810         rm -f z50c.cued.$in_fmt
811         echo OK
812
813         echo -n "testing --cue=-5.0 $desc... "
814         run_flac $dopt -o z50c.cued.$in_fmt --cue=-5.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
815         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-5.0 $desc"
816         rm -f z50c.cued.$in_fmt
817         echo OK
818
819         echo -n "testing --cue=-4.1 $desc... "
820         run_flac $dopt -o z50c.cued.$in_fmt --cue=-4.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
821         $CMP 50c.until40.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-4.1 $desc"
822         rm -f z50c.cued.$in_fmt
823         echo OK
824
825         echo -n "testing --cue=-3.1 $desc... "
826         run_flac $dopt -o z50c.cued.$in_fmt --cue=-3.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
827         $CMP 50c.until40.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-3.1 $desc"
828         rm -f z50c.cued.$in_fmt
829         echo OK
830
831         echo -n "testing --cue=-1.4 $desc... "
832         run_flac $dopt -o z50c.cued.$in_fmt --cue=-1.4 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
833         $CMP 50c.until30.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-1.4 $desc"
834         rm -f z50c.cued.$in_fmt
835         echo OK
836
837         echo -n "testing --cue=1.0-5.0 $desc... "
838         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.0-5.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
839         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.0-5.0 $desc"
840         rm -f z50c.cued.$in_fmt
841         echo OK
842
843         echo -n "testing --cue=1.1-5.0 $desc... "
844         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.1-5.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
845         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.1-5.0 $desc"
846         rm -f z50c.cued.$in_fmt
847         echo OK
848
849         echo -n "testing --cue=1.2-4.1 $desc... "
850         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.2-4.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
851         $CMP 50c.skip10.until40.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.2-4.1 $desc"
852         rm -f z50c.cued.$in_fmt
853         echo OK
854
855         echo -n "testing --cue=1.4-2.0 $desc... "
856         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.4-2.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
857         $CMP 50c.skip20.until30.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.4-2.0 $desc"
858         rm -f z50c.cued.$in_fmt
859         echo OK
860
861         rm -f z50c.cue.$out_fmt
862 }
863
864 test_cue raw flac
865 test_cue wav flac
866 test_cue aiff flac
867 test_cue flac flac
868 #@@@if [ $has_ogg = yes ] ; then
869 #@@@    #@@@ doesn't work yet because md5cmp doesn't work because metaflac doesn't work on ogg flac yet
870 #@@@    test_cue ogg flac
871 #@@@fi
872
873 if [ $has_ogg = yes ] ; then
874         test_cue raw ogg
875         test_cue wav ogg
876         test_cue aiff ogg
877         #@@@ doesn't work yet because md5cmp doesn't work because metaflac doesn't work on ogg flac yet
878         #@@@test_cue flac ogg
879         #@@@test_cue ogg ogg
880 fi
881
882 ############################################################################
883 # test 'fixup' code that happens when a FLAC file with total_samples == 0
884 # in the STREAMINFO block is converted to WAVE or AIFF, requiring the
885 # decoder go back and fix up the chunk headers
886 ############################################################################
887
888 echo -n "WAVE fixup test... "
889
890 echo -n "prepare... "
891 convert_to_wav noise "$raw_eopt" "$wav_dopt" || die "ERROR creating reference WAVE"
892
893 echo -n "encode... "
894 # the pipe from 'cat' to 'flac' does not work on cygwin because of the EOF/
895 # binary-mode stdin problem, so we use an undocumented option to metaflac to
896 # set the total sample count to 0
897 if [ $is_win = yes ] ; then
898         run_flac $raw_eopt noise.raw -o fixup.flac || die "ERROR generating FLAC file"
899         run_metaflac --set-total-samples=0 fixup.flac 2> /dev/null
900 else
901         cat noise.raw | run_flac $raw_eopt - -c > fixup.flac || die "ERROR generating FLAC file"
902 fi
903
904 echo -n "decode... "
905 run_flac $wav_dopt fixup.flac -o fixup.wav || die "ERROR decoding FLAC file"
906
907 echo -n "compare... "
908 cmp noise.wav fixup.wav || die "ERROR: file mismatch"
909
910 echo OK
911 rm -f noise.wav fixup.wav fixup.flac
912
913 echo -n "AIFF fixup test... "
914
915 echo -n "prepare... "
916 convert_to_aiff noise "$raw_eopt" "$wav_dopt" || die "ERROR creating reference AIFF"
917
918 echo -n "encode... "
919 # the pipe from 'cat' to 'flac' does not work on cygwin because of the EOF/
920 # binary-mode stdin problem, so we use an undocumented option to metaflac to
921 # set the total sample count to 0
922 if [ $is_win = yes ] ; then
923         run_flac $raw_eopt noise.raw -o fixup.flac || die "ERROR generating FLAC file"
924         run_metaflac --set-total-samples=0 fixup.flac 2> /dev/null
925 else
926         cat noise.raw | run_flac $raw_eopt - -c > fixup.flac || die "ERROR generating FLAC file"
927 fi
928
929 echo -n "decode... "
930 run_flac $wav_dopt fixup.flac -o fixup.aiff || die "ERROR decoding FLAC file"
931
932 echo -n "compare... "
933 cmp noise.aiff fixup.aiff || die "ERROR: file mismatch"
934
935 echo OK
936 rm -f noise.aiff fixup.aiff fixup.flac
937
938
939 ############################################################################
940 # multi-file tests
941 ############################################################################
942
943 echo "Generating multiple input files from noise..."
944 multifile_format_decode="--endian=big --sign=signed"
945 multifile_format_encode="$multifile_format_decode --sample-rate=44100 --bps=16 --channels=2"
946 short_noise_cdda_samples=`expr $total_noise_cdda_samples / 8`
947 run_flac --verify --force $SILENT --force-raw-format $multifile_format_encode --until=$short_noise_cdda_samples -o shortnoise.flac noise.raw || die "ERROR generating FLAC file"
948 run_flac --decode --force $SILENT shortnoise.flac -o shortnoise.raw --force-raw-format $multifile_format_decode || die "ERROR generating RAW file"
949 run_flac --decode --force $SILENT shortnoise.flac || die "ERROR generating WAVE file"
950 run_flac --decode --force $SILENT shortnoise.flac -o shortnoise.aiff || die "ERROR generating AIFF file"
951 cp shortnoise.flac file0.flac
952 cp shortnoise.flac file1.flac
953 cp shortnoise.flac file2.flac
954 rm -f shortnoise.flac
955 cp shortnoise.wav file0.wav
956 cp shortnoise.wav file1.wav
957 cp shortnoise.wav file2.wav
958 rm -f shortnoise.wav
959 cp shortnoise.aiff file0.aiff
960 cp shortnoise.aiff file1.aiff
961 cp shortnoise.aiff file2.aiff
962 rm -f shortnoise.aiff
963 cp shortnoise.raw file0.raw
964 cp shortnoise.raw file1.raw
965 cp shortnoise.raw file2.raw
966 rm -f shortnoise.raw
967 # create authoritative sector-aligned files for comparison
968 file0_samples=`expr \( $short_noise_cdda_samples / 588 \) \* 588`
969 file0_remainder=`expr $short_noise_cdda_samples - $file0_samples`
970 file1_samples=`expr \( \( $file0_remainder + $short_noise_cdda_samples \) / 588 \) \* 588`
971 file1_remainder=`expr $file0_remainder + $short_noise_cdda_samples - $file1_samples`
972 file1_samples=`expr $file1_samples - $file0_remainder`
973 file2_samples=`expr \( \( $file1_remainder + $short_noise_cdda_samples \) / 588 \) \* 588`
974 file2_remainder=`expr $file1_remainder + $short_noise_cdda_samples - $file2_samples`
975 file2_samples=`expr $file2_samples - $file1_remainder`
976 if [ $file2_remainder != '0' ] ; then
977         file2_samples=`expr $file2_samples + $file2_remainder`
978         file2_remainder=`expr 588 - $file2_remainder`
979 fi
980
981 dd if=file0.raw ibs=4 count=$file0_samples of=file0s.raw 2>/dev/null || $dddie
982 dd if=file0.raw ibs=4 count=$file0_remainder of=file1s.raw skip=$file0_samples 2>/dev/null || $dddie
983 dd if=file1.raw ibs=4 count=$file1_samples of=z.raw 2>/dev/null || $dddie
984 cat z.raw >> file1s.raw || die "ERROR: cat-ing sector-aligned files"
985 dd if=file1.raw ibs=4 count=$file1_remainder of=file2s.raw skip=$file1_samples 2>/dev/null || $dddie
986 dd if=file2.raw ibs=4 count=$file2_samples of=z.raw 2>/dev/null || $dddie
987 cat z.raw >> file2s.raw || die "ERROR: cat-ing sector-aligned files"
988 dd if=/dev/zero ibs=4 count=$file2_remainder of=z.raw 2>/dev/null || $dddie
989 cat z.raw >> file2s.raw || die "ERROR: cat-ing sector-aligned files"
990 rm -f z.raw
991
992 convert_to_wav file0s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned WAVE"
993 convert_to_wav file1s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned WAVE"
994 convert_to_wav file2s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned WAVE"
995
996 convert_to_aiff file0s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned AIFF"
997 convert_to_aiff file1s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned AIFF"
998 convert_to_aiff file2s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned AIFF"
999
1000 test_multifile ()
1001 {
1002         input_type=$1
1003         streamtype=$2
1004         sector_align=$3
1005         encode_options="$4"
1006
1007         extra_encode_options=""
1008         extra_decode_options=""
1009         if [ $input_type = "raw" ] ; then
1010                 extra_encode_options="--force-raw-format $multifile_format_encode"
1011                 extra_decode_options="--force-raw-format $multifile_format_decode"
1012         else
1013                 if [ $input_type = "aiff" ] ; then
1014                         extra_decode_options="--force-aiff-format"
1015                 fi
1016         fi
1017
1018         if [ $streamtype = ogg ] ; then
1019                 suffix=ogg
1020                 encode_options="$encode_options --ogg"
1021         else
1022                 suffix=flac
1023         fi
1024
1025         if [ $sector_align = sector_align ] ; then
1026                 encode_options="$encode_options --sector-align"
1027         fi
1028
1029         if [ $input_type = flac ] || [ $input_type = ogg ] ; then
1030                 CMP=md5cmp
1031         else
1032                 CMP=cmp
1033         fi
1034
1035         for n in 0 1 2 ; do
1036                 cp file$n.$input_type file${n}x.$input_type
1037         done
1038         run_flac --force $encode_options $extra_encode_options file0x.$input_type file1x.$input_type file2x.$input_type || die "ERROR"
1039         run_flac --force --decode $extra_decode_options file0x.$suffix file1x.$suffix file2x.$suffix || die "ERROR"
1040         if [ $sector_align != sector_align ] ; then
1041                 for n in 0 1 2 ; do
1042                         $CMP file$n.$input_type file${n}x.$input_type || die "ERROR: file mismatch on file #$n"
1043                 done
1044         else
1045                 for n in 0 1 2 ; do
1046                         $CMP file${n}s.$input_type file${n}x.$input_type || die "ERROR: file mismatch on file #$n"
1047                 done
1048         fi
1049         for n in 0 1 2 ; do
1050                 rm -f file${n}x.$suffix file${n}x.$input_type
1051         done
1052 }
1053
1054 input_types="raw wav aiff flac"
1055 #@@@ doesn't work yet because md5cmp doesn't work because metaflac doesn't work on ogg flac yet
1056 #@@@if [ $has_ogg = yes ] ; then
1057 #@@@    input_types="$input_types ogg"
1058 #@@@fi
1059 for input_type in $input_types ; do
1060         echo "Testing multiple $input_type files without verify..."
1061         test_multifile $input_type flac no_sector_align ""
1062
1063         echo "Testing multiple $input_type files with verify..."
1064         test_multifile $input_type flac no_sector_align "--verify"
1065
1066         if [ $input_type != flac ] && [ $input_type != ogg ] ; then # --sector-align not supported for FLAC input
1067                 echo "Testing multiple $input_type files with --sector-align, without verify..."
1068                 test_multifile $input_type flac sector_align ""
1069
1070                 echo "Testing multiple $input_type files with --sector-align, with verify..."
1071                 test_multifile $input_type flac sector_align "--verify"
1072         fi
1073
1074         if [ $has_ogg = yes ] ; then
1075                 echo "Testing multiple $input_type files with --ogg, without verify..."
1076                 test_multifile $input_type ogg no_sector_align ""
1077
1078                 echo "Testing multiple $input_type files with --ogg, with verify..."
1079                 test_multifile $input_type ogg no_sector_align "--verify"
1080
1081                 if [ $input_type != flac ] ; then # --sector-align not supported for FLAC input
1082                         echo "Testing multiple $input_type files with --ogg and --sector-align, without verify..."
1083                         test_multifile $input_type ogg sector_align ""
1084
1085                         echo "Testing multiple $input_type files with --ogg and --sector-align, with verify..."
1086                         test_multifile $input_type ogg sector_align "--verify"
1087                 fi
1088
1089                 echo "Testing multiple $input_type files with --ogg and --serial-number, with verify..."
1090                 test_multifile $input_type ogg no_sector_align "--serial-number=321 --verify"
1091         fi
1092 done
1093
1094
1095 ############################################################################
1096 # test the metadata-handling properties of flac-to-flac encoding
1097 ############################################################################
1098
1099 echo "Testing the metadata-handling properties of flac-to-flac encoding..."
1100
1101 testdir="flac-to-flac-metadata-test-files"
1102 filter ()
1103 {
1104         # minor danger, changing vendor strings will change the length of the
1105         # VORBIS_COMMENT block, but if we add "^  length: " to the patterns,
1106         # we lose info about PADDING size that we need
1107         grep -Ev '^  vendor string: |^  m..imum .....size: ' | sed -e 's/, stream_offset.*//'
1108 }
1109 flac2flac ()
1110 {
1111         file="$1"
1112         case="$2"
1113         args="$3"
1114         expect="$case-expect.meta"
1115         echo -n "$case... "
1116         run_flac $SILENT -f -o out.flac $args $file || die "ERROR encoding FLAC file"
1117         run_metaflac --list out.flac | filter > out.meta || die "ERROR listing metadata of output FLAC file"
1118         diff -q -w $expect out.meta 2>/dev/null || die "ERROR: metadata does not match expected $expect"
1119         echo OK
1120 }
1121
1122 #filter=', stream_offset.*|^  vendor string: |^  length: |^  m..imum .....size: '
1123 cd $testdir || die "ERROR changing to directory $testdir"
1124
1125 # case 00a: no alterations on a file with all metadata types, keep all metadata, in same order
1126 flac2flac input-SCVAUP.flac case00a ""
1127 # case 01a: on file with multiple PADDING blocks, they should be aggregated into one at the end
1128 flac2flac input-SCVPAP.flac case01a ""
1129 # case 01b: on file with multiple PADDING blocks and --no-padding specified, they should all be deleted
1130 flac2flac input-SCVPAP.flac case01b "--no-padding"
1131 # case 01c: on file with multiple PADDING blocks and -P specified, they should all be overwritten with -P value
1132 flac2flac input-SCVPAP.flac case01c "-P 1234"
1133 # case 01d: on file with no PADDING blocks, use -P setting
1134 flac2flac input-SCVA.flac case01d "-P 1234"
1135 # case 01e: on file with no PADDING blocks and no -P given, use default padding
1136 flac2flac input-SCVA.flac case01e ""
1137 # case 02a: on file with no VORBIS_COMMENT block, add new VORBIS_COMMENT
1138 flac2flac input-SCPAP.flac case02a ""
1139 # case 02b: on file with no VORBIS_COMMENT block and --tag, add new VORBIS_COMMENT with tags
1140 flac2flac input-SCPAP.flac case02b "--tag=artist=0"
1141 # case 02c: on file with VORBIS_COMMENT block and --tag, replace existing VORBIS_COMMENT with new tags
1142 flac2flac input-SCVAUP.flac case02c "$TOTALLY_SILENT --tag=artist=0"
1143 # case 03a: on file with no CUESHEET block and --cuesheet specified, add it
1144 flac2flac input-SVAUP.flac case03a "--cuesheet=input0.cue"
1145 # case 03b: on file with CUESHEET block and --cuesheet specified, overwrite existing CUESHEET
1146 flac2flac input-SCVAUP.flac case03b "$TOTALLY_SILENT --cuesheet=input0.cue"
1147 # case 03c: on file with CUESHEET block and size-changing option specified, drop existing CUESHEET
1148 flac2flac input-SCVAUP.flac case03c "$TOTALLY_SILENT --skip=1"
1149 # case 04a: on file with no SEEKTABLE block and --no-seektable specified, no SEEKTABLE
1150 flac2flac input-VA.flac case04a "--no-padding --no-seektable"
1151 # case 04b: on file with no SEEKTABLE block and -S specified, new SEEKTABLE
1152 flac2flac input-VA.flac case04b "--no-padding -S 5x"
1153 # case 04c: on file with no SEEKTABLE block and no seektable options specified, new SEEKTABLE with default points
1154 flac2flac input-VA.flac case04c "--no-padding"
1155 # case 04d: on file with SEEKTABLE block and --no-seektable specified, drop existing SEEKTABLE
1156 flac2flac input-SCVA.flac case04d "--no-padding --no-seektable"
1157 # case 04e: on file with SEEKTABLE block and -S specified, overwrite existing SEEKTABLE
1158 flac2flac input-SCVA.flac case04e "$TOTALLY_SILENT --no-padding -S 5x"
1159 # case 04f: on file with SEEKTABLE block and size-changing option specified, drop existing SEEKTABLE, new SEEKTABLE with default points
1160 #(already covered by case03c)
1161
1162 rm -f out.flac out.meta
1163
1164 #@@@ when metaflac handles ogg flac, duplicate flac2flac tests here
1165
1166 cd ..