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