Add missing eyed3 TAGGEROPTS
[clinton/abcde.git] / abcde
CommitLineData
7768d908 1#!/bin/bash
c9c2ca27 2# Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
56672932 3# Copyright (c) 2003-2006 Jesus Climent <jesus.climent@hispalinux.es>
01deb17f
E
4# Copyright (c) 2009-2012 Colin Tuckley <colint@debian.org>
5# Copyright (c) 2012 Steve McIntyre <93sam@@debian.org>
c9c2ca27 6# This code is hereby licensed for public consumption under either the
7# GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
8#
56672932 9# You should have received a copy of the GNU General Public License along
10# with this program; if not, write to the Free Software Foundation, Inc.,
11# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
c9c2ca27 12#
13# Copyright for this work is to expire January 1, 2010, after which it
14# shall be public domain.
7768d908 15#
16# $Id$
c9c2ca27 17
ccc8ddfb 18VERSION='2.5.4-UNRELEASED'
c9c2ca27 19
20usage ()
21{
22echo "This is abcde v$VERSION."
23echo "Usage: abcde [options] [tracks]"
24echo "Options:"
ad8b6531 25echo "-1 Encode the whole CD in a single file"
26echo "-a <action1[,action2]...>"
22b54460 27echo " Actions to perform:"
dba06003 28echo " cddb,read,normalize,encode,tag,move,replaygain,playlist,clean"
9a0af871 29#echo "-A Experimental actions (retag, transcode)"
0709e644 30echo "-b Enable batch normalization"
6fba8f12 31#echo "-B Disable batch replaygain (do file by file)"
ad8b6531 32echo "-c <file>"
33echo " Specify a configuration file (overrides system and user config files)"
34echo "-C <discid#>"
35echo " Specify discid to resume from (only needed if you no longer have the cd)"
36echo "-d <device>"
1b94d6b7 37echo " Specify CDROM device to grab (flac uses a single-track flac file)"
ad8b6531 38echo "-D Debugging mode (equivalent to sh -x abcde)"
bef9c2d8 39echo "-e Erase encoded track information from status file"
75c2c424 40echo "-f Force operations that otherwise are considered harmful. Read \"man abcde\""
ed01a270 41echo "-g Use \"lame --nogap\" for MP3 encoding. Disables low disk and pipes flags"
ad8b6531 42echo "-h This help information"
c9c2ca27 43#echo "-i Tag files while encoding, when possible (local only) -NWY-"
ad8b6531 44echo "-j <#> Number of encoder processes to run at once (localhost)"
45echo "-k Keep the wav tracks for later use"
46echo "-l Use low disk space algorithm"
47echo "-L Use local CDDB storage directory"
ad8b6531 48echo "-m Modify playlist to include CRLF endings, to comply with some players"
b546a9eb 49#echo " WARNING: Deprecated. Use \"cue\" action"
93a69eff 50#echo "-M Create a CUE file"
aabd3397
E
51echo "-n No lookup. Don't query CDDB, just create and use template"
52echo "-N Noninteractive. Never prompt for anything"
ad8b6531 53echo "-o <type1[,type2]...>"
dba06003 54echo " Output file type(s) (vorbis,mp3,flac,spx,mpc,wav,m4a). Defaults to vorbis"
ad8b6531 55echo "-p Pad track numbers with 0's (if less than 10 tracks)"
d5200e72 56echo "-P Use UNIX pipes to read+encode without wav files"
93a69eff 57echo "-q <level>"
58echo " Set quality level (high,medium,low)"
ad8b6531 59echo "-r <host1[,host2]...>"
60echo " Also encode on these remote hosts"
9ff5088c 61echo "-s <field>"
3279389d 62echo " Show fields from the CDDB info (year,genre)"
ad8b6531 63echo "-S <#> Set the CD speed"
ad8b6531 64echo "-t <#> Start the track numbering at a given number"
65echo "-T <#> Same as -t but modifies tag numbering"
3b9371f1 66echo "-U Do NOT use UNICODE (UTF8) tags and comments"
ad8b6531 67echo "-v Show version number and exit"
68echo "-V Be a bit more verbose about what is happening behind the scenes"
69echo "-x Eject CD after all tracks are read"
70echo "-w <comment>"
71echo " Add a comment to the CD tracks"
b546a9eb 72echo "-W <#> Concatenate CDs: -T #01 -w \"CD #\""
cb801544 73echo "-z Use debug CDROMREADERSYNTAX option (needs cdparanoia)"
ad8b6531 74echo ""
c9c2ca27 75echo "Tracks is a space-delimited list of tracks to grab."
6119aaa1 76echo "Ranges specified with hyphens are allowed (i.e., 1-5)."
77echo ""
efc6de2f 78#echo "Double hyphens are used to concatenate tracks"
c9c2ca27 79}
80
535944df 81addstatus ()
82{
83 echo "$@" >> "$ABCDETEMPDIR/status"
84}
85
86# log [level] [message]
b546a9eb 87#
535944df 88# log outputs the right message in a common format
89log ()
90{
91 BLURB="$1"
92 shift
93 case $BLURB in
bb87e4fd 94 error) echo "[ERROR] abcde: $@" >&2 ;;
95 warning) echo "[WARNING] $@" >&2 ;;
96 info) echo "[INFO] $@" ;;
535944df 97 esac
98}
99
52e09581 100# Functions to replace the need of seq, which is too distribution dependent.
c9c2ca27 101f_seq_row ()
102{
103 i=$1
104 while [ $i -ne `expr $2 + 1` ]
105 do
106 echo $i
107 i=`expr $i + 1`
108 done
109}
110
111f_seq_line ()
112{
113 i=$1
43559afb 114 if echo $i | grep "[[:digit:]]" > /dev/null 2>&1 ; then
00840d9a 115 while [ $i -ne `expr $2 + 1` ]
116 do
117 printf $i" "
118 i=`expr $i + 1`
119 done
120 echo
121 else
f0bce1c3 122 log error "syntax error while processing track numbers ($i)"
00840d9a 123 exit 1
124 fi
c9c2ca27 125}
126
bef9c2d8 127# Functions to replace the need of awk {print $1} and {print $NF}
128get_first()
129{
130if [ X"$1" = "X" ]; then
131 for first in `cat`; do
132 break
133 done
134else
135 first=$1
136fi
137echo $first
138}
139
140get_last()
141{
142if [ X"$1" = "X" ]; then
143 for stdin in `cat`; do
144 last=$stdin
145 done
146else
147 for last in $@ ; do :; done
148fi
149echo $last
150}
151
c9c2ca27 152# checkstatus [blurb]
153# Returns "0" if the blurb was found, returns 1 if it wasn't
154# Puts the blurb content, if available, on stdout.
155# Otherwise, returns "".
156checkstatus ()
157{
158 # Take the last line in the status file if there's multiple matches
159 PATTERN="^$1(=.*)?$"
991009a4 160 BLURB=$(grep -E $PATTERN "$ABCDETEMPDIR/status" | tail -n 1)
c9c2ca27 161
162 if [ -z "$BLURB" ]; then
163 # No matches found
164 return 1
165 else
166 # Matches found
167 # See if there's a = in it
168 if [ "$(echo $BLURB | grep -c =)" != "0" ]; then
169 echo "$(echo $BLURB | cut -f2- -d=)"
170 fi
171 return 0
172 fi
173}
174
d68ebfff 175# checkwarnings [blurb]
cb801544 176# Returns "0" if the blurb was found (meaning there was an warning),
177# returns 1 if it wasn't (yes this is a little backwards).
178# Does not print the blurb on stdout.
179# Otherwise, returns "".
180checkwarnings ()
181{
182 if [ -e "$ABCDETEMPDIR/warnings" ]; then :; else
183 return 1
184 fi
185 # Take the last line in the status file if there's multiple matches
186 PATTERN="^$1(:.*)?$"
991009a4 187 BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/warnings" | tail -n 1)"
cb801544 188
189 if [ -z "$BLURB" ]; then
190 # negative, we did not have a negative...
191 return 1
192 else
193 # affirmative, we had a negative...
194 return 0
195 fi
196}
197
c9c2ca27 198# checkerrors [blurb]
199# Returns "0" if the blurb was found (meaning there was an error),
200# returns 1 if it wasn't (yes this is a little backwards).
201# Does not print the blurb on stdout.
202# Otherwise, returns "".
203checkerrors ()
204{
99d009fa 205 if [ -e "$ABCDETEMPDIR/errors" ]; then :; else
c9c2ca27 206 return 1
207 fi
208 # Take the last line in the status file if there's multiple matches
209 PATTERN="^$1(:.*)?$"
991009a4 210 BLURB="$(grep -E $PATTERN "$ABCDETEMPDIR/errors" | tail -n 1)"
c9c2ca27 211
212 if [ -z "$BLURB" ]; then
213 # negative, we did not have a negative...
214 return 1
215 else
216 # affirmative, we had a negative...
217 return 0
218 fi
219}
220
913e4339 221# page [file]
222# Finds the right pager in the system to display a file
223page ()
224{
225 PAGEFILE="$1"
226 # Use the debian sensible-pager wrapper to pick the pager
227 # user has requested via their $PAGER environment variable
228 if [ -x "/usr/bin/sensible-pager" ]; then
229 /usr/bin/sensible-pager "$PAGEFILE"
230 elif [ -x "$PAGER" ]; then
231 # That failed, try to load the preferred editor, starting
232 # with their PAGER variable
233 $PAGER "$PAGEFILE"
234 # If that fails, check for less
235 elif [ -x /usr/bin/less ]; then
236 /usr/bin/less -f "$PAGEFILE"
237 # more should be on all UNIX systems
238 elif [ -x /bin/more ]; then
239 /bin/more "$PAGEFILE"
240 else
241 # No bananas, just cat the thing
242 cat "$PAGEFILE" >&2
243 fi
244}
245
c9c2ca27 246# run_command [blurb] [command...]
247# Runs a command, silently if necessary, and updates the status file
248run_command ()
249{
98bea0a0 250 vvecho "$@"
c9c2ca27 251 BLURB="$1"
252 shift
253 # See if this is supposed to be silent
254 if [ "$(checkstatus encode-output)" = "loud" ]; then
255 "$@" >&2
256 RETURN=$?
257 else
258 # Special case for SMP, since
259 # encoder output is never displayed, don't mute echos
260 if [ -z "$BLURB" -a "$MAXPROCS" != "1" ]; then
261 "$@" >&2
262 RETURN=$?
263 else
264 "$@" >/dev/null 2>&1
265 RETURN=$?
266 fi
267 fi
268 case "$1" in
1a064efb 269 normalize|normalize-audio)
c9c2ca27 270 if [ "$RETURN" = "2" ]; then
271 # File was already normalized.
272 RETURN=0
273 fi
274 ;;
275 esac
276 if [ "$RETURN" != "0" ]; then
277 # Put an error in the errors file. For various reasons we
278 # can't capture a copy of the program's output but we can
279 # log what we attempted to execute and the error code
280 # returned by the program.
281 if [ "$BLURB" ]; then
282 TWEAK="$BLURB: "
283 fi
284 echo "${TWEAK}returned code $RETURN: $@" >> "$ABCDETEMPDIR/errors"
285 return $RETURN # Do not pass go, do not update the status file
286 fi
287 if [ "$BLURB" ]; then
288 echo $BLURB >> "$ABCDETEMPDIR/status"
289 fi
290}
291
292# relpath() and slash() are Copyright (c) 1999 Stuart Ballard and
293# distributed under the terms of the GNU GPL v2 or later, at your option
294
295# Function to determine if a word contains a slash.
296slash ()
297{
298 case "$1" in
299 */*) return 0;;
300 *) return 1;;
301 esac
302}
303
304# Function to give the relative path from one file to another.
305# Usage: relpath fromfile tofile
306# eg relpath music/Artist/Album.m3u music/Artist/Album/Song.mp3
307# (the result would be Album/Song.mp3)
308# Output is relative path to $2 from $1 on stdout
309
310# This code has the following restrictions:
311# Multiple ////s are not collapsed into single /s, with strange effects.
312# Absolute paths and ../s are handled wrong in FR (but they work in TO)
313# If FR is a directory it must have a trailing /
314
315relpath ()
316{
317 FR="$1"
318 TO="$2"
319
320 case "$TO" in
321 /*) ;; # No processing is needed for absolute paths
322 *)
323 # Loop through common prefixes, ignoring them.
324 while slash "$FR" && [ "$(echo "$FR" | cut -d/ -f1)" = "$(echo "$TO" | cut -d/ -f1)" ]
325 do
326 FR="$(echo "$FR" | cut -d/ -f2-)"
327 TO="$(echo "$TO" | cut -d/ -f2-)"
328 done
329 # Loop through directory portions left in FR, adding appropriate ../s.
330 while slash "$FR"
331 do
332 FR="$(echo "$FR" | cut -d/ -f2-)"
333 TO="../$TO"
334 done
6c124acf 335 ;;
c9c2ca27 336 esac
337
338 echo $TO
339}
340
43559afb 341new_checkexec ()
342{
343 if [ ! "$@" = "" ]; then
344 # Cut off any command-line option we added in
345 X=$(echo $@ | cut -d' ' -f2)
346 if [ "$(which $X)" = "" ]; then
347 return 1
348 elif [ ! -x $(which $X) ]; then
349 return 2
350 fi
351 fi
352 return 0
353}
354
f6249f03 355checkexec ()
356{
357 if [ ! "$@" = "" ]; then
43559afb 358 # Cut off any command-line option we added in
f6249f03 359 X=$(echo $@ | cut -d' ' -f2)
4562fac6 360 # Test for built-in abcde.function
581a2153 361 [ "$X" != "${X#abcde.}" ] && type $X >/dev/null 2>&1 && return
f6249f03 362 if [ "$(which $X)" = "" ]; then
363 log error "$X is not in your path." >&2
43559afb 364 log info "Define the full path to the executable if it exists on your system." >&2
61f1b969 365 if [ -e /etc/debian_release ] ; then
366 case $X in
7dd0b67a 367 oggenc) MISSING_PACKAGE=vorbis-tools ;;
368 lame|flac) MISSING_PACKAGE=$X ;;
61f1b969 369 esac
370 log info "Hint: apt-get install $MISSING_PACKAGE" >&2
7dd0b67a 371 fi
f6249f03 372 exit 1
634e77be 373 elif [ ! -x "$(which $X)" ]; then
f6249f03 374 log error "$X is not executable." >&2
375 exit 1
376 fi
377 fi
378}
379
d100b6be 380# diffentries <filename> <max_value> <entry1>,<entry2>
43559afb 381# max_value: the range of entries goes from 1 to <max_value>
d100b6be 382diffentries ()
383{
384 FILENAME=$1
385 shift
386 local CDDBDIFFCHOICES=$1
387 shift
388 local CDDBDIFFCHOICE="$@"
389 if [ ! X"$DIFF" = "X" ]; then
390 PARSECHOICE1=$(echo $CDDBDIFFCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
391 PARSECHOICE2=$(echo $CDDBDIFFCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
392 if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBDIFFCHOICES ] || \
393 [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBDIFFCHOICES ] || \
b546a9eb 394 [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then
8f4189b3 395 echo "Invalid diff range. Please select two comma-separated numbers between 1 and $CDDBDIFFCHOICES" >&2
d100b6be 396 else
397 # We parse the 2 choices to diff, store them in temporary files and diff them.
398 for PARSECHOICE in $(echo $CDDBDIFFCHOICE | tr , \ ); do
399 do_cddbparse "$ABCDETEMPDIR/$FILENAME.$PARSECHOICE" > "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE"
400 done
401 echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "$ABCDETEMPDIR/$FILENAME.diff"
402 $DIFF $DIFFOPTS "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/$FILENAME.parsechoice.$PARSECHOICE2" >> "$ABCDETEMPDIR/$FILENAME.diff"
403 if [ $(cat "$ABCDETEMPDIR/$FILENAME.diff" | wc -l) -ge 24 ]; then
404 page "$ABCDETEMPDIR/$FILENAME.diff"
405 else
406 cat "$ABCDETEMPDIR/$FILENAME.diff" >&2
407 fi
408 fi
409 else
410 echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBDIFFCHOICES." >&2
411 fi
412}
413
75285ac8 414# getcddbinfo
cbdb0706 415# Finds an specific field from cddbinfo
75285ac8 416getcddbinfo()
cbdb0706 417{
c0fc2e81 418 case $1 in
419 TRACKNAME1)
420 TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | head -n 1 | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
421 ;;
422 TRACKNAME)
423 TRACKNAME="$(grep ^TTITLE$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\ \+$//')"
424 ;;
3279389d 425 TRACK-INFO)
b546a9eb 426 grep ^EXTT$CDDBTRACKNUM= "$CDDBDATA" | cut -f2- -d= | tr -d \[:cntrl:\] | sed 's/\\n/\n/g'
3279389d 427 ;;
c0fc2e81 428 esac
429}
430
75285ac8 431# gettracknum
c0fc2e81 432# Get the track number we are going to use for different actions
75285ac8 433gettracknum()
c0fc2e81 434{
435 if [ -n "$STARTTRACKNUMBER" ] ; then
436 # Get the trackpadding from the current track
437 CURRENTTRACKPADDING=$(echo -n $UTRACKNUM | wc -c)
438 TRACKNUM=$( printf %0.${CURRENTTRACKPADDING}d $(expr ${UTRACKNUM} + ${STARTTRACKNUMBER} - $FIRSTTRACK ))
439 else
440 TRACKNUM=${UTRACKNUM}
441 fi
442}
443
d68ebfff 444# makeids
445#
446# Calculate cddb disc ids without requiring specialized helper programs.
447# largely copied from cd-discid and musicbrainz examples. some of the steps
448# don't make sense, but they're necessary to match the ids generated by other
449# programs.
450#
451## FIXME ## Right now, we get 2 frames more than with cue2discid ??
452# data@petit:~$ sh /tmp/cue2discid /home/data/tmp/flac/01.Roisin_Murphy--Ruby_Blue.flac
453# processing offsetimes 00:00:00 04:47:10 08:20:37 11:46:46 17:45:36 21:41:57 27:32:21 32:03:73 35:39:28 38:27:33 43:50:38 44:42:34
454# 980b4b0c 12 150 21685 37687 53146 80061 97782 124071 144448 160603 173208 197438 201334 2895
455# data@petit:~$ metaflac --export-cuesheet-to=- /home/data/tmp/flac/01.Roisin_Murphy--Ruby_Blue.flac| python /home/data/sources/abcde/trunk/examples/cue2discid
456# 980b4b0c 12 150 21685 37687 53146 80061 97782 124071 144448 160603 173208 197438 201334 2893
457#
458# Variables: OFFSETS, TRACKS, LEADOUT, [LEADIN]
459makeids ()
460{
461 if [ X"$LEADOUT" = "X" ]; then
462 log warning "Error trying to calculate disc ids without lead-out information."
463 exit 1
464 fi
465
466 # default to a two second lead-in
467 IDMAGICNUM=150
468 LEADIN=${LEADIN:=150}
469
470 # number of cdframes per second
471 CDFRAMES=75
472
473 # reset cddb checksum for cddb disc-id calululation
474 CDDBCKSUM=0
475
476 COOKEDOFFSETS=""
477 for OFFSET in $(echo $OFFSETS)
478 do
479 COOKEDOFFSETS="${COOKEDOFFSETS} $(($OFFSET + $LEADIN))"
480
481 OFFSETTIME=$(( ($OFFSET + $LEADIN) / $CDFRAMES ))
482 while [ $OFFSETTIME -gt 0 ]; do
483 CDDBCKSUM=$(($CDDBCKSUM + $OFFSETTIME % 10))
484 OFFSETTIME=$(($OFFSETTIME / 10))
485 done
486
487 done
488
489 COOKEDOFFSETS="${COOKEDOFFSETS:1}" # eat the leading space
490
491 PREGAP=$(($(echo $OFFSETS | cut -f1 -d' ')))
492 TOTALTIME=$(( (($LEADOUT + $LEADIN + $PREGAP) / $CDFRAMES) - (($LEADIN + $PREGAP) / $CDFRAMES)))
493
4942842f
E
494 case "$CDDBMETHOD" in
495 cddb)
496 printf -v DISCID "%08lx" $(( ($CDDBCKSUM % 0xff) << 24 | $TOTALTIME << 8 | $TRACKS))
497 ;;
498 musicbrainz)
499 # FIXME: don't assume the first track is 1
500 echo "dasd: 1 $TRACKS $LEADIN $LEADOUT $OFFSETS "
501 DISCID=$($MUSICBRAINZ --command calcid --discinfo 1 $TRACKS $LEADIN $LEADOUT $OFFSETS)
502 ;;
503 esac
504
505 TRACKINFO="${DISCID} $((TRACKS)) ${COOKEDOFFSETS} $((($LEADOUT + $LEADIN + $IDMAGICNUM) / $CDFRAMES))"
d68ebfff 506}
507
9c4b8a8e 508do_replaygain()
c0fc2e81 509{
00840d9a 510 if checkstatus replaygain; then :; else
59a103ca 511 run_command "" echo "Adding replaygain information..."
3b4d8f9b 512 for TMPOUTPUT in $( echo $OUTPUTTYPE | tr , \ )
c0fc2e81 513 do
3b4d8f9b 514 case $TMPOUTPUT in
b1d7b11a 515 vorbis|ogg)
516 OUTPUT=$OGGOUTPUTCONTAINER
517 ;;
518 flac)
519 OUTPUT=$FLACOUTPUTCONTAINER
520 ;;
3b4d8f9b 521 *)
522 OUTPUT=$TMPOUTPUT
523 ;;
b1d7b11a 524 esac
00840d9a 525 OUTPUTFILES=""
cdbd4cc8 526 REPLAYINDEX=0
00840d9a 527 for UTRACKNUM in $TRACKQUEUE
528 do
529 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
75285ac8 530 getcddbinfo TRACKNAME
00840d9a 531 splitvarious
fe4d186c 532 TRACKFILE="$(mungefilename "$TRACKNAME")"
533 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
534 ALBUMFILE="$(mungefilename "$DALBUM")"
b546a9eb 535 GENRE="$(mungegenre "$GENRE")"
536 YEAR=${CDYEAR:-$CDYEAR}
75285ac8 537 gettracknum
b546a9eb 538 if [ "$ONETRACK" = "y" ]; then
56672932 539 if [ "$VARIOUSARTISTS" = "y" ]; then
540 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
541 else
542 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
543 fi
544 else
545 if [ "$VARIOUSARTISTS" = "y" ]; then
546 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
547 else
548 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
549 fi
00840d9a 550 fi
cdbd4cc8 551 OUTPUTFILES[$REPLAYINDEX]="$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
552 (( REPLAYINDEX = $REPLAYINDEX + 1 ))
00840d9a 553 done
554 case "$OUTPUT" in
b1d7b11a 555 flac)
b8fa3593 556 run_command replaygain-flac nice $ENCNICE $METAFLAC $FLACGAINOPTS "${OUTPUTFILES[@]}"
3279389d 557 #run_command replaygain-flac true
b1d7b11a 558 ;;
559 vorbis|ogg)
b8fa3593 560 run_command replaygain-vorbis nice $ENCNICE $VORBISGAIN $VORBISGAINOPTS "${OUTPUTFILES[@]}"
b1d7b11a 561 ;;
6fba8f12 562 mp3)
b8fa3593 563 run_command replaygain-mp3 nice $ENCNICE $MP3GAIN $MP3GAINOPTS "${OUTPUTFILES[@]}"
6fba8f12 564 ;;
cb414c1e 565 mpc)
f7c6d185 566 run_command replaygain-mpc nice $ENCNICE $MPPGAIN --auto "${OUTPUTFILES[@]}"
cb414c1e 567 ;;
00840d9a 568 *);;
569 esac
c0fc2e81 570 done
6fba8f12 571 if checkerrors "replaygain-.{3,6}"; then :; else
1750d493 572 run_command replaygain true
573 fi
00840d9a 574 fi
cbdb0706 575}
576
c9c2ca27 577# This code splits the a Various Artist track name from one of the following
578# forms:
579#
580# forward: Artist / Track
581# forward-dash: Artist - Track
582# reverse: Track / Artist
583# reverse-dash: Track - Artist
584# colon: Artist: Track
585# trailing-paren: Artist (Track)
586#
587# variables used:
588# VARIOUSARTISTS, VARIOUSARTISTSTYLE, TRACKNAME, TRACKARTIST
589splitvarious ()
590{
591 if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
592 case "$VARIOUSARTISTSTYLE" in
593 forward)
fe4d186c 594 DTITLEARTIST="$(echo "$TRACKNAME" | sed 's- / -~-g')"
595 TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
596 TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
c9c2ca27 597 ;;
598 forward-dash)
fe4d186c 599 DTITLEARTIST="$(echo "$TRACKNAME" | sed 's, - ,~,g')"
600 TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
601 TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
c9c2ca27 602 ;;
603 reverse)
fe4d186c 604 DTITLEARTIST="$(echo "$TRACKNAME" | sed 's- / -~-g')"
605 TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
606 TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
c9c2ca27 607 ;;
608 reverse-dash)
fe4d186c 609 DTITLEARTIST="$(echo "$TRACKNAME" | sed 's, - ,~,g')"
610 TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
611 TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
c9c2ca27 612 ;;
613 colon)
fe4d186c 614 DTITLEARTIST="$(echo "$TRACKNAME" | sed 's-: -~-g')"
615 TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
616 TRACKNAME="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
c9c2ca27 617 ;;
618 trailing-paren)
fe4d186c 619 DTITLEARTIST="$(echo "$TRACKNAME" | sed 's,^\(.*\) (\(.*\)),\1~\2,')"
620 TRACKARTIST="$(echo "$DTITLEARTIST" | cut -f2 -d~)"
621 TRACKNAME="$(echo "$DTITLEARTIST" | cut -f1 -d~)"
c9c2ca27 622 ;;
623 esac
a9a23379 624 elif [ "$VARIOUSARTISTS" = "y" ] && [ "$ONETRACK" = "y" ]; then
c9c2ca27 625 TRACKARTIST="Various"
626 else
eb1474fe 627 TRACKARTIST="$DARTIST"
c9c2ca27 628 fi
629}
630
2994a485 631do_getgenreid () {
632local genre=$(echo "${@}" | tr '[A-Z]' '[a-z]')
633local id=""
634 case ${genre} in
635 "blues") id=0 ;;
636 "classic rock") id=1 ;;
637 "country") id=2 ;;
638 "dance") id=3 ;;
639 "disco") id=4 ;;
640 "funk") id=5 ;;
641 "grunge") id=6 ;;
642 "hip-hop") id=7 ;;
643 "jazz") id=8 ;;
644 "metal") id=9 ;;
645 "new age") id=10 ;;
646 "oldies") id=11 ;;
647 "other") id=12 ;;
648 "pop") id=13 ;;
649 "r&b") id=14 ;;
650 "rap") id=15 ;;
651 "reggae") id=16 ;;
652 "rock") id=17 ;;
653 "techno") id=18 ;;
654 "industrial") id=19 ;;
655 "alternative") id=20 ;;
656 "ska") id=21 ;;
657 "death metal") id=22 ;;
658 "pranks") id=23 ;;
659 "soundtrack") id=24 ;;
660 "euro-techno") id=25 ;;
661 "ambient") id=26 ;;
662 "trip-hop") id=27 ;;
663 "vocal") id=28 ;;
664 "jazz+funk") id=29 ;;
665 "fusion") id=30 ;;
666 "trance") id=31 ;;
667 "classical") id=32 ;;
668 "instrumental") id=33 ;;
669 "acid") id=34 ;;
670 "house") id=35 ;;
671 "game") id=36 ;;
672 "sound clip") id=37 ;;
673 "gospel") id=38 ;;
674 "noise") id=39 ;;
675 "alt. rock") id=40 ;;
676 "bass") id=41 ;;
677 "soul") id=42 ;;
678 "punk") id=43 ;;
679 "space") id=44 ;;
680 "meditative") id=45 ;;
681 "instrum. pop") id=46 ;;
682 "instrum. rock") id=47 ;;
683 "ethnic") id=48 ;;
684 "gothic") id=49 ;;
685 "darkwave") id=50 ;;
686 "techno-indust.") id=51 ;;
687 "electronic") id=52 ;;
688 "pop-folk") id=53 ;;
689 "eurodance") id=54 ;;
690 "dream") id=55 ;;
691 "southern rock") id=56 ;;
692 "comedy") id=57 ;;
693 "cult") id=58 ;;
694 "gangsta") id=59 ;;
695 "top 40") id=60 ;;
696 "christian rap") id=61 ;;
a9a23379 697 "pop/funk"|"pop / funk") id=62 ;;
2994a485 698 "jungle") id=63 ;;
699 "native american") id=64 ;;
700 "cabaret") id=65 ;;
701 "new wave") id=66 ;;
702 "psychadelic") id=67 ;;
703 "rave") id=68 ;;
704 "showtunes") id=69 ;;
705 "trailer") id=70 ;;
706 "lo-fi") id=71 ;;
707 "tribal") id=72 ;;
708 "acid punk") id=73 ;;
709 "acid jazz") id=74 ;;
710 "polka") id=75 ;;
711 "retro") id=76 ;;
712 "musical") id=77 ;;
713 "rock & roll") id=78 ;;
714 "hard rock") id=79 ;;
715 "folk") id=80 ;;
716 "folk/rock") id=81 ;;
717 "national folk") id=82 ;;
718 "swing") id=83 ;;
719 "fusion") id=84 ;;
720 "bebob") id=85 ;;
721 "latin") id=86 ;;
722 "revival") id=87 ;;
723 "celtic") id=88 ;;
724 "bluegrass") id=89 ;;
725 "avantgarde") id=90 ;;
726 "gothic rock") id=91 ;;
727 "progress. rock") id=92 ;;
728 "psychadel. rock") id=93 ;;
729 "symphonic rock") id=94 ;;
730 "slow rock") id=95 ;;
731 "big band") id=96 ;;
732 "chorus") id=97 ;;
733 "easy listening") id=98 ;;
734 "acoustic") id=99 ;;
735 "humour") id=100 ;;
736 "speech") id=101 ;;
737 "chanson") id=102 ;;
738 "opera") id=103 ;;
739 "chamber music") id=104 ;;
740 "sonata") id=105 ;;
741 "symphony") id=106 ;;
742 "booty bass") id=107 ;;
743 "primus") id=108 ;;
744 "porn groove") id=109 ;;
745 "satire") id=110 ;;
746 "slow jam") id=111 ;;
747 "club") id=112 ;;
748 "tango") id=113 ;;
749 "samba") id=114 ;;
750 "folklore") id=115 ;;
751 "ballad") id=116 ;;
752 "power ballad") id=117 ;;
753 "rhythmic soul") id=118 ;;
754 "freestyle") id=119 ;;
755 "duet") id=120 ;;
756 "punk rock") id=121 ;;
757 "drum solo") id=122 ;;
758 "a capella") id=123 ;;
759 "euro-house") id=124 ;;
760 "dance hall") id=125 ;;
761 "goa") id=126 ;;
762 "drum & bass") id=127 ;;
763 "club-house") id=128 ;;
764 "hardcore") id=129 ;;
765 "terror") id=130 ;;
766 "indie") id=131 ;;
767 "britpop") id=132 ;;
768 "negerpunk") id=133 ;;
769 "polsk punk") id=134 ;;
770 "beat") id=135 ;;
771 "christian gangsta rap") id=136 ;;
772 "heavy metal") id=137 ;;
773 "black metal") id=138 ;;
774 "crossover") id=139 ;;
775 "contemporary christian")id=140 ;;
776 "christian rock") id=141 ;;
777 "merengue") id=142 ;;
778 "salsa") id=143 ;;
779 "thrash metal") id=144 ;;
780 "anime") id=145 ;;
781 "jpop") id=146 ;;
782 "synthpop") id=147 ;;
a9a23379 783 "rock/pop"|"rock / pop") id=148 ;;
2994a485 784 *) return 1 ;;
785 esac
786echo ${id}
787return 0
788}
789
c9c2ca27 790# do_tag [tracknumber]
791# id3 tags a filename
792# variables used:
b546a9eb 793# TRACKS, TRACKNAME, TRACKARTIST, TAGGER, TAGGEROPTS, VORBISCOMMENT, METAFLAC, ATOMICPARSLEY
f2eadfe8 794# COMMENT, DALBUM, DARTIST, CDYEAR, CDGENRE
c9c2ca27 795do_tag ()
796{
797 COMMENTOUTPUT="$(eval echo ${COMMENT})"
3279389d 798 if [ -z "$COMMENTOUTPUT" ]; then
799 COMMENTOUTPUT="$(getcddbinfo TRACK-INFO)"
800 fi
0aeff935
E
801 if [ "$CDDBMETHOD" = "cddb" ]; then
802 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
803 fi
c9c2ca27 804 run_command '' echo "Tagging track $1 of $TRACKS: $TRACKNAME..."
79e3928b 805 # If we want to start the tracks with a given number, we need to modify the
806 # TRACKNUM value before evaluation
c0fc2e81 807 if [ -n "$STARTTRACKNUMBERTAG" ] ; then
75285ac8 808 gettracknum
79e3928b 809 fi
c9c2ca27 810 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
811 do
1a064efb 812 case "$OUTPUT" in
813 mp3)
814 # id3v2 v0.1.9 claims to have solved the -c bug, so we merge both id3 and id3v2
815 GENREID=$(do_getgenreid "${CDGENRE}")
37bd22e5 816 # Set TPE2 in case we have a Various Artists rip.
817 TPE2=""
1e052c33 818 if [ "$VARIOUSARTISTS" = "y" ]; then
37bd22e5 819 TPE2="Various"
820 fi
1a064efb 821
822 case "$ID3SYNTAX" in
823 id3);;
824 eyed3)
634e77be 825 # FIXME # track numbers in mp3 come with 1/10, so we cannot
826 # happily substitute them with $TRACKNUM
f7c6d185 827 run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $TAGGER $TAGGEROPTS \
634e77be 828 --comment=::"$COMMENTOUTPUT" -A "$DALBUM" \
829 -a "$TRACKARTIST" -t "$TRACKNAME" -Y "$CDYEAR" \
93a69eff 830 -G "$GENREID" -n "${TRACKNUM:-$1}" \
11ce3af0
E
831 ${TRACKNUM:+-N "$TRACKS"} \
832 ${ENCODING:+--set-encoding="$ENCODING"} \
833 ${TPE2:+--set-text-frame=TPE2:"$TPE2"} \
634e77be 834 "$ABCDETEMPDIR/track$1.$OUTPUT"
835 ;;
836 # FIXME # Still not activated...
837 id3ed)
93a69eff 838 run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
839 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
840 -a "$DALBUM" -n "$TRACKARTIST" -s "$TRACKNAME" \
841 -y "$CDYEAR" -g "$GENREID" -k "${TRACKNUM:-$1}" \
1a064efb 842 "$ABCDETEMPDIR/track$1.$OUTPUT"
843 ;;
844 *)
634e77be 845 # FIXME # track numbers in mp3 come with 1/10, so we cannot
846 # happily substitute them with $TRACKNUM
93a69eff 847 run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
848 $TAGGER $TAGGEROPTS -c "$COMMENTOUTPUT" \
849 -A "$DALBUM" -a "$TRACKARTIST" -t "$TRACKNAME" \
850 -y "$CDYEAR" -g "$GENREID" \
10bfc8c6 851 -T "${TRACKNUM:-$1}/$TRACKS" \
22617ea8 852 ${TPE2:+--TPE2 "$TPE2"} \
1a064efb 853 "$ABCDETEMPDIR/track$1.$OUTPUT"
854 ;;
855 esac
856 ;;
857 vorbis|ogg)
858 case "$OGGENCODERSYNTAX" in
859 vorbize|oggenc)
860 # vorbiscomment can't do in-place modification, mv the file first
861 if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" -a ! -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" ]; then
862 mv "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
863 fi
864 (
93a69eff 865 # These are from
866 # http://www.xiph.org/ogg/vorbis/doc/v-comment.html
867
1a064efb 868 echo ARTIST="$TRACKARTIST"
869 echo ALBUM="$DALBUM"
870 echo TITLE="$TRACKNAME"
871 if [ -n "$CDYEAR" ]; then
872 echo DATE="$CDYEAR"
873 fi
874 if [ -n "$CDGENRE" ]; then
875 echo GENRE="$CDGENRE"
876 fi
877 echo TRACKNUMBER=${TRACKNUM:-$1}
101e0368 878 if [ -n "$DISCNUMBER" ]; then
879 echo DISCNUMBER="$DISCNUMBER"
880 fi
1a064efb 881 echo CDDB=$CDDBDISCID
882 if [ "$(eval echo ${COMMENT})" != "" ]; then
883 case "$COMMENTOUTPUT" in
884 *=*) echo "$COMMENTOUTPUT";;
885 *) echo COMMENT="$COMMENTOUTPUT";;
886 esac
887 fi
93a69eff 888 ) | run_command tagtrack-$OUTPUT-$1 nice $ENCNICE \
889 $VORBISCOMMENT $VORBISCOMMENTOPTS -w \
1a064efb 890 "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
93a69eff 891 # Doublecheck that the commented file was created
892 # successfully before wiping the original
1a064efb 893 if [ -f "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER" ]; then
894 rm -f "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER"
895 else
896 mv "$ABCDETEMPDIR/track$1.uncommented.$OGGOUTPUTCONTAINER" "$ABCDETEMPDIR/track$1.$OGGOUTPUTCONTAINER"
897 fi
898 ;;
899 esac
900 ;;
901 flac)
902 (
903 echo ARTIST="$TRACKARTIST"
904 echo ALBUM="$DALBUM"
905 echo TITLE="$TRACKNAME"
906 if [ -n "$CDYEAR" ]; then
907 echo DATE="$CDYEAR"
908 fi
909 if [ -n "$CDGENRE" ]; then
910 echo GENRE="$CDGENRE"
911 fi
912 echo TRACKNUMBER="${TRACKNUM:-$1}"
101e0368 913 if [ -n "$DISCNUMBER" ]; then
914 echo DISCNUMBER="$DISCNUMBER"
915 fi
1a064efb 916 echo CDDB="$CDDBDISCID"
917 if [ "$(eval echo ${COMMENT})" != "" ]; then
918 case "$COMMENTOUTPUT" in
919 *=*) echo "$COMMENTOUTPUT";;
920 *) echo COMMENT="$COMMENTOUTPUT";;
921 esac
922 fi
f7c6d185 923 ) | run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $METAFLAC $METAFLACOPTS ${IMPORTCUESHEET:+--import-cuesheet-from="$ABCDETEMPDIR/$CUEFILE"} --import-tags-from=- "$ABCDETEMPDIR/track$1.$FLACOUTPUTCONTAINER"
1a064efb 924 ;;
925 spx)
926 run_command tagtrack-$OUTPUT-$1 true
927 ;;
928 mpc)
929 run_command tagtrack-$OUTPUT-$1 true
930 ;;
dba06003 931 m4a)
3279389d 932 # Use a temp-file of our choice. --overWrite seems to
933 # case core dumps with AtomicParsley 0.9.0
934 ATOMICTEMPFILE="$ABCDETEMPDIR/track$1.m4a-atomic"
935
936 VARIOUSBOOL="false"
937 if [ "$VARIOUSARTISTS" = "y" ]; then
938 VARIOUSBOOL="true"
939 fi
940
941 #It has to be command file opts for AtomicParsley
9d790401 942 run_command tagtrack-$OUTPUT-$1 nice $ENCNICE $ATOMICPARSLEY "$ABCDETEMPDIR/track$1.m4a" --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --tracknum ${TRACKNUM:-$1} --year "$CDYEAR" --genre "$CDGENRE" --compilation $VARIOUSBOOL --comment "$COMMENTOUTPUT" --output $ATOMICTEMPFILE
3279389d 943 if [ -f $ATOMICTEMPFILE ]; then
944 mv "$ATOMICTEMPFILE" "$ABCDETEMPDIR/track$1.m4a"
945 fi
dba06003 946 ;;
b69a8bb4 947 wav)
948 run_command tagtrack-$OUTPUT-$1 true
949 ;;
c9c2ca27 950 esac
c9c2ca27 951 done
33bc33c3 952 if checkerrors "tagtrack-(.{3,6})-$1"; then :; else
1a064efb 953 run_command tagtrack-$1 true
954 fi
955
c9c2ca27 956}
957
0709e644 958# do_nogap_encode
c9c2ca27 959# variables used:
960# OUTPUTTYPE, {FOO}ENCODERSYNTAX, ENCNICE, ENCODER, ENCODEROPTS
0709e644 961do_nogap_encode ()
c9c2ca27 962{
93a69eff 963 # The commands here don't go through run_command because they're never
964 # supposed to be silenced
0709e644 965 echo "Encoding gapless MP3 tracks: $TRACKQUEUE"
966 for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
967 do
968 case "$OUTPUT" in
969 mp3)
970 case "$MP3ENCODERSYNTAX" in
4562fac6 971 lame|toolame)
0709e644 972 (
973 cd "$ABCDETEMPDIR"
974 TRACKFILES=
c9c2ca27 975 for UTRACKNUM in $TRACKQUEUE
976 do
0709e644 977 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
c9c2ca27 978 done
0709e644 979 nice $ENCNICE $MP3ENCODER $MP3ENCODEROPTS --nogap $TRACKFILES
980 RETURN=$?
981 if [ "$RETURN" != "0" ]; then
982 echo "nogap-encode: $ENCODER returned code $RETURN" >> errors
983 else
984 for UTRACKNUM in $TRACKQUEUE
985 do
986 run_command encodetrack-$OUTPUT-$UTRACKNUM true
987 #run_command encodetrack-$UTRACKNUM true
988 done
989 fi
990 )
991 ;;
992 esac
c9c2ca27 993 ;;
994 esac
0709e644 995 done
996 if checkerrors "nogap-encode"; then :; else
997 if [ ! "$KEEPWAVS" = "y" ] ; then
998 if [ ! "$KEEPWAVS" = "move" ] ; then
999 rm -f "$IN"
1000 fi
1001 fi
1002 fi
93a69eff 1003 # Other encoders fall through to normal encoding as the tracks have not
1004 # been entered in the status file.
c9c2ca27 1005}
1006
1007# do_encode [tracknumber] [hostname]
1008# If no hostname is specified, encode locally
1009# variables used:
1010# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1011do_encode ()
1012{
0a3a2dcc 1013 if [ "$USEPIPES" = "y" ]; then
1014 case "$OUTPUT" in
1015 mp3)
1016 TEMPARG="PIPE_$MP3ENCODERSYNTAX"
1017 ;;
b82e2042 1018 vorbis|ogg)
0a3a2dcc 1019 TEMPARG="PIPE_$OGGENCODERSYNTAX"
1020 ;;
1021 flac)
1022 TEMPARG="PIPE_$FLACENCODERSYNTAX"
1023 ;;
1024 spx)
1025 TEMPARG="PIPE_$SPEEXENCODER"
1026 ;;
1027 mpc)
1028 TEMPARG="PIPE_$MPPENCODER"
1029 ;;
dba06003 1030 m4a)
ab22c9f9 1031 TEMPARG="PIPE_$AACENCODERSYNTAX"
dba06003 1032 ;;
0a3a2dcc 1033 esac
a9a23379 1034 IN="$( eval echo "\$$TEMPARG" )"
0a3a2dcc 1035 else
1036 IN="$ABCDETEMPDIR/track$1.wav"
1037 case "$OUTPUT" in
1038 mp3)
1039 case "$MP3ENCODERSYNTAX" in
1040 # FIXME # check if mp3enc needs -if for pipes
cb801544 1041 # FIXME # I have not been able to find a working mp3enc binary
0a3a2dcc 1042 mp3enc)
1043 FILEARG="-if $IN"
1044 ;;
1045 *)
03308bc7 1046 FILEARG="$IN"
0a3a2dcc 1047 ;;
1048 esac
1049 ;;
1050 *)
03308bc7 1051 FILEARG="$IN"
0a3a2dcc 1052 ;;
1053 esac
1054 fi
1055 # We need IN to proceed, if we are not using pipes.
1056 if [ -s "$IN" -o X"$USEPIPES" = "Xy" ] ; then
3b4d8f9b 1057 for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
c9c2ca27 1058 do
3b4d8f9b 1059 case "$TMPOUTPUT" in
b82e2042 1060 vorbis|ogg)
a9b99f53 1061 OUTPUT=$OGGOUTPUTCONTAINER
b82e2042 1062 ;;
1063 flac)
a9b99f53 1064 OUTPUT=$FLACOUTPUTCONTAINER
b82e2042 1065 ;;
1066 *)
3b4d8f9b 1067 OUTPUT=$TMPOUTPUT
b82e2042 1068 ;;
1069 esac
9e7e3b69 1070 OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
b546a9eb 1071 if [ "$NOGAP" = "y" ] && checkstatus encodetrack-$OUTPUT-$1 ; then
a9b99f53 1072 continue
1073 fi
0a3a2dcc 1074 if [ X"$USEPIPES" = "Xy" ]; then
1075 RUN_COMMAND=""
a40a6c27 1076 # We need a way to store the creation of the files when using PIPES
1077 RUN_COMMAND_PIPES="run_command encodetrack-$OUTPUT-$1 true"
f7c6d185 1078 # When pipping it does not make sense to have a higher nice for
1079 # reading than for encoding, since it will be hold by the
1080 # encoding process. Setting an effective nice, to calm down a
1081 # bit the reading process.
1082 EFFECTIVE_NICE=$READNICE
0a3a2dcc 1083 else
1084 run_command '' echo "Encoding track $1 of $TRACKS: $TRACKNAME..."
1085 RUN_COMMAND="run_command encodetrack-$OUTPUT-$1"
f7c6d185 1086 EFFECTIVE_NICE=$ENCNICE
0a3a2dcc 1087 fi
c9c2ca27 1088 case "$OUTPUT" in
1089 mp3)
1090 case "$2" in
1091 %local*%)
1092 case "$MP3ENCODERSYNTAX" in
f7c6d185 1093 lame|toolame|gogo) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS "$IN" "$OUT" ;;
1094 bladeenc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER $MP3ENCODEROPTS -quit "$IN" ;;
1095 l3enc|xingmp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER "$IN" "$OUT" $MP3ENCODEROPTS ;;
0a3a2dcc 1096 # FIXME # Relates to the previous FIXME since it might need the "-if" removed.
f7c6d185 1097 mp3enc) $RUN_COMMAND nice $EFFECTIVE_NICE $MP3ENCODER -if "$IN" -of "$OUT" $MP3ENCODEROPTS ;;
c9c2ca27 1098 esac
1099 ;;
1100 *)
0a3a2dcc 1101 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
c9c2ca27 1102 ;;
1103 esac
1104 ;;
b82e2042 1105 vorbis|ogg)
c9c2ca27 1106 case "$2" in
1107 %local*%)
1108 case "$OGGENCODERSYNTAX" in
f7c6d185 1109 vorbize) $RUN_COMMAND nice $EFFECTIVE_NICE $OGGENCODER $OGGENCODEROPTS -w "$OUT" "$IN" ;;
1110 oggenc) $RUN_COMMAND nice $EFFECTIVE_NICE $OGGENCODER $OGGENCODEROPTS -o "$OUT" "$IN" ;;
c9c2ca27 1111 esac
1112 ;;
1113 *)
0a3a2dcc 1114 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" >/dev/null 2>&1
c9c2ca27 1115 ;;
1116 esac
1117 ;;
1118 flac)
c9c2ca27 1119 case "$2" in
535944df 1120 %local*%)
c9c2ca27 1121 case "$FLACENCODERSYNTAX" in
b546a9eb 1122 flac) $RUN_COMMAND nice $EFFECTIVE_NICE $FLACENCODER -f $FLACENCODEROPTS -o "$OUT" "$IN" ;;
1123 esac
535944df 1124 ;;
1125 *)
1126 vecho -n "DISTMP3:"
1127 vecho "$DISTMP3 $DISTMP3OPTS $2 $IN $OUT >/dev/null 2>&1"
1128 $RUN_COMMAND nice $DISTMP3NICE $DISTMP3 $DISTMP3OPTS "$2" "$IN" "$OUT" > /dev/null 2>&1
c9c2ca27 1129 ;;
1130 esac
1131 ;;
1132 spx)
1133 if [ "$(eval echo ${COMMENT})" != "" ]; then
1134 case "$COMMENT" in
1135 *=*) ;;
1136 *) COMMENT="COMMENT=$COMMENT" ;;
1137 esac
c9c2ca27 1138 fi
0114d728
E
1139 # Tag the file at encode time, as it can't be done after encoding.
1140 if [ "$DOTAG" = "y" ]; then
26cae4de 1141 $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS --author "$TRACKARTIST" --title "$TRACKNAME" ${COMMENT:+--comment "$COMMENT"} "$IN" "$OUT"
c9c2ca27 1142 else
f7c6d185 1143 $RUN_COMMAND nice $EFFECTIVE_NICE $SPEEXENCODER $SPEEXENCODEROPTS "$IN" "$OUT"
c9c2ca27 1144 fi
1145 ;;
99d009fa 1146 mpc)
1147 # MPP/MP+(Musepack) format (.mpc) is done locally, with inline
1148 # tagging.
1149 # I tried compiling the mppenc from corecodecs.org and got some
1150 # errors, so I have not tried it myself.
1151 ## FIXME ## Needs some cleanup to determine if an empty tag sent
1152 ## FIXME ## to the encoder ends up empty.
f7c6d185 1153 $RUN_COMMAND nice $EFFECTIVE_NICE $MPPENCODER $MPPENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" "$IN" "$OUT"
99d009fa 1154 ;;
dba06003 1155 m4a)
2b5ab14a
E
1156 # Tag the file at encode time, as it can't be done after encoding.
1157 if [ "$DOTAG" = "y" ]; then
f7c6d185 1158 $RUN_COMMAND nice $EFFECTIVE_NICE $AACENCODER $AACENCODEROPTS --artist "$TRACKARTIST" --album "$DALBUM" --title "$TRACKNAME" --track "$1" --genre "$CDGENRE" --year "$CDYEAR" --comment "$COMMENT" -o "$OUT" "$IN"
dba06003 1159
1160 else
1161 $RUN_COMMAND nice $ENCNICE $AACENCODER $AACENCODEROPTS -o "$OUT" "$IN"
1162 fi
1163 ;;
b69a8bb4 1164 wav)
1165 # In case of wav output we need nothing. Just keep the wavs.
b69a8bb4 1166 ;;
c9c2ca27 1167 esac
a40a6c27 1168 $RUN_COMMAND_PIPES
c9c2ca27 1169 done
1170 # Only remove .wav if the encoding succeeded
33bc33c3 1171 if checkerrors "encodetrack-(.{3,6})-$1"; then :; else
c9c2ca27 1172 run_command encodetrack-$1 true
1173 if [ ! "$KEEPWAVS" = "y" ] ; then
b69a8bb4 1174 if [ ! "$KEEPWAVS" = "move" ] ; then
1175 rm -f "$IN"
1176 fi
c9c2ca27 1177 fi
1178 fi
1179 else
1a064efb 1180 run_command "" echo "HEH! The file we were about to encode disappeared:"
1181 run_command "" echo ">> $IN"
c9c2ca27 1182 run_command encodetrack-$1 false
1183 fi
1184}
1185
99d009fa 1186# do_preprocess [tracknumber]
1187# variables used:
1188# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1189#do_preprocess ()
1190#{
1191# IN="$ABCDETEMPDIR/track$1.wav"
1192# # We need IN to proceed.
1193# if [ -s "$IN" ] ; then
1194# for OUTPUT in $(echo $OUTPUTTYPE | tr , \ )
1195# do
1196# #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
1197# run_command '' echo "Pre-processing track $1 of $TRACKS..."
1198# case "$POSTPROCESSFORMAT" in
1199# all|wav*)
1200# run_command preprocess-$OUTPUT-$1 nice $PRENICE $WAV_PRE $IF $OF ;;
1201# mp3)
1202# run_command preprocess-$OUTPUT-$1 nice $PRENICE $MP3_PRE $IF $OF ;;
1203# ogg)
1204# run_command preprocess-$OUTPUT-$1 nice $PRENICE $OGG_PRE $IF $OF ;;
1205# flac)
1206# run_command preprocess-$OUTPUT-$1 nice $PRENICE $FLAC_PRE $IF $OF ;;
1207# spx)
1208# run_command preprocess-$OUTPUT-$1 nice $PRENICE $SPX_PRE $IF $OF ;;
1209# esac
1210# done
1211# # Only remove .wav if the encoding succeeded
b546a9eb 1212# if checkerrors "preprocess-(.{3,4})-$1"; then
99d009fa 1213# run_command preprocess-$1 false
1214# else
1215# run_command preprocess-$1 true
1216# fi
1217# else
1218# if [ "$(checkstatus encode-output)" = "loud" ]; then
1219# echo "HEH! The file we were about to pre-process disappeared:"
1220# echo ">> $IN"
1221# fi
1222# run_command preprocess-$1 false
1223# fi
1224#}
1225
1226
1227# do_postprocess [tracknumber]
1228# variables used:
1229# TRACKS, TRACKNAME, TRACKARTIST, DISTMP3, DISTMP3OPTS, {FOO}ENCODERSYNTAX, OUTPUTTYPE, ENCODEROPTS, DALBUM, DARTIST, ENCNICE, CDYEAR, CDGENRE, COMMENT
1230#do_postprocess ()
1231#{
1232# for POSTPROCESSFORMAT in $(echo $POSTPROCESSFORMATS | tr , \ )
1233# do
1234# IN="$ABCDETEMPDIR/track$1.$POSTPROCESSFORMAT"
1235# # We need IN to proceed.
1236# if [ -s "$IN" ] ; then
1237# #OUT="$ABCDETEMPDIR/track$1.$OUTPUT"
1238# run_command '' echo "Post-processing track $1 of $TRACKS..."
1239# case "$POSTPROCESSFORMAT" in
1240# mp3)
1241# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $MP3_POST $IF $OF ;;
1242# ogg)
1243# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $OGG_POST $IF $OF ;;
1244# flac)
1245# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $FLAC_POST $IF $OF ;;
1246# spx)
1247# run_command postprocess-$OUTPUT-$1 nice $POSTNICE $SPX_POST $IF $OF ;;
1248# esac
1249# # Only remove .wav if the encoding succeeded
b546a9eb 1250# if checkerrors "postprocess-(.{3,4})-$1"; then
99d009fa 1251# run_command postprocess-$1 false
1252# else
1253# run_command postprocess-$1 true
1254# fi
1255# else
1256# if [ "$(checkstatus encode-output)" = "loud" ]; then
1257# echo "HEH! The file we were about to post-process disappeared:"
1258# echo ">> $IN"
1259# fi
1260# run_command postprocess-$1 false
1261# fi
1262# done
1263#}
1264
79c8f4d0 1265# do_single_gain
1266# variables used:
1267# FIXME #
1268do_single_gain ()
1269{
1270:
1271}
1272
99d009fa 1273# do_batch_gain
1274# variables used:
cb414c1e 1275# MP3GAIN, MP3GAINOPTS, VORBISGAIN, VORBISGAINOPTS, MPPGAIN, MPPGAINOPTS
79c8f4d0 1276# FIXME #
99d009fa 1277do_batch_gain ()
1278{
1279 # The commands here don't go through run_command because they're never supposed to be silenced
1280 echo "Batch analizing gain in tracks: $TRACKQUEUE"
1281 (
1282 cd "$ABCDETEMPDIR"
1283 BLURB=
1284 TRACKFILES=
1285 for UTRACKNUM in $TRACKQUEUE
1286 do
1287 MP3FILES="$TRACKFILES track$UTRACKNUM.mp3"
1288 done
cb801544 1289 # FIXME # Hard-coded batch option!
99d009fa 1290 $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
1291 RETURN=$?
1292 if [ "$RETURN" != "0" ]; then
1293 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
1294 else
1295 for UTRACKNUM in $TRACKQUEUE
1296 do
1297 echo normalizetrack-$UTRACKNUM >> status
1298 done
1299 fi
1300 )
1301}
1302
c9c2ca27 1303# do_batch_normalize
1304# variables used:
1305# NORMALIZER, NORMALIZEROPTS
1306do_batch_normalize ()
1307{
1308 # The commands here don't go through run_command because they're never supposed to be silenced
1309 echo "Batch normalizing tracks: $TRACKQUEUE"
1310 (
1311 cd "$ABCDETEMPDIR"
1312 BLURB=
1313 TRACKFILES=
1314 for UTRACKNUM in $TRACKQUEUE
1315 do
1316 TRACKFILES="$TRACKFILES track$UTRACKNUM.wav"
1317 done
1318 # XXX: Hard-coded batch option!
1319 $NORMALIZER -b $NORMALIZEROPTS $TRACKFILES
1320 RETURN=$?
1321 if [ "$RETURN" != "0" ]; then
1322 echo "batch-normalize: $NORMALIZER returned code $RETURN" >> errors
1323 else
1324 for UTRACKNUM in $TRACKQUEUE
1325 do
1326 echo normalizetrack-$UTRACKNUM >> status
1327 done
1328 fi
1329 )
1330}
1331
1332# do_normalize [tracknumber]
1333# variables used:
1334# TRACKS, TRACKNAME, NORMALIZER, NORMALIZEROPTS
1335do_normalize ()
1336{
1337 IN="$ABCDETEMPDIR/track$1.wav"
1338 if [ -e "$IN" ] ; then
1339 run_command '' echo "Normalizing track $1 of $TRACKS: $TRACKNAME..."
1340 run_command normalizetrack-$1 $NORMALIZER $NORMALIZEROPTS "$IN"
1341 else
1342 if [ "$(checkstatus encode-output)" = "loud" ]; then
1343 echo "HEH! The file we were about to normalize disappeared:"
1344 echo ">> $IN"
1345 fi
1346 run_command normalizetrack-$1 false "File $IN was not found"
1347 fi
1348}
1349
1350# do_move [tracknumber]
1351# Deduces the outfile from environment variables
1352# Creates directory if necessary
1353# variables used:
99d009fa 1354# TRACKNUM, TRACKNAME, TRACKARTIST, DALBUM, OUTPUTFORMAT, CDGENRE, CDYEAR
c9c2ca27 1355do_move ()
1356{
75285ac8 1357 for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
c9c2ca27 1358 do
75285ac8 1359 # For now, set OUTPUT as TMPOUTPUT, and then change it once we have
1360 # defined the OUTPUTFILE:
1361 OUTPUT="$TMPOUTPUT"
1362
c9c2ca27 1363 # Create ALBUMFILE, ARTISTFILE, TRACKFILE
1364 # Munge filenames as follows:
1365 # ' ' -> '_'
1366 # '/' -> '_'
1367 # ''' -> ''
1368 # '?' -> ''
1369 # Eat control characters
fe4d186c 1370 ALBUMFILE="$(mungefilename "$DALBUM")"
1371 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
1372 TRACKFILE="$(mungefilename "$TRACKNAME")"
1373 GENRE="$(mungegenre "$GENRE")"
75285ac8 1374 YEAR=${CDYEAR:-$CDYEAR}
1375 # If we want to start the tracks with a given number, we need to modify
1376 # the TRACKNUM value before evaluation
1377 gettracknum
1378 # Supported variables for OUTPUTFORMAT are GENRE, YEAR, ALBUMFILE,
1379 # ARTISTFILE, TRACKFILE, and TRACKNUM.
b546a9eb 1380 if [ "$ONETRACK" = "y" ]; then
56672932 1381 if [ "$VARIOUSARTISTS" = "y" ]; then
1382 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\")"
1383 else
1384 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\")"
b546a9eb 1385 fi
56672932 1386 else
1387 if [ "$VARIOUSARTISTS" = "y" ]; then
1388 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT"\")"
1389 else
1390 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT"\")"
1391 fi
b546a9eb 1392 fi
1a064efb 1393 if checkerrors "tagtrack-$OUTPUT-$1"; then :; else
75285ac8 1394 # Once we know the specific output was successful, we can change
1395 # the OUTPUT to the value containing the container
1396 case $TMPOUTPUT in
1a064efb 1397 vorbis|ogg)
1398 OUTPUT=$OGGOUTPUTCONTAINER
1399 ;;
1400 flac)
1401 OUTPUT=$FLACOUTPUTCONTAINER
1402 ;;
75285ac8 1403 *)
1404 OUTPUT=$TMPOUTPUT
1405 ;;
1a064efb 1406 esac
1407 # Check that the directory for OUTPUTFILE exists, if it doesn't, create it
fe4d186c 1408 OUTPUTFILEDIR="$(dirname "$OUTPUTDIR/$OUTPUTFILE")"
75c2c424 1409 case $OUTPUT in
1410 wav)
1411 if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
cb801544 1412 # FIXME # introduce warnings?
75c2c424 1413 :
1414 else
1415 # mkdir -p shouldn't return an error if the directory already exists
1416 mkdir -p "$OUTPUTFILEDIR"
1417 run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
1418 if checkstatus movetrack-output-$OUTPUT; then :; else
1419 run_command movetrack-output-$OUTPUT true
1420 fi
1421 fi
1422 ;;
1423 *)
1424 # mkdir -p shouldn't return an error if the directory already exists
1425 mkdir -p "$OUTPUTFILEDIR"
1426 run_command movetrack-$1 mv "$ABCDETEMPDIR/track$1.$OUTPUT" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT"
1427 if checkstatus movetrack-output-$OUTPUT; then :; else
1428 run_command movetrack-output-$OUTPUT true
1429 fi
1430 ;;
1431 esac
1a064efb 1432 # Lets move the cue file
b546a9eb 1433 if CUEFILE=$(checkstatus cuefile) >/dev/null ; then
1a064efb 1434 if [ -r "$ABCDETEMPDIR/$CUEFILE" ]; then
1435 if checkstatus movecue-$OUTPUT; then :; else
1436 # Silence the Copying output since it overlaps with encoding processes...
1437 #run_command '' vecho "Copying cue file to its destination directory..."
1438 if checkstatus onetrack >/dev/null ; then
1439 case $OUTPUT in
75c2c424 1440 wav)
1441 if [ "$DOCLEAN" != "y" ] && [ "$FORCE" != "y" ]; then
1442 # We dont have the dir, since it was not created before.
1443 :
1444 else
1445 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
1446 fi
1447 ;;
1a064efb 1448 # NOTE: Creating a cue file with the 3-char-extension files is to comply with
1449 # http://brianvictor.tripod.com/mp3cue.htm#details
1450 [a-z0-9][a-z0-9][a-z0-9])
1451 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.cue"
1452 ;;
1453 *)
1454 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTDIR/$OUTPUTFILE.$OUTPUT.cue"
1455 ;;
1456 esac
1457 else
1458 run_command movecue-$OUTPUT cp "$ABCDETEMPDIR/$CUEFILE" "$OUTPUTFILEDIR/$CUEFILE"
1459 fi
1460 echo movecue-$OUTPUT >> "$ABCDETEMPDIR/status"
3416c114 1461 fi
b82e2042 1462 fi
1463 fi
1464 fi
c9c2ca27 1465 done
1466}
1467
1468# do_playlist
1469# Create the playlist if wanted
1470# Variables used:
1471# PLAYLISTFORMAT, PLAYLISTDATAPREFIX, VAPLAYLISTFORMAT, VAPLAYLISTDATAPREFIX,
1472# VARIOUSARTISTS, OUTPUTDIR
1473do_playlist ()
1474{
3b4d8f9b 1475 for TMPOUTPUT in $(echo $OUTPUTTYPE | tr , \ )
c9c2ca27 1476 do
3b4d8f9b 1477 case $TMPOUTPUT in
0705fcef 1478 vorbis|ogg)
1479 OUTPUT=$OGGOUTPUTCONTAINER
1480 ;;
1481 flac)
1482 OUTPUT=$FLACOUTPUTCONTAINER
1483 ;;
3b4d8f9b 1484 *)
1485 OUTPUT=$TMPOUTPUT
1486 ;;
0705fcef 1487 esac
c9c2ca27 1488 # Create a playlist file for the playlist data to go into.
1489 # We used to wipe it out if it existed. Now we request permision if interactive.
1490 for LASTTRACK in $TRACKQUEUE; do :; done
fe4d186c 1491 ALBUMFILE="$(mungefilename "$DALBUM")"
1492 ARTISTFILE="$(mungefilename "$DARTIST")"
75285ac8 1493 GENRE="$(mungegenre "$GENRE")"
76448acf 1494 YEAR=${CDYEAR:-$CDYEAR}
c9c2ca27 1495 if [ "$VARIOUSARTISTS" = "y" ] ; then
75285ac8 1496 PLAYLISTFILE="$(eval echo "$VAPLAYLISTFORMAT")"
c9c2ca27 1497 else
75285ac8 1498 PLAYLISTFILE="$(eval echo "$PLAYLISTFORMAT")"
c9c2ca27 1499 fi
75285ac8 1500 FINALPLAYLISTDIR="$(dirname "$OUTPUTDIR/$PLAYLISTFILE")"
c9c2ca27 1501 mkdir -p "$FINALPLAYLISTDIR"
1502 if [ -s "$OUTPUTDIR/$PLAYLISTFILE" ]; then
99d009fa 1503 echo -n "Erase, Append to, or Keep the existing playlist file? [e/a/k] (e): " >&2
c9c2ca27 1504 if [ "$INTERACTIVE" = "y" ]; then
99d009fa 1505 while [ "$DONE" != "y" ]; do
1506 read ERASEPLAYLIST
1507 case $ERASEPLAYLIST in
1508 e|E|a|A|k|K) DONE=y ;;
b8d3298b 1509 "") ERASEPLAYLIST=e ; DONE=y ;;
99d009fa 1510 *) ;;
1511 esac
1512 done
c9c2ca27 1513 else
99d009fa 1514 echo e >&2
1515 ERASEPLAYLIST=e
c9c2ca27 1516 fi
40c61684 1517 # Once we erase the playlist, we use append to create the new one.
1518 [ "$ERASEPLAYLIST" = "e" -o "$ERASEPLAYLIST" = "E" ] && rm -f "$OUTPUTDIR/$PLAYLISTFILE" && ERASEPLAYLIST=a
1519 else
1520 # The playlist does not exist, so we can safelly use append to create the new list
1521 ERASEPLAYLIST=a
c9c2ca27 1522 fi
99d009fa 1523 if [ "$ERASEPLAYLIST" = "a" -o "$ERASEPLAYLIST" = "A" ]; then
1524 touch "$OUTPUTDIR/$PLAYLISTFILE"
1525 for UTRACKNUM in $TRACKQUEUE
1526 do
1527 # Shares some code with do_move since the filenames have to match
1528 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
75285ac8 1529 getcddbinfo TRACKNAME
99d009fa 1530 splitvarious
fe4d186c 1531 TRACKFILE="$(mungefilename "$TRACKNAME")"
1532 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
1533 ALBUMFILE="$(mungefilename "$DALBUM")"
99d009fa 1534 # If we want to start the tracks with a given number, we need to modify the
1535 # TRACKNUM value before evaluation
75285ac8 1536 gettracknum
99d009fa 1537 if [ "$VARIOUSARTISTS" = "y" ]; then
3b9371f1 1538 OUTPUTFILE="$(eval echo \""$VAOUTPUTFORMAT\"")"
c9c2ca27 1539 else
3b9371f1 1540 OUTPUTFILE="$(eval echo \""$OUTPUTFORMAT\"")"
c9c2ca27 1541 fi
99d009fa 1542 if [ "$VARIOUSARTISTS" = "y" ]; then
1543 if [ "$VAPLAYLISTDATAPREFIX" ] ; then
1544 echo ${VAPLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
1545 else
1546 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1547 fi
1548 else
1549 if [ "$PLAYLISTDATAPREFIX" ]; then
1550 echo ${PLAYLISTDATAPREFIX}$OUTPUTFILE.$OUTPUT >> "$OUTPUTDIR/$PLAYLISTFILE"
1551 else
1552 relpath "$PLAYLISTFILE", "$OUTPUTFILE.$OUTPUT" >> "$OUTPUTDIR/$PLAYLISTFILE"
1553 fi
1554 fi
1555 done
1556 fi
c9c2ca27 1557 ## this will convert the playlist to have CRLF line-endings, if specified
1558 ## (some hardware players insist on CRLF endings)
1559 if [ "$DOSPLAYLIST" = "y" ]; then
66cd5a85 1560 awk '{sub("\r$",""); printf "%s\r\n", $0}' "$OUTPUTDIR/$PLAYLISTFILE" > "$ABCDETEMPDIR/PLAYLISTFILE.tmp"
99d009fa 1561# mv -f "$ABCDETEMPDIR/PLAYLISTFILE.tmp" "$OUTPUTDIR/$PLAYLISTFILE"
1562 cat "$ABCDETEMPDIR/PLAYLISTFILE.tmp" | sed 's/\//\\/' > "$OUTPUTDIR/$PLAYLISTFILE"
c9c2ca27 1563 fi
1564 echo "playlistcomplete" >> "$ABCDETEMPDIR/status"
1565 done
1566}
1567
4562fac6 1568# abcde.cue2discid
1569# This function reads a cuefile on stdin and writes an extended
1570# cddb query on stdout. Any PREGAP for track 1 is properly
1571# handled, although cue files embedded in FLAC files do not
1572# appear to properly store the PREGAP setting. :(
1573abcde.cue2discid () {
1574
1575 cddb_sum () {
1576 val=$1
1577 ret=0
1578 while [ $val -gt 0 ] ; do
1579 ret=$(( $ret + ( $val % 10) ))
1580 val=$(( $val / 10 ))
1581 done
1582 echo $ret
1583 }
1584
1585 msf2lba () {
1586 OIFS="$IFS"
1587 IFS=":"
1588 set -- $1
1589 IFS="$OIFS"
a9c088da 1590 local first second third
1591 first=$(expr ${1} + 0 )
1592 second=$(expr ${2} + 0 )
1593 third=$(expr ${3} + 0 )
4562fac6 1594
a9c088da 1595 echo $(( ((($first * 60) + $second) * 75) + $third ))
4562fac6 1596 }
1597
1598 OFFSET=150
1599 PREGAP=0
1600 LEADOUT=0
1601 LEADIN=88200
1602 i=0
1603 N=0
1604
1605 while read line ; do
1606 set -- $line
1607 case "$1" in
1608 TRACK) i=$(( i + 1 ))
1609 ;;
1610 INDEX) if [ "$2" -eq 1 ] ; then
1611 LBA=$(msf2lba $3)
1612 START=$(( $LBA + $PREGAP + $OFFSET ))
1613 eval TRACK$i=$START
1614 X=$(cddb_sum $(( $START / 75 )) )
1615 N=$(( $N + $X ))
1616 fi
1617 ;;
1618 PREGAP) PREGAP=$(msf2lba $2)
1619 ;;
b546a9eb 1620 REM) case "$2" in
4562fac6 1621 FLAC__lead-out)
1622 LEADOUT=$(( $4 / 588 ))
1623 ;;
1624 FLAC__lead-in)
1625 LEADIN=$(( $3 / 588 ))
1626 ;;
1627 esac
1628 ;;
1629 esac
1630
1631 done
1632
1633 TRACKS=$i
1634 LEADOUT=$(( $LEADOUT + $LEADIN ))
1635
1636 LENGTH=$(( $LEADOUT/75 - $TRACK1/75 ))
1637 DISCID=$(( ( $N % 255 ) * 2**24 | $LENGTH * 2**8 | $TRACKS ))
1638 printf "%08x %i" $DISCID $TRACKS
1639
1640 j=1
1641 while [ $j -le $TRACKS ] ; do
1642 eval echo -n "\" \$TRACK$j\""
1643 j=$((j+1))
1644 done
1645 echo " $(( $LEADOUT / 75 ))"
1646}
1647
1648# abcde.mkcue
1649# abcde.mkcue [--wholedisk]
1650# This creates a cuefile directly from the extended discid information
1651# The --wholedisk option controls whether we're ripping data from the
1652# start of track one or from the start of the disk (usually, but not
1653# always the same thing!)
1654#
1655# Track one leadin/pregap (if any) handeling:
1656# --wholedisk specified:
1657# TRACK 01 AUDIO
1658# INDEX 00 00:00:00
1659# INDEX 01 <pregap value>
1660# Remaining track index values unchanged from disc TOC
1661#
1662# --wholedisk not specified
1663# TRACK 01 AUDIO
1664# PREGAP <pregap value>
1665# INDEX 01 00:00:00
1666# Remaining track index values offset by <pregap value>
1667#
1668# Variables used:
1669# TRACKINFO
1670abcde.mkcue () {
1671
1672 echomsf () {
1673 printf "$1%02i:%02i:%02i\n" $(($2/4500)) $((($2/75)%60)) $(($2%75))
b546a9eb 1674 }
4562fac6 1675
1676 local MODE DISCID TRACKS
b546a9eb 1677 local i OFFSET LBA
1678 local CUEWAVFILE
1679
1680 if [ "$1" = --wholedisc ] ; then
1681 MODE=INDEX
1682 else
1683 MODE=PREGAP
1684 fi
1685
1686 vecho "One track is $ONETRACK"
1687 TRACKFILE="$(mungefilename "$TRACKNAME")"
1688 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
1689 ALBUMFILE="$(mungefilename "$DALBUM")"
1690 if [ "$ONETRACK" = "y" ]; then
1691 if [ "$VARIOUSARTISTS" = "y" ]; then
1692 CUEWAVFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
1693 else
1694 CUEWAVFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
4562fac6 1695 fi
b546a9eb 1696 vecho "Cue wav file is $CUEWAVFILE"
1697 else
1698 CUEWAVFILE="dummy.wav"
1699 fi
1700
1701 set -- $TRACKINFO
1702
1703 DISCID=$1
1704 TRACKS=$2
1705 shift 2
1706
1707 echo REM DISCID $DISCID
1708 echo FILE \""$CUEWAVEFILE"\" WAVE
1709
1710 if [ $1 -ne 150 ] && [ $MODE = "PREGAP" ] ; then
1711 OFFSET=$1
1712 else
1713 OFFSET=150
1714 fi
1715
1716 i=1
1717 while [ $i -le "$TRACKS" ] ; do
1718 LBA=$(( $1 - $OFFSET ))
1719 printf " TRACK %02i AUDIO\n" $i
1720 if [ $i -eq 1 -a $1 -ne 150 ] ; then
1721 if [ $MODE = PREGAP ] ; then
1722 echomsf " PREGAP " $(($OFFSET-150))
1723 else
1724 echo " INDEX 00 00:00:00"
1725 fi
1726 fi
1727 echomsf " INDEX 01 " $LBA
4562fac6 1728 i=$(($i+1))
b546a9eb 1729 shift
1730 done
4562fac6 1731}
1732
c9c2ca27 1733# do_discid
1734# This essentially the start of things
1735do_discid ()
1736{
1737 # Query the CD to get the track info, unless the user specified -C
12c5d978 1738 # or we are using some actions which do not need the CDDB data at all
1739 #if [ ! X"$EXPACTIONS" = "X" ]; then
1740 # :
1741 #elif [ -z "$DISCID" ]; then
c9c2ca27 1742 if [ -z "$DISCID" ]; then
9f659ada 1743 vecho -n "Getting CD track info... "
b82e2042 1744 # In OSX, unmount the disc before a query
f97e22a3 1745 if [ "$OSFLAVOUR" = "OSX" ]; then
a1d79009 1746 diskutil unmount ${CDROM#/dev/}
f97e22a3 1747 fi
d68ebfff 1748 case "$CDROMREADERSYNTAX" in
1749 flac)
1750 if $METAFLAC $METAFLACOPTS --export-cuesheet-to=- "$CDROM" > /dev/null 2>&1 ; then
1751 case "$CUE2DISCID" in
581a2153 1752 # FIXME # right now we have 2 cue2discid internal
1753 # implementations: builtin and abcde.cue2discid. Test
1754 # both of them and decide which one we want to use.
d68ebfff 1755 builtin)
ffd57cfc 1756 #vecho "Using builtin cue2discid implementation..."
d68ebfff 1757 CUESHEET="$(metaflac $METAFLACOPTS --export-cuesheet-to=- "$CDROM")"
1758
991009a4 1759 #TRACKS=$(echo $CUESHEET | grep -E "TRACK \+[[:digit:]]\+ \+AUDIO" |wc -l)
d68ebfff 1760 #TRACKS=0
1761 OFFSETTIMES=( $(echo "$CUESHEET" | sed -n -e's/\ *INDEX 01\ \+//p' ) )
1762 TRACKS=${#OFFSETTIMES[@]}
1763 unset OFFSETS
1764 #echo "processing offsetimes ${OFFSETTIMES[@]}"
1765 for OFFSETTIME in ${OFFSETTIMES[@]}; do
1766 OFFSETS="$OFFSETS $(( 10#${OFFSETTIME:0:2} * 4500 + 10#${OFFSETTIME:3:2} * 75 + 10#${OFFSETTIME:6:2} ))"
1767 #OFFSETS[${#OFFSETS[*]}]=$(( 10#${OFFSETTIME:0:2} * 4500 + 10#${OFFSETTIME:3:2} * 75 + 10#${OFFSETTIME:6:2} ))
1768 done
1769
46ade197 1770 LEADOUT=$(( $(echo "$CUESHEET" | grep lead-out | get_last) * 75 / 44100 ))
1771 LEADIN=$(( $(echo "$CUESHEET" | grep lead-in | get_last) * 75 / 44100 ))
d68ebfff 1772 makeids
1773 ;;
1774 *)
ffd57cfc 1775 #vecho "Using external python cue2discid implementation..."
d68ebfff 1776 TRACKINFO=$($METAFLAC $METAFLACOPTS --export-cuesheet-to=- "$CDROM" | $CUE2DISCID)
1777 ;;
1778 esac
1779 else
1780 log error "the input flac file does not contain a cuesheet."
1781 exit 1
1782 fi
1783 ;;
1784# cdparanoia|debug)
4562fac6 1785# CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -Q --verbose 2>&1 )"
d68ebfff 1786# RET=$?
1787# if [ ! "$RET" = "0" ];then
1788# log warning "something went wrong while querying the CD... Maybe a DATA CD?"
1789# fi
1790#
991009a4 1791# TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
d68ebfff 1792# CDPARANOIAAUDIOTRACKS="$TRACKS"
1793#
991009a4 1794# LEADOUT="$(echo "$CDPARANOIAOUTPUT" | grep -Eo '^TOTAL[[:space:]]+([[:digit:]]+)' | get_last)"
b546a9eb 1795# OFFSETS="$(echo "$CDPARANOIAOUTPUT" | sed -n -e's/^ .* \([0-9]\+\) \[.*/\1/p')"
d68ebfff 1796# makeids
1797# ;;
1798 *)
1799 case "$CDDBMETHOD" in
4562fac6 1800 cddb) TRACKINFO=$($CDDISCID "$CDROM") ;;
2c2dffe6 1801 musicbrainz) TRACKINFO=$($MUSICBRAINZ --command id --device "$CDROM") ;;
d68ebfff 1802 esac
1803 ;;
1804 esac
c9c2ca27 1805 # Make sure there's a CD in there by checking cd-discid's return code
1b94d6b7 1806 if [ ! "$?" = "0" ]; then
1807 if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
535944df 1808 log error "cuesheet information from the flac file could not be read."
1809 log error "Perhaps the flac file does not contain a cuesheet?."
1b94d6b7 1810 exit 1
1811 else
535944df 1812 log error "CD could not be read. Perhaps there's no CD in the drive?"
1b94d6b7 1813 exit 1
1814 fi
c9c2ca27 1815 fi
b82e2042 1816 # In OSX, remount the disc again
1817 if [ "$OSFLAVOUR" = "OSX" ]; then
a1d79009 1818 diskutil mount ${CDROM#/dev/}
b82e2042 1819 fi
99d009fa 1820 WEHAVEACD=y
70da1809 1821 DISCID=$(echo $TRACKINFO | cut -f1 -d' ')
c9c2ca27 1822 else
1823 TRACKINFO=$(cat "$WAVOUTPUTDIR/abcde.$DISCID/discid")
1824 fi
1825
1826 # Get a full enumeration of tracks, sort it, and put it in the TRACKQUEUE.
1827 # This needs to be done now because a section of the resuming code will need
1828 # it later.
1829
1830 # get the number of digits to pad TRACKNUM with - we'll use this later
12c5d978 1831 # a CD can only hold 99 tracks, but since we support a feature for starting
1832 # numbering the tracks from a given number, we might need to set it as a
1833 # variable for the user to define... or obtain it somehow.
c9c2ca27 1834 if [ "$PADTRACKS" = "y" ] ; then
1835 TRACKNUMPADDING=2
1836 fi
1837
99d009fa 1838 ABCDETEMPDIR="$WAVOUTPUTDIR/abcde.$(echo $TRACKINFO | cut -f1 -d' ')"
c9c2ca27 1839 if [ -z "$TRACKQUEUE" ]; then
d68ebfff 1840 if [ ! "$STRIPDATATRACKS" = "n" ]; then
99d009fa 1841 case "$CDROMREADERSYNTAX" in
1842 cdparanoia|debug)
1843 if [ "$WEHAVEACD" = "y" ]; then
1844 vecho "Querying the CD for audio tracks..."
4562fac6 1845 CDPARANOIAOUTPUT="$( $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -Q --verbose 2>&1 )"
09a85005 1846 RET=$?
1847 if [ ! "$RET" = "0" ];then
535944df 1848 log warning "something went wrong while querying the CD... Maybe a DATA CD?"
09a85005 1849 fi
991009a4 1850 TRACKS="$(echo "$CDPARANOIAOUTPUT" | grep -E '^[[:space:]]+[[:digit:]]' | tail -n 1 | get_first | tr -d "." | tr '\n' ' ')"
99d009fa 1851 CDPARANOIAAUDIOTRACKS="$TRACKS"
1852 else
9cf8eb81 1853 # Previous versions of abcde would store the tracks on a file, instead of the status record.
1854 if [ -f "$ABCDETEMPDIR/cdparanoia-audio-tracks" ]; then
1855 echo cdparanoia-audio-tracks=$( cat "$ABCDETEMPDIR/cdparanoia-audio-tracks" ) >> "$ABCDETEMPDIR/status"
1856 rm -f "$ABCDETEMPDIR/cdparanoia-audio-tracks"
1857 fi
088b03ff 1858 if [ -f "$ABCDETEMPDIR/status" ] && TRACKS=$(checkstatus cdparanoia-audio-tracks); then :; else
99d009fa 1859 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1860 fi
1861 fi
1862 ;;
1863 *) TRACKS=$(echo $TRACKINFO | cut -f2 -d' ') ;;
1864 esac
1865 else
1866 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
1867 fi
bef9c2d8 1868 if echo "$TRACKS" | grep "[[:digit:]]" > /dev/null 2>&1 ;then :;else
535944df 1869 log info "The disc does not contain any tracks. Giving up..."
09a85005 1870 exit 0
1871 fi
c9c2ca27 1872 echo -n "Grabbing entire CD - tracks: "
1873 if [ ! "$PADTRACKS" = "y" ] ; then
1874 TRACKNUMPADDING=$(echo -n $TRACKS | wc -c | tr -d ' ')
1875 fi
1876 TRACKS=$(printf "%0.${TRACKNUMPADDING}d" $TRACKS)
1877 X=0
1878 while [ "$X" -ne "$TRACKS" ]
1879 do
1880 X=$(printf "%0.${TRACKNUMPADDING}d" $(expr $X + 1))
0709e644 1881 TRACKQUEUE=$(echo $TRACKQUEUE $X)
c9c2ca27 1882 done
1883 echo $TRACKQUEUE
1884 else
99d009fa 1885 TRACKS=$(echo $TRACKINFO | cut -f2 -d' ')
c9c2ca27 1886 # User-supplied track queue.
1887 # Weed out non-numbers, whitespace, then sort and weed out duplicates
1888 TRACKQUEUE=$(echo $TRACKQUEUE | sed 's-[^0-9 ]--g' | tr ' ' '\n' | grep -v ^$ | sort -n | uniq | tr '\n' ' ' | sed 's- $--g')
1889 # Once cleaned, obtain the highest value in the trackqueue for number padding
1890 for LASTTRACK in $TRACKQUEUE; do :; done
1891 if [ ! "$PADTRACKS" = "y" ] ; then
1892 TRACKNUMPADDING=$(echo -n $LASTTRACK | wc -c | tr -d ' ')
1893 fi
1894 # Now we normalize the trackqueue
1895 for TRACK in $TRACKQUEUE ; do
1896 TRACKNUM=$(printf %0.${TRACKNUMPADDING}d $(expr ${TRACK} + 0 ))
1897 PADTRACKQUEUE=$(echo $PADTRACKQUEUE $TRACKNUM)
1898 done
1899 TRACKQUEUE=$PADTRACKQUEUE
1900 echo Grabbing tracks: "$TRACKQUEUE"
1901 fi
1902
c9c2ca27 1903 QUEUEDTRACKS=$(echo $TRACKQUEUE | wc -w | tr -d ' ')
1904
1905 # We have the discid, create a temp directory after it to store all the temp
1906 # info
1907
c9c2ca27 1908 if [ -e "$ABCDETEMPDIR" ]; then
1909 echo -n "abcde: attempting to resume from $ABCDETEMPDIR"
1910 # It already exists, see if it's a directory
1911 if [ ! -d "$ABCDETEMPDIR" ]; then
1912 # This is a file/socket/fifo/device/etc, not a directory
1913 # Complain and exit
1914 echo >&2
1915 echo "abcde: file $ABCDETEMPDIR already exists and does not belong to abcde." >&2
1916 echo "Please investigate, remove it, and rerun abcde." >&2
1917 exit 1
1918 fi
1919 echo -n .
2d0b3b3c 1920 # It's a directory, let's see if it's writable by us
1921 if [ ! -r "$ABCDETEMPDIR" ] || [ ! -w "$ABCDETEMPDIR" ] || [ ! -x "$ABCDETEMPDIR" ]; then
c9c2ca27 1922 # Nope, complain and exit
1923 echo >&2
2d0b3b3c 1924 echo "abcde: directory $ABCDETEMPDIR already exists and is not writeable." >&2
c9c2ca27 1925 echo "Please investigate, remove it, and rerun abcde." >&2
1926 exit 1
1927 fi
1928 echo .
1929 # See if it's populated
1930 if [ ! -f "$ABCDETEMPDIR/discid" ]; then
1931 # Wipe and start fresh
1932 echo "abcde: $ABCDETEMPDIR/discid not found. Abcde must remove and recreate" >&2
8d685d48 1933 echo -n "this directory to continue. Continue [y/N]? " >&2
c9c2ca27 1934 if [ "$INTERACTIVE" = "y" ]; then
1935 read ANSWER
1936 else
1937 echo y >&2
1938 ANSWER=y
1939 fi
1940 if [ "$ANSWER" != "y" ]; then
1941 exit 1
1942 fi
1943 rm -rf "$ABCDETEMPDIR" || exit 1
66fd4e44 1944 mkdir -p "$ABCDETEMPDIR"
c9c2ca27 1945 if [ "$?" -gt "0" ]; then
1946 # Directory already exists or could not be created
1947 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
1948 exit 1
1949 fi
1950 else
1951 # Everything is fine. Check for ^encodetracklocation-
1952 # and encode-output entries in the status file and
1953 # remove them. These are not relevant across sessions.
1954 if [ -f "$ABCDETEMPDIR/status" ]; then
1955 mv "$ABCDETEMPDIR/status" "$ABCDETEMPDIR/status.old"
1956 grep -v ^encodetracklocation- < "$ABCDETEMPDIR/status.old" \
1957 | grep -v ^encode-output > "$ABCDETEMPDIR/status"
1958 fi
1959 # Remove old error messages
1960 if [ -f "$ABCDETEMPDIR/errors" ]; then
1961 rm -f "$ABCDETEMPDIR/errors"
1962 fi
1963 fi
1964 else
1965 # We are starting from scratch
66fd4e44 1966 mkdir -p "$ABCDETEMPDIR"
c9c2ca27 1967 if [ "$?" -gt "0" ]; then
1968 # Directory already exists or could not be created
1969 echo "abcde: Temp directory $ABCDETEMPDIR could not be created." >&2
1970 exit 1
1971 fi
1972 cat /dev/null > "$ABCDETEMPDIR/status"
9cf8eb81 1973 # Store the abcde version in the status file.
1974 echo "abcde-version=$VERSION" >> "$ABCDETEMPDIR/status"
c9c2ca27 1975 fi
f7c6d185 1976 if [ X"$DOCUE" = "Xy" -a X"$WEHAVEACD" = "Xy" ]; then
b82e2042 1977 if checkstatus cuefile > /dev/null 2>&1 ; then :; else
1978 CUEFILE=cue-$(echo "$TRACKINFO" | cut -f1 -d' ').txt
1979 vecho "Creating cue file..."
f37b07d0 1980 case $CDROMREADERSYNTAX in
1981 flac)
b546a9eb 1982 if $METAFLAC --export-cuesheet-to=- "$CDROM" > "$ABCDETEMPDIR/$CUEFILE"; then
4562fac6 1983 echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
1984 else
f37b07d0 1985 log warning "the input flac file does not contain a cuesheet."
1986 fi
1987 ;;
1988 *)
1989 if $CUEREADER $CUEREADEROPTS > "$ABCDETEMPDIR/$CUEFILE"; then
1990 echo cuefile=$CUEFILE >> "$ABCDETEMPDIR/status"
1991 else
d68ebfff 1992 log warning "reading the CUE sheet is still considered experimental"
f37b07d0 1993 log warning "and there was a problem with the CD reading. abcde will continue,"
1994 log warning "but consider reporting the problem to the abcde author"
1995 fi
1996 ;;
1997 esac
24f444d7 1998 fi
1999 fi
99d009fa 2000 # If we got the CDPARANOIA status and it is not recorded, save it now
c7ea851d 2001 if [ -n "$CDPARANOIAAUDIOTRACKS" ]; then
088b03ff 2002 if checkstatus cdparanoia-audio-tracks > /dev/null 2>&1; then :; else
79558516 2003 echo cdparanoia-audio-tracks=$CDPARANOIAAUDIOTRACKS >> "$ABCDETEMPDIR/status"
99d009fa 2004 fi
2005 fi
2006
c9c2ca27 2007 # Create the discid file
2008 echo "$TRACKINFO" > "$ABCDETEMPDIR/discid"
dba06003 2009 if checkstatus cddbmethod > /dev/null 2>&1 ; then :; else
2010 echo "cddbmethod=$CDDBMETHOD" >> "$ABCDETEMPDIR/status"
2011 fi
c9c2ca27 2012}
2013
79c8f4d0 2014# do_cleancue
2015# Create a proper CUE file based on the CUE file we created before.
2016do_cleancue()
f97e22a3 2017{
b82e2042 2018 if CUEFILE_IN="$ABCDETEMPDIR"/$(checkstatus cuefile); then
2019 CUEFILE_OUT=$CUEFILE_IN.out
79c8f4d0 2020 ### FIXME ### checkstatus cddb
b82e2042 2021 if [ -e "$CDDBDATA" ]; then
2022 vecho "Adding metadata to the cue file..."
2023 # FIXME It doesn't preserve spaces! Why?
2024 # FIXME parse $track into PERFORMER and TITLE - abcde already has code for this?
2025 n=1
2026 echo "PERFORMER \"$DARTIST\"" >> "$CUEFILE_OUT"
2027 echo "TITLE \"$DALBUM\"" >> "$CUEFILE_OUT"
4562fac6 2028 # Set IFS to <newline> to prevent read from swallowing spaces and tabs
2029 OIFS="$IFS"
2030 IFS='
2031'
b82e2042 2032 cat "$CUEFILE_IN" | while read line
2033 do
3b9371f1 2034 if echo "$line" | grep "INDEX 01" > /dev/null 2>&1 ; then
4562fac6 2035# FIXME # Possible patch: remove the line above, uncomment the 2 lines below.
2036# echo "$line" >> "$CUEFILE_OUT"
2037# if echo "$line" | grep "^[[:space:]]*TRACK" > /dev/null 2>&1 ; then
b82e2042 2038 eval track="\$TRACK$n"
2039 n=$(expr $n + 1)
4562fac6 2040 echo " TITLE \"$track\"" >> "$CUEFILE_OUT"
34cdb166 2041 # When making a single-track rip, put the
2042 # actual file name into the file declaration
2043 # in the cue file so that it is usable by
2044 # music players and the like
2045 elif [ "$ONETRACK" = "y" ] &&
2046 echo "$line" | grep '^FILE "dummy.wav" WAVE' > /dev/null 2>&1 ; then
2047
2048 TRACKFILE="$(mungefilename "$TRACKNAME")"
2049 ARTISTFILE="$(mungefilename "$TRACKARTIST")"
2050 ALBUMFILE="$(mungefilename "$DALBUM")"
2051
2052 if [ "$VARIOUSARTISTS" = "y" ]; then
2053 OUTPUTFILE="$(eval echo \""$VAONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2054 else
2055 OUTPUTFILE="$(eval echo \""$ONETRACKOUTPUTFORMAT"\" | sed -e 's@^.*/@@').$OUTPUT"
2056 fi
2057
2058 echo "FILE \"$OUTPUTFILE\" WAVE" >> "$CUEFILE_OUT"
2059 continue
b82e2042 2060 fi
4562fac6 2061# FIXME # If the lines above are uncommented, remove the line below.
b82e2042 2062 echo "$line" >> "$CUEFILE_OUT"
2063 done
4562fac6 2064 IFS="$OIFS"
b82e2042 2065 mv "$CUEFILE_OUT" "$CUEFILE_IN"
2066 echo "cleancuefile" >> "$ABCDETEMPDIR/status"
79c8f4d0 2067 fi
2068 fi
f97e22a3 2069}
2070
c9c2ca27 2071# do_cddbparse
2072# Parses a CDDB file and outputs the title and the track names.
2073# Variables: CDDBFILE
2074do_cddbparse ()
2075{
2076 CDDBPARSEFILE="$1"
2077 # List out disc title/author and contents
2078 if [ "$ONETRACK" = "y" ]; then
2079 vecho "ONETRACK mode selected: displaying only the title of the CD..."
2080 fi
2081 echo "---- $(grep DTITLE "${CDDBPARSEFILE}" | cut '-d=' -f2- | tr -d \\r\\n ) ----"
9ff5088c 2082 if [ X"$SHOWCDDBYEAR" = "Xy" ]; then
2083 PARSEDYEAR=$(grep DYEAR "${CDDBPARSEFILE}" | cut '-d=' -f2-)
913e4339 2084 if [ ! X"$PARSEDYEAR" = "X" ]; then
f75dae43 2085 echo "Year: $PARSEDYEAR"
9ff5088c 2086 fi
2087 fi
2088 if [ X"$SHOWCDDBGENRE" = "Xy" ]; then
2089 PARSEDGENRE=$(grep DGENRE "${CDDBPARSEFILE}" | cut '-d=' -f2-)
913e4339 2090 if [ ! X"$PARSEDGENRE" = "X" ]; then
f75dae43 2091 echo "Genre: $PARSEDGENRE"
9ff5088c 2092 fi
2093 fi
99d009fa 2094 if [ ! "$ONETRACK" = "y" ]; then
c9c2ca27 2095 for TRACK in $(f_seq_row 1 $TRACKS)
2096 do
2097 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "${CDDBPARSEFILE}" | cut -f2- -d= | tr -d \\r\\n)"
2098 done
2099 fi
2100}
2101
2102# do_localcddb
2103# Check for a local CDDB file, and report success
2104do_localcddb ()
2105{
2106 if checkstatus cddb-readcomplete && checkstatus cddb-choice >/dev/null; then :; else
2107
f23735e2 2108 CDDBLOCALSTATUS="notfound"
c9c2ca27 2109 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
c9c2ca27 2110 USELOCALRESP="y"
6d81edb6 2111
2112 if [ "$CDDBLOCALRECURSIVE" = "y" ]; then
2113 CDDBLOCALRESULTS="$(find ${CDDBLOCALDIR} -name "${CDDBDISCID}" -type f 2> /dev/null)"
0059875e 2114 if [ ! "${CDDBLOCALRESULTS}" = "" ]; then
2115 if (( $(echo "${CDDBLOCALRESULTS}" | wc -l) == 1 )); then
2116 CDDBLOCALFILE="${CDDBLOCALRESULTS}"
2117 CDDBLOCALMATCH=single
0059875e 2118 elif (( $(echo "${CDDBLOCALRESULTS}" | wc -l) > 1 )); then
2119 CDDBLOCALMATCH=multiple
2120 fi
c9c2ca27 2121 else
6d81edb6 2122 CDDBLOCALMATCH=none
c9c2ca27 2123 fi
0059875e 2124 elif [ "$CDDBLOCALMATCH" = "none" ] && [ -r "${CDDBLOCALDIR}/${CDDBDISCID}" ]; then
6d81edb6 2125 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
2126 CDDBLOCALMATCH=single
04609998 2127 else
6d81edb6 2128 CDDBLOCALMATCH=none
c9c2ca27 2129 fi
6d81edb6 2130
2131 # If the user has selected to check a local CDDB repo, we proceed with it
2132 case $CDDBLOCALMATCH in
f23735e2 2133 multiple)
98a4933c 2134 echo "Processing multiple matching CDDB entries..." > "$ABCDETEMPDIR/cddblocalchoices"
f23735e2 2135 X=0
1362d8f6 2136 echo "$CDDBLOCALRESULTS" | while read RESULT ; do
f23735e2 2137 X=$(expr $X + 1)
2138 # List out disc title/author and contents
2139 CDDBLOCALREAD="$ABCDETEMPDIR/cddblocalread.$X"
98a4933c 2140 cat "$RESULT" > "${CDDBLOCALREAD}"
f23735e2 2141 {
1362d8f6 2142 echo -n "#$X: "
b546a9eb 2143 do_cddbparse "${CDDBLOCALREAD}"
0059875e 2144 echo ""
302591a1 2145 ##FIXME## QUICK HACK !!!!
b2dd627e 2146 if [ ! "$INTERACTIVE" = "y" ]; then break ; fi
f23735e2 2147 } >> "$ABCDETEMPDIR/cddblocalchoices"
2148 done
b6d0a5b8 2149 if [ $(cat "$ABCDETEMPDIR/cddblocalchoices" | wc -l) -ge 24 ] && [ "$INTERACTIVE" = "y" ]; then
d100b6be 2150 page "$ABCDETEMPDIR/cddblocalchoices"
2151 else
2152 # It's all going to fit in one page, cat it
2153 cat "$ABCDETEMPDIR/cddblocalchoices" >&2
2154 fi
1362d8f6 2155 CDDBLOCALCHOICES=$( echo "$CDDBLOCALRESULTS" | wc -l )
d100b6be 2156 # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
1362d8f6 2157 CDDBLOCALCHOICENUM=-1
f23735e2 2158 if [ "$INTERACTIVE" = "y" ]; then
2159 while [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; do
2160 echo -n "Locally cached CDDB entries found. Which one would you like to use (0 for none)? [0-$CDDBLOCALCHOICES]: " >&2
d100b6be 2161 read CDDBLOCALCHOICE
2162 [ x"$CDDBLOCALCHOICE" = "x" ] && CDDBLOCALCHOICE="1"
2163 # FIXME # Introduce diff's
991009a4 2164 if echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
d100b6be 2165 diffentries cddblocalread "$CDDBLOCALCHOICES" "$CDDBLOCALCHOICE"
991009a4 2166 elif echo $CDDBLOCALCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
d100b6be 2167 # Make sure we get a valid choice
2168 CDDBLOCALCHOICENUM=$(echo $CDDBLOCALCHOICE | xargs printf %d 2>/dev/null)
2169 if [ $CDDBLOCALCHOICENUM -lt 0 ] || [ $CDDBLOCALCHOICENUM -gt $CDDBLOCALCHOICES ]; then
2170 echo "Invalid selection. Please choose a number between 0 and $CDDBLOCALCHOICES." >&2
2171 fi
2172 fi
f23735e2 2173 done
2174 else
2175 ### FIXME ###
b2dd627e 2176 #echo "Selected ..."
f23735e2 2177 CDDBLOCALRESP=y
b2dd627e 2178 CDDBLOCALCHOICENUM=1
f23735e2 2179 fi
1362d8f6 2180 if [ ! "$CDDBLOCALCHOICENUM" = "0" ]; then
f23735e2 2181 #echo "Using local copy of CDDB data"
2182 echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
1362d8f6 2183 cat "$ABCDETEMPDIR/cddblocalread.$CDDBLOCALCHOICENUM" >> "$ABCDETEMPDIR/cddbread.1"
f23735e2 2184 echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
2185 echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
2186 do_cddbparse "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbchoices"
2187 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2188 CDDBLOCALSTATUS="found"
2189 else
2190 #echo "Not using local copy of CDDB data"
2191 CDDBLOCALSTATUS="notfound"
2192 fi
2193 ;;
6d81edb6 2194 single)
2195 # List out disc title/author and contents
2196 do_cddbparse "${CDDBLOCALFILE}"
ede6c120 2197 #if [ "$CDROMREADERSYNTAX" = "flac" ] ; then
8d685d48 2198 # echo -n "Embedded cuesheet entry found, use it [Y/n]? " >&2
ede6c120 2199 #else
8d685d48 2200 echo -n "Locally cached CDDB entry found, use it [Y/n]? " >&2
ede6c120 2201 #fi
6d81edb6 2202 if [ "$INTERACTIVE" = "y" ]; then
2203 read USELOCALRESP
2204 while [ "$USELOCALRESP" != "y" ] && [ "$USELOCALRESP" != "n" ] && [ "$USELOCALRESP" != "" ] ; do
f23735e2 2205 echo -n 'Invalid selection. Please answer "y" or "n": ' >&2
6d81edb6 2206 read USELOCALRESP
2207 done
2208 [ x"$USELOCALRESP" = "x" ] && USELOCALRESP="y"
2209 else
2210 echo "y" >&2
2211 fi
2212 if [ "$USELOCALRESP" = "y" ]; then
2213 #echo "Using local copy of CDDB data"
2214 echo "# DO NOT ERASE THIS LINE! Added by abcde to imitate cddb output" > "$ABCDETEMPDIR/cddbread.1"
2215 cat "${CDDBLOCALFILE}" >> "$ABCDETEMPDIR/cddbread.1"
2216 echo 999 > "$ABCDETEMPDIR/cddbquery" # Assuming 999 isn't used by CDDB
2217 echo cddb-readcomplete >> "$ABCDETEMPDIR/status"
2218 do_cddbparse "${CDDBLOCALFILE}" > "$ABCDETEMPDIR/cddbchoices"
2219 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
f23735e2 2220 CDDBLOCALSTATUS="single"
6d81edb6 2221 else
2222 #echo "Not using local copy of CDDB data"
f23735e2 2223 CDDBLOCALSTATUS="notfound"
6d81edb6 2224 fi
2225 ;;
2226 none)
f23735e2 2227 CDDBLOCALSTATUS="notfound"
6d81edb6 2228 ;;
2229 esac
c9c2ca27 2230 fi
2231}
2232
a778f99c
E
2233# do_cdtext
2234# Try to read CD-Text from the drive using icedax / cdda2wav
2235do_cdtext ()
2236{
2237 if new_checkexec icedax; then
2238 CDTEXT_READER=icedax
2239 elif new_checkexec cdda2wav; then
2240 CDTEXT_READER=cdda2wav
2241 else
2242 # Didn't find either, bail
2243 return 0
2244 fi
2245
b3f9fb58
E
2246 if [ "$OSFLAVOUR" = "OSX" ] ; then
2247 # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
2248 diskutil unmount ${CDROM#/dev/}
2249 # Also, in OSX the cdrom device for cdda2wav/icedax changes...
2250 CDDA2WAVCDROM="IODVDServices"
2251 elif [ "$OSFLAVOUR" = "FBSD" ] ; then
a778f99c 2252 CDDA2WAVCDROM="$CDROMID"
b3f9fb58
E
2253 else
2254 if [ "$CDROMID" = "" ]; then
2255 CDDA2WAVCDROM="$CDROM"
2256 else
2257 CDDA2WAVCDROM="$CDROMID"
2258 fi
a778f99c
E
2259 fi
2260
2261 # Do we have CD-Text on the disc (and can the drive read it?)
9d790401
E
2262 ${CDTEXT_READER} -J -N -D ${CDDA2WAVCDROM} > "$ABCDETEMPDIR/cd-text" 2>&1
2263 grep -q '^CD-Text: detected' "$ABCDETEMPDIR/cd-text"
a778f99c
E
2264 ERRORCODE=$?
2265 if [ $ERRORCODE -ne 0 ]; then
2266 # No CD-Text found, bail
2267 return 0
2268 fi
2269
2270 rm -f "$ABCDETEMPDIR/cddbchoices"
2271 CDDBCHOICES=1
2272 # Make an empty template
2273 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.1"
2274 echo -n "Retrieved 1 CD-Text match..." >> "$ABCDETEMPDIR/cddbchoices"
2275 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
2276 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2277 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
9d790401 2278 ATITLE=$(grep -e '^Album title:' "${ABCDETEMPDIR}/cd-text" | cut -c14- )
a778f99c
E
2279 echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
2280 # List out disc title/author and contents
2281 echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
2282 for TRACK in $(f_seq_row 1 $TRACKS)
2283 do
2284 TRACKM1=$(($TRACK - 1))
abd11d6a 2285 TITLE="$(grep -E ^Track\ +$TRACK: "$ABCDETEMPDIR/cd-text" | tr -d \\r\\n | sed 's~^Track ..: .~~g;'"s~'$~~g")"
a778f99c 2286 echo "$TRACK: $TITLE" >> "$ABCDETEMPDIR/cddbchoices"
0c00a676
E
2287 sed "s~^TTITLE${TRACKM1}=.*~TTITLE${TRACKM1}=${TITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
2288 mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
a778f99c 2289 done
0c00a676
E
2290 sed "s~^DTITLE=.*~DTITLE=${ATITLE}~" "$ABCDETEMPDIR/cddbread.1" > "$ABCDETEMPDIR/cddbread.new"
2291 mv -f "$ABCDETEMPDIR/cddbread.new" "$ABCDETEMPDIR/cddbread.1"
a778f99c
E
2292 echo >> "$ABCDETEMPDIR/cddbchoices"
2293 echo "cdtext-readcomplete" >> "$ABCDETEMPDIR/status"
2294}
2295
2c2dffe6
E
2296# do_musicbrainz
2297# Work with the musicbrainz WS API, then transform the results here so
2298# they look (very) like the results from CDDB. Maybe not the best way
2299# to go, but it Works For Me (TM)
dba06003 2300
3b9371f1 2301do_musicbrainz ()
dba06003 2302{
2c2dffe6
E
2303 if checkstatus musicbrainz-readcomplete; then :; else
2304 vecho "Obtaining Musicbrainz results..."
2305 # If MB is to be used, interpret the query results and read all
2306 # the available entries.
2307 rm -f "$ABCDETEMPDIR/cddbchoices"
2308 CDDBCHOICES=1 # Overridden by multiple matches
4942842f 2309 MBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
9d790401 2310 ${MUSICBRAINZ} --command data --discid "$MBDISCID" --workdir "$ABCDETEMPDIR"
2c2dffe6
E
2311
2312 # The helper script will write disc matches out to
2313 # cddbread.*. Count how many we have
9d790401 2314 if [ ! -f "${ABCDETEMPDIR}/cddbread.1" ] ; then
2c2dffe6
E
2315 # No matches. Use the normal cddb template for the user to
2316 # fill in
2317 echo "No Musicbrainz match." >> "$ABCDETEMPDIR/cddbchoices"
2318 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
2319 # List out disc title/author and contents of template
2320 echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
2321 UNKNOWNDISK=y
2322 for TRACK in $(f_seq_row 1 $TRACKS)
2323 do
2324 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2325 done
2326 echo >> "$ABCDETEMPDIR/cddbchoices"
2327 echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
2328 echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
2329 echo 503 > "$ABCDETEMPDIR/cddbquery"
2330 else
43bd68de 2331 # We have some matches
9d790401 2332 NUM_RESPONSES=$(echo "${ABCDETEMPDIR}"/cddbread.* | wc -w)
43bd68de
E
2333 if [ "$NUM_RESPONSES" -eq 1 ] ; then
2334 # One exact match
2335 echo -n "Retrieved 1 Musicbrainz match..." >> "$ABCDETEMPDIR/cddbchoices"
2336 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
2337 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2338 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
9d790401 2339 ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}/cddbread.1" | cut -c8- )
43bd68de 2340 echo "200 none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
2c2dffe6 2341 # List out disc title/author and contents
43bd68de 2342 echo ---- ${ATITLE} ---- >> "$ABCDETEMPDIR/cddbchoices"
2c2dffe6
E
2343 for TRACK in $(f_seq_row 1 $TRACKS)
2344 do
43bd68de 2345 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2c2dffe6
E
2346 done
2347 echo >> "$ABCDETEMPDIR/cddbchoices"
43bd68de
E
2348 else
2349 echo "210 Found exact matches, list follows (until terminating .)" > "$ABCDETEMPDIR/cddbquery"
2350 echo "Multiple Musicbrainz matches:" >> "$ABCDETEMPDIR/cddbchoices"
9d790401 2351 for file in "$ABCDETEMPDIR"/cddbread.*
43bd68de
E
2352 do
2353 X=$(echo $file | sed 's/^.*cddbread\.//g')
2354 echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
9d790401 2355 ATITLE=$(grep -e '^DTITLE=' "${ABCDETEMPDIR}"/cddbread.$X | cut -c8- )
43bd68de
E
2356 echo "none ${ATITLE}" >> "$ABCDETEMPDIR/cddbquery"
2357 # List out disc title/author and contents
2358 echo "#$X: ---- ${ATITLE} ----" >> "$ABCDETEMPDIR/cddbchoices"
2359 for TRACK in $(f_seq_row 1 $TRACKS)
2360 do
2361 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2362 done
2363 echo >> "$ABCDETEMPDIR/cddbchoices"
2364 done
2365 echo "." >> "$ABCDETEMPDIR/cddbquery"
2366 fi
2c2dffe6
E
2367 fi
2368 echo "musicbrainz-readcomplete" >> "$ABCDETEMPDIR/status"
2369 fi
dba06003 2370}
2371
c9c2ca27 2372# do_cddbstat
2373do_cddbstat ()
2374{
2375 # Perform CDDB protocol version check if it hasn't already been done
2376 if checkstatus cddb-statcomplete; then :; else
2377 if [ "$CDDBAVAIL" = "n" ]; then
2378 ERRORCODE=no_query
2379 echo 503 > "$ABCDETEMPDIR/cddbstat"
2380 else
2381 rc=1
2382 CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
2383 CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
2384 while test $rc -eq 1 -a $CDDBPROTO -ge 3; do
2385 vecho "Checking CDDB server status..."
2386 $CDDBTOOL stat $CDDBURL $CDDBUSER $CDDBHOST $CDDBPROTO > "$ABCDETEMPDIR/cddbstat"
2387 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
2388 case "$RESPONSECODE" in
2389 210) # 210 OK, status information follows (until terminating `.')
d712ef98 2390 rc=0
c9c2ca27 2391 ;;
b546a9eb 2392 501) # 501 Illegal CDDB protocol level: <n>.
c9c2ca27 2393 CDDBPROTO=`expr $CDDBPROTO - 1`
2394 ;;
d712ef98 2395 *) # Try a cddb query, since freedb2.org doesn't support the stat or ver commands
2396 # FreeDB TESTCD disc-id is used for query
2397 $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST 03015501 1 296 344 > "$ABCDETEMPDIR/cddbstat"
2398 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbstat" | cut -f1 -d' ')
2399 case "$RESPONSECODE" in
2400 2??) # Server responded, everything seems OK
2401 rc=0
2402 ;;
2403 esac
2404 ;;
b546a9eb 2405 esac
c9c2ca27 2406 done
2407 if test $rc -eq 1; then
b546a9eb 2408 CDDBAVAIL="n"
c9c2ca27 2409 fi
2410 fi
2411 echo cddb-statcomplete >> "$ABCDETEMPDIR/status"
2412 fi
2413}
2414
2415
2416# do_cddbquery
2417do_cddbquery ()
2418{
2419 CDDBDISCID=$(echo $TRACKINFO | cut -d' ' -f1)
2420 CDDBLOCALFILE="${CDDBLOCALDIR}/${CDDBDISCID}"
2421
2422 # Perform CDDB query if it hasn't already been done
2423 if checkstatus cddb-querycomplete; then :; else
2424 if [ "$CDDBAVAIL" = "n" ]; then
2425 ERRORCODE=no_query
2426 echo 503 > "$ABCDETEMPDIR/cddbquery"
f23735e2 2427 # The default CDDBLOCALSTATUS is "notfound"
b546a9eb 2428 # This part will be triggered if the user CDDB repo does not
c9c2ca27 2429 # contain the entry, or if we are not trying to use the repo.
2430 else
2431 vecho "Querying the CDDB server..."
2432 CDDBUSER=$(echo $HELLOINFO | cut -f1 -d'@')
2433 CDDBHOST=$(echo $HELLOINFO | cut -f2- -d'@')
2434 $CDDBTOOL query $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $TRACKINFO > "$ABCDETEMPDIR/cddbquery"
2435 ERRORCODE=$?
2436 case $ERRORCODE in
2437 0) # success
2438 ;;
2439 12|13|14)
2440 # no match found in database,
2441 # wget/fetch error, or user requested not to use CDDB
2442 # Make up an error code (503) that abcde
2443 # will recognize in do_cddbread
2444 # and compensate by making a template
2445 echo 503 > "$ABCDETEMPDIR/cddbquery"
2446 ;;
2447 *) # strange and unknown error
2448 echo ERRORCODE=$ERRORCODE
2449 echo "abcde: $CDDBTOOL returned unknown error code"
2450 ;;
2451 esac
2452 fi
2453 echo cddb-querycomplete >> "$ABCDETEMPDIR/status"
2454 fi
2455}
2456
2457# do_cddbread
2458do_cddbread ()
2459{
2460 # If it's not to be used, generate a template.
2461 # Then, display it (or them) and let the user choose/edit it
2462 if checkstatus cddb-readcomplete; then :; else
2463 vecho "Obtaining CDDB results..."
2464 # If CDDB is to be used, interpret the query results and read all
2465 # the available entries.
2466 rm -f "$ABCDETEMPDIR/cddbchoices"
2467 CDDBCHOICES=1 # Overridden by multiple matches
2468 RESPONSECODE=$(head -n 1 "$ABCDETEMPDIR/cddbquery" | cut -f1 -d' ')
2469 case "$RESPONSECODE" in
2470 200)
2471 # One exact match, retrieve it
2472 # 200 [section] [discid] [artist] / [title]
2473 if checkstatus cddb-read-1-complete; then :; else
2474 echo -n "Retrieving 1 CDDB match..." >> "$ABCDETEMPDIR/cddbchoices"
2475 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(cut -f2,3 -d' ' "$ABCDETEMPDIR/cddbquery") > "$ABCDETEMPDIR/cddbread.1"
2476 echo "done." >> "$ABCDETEMPDIR/cddbchoices"
2477 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2478 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2479 fi
2480 # List out disc title/author and contents
2481 echo ---- "$(cut '-d ' -f4- "$ABCDETEMPDIR/cddbquery")" ---- >> "$ABCDETEMPDIR/cddbchoices"
2482 for TRACK in $(f_seq_row 1 $TRACKS)
2483 do
2484 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2485 done
2486 echo >> "$ABCDETEMPDIR/cddbchoices"
2487 ;;
2488 202|403|409|503)
2489 # No match
2490 case "$RESPONSECODE" in
2491 202) echo "No CDDB match." >> "$ABCDETEMPDIR/cddbchoices" ;;
2492 403|409) echo "CDDB entry is corrupt, or the handshake failed." >> "$ABCDETEMPDIR/cddbchoices" ;;
2493 503) echo "CDDB unavailable." >> "$ABCDETEMPDIR/cddbchoices" ;;
2494 esac
99d009fa 2495 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
c9c2ca27 2496 # List out disc title/author and contents of template
2497 echo ---- Unknown Artist / Unknown Album ---- >> "$ABCDETEMPDIR/cddbchoices"
2498 UNKNOWNDISK=y
2499 for TRACK in $(f_seq_row 1 $TRACKS)
2500 do
99d009fa 2501 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.0" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
c9c2ca27 2502 done
2503 echo >> "$ABCDETEMPDIR/cddbchoices"
99d009fa 2504 echo cddb-read-0-complete >> "$ABCDETEMPDIR/status"
2505 echo cddb-choice=0 >> "$ABCDETEMPDIR/status"
c9c2ca27 2506 ;;
2507 210|211)
2508 # Multiple exact, (possibly multiple) inexact matches
2509 IN=
2510 if [ "$RESPONSECODE" = "211" ]; then IN=in; fi
b82e2042 2511 if [ "$(wc -l < "$ABCDETEMPDIR/cddbquery" | tr -d ' ')" -eq 3 ]; then
c9c2ca27 2512 echo "One ${IN}exact match:" >> "$ABCDETEMPDIR/cddbchoices"
2513 tail -n +2 "$ABCDETEMPDIR/cddbquery" | head -n 1 >> "$ABCDETEMPDIR/cddbchoices"
b546a9eb 2514 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
c9c2ca27 2515 else
2516 echo "Multiple ${IN}exact matches:" >> "$ABCDETEMPDIR/cddbchoices"
2517 fi
99d009fa 2518 vecho -n "Retrieving multiple matches... "
c9c2ca27 2519 grep -v ^[.]$ "$ABCDETEMPDIR/cddbquery" | ( X=0
2520 read DISCINFO # eat top line
2521 while read DISCINFO
2522 do
2523 X=$(expr $X + 1)
2524 if checkstatus cddb-read-$X-complete; then :; else
2525 $CDDBTOOL read $CDDBURL $CDDBPROTO $CDDBUSER $CDDBHOST $(echo $DISCINFO | cut -f1,2 -d' ') > "$ABCDETEMPDIR/cddbread.$X"
2526 echo cddb-read-$X-complete >> "$ABCDETEMPDIR/status"
2527 fi
2528 # List out disc title/author and contents
2529 echo \#$X: ---- "$DISCINFO" ---- >> "$ABCDETEMPDIR/cddbchoices"
2530 for TRACK in $(f_seq_row 1 $TRACKS)
2531 do
2532 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.$X" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2533 done
2534 echo >> "$ABCDETEMPDIR/cddbchoices"
2535 done )
99d009fa 2536 vecho "done."
c9c2ca27 2537 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
2538 ;;
2539 999)
2540 # Using local copy.
2541 for TRACK in $(f_seq_row 1 $TRACKS)
2542 do
2543 echo $TRACK: "$(grep ^TTITLE$(expr $TRACK - 1)= "$ABCDETEMPDIR/cddbread.1" | cut -f2- -d= | tr -d \\r\\n)" >> "$ABCDETEMPDIR/cddbchoices"
2544 done
2545 echo >> "$ABCDETEMPDIR/cddbchoices"
2546 echo cddb-read-1-complete >> "$ABCDETEMPDIR/status"
2547 echo cddb-choice=1 >> "$ABCDETEMPDIR/status"
2548 ;;
2549 esac
2550 echo "cddb-readcomplete" >> "$ABCDETEMPDIR/status"
2551 fi
2552}
2553
2554# do_cddbedit
2555do_cddbedit ()
2556{
2557 if checkstatus cddb-edit >/dev/null; then
2558 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
2559 VARIOUSARTISTS="$(checkstatus variousartists)"
2560 VARIOUSARTISTSTYLE="$(checkstatus variousartiststyle)"
2561 return 0
2562 fi
2563 if [ "$INTERACTIVE" = "y" ]; then
2564 # We should show the CDDB results both when we are not using the local CDDB repo
2565 # or when we are using it but we could not find a proper match
f23735e2 2566 if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
c9c2ca27 2567 # Display the $ABCDETEMPDIR/cddbchoices file created above
2568 # Pick a pager so that if the tracks overflow the screen the user can still view everything
2569 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
2570 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
2571 CHOICE=$(checkstatus cddb-choice)
2572 if [ -n "$CHOICE" ] ; then
2573 case $CDDBCHOICES in
43559afb 2574 -1) if head -1 "$ABCDETEMPDIR/cddbquery" | grep "^$" > /dev/null 2>&1 ; then
b546a9eb 2575 log error "CDDB query failed!"
6d81edb6 2576 exit 1
2577 else
2578 cat "$ABCDETEMPDIR/cddbchoices"
2579 fi
dba06003 2580 ;;
c9c2ca27 2581 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
2582 *)
2583 echo "Selected: #$CHOICE"
2584 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
2585 ;;
2586 esac
2587 else
2588 # The user has a choice to make, display the info in a pager if necessary
2589 if [ $(cat "$ABCDETEMPDIR/cddbchoices" | wc -l) -ge 24 ]; then
913e4339 2590 page "$ABCDETEMPDIR/cddbchoices"
c9c2ca27 2591 else
2592 # It's all going to fit in one page, cat it
2593 cat "$ABCDETEMPDIR/cddbchoices" >&2
2594 fi
2595
9ff5088c 2596 CDDBCHOICENUM=""
913e4339 2597 # Setting the choice to an impossible integer to avoid errors in the numeric comparisons
f75dae43 2598 CDCHOICENUM=-1
c9c2ca27 2599 # I'll take CDDB read #3 for $400, Alex
9ff5088c 2600 while [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; do
2601 echo -n "Which entry would you like abcde to use (0 for none)? [0-$CDDBCHOICES]: " >&2
c9c2ca27 2602 read CDDBCHOICE
9ff5088c 2603 [ X"$CDDBCHOICE" = "X" ] && CDDBCHOICE=1
991009a4 2604 if echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+,[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
9ff5088c 2605 if [ ! X"$DIFF" = "X" ]; then
f0582268 2606 PARSECHOICE1=$(echo $CDDBCHOICE | cut -d"," -f1 | xargs printf %d 2>/dev/null)
2607 PARSECHOICE2=$(echo $CDDBCHOICE | cut -d"," -f2 | xargs printf %d 2>/dev/null)
913e4339 2608 if [ $PARSECHOICE1 -lt 1 ] || [ $PARSECHOICE1 -gt $CDDBCHOICES ] || \
2609 [ $PARSECHOICE2 -lt 1 ] || [ $PARSECHOICE2 -gt $CDDBCHOICES ] || \
b546a9eb 2610 [ $PARSECHOICE1 -eq $PARSECHOICE2 ]; then
8f4189b3 2611 echo "Invalid diff range. Please select two comma-separated numbers between 1 and $CDDBCHOICES" >&2
913e4339 2612 else
913e4339 2613 # We parse the 2 choices to diff, store them in temporary files and diff them.
2614 for PARSECHOICE in $(echo $CDDBCHOICE | tr , \ ); do
2615 do_cddbparse "$ABCDETEMPDIR/cddbread.$PARSECHOICE" > "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE"
2616 done
2617 echo "Showing diff between choices $PARSECHOICE1 and $PARSECHOICE2..." > "$ABCDETEMPDIR/cddbread.diff"
2618 $DIFF $DIFFOPTS "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE1" "$ABCDETEMPDIR/cddbread.parsechoice.$PARSECHOICE2" >> "$ABCDETEMPDIR/cddbread.diff"
2619 if [ $(cat "$ABCDETEMPDIR/cddbread.diff" | wc -l) -ge 24 ]; then
2620 page "$ABCDETEMPDIR/cddbread.diff"
2621 else
2622 cat "$ABCDETEMPDIR/cddbread.diff" >&2
2623 fi
2624 fi
9ff5088c 2625 else
2626 echo "The diff program was not found in your path. Please choose a number between 0 and $CDDBCHOICES." >&2
2627 fi
991009a4 2628 elif echo $CDDBCHOICE | grep -E "[[:space:]]*[[:digit:]]+[[:space:]]*" > /dev/null 2>&1 ; then
f0582268 2629 # Make sure we get a valid choice
2630 CDCHOICENUM=$(echo $CDDBCHOICE | xargs printf %d 2>/dev/null)
9ff5088c 2631 if [ $CDCHOICENUM -lt 0 ] || [ $CDCHOICENUM -gt $CDDBCHOICES ]; then
9ff5088c 2632 echo "Invalid selection. Please choose a number between 0 and $CDDBCHOICES." >&2
2633 fi
2634 fi
c9c2ca27 2635 done
99d009fa 2636 if [ "$CDCHOICENUM" = "0" ]; then
2637 vecho "Creating empty CDDB template..."
2638 UNKNOWNDISK=y
b82e2042 2639 $CDDBTOOL template $(cat "$ABCDETEMPDIR/discid") > "$ABCDETEMPDIR/cddbread.0"
99d009fa 2640 else
03308bc7 2641 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= "$ABCDETEMPDIR/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
99d009fa 2642 do_cddbparse "$ABCDETEMPDIR/cddbread.$CDCHOICENUM"
2643 fi
c9c2ca27 2644 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
2645 fi
2646 fi
2647 else
2648 # We need some code to show the selected option when local repository is selected and we have found a match
9f659ada 2649 vecho "Using cached CDDB match..." >&2
c9c2ca27 2650 # Display the $ABCDETEMPDIR/cddbchoices file created above
2651 # Pick a pager so that if the tracks overflow the screen the user can still view everything
2652 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
2653 CDDBCHOICES=$(expr $(cat "$ABCDETEMPDIR/cddbquery" | wc -l) - 2)
2654 CHOICE=$(checkstatus cddb-choice)
2655 if [ "$USELOCALRESP" = "y" ]; then :; else
2656 if [ -n "$CHOICE" ] ; then
2657 case $CDDBCHOICES in
b546a9eb 2658 0)
04609998 2659 UNKNOWNDISK=y
2660 echo "Selected template."
2661 ;;
c9c2ca27 2662 1) cat "$ABCDETEMPDIR/cddbchoices" ;;
2663 *)
2664 echo "Selected: #$CHOICE"
2665 do_cddbparse "$ABCDETEMPDIR/cddbread.$CHOICE"
2666 ;;
2667 esac
2668 fi
2669 fi
2670 fi
2671 fi
2672 else
2673 # We're noninteractive - pick the first choice.
2674 # But in case we run a previous instance and selected a choice, use it.
2675 if [ -r "$ABCDETEMPDIR/cddbchoices" ]; then
2676 # Show the choice if we are not using the locally stored one
2677 # or when the local search failed to find a match.
2678 PREVIOUSCHOICE=$(checkstatus cddb-choice)
f23735e2 2679 if [ "$CDDBUSELOCAL" = "y" ] && [ "$CDDBLOCALSTATUS" = "notfound" ] || [ ! "$CDDBUSELOCAL" = "y" ]; then
c9c2ca27 2680 #if [ "$PREVIOUSCHOICE" ]; then
2681 cat "$ABCDETEMPDIR/cddbchoices"
2682 #fi
2683 fi
2684 if [ ! -z "$PREVIOUSCHOICE" ] ; then
2685 CDCHOICENUM=$PREVIOUSCHOICE
2686 else
2687 CDCHOICENUM=1
2688 echo "cddb-choice=$CDCHOICENUM" >> "$ABCDETEMPDIR/status"
2689 fi
03308bc7 2690 echo "Selected: #$CDCHOICENUM ($(grep ^DTITLE= "$ABCDETEMPDIR/cddbread.$CDCHOICENUM" | cut -f2- -d= | tr -d \\r\\n))" >&2
c9c2ca27 2691 fi
2692 fi
2693
2694 # sanity check
2695 if checkstatus cddb-choice >/dev/null; then :; else
2696 echo "abcde: internal error: cddb-choice not recorded." >&2
2697 exit 1
2698 fi
2699 CDDBDATA="$ABCDETEMPDIR/cddbread.$(checkstatus cddb-choice)"
9f659ada 2700 echo -n "Edit selected CDDB data? [y/n] (" >&2
c9c2ca27 2701 if [ "$INTERACTIVE" = "y" ]; then
2702 if [ "$UNKNOWNDISK" = "y" ]; then
2703 echo -n "y): " >&2
2704 read EDITCDDB
2705 [ "$EDITCDDB" != "n" ] && EDITCDDB=y
2706 else
2707 echo -n "n): " >&2
2708 read EDITCDDB
2709 fi
2710 else
2711 echo "n): n" >&2
2712 EDITCDDB=n
2713 fi
2714 if [ "$EDITCDDB" = "y" ]; then
2715 CDDBDATAMD5SUM=$($MD5SUM "$CDDBDATA" | cut -d " " -f 1);
2716
2717 # Use the debian sensible-editor wrapper to pick the editor that the
2718 # user has requested via their $EDITOR environment variable
2719 if [ -x "/usr/bin/sensible-editor" ]; then
2720 /usr/bin/sensible-editor "$CDDBDATA"
2721 elif [ -n "$EDITOR" ]; then
2722 if [ -x $(which "${EDITOR%%\ *}") ]; then
2723 # That failed, try to load the preferred editor, starting
2724 # with their EDITOR variable
03308bc7 2725 eval $(echo "$EDITOR") \"$CDDBDATA\"
c9c2ca27 2726 fi
2727 # If that fails, check for a vi
2994a485 2728 elif which vi >/dev/null 2>&1; then
2729 vi "$CDDBDATA"
fd34b28b 2730 elif [ -x /usr/bin/vim ]; then
2731 /usr/bin/vim "$CDDBDATA"
c9c2ca27 2732 elif [ -x /usr/bin/vi ]; then
2733 /usr/bin/vi "$CDDBDATA"
fd34b28b 2734 elif [ -x /bin/vi ]; then
2735 /bin/vi "$CDDBDATA"
c9c2ca27 2736 # nano should be on all (modern, i.e., sarge) debian systems
2994a485 2737 elif which nano >/dev/null 2>&1 ; then
2738 nano "$CDDBDATA"
c9c2ca27 2739 elif [ -x /usr/bin/nano ]; then
2740 /usr/bin/nano "$CDDBDATA"
2741 # mg should be on all OpenBSD systems
2994a485 2742 elif which mg >/dev/null 2>&1 ; then
2743 mg "$CDDBDATA"
c9c2ca27 2744 elif [ -x /usr/bin/mg ]; then
2745 /usr/bin/mg "$CDDBDATA"
2746 # bomb out
2747 else
535944df 2748 log warning "no editor available. Check your EDITOR environment variable."
c9c2ca27 2749 fi
2750 # delete editor backup file if it exists
2751 if [ -w "$CDDBDATA~" ]; then
2752 rm -f "$CDDBDATA~"
2753 fi
2754 fi
2755
2756 # Some heuristics first. Look at Disc Title, and if it starts with
2757 # "Various", then we'll assume Various Artists
991009a4 2758 if [ "$(grep ^DTITLE= "$CDDBDATA" | cut -f2- -d= | grep -Eci '^(various|soundtrack|varios|sonora|ost)')" != "0" ]; then
c9c2ca27 2759 echo "Looks like a Multi-Artist CD" >&2
2760 VARIOUSARTISTS=y
2761 else
8d685d48 2762 echo -n "Is the CD multi-artist [y/N]? " >&2
c9c2ca27 2763 if [ "$INTERACTIVE" = "y" ]; then
2764 read VARIOUSARTISTS
2765 else
2766 echo n >&2
2767 VARIOUSARTISTS=n
2768 fi
2769 fi
2770 if [ "$VARIOUSARTISTS" = "y" ] && [ ! "$ONETRACK" = "y" ]; then
2771 # Set a default
2772 DEFAULTSTYLE=1
2773 # Need NUMTRACKS before cddb-tool will return it:
991009a4 2774 NUMTRACKS=$(grep -E '^TTITLE[0-9]+=' "$CDDBDATA" | wc -l)
c9c2ca27 2775 if [ "$(grep -c "^TTITLE.*\/" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
2776 # More than 1/2 tracks contain a "/", so guess forward
2777 DEFAULTSTYLE=1
2778 elif [ "$(grep -c "^TTITLE.*\-" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
2779 # More than 1/2 contain a "-", so guess forward-dash
2780 DEFAULTSTYLE=2
2781 elif [ "$(grep -c "^TTITLE.*(.*)" "$CDDBDATA")" -gt "$(expr $NUMTRACKS / 2 )" ]; then
2782 # More than 1/2 contain something in parens, so guess trailing-paren
2783 DEFAULTSTYLE=6
2784 fi
2785
2786 echo "1) Artist / Title" >&2
2787 echo "2) Artist - Title" >&2
2788 echo "3) Title / Artist" >&2
2789 echo "4) Title - Artist" >&2
2790 echo "5) Artist: Title" >&2
2791 echo "6) Title (Artist)" >&2
2792 echo "7) This is a single-artist CD" >&2
2793 echo -n "Which style of multiple artist entries is it? [1-7] ($DEFAULTSTYLE): " >&2
2794 if [ "$INTERACTIVE" = "y" ]; then
2795 read VARIOUSARTISTSTYLE
2796 else
2797 echo $DEFAULTSTYLE >&2
2798 VARIOUSARTISTSTYLE=$DEFAULTSTYLE
2799 fi
2800 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
2801 # If they press Enter, then the default style (0) was chosen
2802 while [ $VARIOUSARTISTSTYLE -lt 0 ] || [ $VARIOUSARTISTSTYLE -gt 7 ]; do
2803 echo "Invalid selection. Please choose a number between 1 and 7."
2804 echo -n "Selection [1-7]: "
2805 read VARIOUSARTISTSTYLE
2806 VARIOUSARTISTSTYLE=$(echo 0$VARIOUSARTISTSTYLE | xargs printf %d)
2807 done
2808 if [ "$VARIOUSARTISTSTYLE" = "0" ]; then
2809 VARIOUSARTISTSTYLE=$DEFAULTSTYLE
2810 fi
99d009fa 2811 vecho "Selected: $VARIOUSARTISTSTYLE"
c9c2ca27 2812 case "$VARIOUSARTISTSTYLE" in
2813 1) # Artist / Title
2814 VARIOUSARTISTSTYLE=forward
2815 ;;
2816 2) # Artist - Title
2817 VARIOUSARTISTSTYLE=forward-dash
2818 ;;
2819 3) # Title / Artist
2820 VARIOUSARTISTSTYLE=reverse
2821 ;;
2822 4) # Title - Artist
2823 VARIOUSARTISTSTYLE=reverse-dash
2824 ;;
2825 5) # Artist: Title
2826 VARIOUSARTISTSTYLE=colon
2827 ;;
2828 6) # Title (Artist)
2829 VARIOUSARTISTSTYLE=trailing-paren
2830 ;;
2831 7) # Single Artist
2832 VARIOUSARTISTS=n
2833 ;;
2834 esac
2835 fi
2836
2837 echo "variousartists=$VARIOUSARTISTS" >> "$ABCDETEMPDIR/status"
2838 echo "variousartiststyle=$VARIOUSARTISTSTYLE" >> "$ABCDETEMPDIR/status"
2839
a1c07a84 2840 if [ "$EDITCDDB" = "y" ] && [ "$CDDBMETHOD" = "cddb" ] && [ "$UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE" = "y" ]; then
a9a23379 2841 if [ "$CDDBDATAMD5SUM" != "" ] && [ "$CDDBDATAMD5SUM" != "$($MD5SUM "$CDDBDATA" | cut -d " " -f 1)" ]; then
c9c2ca27 2842 # This works but does not have the necessary error checking
2843 # yet. If you are familiar with the CDDB spec
b546a9eb 2844 # (see http://www.freedb.org/src/latest/DBFORMAT)
c9c2ca27 2845 # and can create an error-free entry on your own, then put
2846 # UNINTENTIONALLY_ANGER_THE_FREEDB_PEOPLE=y in your
2847 # abcde.conf to enable it. Put CDDBSUBMIT=email@address in
2848 # your abcde.conf to change the email address submissions are
2849 # sent to.
2850
2851 # submit the modified file, if they want
2852 if [ "$NOSUBMIT" != "y" ]; then
8d685d48 2853 echo -n "Do you want to submit this entry to $CDDBSUBMIT [y/N]? "
c9c2ca27 2854 read YESNO
2855 while [ "$YESNO" != "y" ] && [ "$YESNO" != "n" ] && [ "$YESNO" != "Y" ] && \
2856 [ "$YESNO" != "N" ] && [ "$YESNO" != "" ]
2857 do
2858 echo -n 'Invalid selection. Please answer "y" or "n": '
2859 read YESNO
2860 done
2861 if [ "$YESNO" = "y" ] || [ "$YESNO" = "Y" ]; then
2862 echo -n "Sending..."
2863 $CDDBTOOL send "$CDDBDATA" $CDDBSUBMIT
2864 echo "done."
2865 fi
2866 fi
2867 fi
2868 fi
f23735e2 2869 ### FIXME ###
2870 # User CDDBLOCALPOLICY to find out if we store the file or not...
c9c2ca27 2871 # Cache edited CDDB entry in the user's cddb dir
f23735e2 2872 if [ "$CDDBCOPYLOCAL" = "y" ]; then
2873 # Make sure the cache directory exists
2874 mkdir -p $CDDBLOCALDIR
c9c2ca27 2875 cat "$CDDBDATA" | tail -n $(expr $(cat "$CDDBDATA" | wc -l ) - 1 ) > ${CDDBLOCALDIR}/$(echo "$TRACKINFO" | cut -d' ' -f1)
2876 fi
2877
2878 echo "cddb-edit" >> "$ABCDETEMPDIR/status"
2879}
2880
c9c2ca27 2881# do_cdread [tracknumber]
d5200e72 2882# do_cdread onetrack [firsttrack] [lasttrack]
b546a9eb 2883#
c9c2ca27 2884do_cdread ()
2885{
2886 # The commands here don't go through run_command because they're never supposed to be silenced
2887 # return codes need to be doublechecked anyway, however
d5200e72 2888 if [ "$1" = "onetrack" ]; then
2889 # FIXME # Add the possibility of grabbing ranges of tracks in onetrack
c7ea851d 2890 # FIXME # Until then, we grab the whole CD in one track, no matter what
2891 # the user said
6b005b85 2892 # We need the first and last track for cdda2wav/icedax
d5200e72 2893 FIRSTTRACK=$2
9ec0085f 2894 LASTTRACK=$(expr $3 + 0)
1366e842 2895 UTRACKNUM=$FIRSTTRACK
dba06003 2896 case "$CDROMREADERSYNTAX" in
2897 flac) READTRACKNUMS="$FIRSTTRACK.1-$(($LASTTRACK + 1)).0" ;;
b546a9eb 2898 cdparanoia)
4562fac6 2899 #XX FIXME XX
2900 # Add a variable to check if tracks are provided in command line and if not, use "0-" to rip the tracks
2901 READTRACKNUMS="$FIRSTTRACK-$LASTTRACK" ;;
6b005b85 2902 cdda2wav | icedax) READTRACKNUMS="$FIRSTTRACK+$LASTTRACK" ;;
dba06003 2903 *) echo "abcde error: $CDROMREADERSYNTAX does not support ONETRACK mode"
2904 exit 1 ;;
2905 esac
d5200e72 2906 else
2907 UTRACKNUM=$1
2908 fi
c9c2ca27 2909 CDDBTRACKNUM=$(expr $UTRACKNUM - 1)
49c04b11 2910 if [ "$USEPIPES" = "y" ]; then
a2881eba 2911 TEMPARG="PIPERIPPER_$CDROMREADERSYNTAX"
a9a23379 2912 FILEARG="$( eval echo "\$$TEMPARG" )"
37bd22e5 2913 REDIR="y"
0a3a2dcc 2914 PIPE_MESSAGE="and encoding "
49c04b11 2915 else
0a3a2dcc 2916 WAVDATA="$ABCDETEMPDIR/track$UTRACKNUM.wav"
49c04b11 2917 case "$CDROMREADERSYNTAX" in
1b94d6b7 2918 ## FIXME ## Find the cases for dagrab and flac, to avoid exceptions
2919 flac)
2920 FILEARG="--output-name=$WAVDATA"
2921 ;;
49c04b11 2922 dagrab)
2923 FILEARG="-f $WAVDATA"
2924 ;;
2925 *)
03308bc7 2926 FILEARG="$WAVDATA"
49c04b11 2927 ;;
2928 esac
37bd22e5 2929 REDIR="n"
49c04b11 2930 fi
efc6de2f 2931 if [ "$1" = "onetrack" ]; then
b82e2042 2932 echo "Grabbing ${PIPE_MESSAGE}tracks $UTRACKNUM - $LASTTRACK as one track ..." >&2
0a3a2dcc 2933 else
efc6de2f 2934 if [ -r "$CDDBDATA" ]; then
75285ac8 2935 getcddbinfo TRACKNAME
efc6de2f 2936 echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM: $TRACKNAME..." >&2
2937 else
2938 echo "Grabbing ${PIPE_MESSAGE}track $UTRACKNUM..." >&2
2939 fi
0a3a2dcc 2940 fi
c9c2ca27 2941 case "$CDROMREADERSYNTAX" in
1b94d6b7 2942 ### FIXME ### use an exception for flac, since it uses -o
2943 ### FIXME ### Shall we just use -o $FILEARG ??
2944 flac)
bed60a72 2945 # Avoid problems wit math expressions by unpadding the given UTRACKNUM
69d2aa45 2946 STRIPTRACKNUM=$(expr $UTRACKNUM + 0)
2947 nice $READNICE $FLAC -d -f --cue=${READTRACKNUMS:-$STRIPTRACKNUM.1-$(($STRIPTRACKNUM + 1)).0} "$FILEARG" "$CDROM" ;;
b546a9eb 2948 cdparanoia)
2949 if [ "$REDIR" = "y" ]; then
2950 nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
37bd22e5 2951 else
b546a9eb 2952 nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
37bd22e5 2953 fi;;
2954
6b005b85 2955 cdda2wav | icedax)
c9c2ca27 2956 if [ "$OSFLAVOUR" = "OSX" ] ; then
6b005b85 2957 # Hei, we have to unmount the device before running anything like cdda2wav/icedax in OSX
b3f9fb58 2958 diskutil unmount ${CDROM#/dev/}
6b005b85 2959 # Also, in OSX the cdrom device for cdda2wav/icedax changes...
c9c2ca27 2960 CDDA2WAVCDROM="IODVDServices"
2961 elif [ "$OSFLAVOUR" = "FBSD" ] ; then
2962 CDDA2WAVCDROM="$CDROMID"
2963 else
2964 if [ "$CDROMID" = "" ]; then
2965 CDDA2WAVCDROM="$CDROM"
2966 else
2967 CDDA2WAVCDROM="$CDROMID"
2968 fi
2969 fi
b546a9eb 2970 if [ "$REDIR" = "y" ]; then
37bd22e5 2971 nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG" >&2
b546a9eb 2972 else
37bd22e5 2973 nice $READNICE $CDROMREADER -D $CDDA2WAVCDROM -t ${READTRACKNUMS:-$UTRACKNUM} "$FILEARG"
2974 fi ;;
c7ea851d 2975 ## FIXME ## We have an exception for dagrab, since it uses -f
2976 ## FIXME ## Shall we just use -f $FILEARG ??
b546a9eb 2977 dagrab)
2978 if [ "$REDIR" = "y" ]; then
37bd22e5 2979 nice $READNICE $CDROMREADER -d "$CDROM" -v $UTRACKNUM "$FILEARG" >&2
2980 else
2981 nice $READNICE $CDROMREADER -d "$CDROM" -v $UTRACKNUM "$FILEARG"
2982 fi ;;
c9c2ca27 2983 cddafs)
2984 # Find the track's mounted path
2985 REALTRACKNUM=$(expr $UTRACKNUM + 0)
47589f71 2986 FILEPATH=$(mount | grep "$CDROM on" | sed 's/^[^ ]* on \(.*\) (.*/\1/')
c9c2ca27 2987 FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
2988 # If the file exists, copy it
2989 if [ -e "$FILEPATH" ] ; then
b546a9eb 2990 if [ "$REDIR" = "y" ]; then
37bd22e5 2991 nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" >&2
2992 else
2993 nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG"
2994 fi
c9c2ca27 2995 else
2996 false
2997 fi ;;
37bd22e5 2998 debug)
b546a9eb 2999 if [ "$REDIR" = "y" ]; then
37bd22e5 3000 nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG" >&2
3001 else
3002 nice $READNICE $CDROMREADER -$CDPARANOIACDROMBUS "$CDROM" -w $UTRACKNUM-[:1] "$FILEARG"
3003 fi ;;
c9c2ca27 3004 esac
3005 RETURN=$?
b546a9eb 3006 # If we get some error or we get some missing wav
c7ea851d 3007 # (as long as we dont use pipes)
3008 if [ "$RETURN" != "0" -o \( ! -s "$WAVDATA" -a X"$USEPIPES" != "Xy" \) ]; then
c9c2ca27 3009 # Thank goodness errors is only machine-parseable up to the
3010 # first colon, otherwise this woulda sucked
93415558 3011 if [ "$RETURN" = "0" -a ! -s "$WAVDATA" ]; then
3012 RETURN=73 # fake a return code as cdparanoia return 0 also on aborted reads
3013 fi
49c04b11 3014 if [ "$USEPIPES" = "y" ]; then
3015 echo "readencodetrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
3016 else
3017 echo "readtrack-$UTRACKNUM: $CDROMREADER returned code $RETURN" >> "$ABCDETEMPDIR/errors"
3018 fi
c9c2ca27 3019 return $RETURN
3020 else
49c04b11 3021 if [ "$USEPIPES" = "y" ]; then
3022 echo readencodetrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
3023 else
3024 echo readtrack-$UTRACKNUM >> "$ABCDETEMPDIR/status"
e9b4a89a 3025 fi
3026 if [ "$1" = "onetrack" ]; then
3027 echo onetrack >> "$ABCDETEMPDIR/status"
49c04b11 3028 fi
c9c2ca27 3029 fi
3030}
3031
99d009fa 3032# do_cdspeed
3033# No values accepted, only uses env variables
b546a9eb 3034do_cdspeed ()
c9c2ca27 3035{
3036 if "$CDSPEED" "$CDSPEEDOPTS" "$CDSPEEDVALUE" >/dev/null ; then
3037 vecho "Setting CD speed to ${CDSPEEDVALUE}x"
3038 else
3039 echo "abcde: unable to set the device speed" >&2
3040 fi
3041}
3042
3043# vecho [message]
3044#
7172fbaf 3045# vecho outputs a message if EXTRAVERBOSE is 1 or more
c9c2ca27 3046vecho ()
3047{
7172fbaf
E
3048if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 0 ] ; then
3049 case $1 in
3050 warning) shift ; log warning "$@" ;;
3051 *) echo "$@" ;;
3052 esac
3053fi
3054}
3055
3056# vvecho [message]
3057#
3058# vvecho outputs a message if EXTRAVERBOSE is 2 or more
3059vvecho ()
3060{
3061if [ x"$EXTRAVERBOSE" != "x" ] && [ $EXTRAVERBOSE -gt 1 ] ; then
535944df 3062 case $1 in
bed60a72 3063 warning) shift ; log warning "$@" ;;
535944df 3064 *) echo "$@" ;;
3065 esac
c9c2ca27 3066fi
3067}
3068
0709e644 3069# decho [message]
3070#
3071# decho outputs a debug message if DEBUG is selected
3072decho ()
3073{
3074if [ x"$DEBUG" != "x" ]; then
43559afb 3075 if echo $1 | grep "^\[" > /dev/null 2>&1 ; then
c1bbe6c3 3076 DEBUGECHO=$(echo "$@" | tr -d '[]')
3077 echo "[DEBUG] $DEBUGECHO: `eval echo \\$${DEBUGECHO}`"
3078 else
3079 echo "[DEBUG] $1"
3080 fi
0709e644 3081fi
3082}
3083
b7f1cc84 3084# User-redefinable functions
3085# Custom filename munging:
3086mungefilename ()
3087{
eb1474fe 3088 #echo "$@" | sed s,:,\ -,g | tr \ /\* __+ | tr -d \'\"\?\[:cntrl:\]
3089 echo "$@" | sed s,:,\ -,g | tr \ / __ | tr -d \'\"\?\[:cntrl:\]
b7f1cc84 3090}
3091
3092# Custom genre munging:
3093mungegenre ()
3094{
3095 echo $CDGENRE | tr "[:upper:]" "[:lower:]"
3096}
3097
3098# pre_read
3099# Empty pre_read function, to be defined in the configuration file.
3100pre_read ()
3101{
3102:
3103}
3104
65f3ebb1 3105# post_read
3106# Empty post_read function, to be defined in the configuration file.
3107post_read ()
3108{
3109:
3110}
3111
3279389d 3112# post_encode
3113# Empty post_encode function, to be defined in the configuration file.
3114post_encode ()
3115{
3116:
3117}
3118
a2052a00 3119###############################################################################
b7f1cc84 3120# End of functions
3121#
c9c2ca27 3122# Start of execution
a2052a00 3123###############################################################################
c9c2ca27 3124
3125# Builtin defaults
dba06003 3126
3127# CDDB
2c2dffe6 3128# Defaults to FreeDB, but musicbrainz can be used too, via the abcde-musicbrainz-tool script
dba06003 3129CDDBMETHOD=cddb
c9c2ca27 3130CDDBURL="http://freedb.freedb.org/~cddb/cddb.cgi"
3131CDDBSUBMIT=freedb-submit@freedb.org
3b9371f1 3132CDDBPROTO=6
c9c2ca27 3133HELLOINFO="$(whoami)@$(hostname)"
9ff5088c 3134CDDBCOPYLOCAL="n"
f23735e2 3135CDDBLOCALPOLICY="always"
3136CDDBLOCALRECURSIVE="y"
9ff5088c 3137CDDBLOCALDIR="$HOME/.cddb"
3138CDDBUSELOCAL="n"
3139
3140# List of fields we parse and show during the CDDB parsing...
3141SHOWCDDBFIELDS="year,genre"
3142
c9c2ca27 3143INTERACTIVE=y
dba06003 3144#CDROMREADERSYNTAX=cdparanoia
c9c2ca27 3145ENCODERSYNTAX=default
3146
3147MP3ENCODERSYNTAX=default
3148OGGENCODERSYNTAX=default
3149FLACENCODERSYNTAX=default
3150SPEEXENCODERSYNTAX=default
99d009fa 3151MPPENCODERSYNTAX=default
dba06003 3152AACENCODERSYNTAX=default
72d7162b 3153NORMALIZERSYNTAX=default
411ef0b0 3154CUEREADERSYNTAX=default
c9c2ca27 3155
3156OUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${TRACKNUM}.${TRACKFILE}'
3157# Use the following VAOUTPUTFORMAT to revert to 2.0.x VA format:
3158#VAOUTPUTFORMAT=${OUTPUTFORMAT}
3159VAOUTPUTFORMAT='Various-${ALBUMFILE}/${TRACKNUM}.${ARTISTFILE}-${TRACKFILE}'
3160ONETRACKOUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${ALBUMFILE}'
3161VAONETRACKOUTPUTFORMAT='Various-${ALBUMFILE}/${ALBUMFILE}'
3162PLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
3163PLAYLISTDATAPREFIX=''
3164VAPLAYLISTFORMAT='${ARTISTFILE}-${ALBUMFILE}.${OUTPUT}.m3u'
3165VAPLAYLISTDATAPREFIX=''
3166DOSPLAYLIST=n
3167COMMENT=''
3168ID3TAGV=2
3169ENCNICE=10
3170READNICE=10
3171DISTMP3NICE=10
3172VARIOUSARTISTS=n
3173VARIOUSARTISTSTYLE=forward
c9c2ca27 3174KEEPWAVS=n
3175PADTRACKS=n
0709e644 3176NOGAP=n
3177BATCHNORM=n
4562fac6 3178NOCDDBQUERY=n
c9c2ca27 3179
6b005b85 3180# If using scsi devices, cdda2wav/icedax needs a CDROMID, instead of a device node
c9c2ca27 3181# i.e. CDROMID="1,0,0"
3182CDROMID=""
c008b56d 3183# If we are using the IDE bus, we need CDPARANOIACDROMBUS defined as "d"
3184# If we are using the ide-scsi emulation layer, we need to define a "g"
3185CDPARANOIACDROMBUS="d"
c9c2ca27 3186
3187# program paths - defaults to checking your $PATH
3188# mp3
3189LAME=lame
0709e644 3190TOOLAME=toolame
c9c2ca27 3191GOGO=gogo
3192BLADEENC=bladeenc
3193L3ENC=l3enc
3194XINGMP3ENC=xingmp3enc
3195MP3ENC=mp3enc
3196# ogg
3197VORBIZE=vorbize
3198OGGENC=oggenc
3199# flac
3200FLAC=flac
3201# speex
3202SPEEXENC=speexenc
99d009fa 3203# mpp (Musepack)
3204MPPENC=mppenc
dba06003 3205# m4a
3206AACENC=faac
c9c2ca27 3207
3208ID3=id3
3209ID3V2=id3v2
a9a23379 3210EYED3=eyeD3
f4065e1b 3211VORBISCOMMENT=vorbiscomment
3212METAFLAC=metaflac
3213AACTAG=faac
da56be07 3214ATOMICPARSLEY=AtomicParsley
f4065e1b 3215
c9c2ca27 3216CDPARANOIA=cdparanoia
6b005b85 3217CDDA2WAV=icedax
c9c2ca27 3218DAGRAB=dagrab
3219CDDAFS=cp
3220CDDISCID=cd-discid
3221CDDBTOOL=cddb-tool
2c2dffe6 3222MUSICBRAINZ=abcde-musicbrainz-tool
c9c2ca27 3223EJECT=eject
3224MD5SUM=md5sum
3225DISTMP3=distmp3
72d7162b 3226NORMALIZE=normalize-audio
c9c2ca27 3227CDSPEED=eject
12c5d978 3228VORBISGAIN=vorbisgain
6fba8f12 3229MP3GAIN=mp3gain
cb414c1e 3230MPPGAIN=replaygain
24f444d7 3231MKCUE=mkcue
9ff5088c 3232MKTOC=cdrdao
3233DIFF=diff
ffd57cfc 3234CUE2DISCID=builtin
c9c2ca27 3235
3236# Options for programs called from abcde
3237# mp3
3238LAMEOPTS=
0709e644 3239TOOLAMEOPTS=
c9c2ca27 3240GOGOOPTS=
3241BLADEENCOPTS=
3242L3ENCOPTS=
3243XINGMP3ENCOPTS=
3244MP3ENCOPTS=
b8fa3593 3245MP3GAINOPTS="-a -k"
c9c2ca27 3246# ogg
3247VORBIZEOPTS=
3248OGGENCOPTS=
b8fa3593 3249VORBISGAINOPTS="--album"
c9c2ca27 3250# flac
cb414c1e 3251FLACOPTS=
b8fa3593 3252FLACGAINOPTS="--add-replay-gain"
c9c2ca27 3253# speex
3254SPEEXENCOPTS=
9f659ada 3255# mpc
3256MPPENCOPTS=
dba06003 3257# m4a
3258AACENCOPTS=
c9c2ca27 3259
3260ID3OPTS=
3261ID3V2OPTS=
3262CDPARANOIAOPTS=
3263CDDA2WAVOPTS=
3264DAGRABOPTS=
3265CDDAFSOPTS="-f"
3266CDDBTOOLOPTS=
3267EJECTOPTS=
3268DISTMP3OPTS=
3269NORMALIZEOPTS=
3270CDSPEEDOPTS="-x"
3271CDSPEEDVALUE=
24f444d7 3272MKCUEOPTS=
9ff5088c 3273MKTOCOPTS=""
b33dc0c1 3274VORBISCOMMENTOPTS="-R"
3275METAFLACOPTS="--no-utf8-convert"
c1bbe6c3 3276DIFFOPTS=
c9c2ca27 3277
3278# Default to one process if -j isn't specified
3279MAXPROCS=1
3280
3281# List of actions to perform - by default, run to completion
3282ACTIONS=cddb,read,encode,tag,move,clean
3283
52e09581
E
3284# This option is basically for Debian package dependencies:
3285# List of preferred outputs - by default, run with whatever we have in the path
4562fac6 3286DEFAULT_OUTPUT_BINARIES=vorbis:oggenc,flac:flac,mp3:toolame,mp3:lame,mp3:bladeenc,spx:speex,m4a:faac
b82e2042 3287
52e09581 3288# List of preferred cdromreaders - by default, run whichever we have in the path
6b005b85 3289DEFAULT_CDROMREADERS="cdparanoia icedax cdda2wav"
dba06003 3290
93a69eff 3291# List of quality levels associated with the encoders:
3292DEFAULT_QUALITY_XLO="oggenc:-q -1,lame:-q 9,speex:--quality 1,m4a:"
3293DEFAULT_QUALITY_LO="oggenc:-q 1,lame:-q 7,speex:--quality 5,m4a:"
3294DEFAULT_QUALITY_HI="oggenc:-q 7,lame:--preset standard,speex:--quality 9,m4a:"
3295DEFAULT_QUALITY_XHI="oggenc:-q 10,lame:--preset extreme,speex:--quality 10,m4a:"
3296
52e09581 3297# Assume fetch if under FreeBSD. curl is used for Mac OS X. wget is used for
43559afb 3298# Linux/OpenBSD. ftp is user for NetBSD.
3299# Let's use these checkings to determine the OS flavour, which will be used
3300# later
c9c2ca27 3301if [ X$(uname) = "XFreeBSD" ] ; then
3302 HTTPGET=fetch
a9a23379 3303 MD5SUM=md5
c9c2ca27 3304 NEEDCDROMID=y
3305 OSFLAVOUR=FBSD
3306elif [ X$(uname) = "XDarwin" ] ; then
3307 HTTPGET=curl
3308 OSFLAVOUR=OSX
a1d79009
E
3309 # We should have diskutil in OSX, but let's be sure...
3310 NEEDDISKUTIL=y
f97e22a3 3311 CDROMREADERSYNTAX=cddafs
c9c2ca27 3312elif [ X$(uname) = "XOpenBSD" ] ; then
3313 HTTPGET=wget
3314 MD5SUM=md5
43559afb 3315 OSFLAVOUR=OBSD
a9a23379 3316elif [ X$(uname) = "XNetBSD" ] ; then
3317 HTTPGET=ftp
3318 MD5SUM=md5
43559afb 3319 OSFLAVOUR=NBSD
f6398cd4 3320elif [ X$(uname) = "SunOS" ] ; then
43559afb 3321 HTTPGET=""
3322 MD5SUM=md5
3323 OSFLAVOUR=SunOS
c9c2ca27 3324else
3325 HTTPGET=wget
3326fi
3327
3328# If CDDBAVAIL is set to n, no CDDB read is done
3329# If USEID3 is set to n, no ID3 tagging is done
3330CDDBAVAIL=y
3331USEID3=y
24f444d7 3332USEID3V2=y
c9c2ca27 3333
93a69eff 3334# There we go..
3335# But before we get into business, let us chop off any GREP environmental
3336# variables.
3337GREP_OPTIONS=""
3338GREP_COLOR=""
3339
c9c2ca27 3340if [ -z "$OUTPUTDIR" ]; then
3341 OUTPUTDIR=$(pwd)
3342fi
3343
3344if [ -z "$WAVOUTPUTDIR" ]; then
3345 WAVOUTPUTDIR="$OUTPUTDIR"
3346fi
3347
3348# Load system defaults
3349if [ -r /etc/abcde.conf ]; then
3350 . /etc/abcde.conf
3351fi
3352# Load user preference defaults
3353if [ -r $HOME/.abcde.conf ]; then
3354 . $HOME/.abcde.conf
3355fi
3356
3357# By this time, we need some HTTPGETOPTS already defined.
b7f1cc84 3358# If the user has defined a non-default HTTPGET method, we should not be empty.
c9c2ca27 3359
3360if [ "$HTTPGETOPTS" = "" ] ; then
3361 case $HTTPGET in
0099f076 3362 wget) HTTPGETOPTS="-q -nv -e timestamping=off -O -";;
c9c2ca27 3363 curl) HTTPGETOPTS="-f -s";;
3364 fetch)HTTPGETOPTS="-q -o -";;
5b9629b0 3365 ftp) HTTPGETOPTS="-a -V -o - ";;
535944df 3366 *) log warning "HTTPGET in non-standard and HTTPGETOPTS are not defined." ;;
c9c2ca27 3367 esac
3368fi
3369
3370# If the CDROM has not been set yet, find a suitable one.
3371# If this is a devfs system, default to /dev/cdroms/cdrom0
3372# instead of /dev/cdrom
3373if [ "$CDROM" = "" ] ; then
3374 if [ -e /dev/cdroms/cdrom0 ]; then
3375 CDROM=/dev/cdroms/cdrom0
3376 elif [ -e /dev/cdrom ]; then
3377 CDROM=/dev/cdrom
9ad72a62
E
3378 elif [ -e /dev/sr0 ]; then
3379 CDROM=/dev/sr0
c9c2ca27 3380 elif [ -e /dev/cd0c ]; then
3381 CDROM=/dev/cd0c
3382 elif [ -e /dev/acd0c ]; then
3383 CDROM=/dev/acd0c
3384 elif [ -e /dev/disk1 ]; then
3385 CDROM=/dev/disk1
df2e617f
E
3386 elif [ "$OSFLAVOUR" = "OSX" ] && [[ $(diskutil list) =~ CD_part.*(disk.)$'\n' ]]; then
3387 CDROM=/dev/${BASH_REMATCH[1]}
c9c2ca27 3388 fi
3389fi
3390
7172fbaf
E
3391# We used to use EXTRAVERBOSE=y to turn on more debug output. Now
3392# that's changed to a number to allow for more control. If
3393# EXTRAVERBOSE=y, set it to 1 for backwards compatibility.
3394if [ "$EXTRAVERBOSE" = "y" ]; then
3395 EXTRAVERBOSE=1
3396fi
3397if [ "$EXTRAVERBOSE" = "n" ]; then
3398 EXTRAVERBOSE=0
3399fi
3400
c9c2ca27 3401# Parse command line options
93a69eff 3402#while getopts 1a:bc:C:d:Dehj:klLmMnNo:pPq:r:Rs:S:t:T:vVxw:W: opt ; do
1945e238 3403while getopts 1a:bBc:C:d:Defghj:klLmMnNo:pPq:r:s:S:t:T:UvVxX:w:W:z opt ; do
c9c2ca27 3404 case "$opt" in
3405 1) ONETRACK=y ;;
3406 a) ACTIONS="$OPTARG" ;;
a9a23379 3407 A) EXPACTIONS="$OPTARG" ;;
0709e644 3408 b) BATCHNORM=y ;;
6fba8f12 3409 B) NOBATCHREPLAYGAIN=y ;;
535944df 3410 c) if [ -e "$OPTARG" ] ; then . "$OPTARG" ; else log error "config file \"$OPTARG\" cannot be found." ; exit 1 ; fi ;;
70da1809 3411 C) DISCID="$( echo ${OPTARG#abcde.} | tr -d /)" ;;
c9c2ca27 3412 d) CDROM="$OPTARG" ;;
3413 D) set -x ;;
3414 h) usage; exit ;;
b33dc0c1 3415 e) ERASEENCODEDSTATUS=y ;;
a9a23379 3416 E) ENCODING="$OPTARG" ;;
75c2c424 3417 f) FORCE=y ;;
0709e644 3418 g) NOGAP=y ;;
c9c2ca27 3419 i) INLINETAG=y ;;
3420 j) MAXPROCS="$OPTARG" ;;
3421 k) KEEPWAVS=y ;;
3422 l) LOWDISK=y ;;
b7f1cc84 3423 L) CDDBUSELOCAL=y ;;
3424 n) CDDBAVAIL=n ;;
3425 N) INTERACTIVE=n ;;
c9c2ca27 3426 m) DOSPLAYLIST=y ;;
f7c6d185 3427 M) DOCUE=y ;;
c9c2ca27 3428 o) OUTPUTTYPE="$OPTARG" ;;
b7f1cc84 3429 p) PADTRACKS=y ;;
3430 P) USEPIPES=y ;;
93a69eff 3431 q) QUALITYLEVEL="$OPTARG" ;;
c9c2ca27 3432 r) REMOTEHOSTS="$OPTARG" ;;
43559afb 3433 R) CDDBLOCALRECURSIVE=y ;;
9ff5088c 3434 s) SHOWCDDBFIELDS="$OPTARG" ;;
c9c2ca27 3435 S) CDSPEEDVALUE="$OPTARG" ;;
79e3928b 3436 t) STARTTRACKNUMBER="$OPTARG" ;;
3437 T) STARTTRACKNUMBER="$OPTARG" ; STARTTRACKNUMBERTAG="y" ;;
3b9371f1 3438 U) CDDBPROTO=5 ;;
b546a9eb 3439 v)
c9c2ca27 3440 echo "This is abcde v$VERSION."
3441 echo "Usage: abcde [options] [tracks]"
3442 echo "abcde -h for extra help"
3443 exit
3444 ;;
7172fbaf 3445 V) EXTRAVERBOSE=$(($EXTRAVERBOSE + 1)) ;;
c9c2ca27 3446 x) EJECTCD="y" ;;
ffd57cfc 3447 X) CUE2DISCID="$OPTARG" ;;
79e3928b 3448 w) COMMENT="$OPTARG" ;;
b546a9eb 3449 W) if echo $OPTARG | grep "[[:digit:]]" > /dev/null 2>&1 ; then
3450 STARTTRACKNUMBER="${OPTARG}01"
101e0368 3451 STARTTRACKNUMBERTAG="y"
3452 COMMENT="CD${OPTARG}"
3453 DISCNUMBER="${OPTARG}"
bef9c2d8 3454 else
b546a9eb 3455 log error "argument of -W must be integer"
bef9c2d8 3456 exit 1
3457 fi
3458 ;;
0709e644 3459 z) DEBUG=y ; CDROMREADERSYNTAX=debug ; EJECTCD="n" ;;
c9c2ca27 3460 ?) usage; exit ;;
3461 esac
3462done
3463
3464shift $(($OPTIND - 1))
3465
2d38e786 3466# Here it comes the worse part of the whole thing. From here until we start
3467# ripping, we have a mixture of sanity checks, verifications, default settigs
3468# and other crazy stuff that interdepends, but hey, someone has to do it.
3469
4562fac6 3470# If NOCDDBQUERY is set, don't query the CDDB server.
3471if [ "$NOCDDBQUERY" = "y" ]; then
3472 CDDBAVAIL="n"
3473fi
3474
1b94d6b7 3475# If the user specified a flac file, then switch to special flac mode
2d38e786 3476if echo "$CDROM" | grep -i '.flac$' > /dev/null 2>&1 ; then
3477 if [ ! -f "$CDROM" ]; then
43559afb 3478 log error "the defined file for FLAC ripping cannot be found" >&2
3479 exit 1
3480 fi
2d38e786 3481 vecho warning "switching to flac CDROMREADERSYNTAX..."
1b94d6b7 3482 CDROMREADERSYNTAX=flac
d68ebfff 3483 # We have a builtin version of cue2discid...
3484 case "$CUE2DISCID" in
3485 builtin);;
3486 *) NEEDCUE2DISCID=y;;
3487 esac
f6249f03 3488 NEEDMETAFLAC=y
3489 EJECTCD=n
1b94d6b7 3490fi
3491
5ddcb883 3492# If the user provided a DISCID, disable eject
bed60a72 3493if [ -n "$DISCID" ] || [ "$CDROMREADERSYNTAX" = "flac" ]; then EJECTCD=n ; fi
5ddcb883 3494
dba06003 3495# Check the available cd rippers in the system, from the ones we know.
3496if [ "$CDROMREADERSYNTAX" = "" ]; then
3497 for DEFAULT_CDROMREADER in $DEFAULT_CDROMREADERS; do
43559afb 3498 if new_checkexec $DEFAULT_CDROMREADER; then
dba06003 3499 CDROMREADERSYNTAX=$DEFAULT_CDROMREADER
3500 break
3501 fi
3502 done
3503 if [ "$CDROMREADERSYNTAX" = "" ]; then
3504 log error "no cdreader found in your PATH"
3505 log error "hints: are all dependencies installed? has the \$PATH been modified?"
3506 exit 1
3507 fi
3508fi
3509
d5200e72 3510# Decide if we can continue.
b546a9eb 3511if [ "$ONETRACK" = "y" ]; then
d5200e72 3512 # FIXME # remove check as soon as we find out about the other readers
99d009fa 3513 case "$CDROMREADERSYNTAX" in
1b94d6b7 3514 flac) ;;
b82e2042 3515 cdparanoia) ;;
6b005b85 3516 cdda2wav | icedax) ;;
535944df 3517 *) log error "$CDROMREADERSYNTAX does not support ONETRACK mode"
b546a9eb 3518 exit 1 ;;
99d009fa 3519 esac
0709e644 3520 if [ "$BATCHNORM" = "y" ]; then
535944df 3521 log warning "BATCHNORM mode is not compatible with ONETRACK mode. Disabling..."
0709e644 3522 BATCHNORM=n
3523 fi
3524 if [ "$NOGAP" = "y" ]; then
535944df 3525 log warning "NOGAP mode is not compatible with ONETRACK mode. Disabling..."
0709e644 3526 NOGAP=n
99d009fa 3527 fi
3528 # It does not matter how many tracks we want. In ONETRACK mode we grab them all
d5200e72 3529 # FIXME # allow ranges of tracks to be selected for onetrack ripping
c9c2ca27 3530 if [ $# -gt 0 ]; then
535944df 3531 log warning "ONETRACK mode selected, grabbing all tracks..."
c9c2ca27 3532 fi
3533else
3534 while [ $# -gt 0 ]; do
3535 # Range parsing code courtesy of Vincent Ho
2c7824bc 3536 # Cleaned up to use shell built-ins by Charles Steinkuehler
3537 if [ "${1#*[^0-9-]}" != "$1" ]; then
f0bce1c3
E
3538 log error "syntax error while processing track numbers ($1)"
3539 exit 1
b546a9eb 3540 else
2c7824bc 3541 RSTART=${1%%-*}
3542 REND=${1##*-}
3543 while [ ${RSTART:=1} -le ${REND:=0} ] ; do
3544 TRACKQUEUE="$TRACKQUEUE $RSTART"
3545 RSTART=$(( $RSTART + 1 ))
3546 done
b546a9eb 3547 fi
c9c2ca27 3548 shift
3549 done
3550fi
3551
b7f1cc84 3552# List of valid actions: cddb,read,normalize,encode,tag,move,playlist,clean
3553# List of experimental actions: retag,transcode
3554
3555# Determine what actions are to be done from $ACTIONS and set the
3556# following environment variables for them:
3557DOCDDB=n
3558DOREAD=n
3559DONORMALIZE=n
3560DOPREPROCESS=n
3561DOENCODE=n
3562DOPOSTPROCESS=n
3563DOTAG=n
3564DOMOVE=n
00840d9a 3565DOREPLAYGAIN=n
b7f1cc84 3566DOPLAYLIST=n
3567DOCLEAN=n
93a69eff 3568## FIXME ## Lets keep compatibility with -M
3569[ "$DOCUE" != "y" ] && DOCUE=n
b7f1cc84 3570
3571for ACTION in $(echo $ACTIONS | tr , \ )
3572do
3573 case $ACTION in
2d38e786 3574 default) DOCDDB=y; DOREAD=y; DOENCODE=y; DOTAG=y; DOMOVE=y; DOCLEAN=y;;
f7c6d185 3575 cue) DOCUE=y ; MAKECUEFILE=y ;;
24f444d7 3576 cddb) DOCDDB=y;;
3577 read) DOREAD=y;;
3578 normalize) DONORMALIZE=y; DOREAD=y;;
3579# preprocess) DOPREPROCESS=y; DOREAD=y;;
3580 encode) DOENCODE=y; DOREAD=y;;
3581# postprocess) DOPREPROCESS=y; DOENCODE=y; DOREAD=y;;
3582 tag) DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
3583 move) DOMOVE=y; DOTAG=y; DOREAD=y; DOENCODE=y; DOCDDB=y;;
00840d9a 3584 replaygain) DOCDDB=y; DOREAD=y; DOENCODE=y; DOTAG=y; DOMOVE=y; DOREPLAYGAIN=y;;
24f444d7 3585 playlist) DOCDDB=y; DOPLAYLIST=y;;
3586 clean) DOCLEAN=y;;
b7f1cc84 3587 esac
3588done
3589
1750d493 3590if [ "$DONORMALIZE" = "y" ] && [ "$DOREPLAYGAIN" = "y" ]; then
3591 # FIXME # should we abort on error or just inform the user?
6fba8f12 3592 log warning "selected both normalize and replaygain actions"
1750d493 3593fi
3594
9ff5088c 3595for SHOWCDDBFIELD in $(echo $SHOWCDDBFIELDS | tr , \ ); do
3596 case $SHOWCDDBFIELD in
3597 y*|Y*) SHOWCDDBYEAR="y";;
3598 g*|G*) SHOWCDDBGENRE="y";;
3599 *) ;;
3600 esac
3601done
b7f1cc84 3602
c9c2ca27 3603# At this point a CDROM has to be defined, so we check it exists.
b546a9eb 3604if [ X"$CDROM" != "X" ] ; then
6b005b85 3605 if ( [ "$CDROMREADERSYNTAX" = "cdda2wav" ] || [ "$CDROMREADERSYNTAX" = "icedax" ] ) && [ "$NEEDCDROMID" = "y" ] ; then
c9c2ca27 3606 if [ "$OSFLAVOUR" = "FBSD" ]; then
43559afb 3607 if echo "$CDROMID" | grep "^[0-9],[0-9],[0-9]$" >/dev/null 2>&1 ; then :; else
535944df 3608 log error "CDROMID not in the right format for $CDROMREADERSYNTAX"
52e09581 3609 log error "Use \"cdrecord -scanbus\" to obtain an adequate ID and set CDROMID accordingly"
c9c2ca27 3610 exit 1
8fc41c4f 3611 fi
c9c2ca27 3612 fi
c81531ac 3613 elif [ ! -e "$CDROM" -a X"$DOREAD" = "Xy" ]; then
535944df 3614 log error "CDROM device cannot be found."
c9c2ca27 3615 exit 1
3616 fi
6c124acf 3617# avoid processing if we are not going to hit the CDROM.
c81531ac 3618elif [ X"$DOREAD" = "Xy" ]; then
535944df 3619 log error "CDROM has not been defined or cannot be found"
c9c2ca27 3620 exit 1
3621fi
3622
c7ea851d 3623# USEPIPES pre-tests, before we get into more serious stuff
3624# Not compatible with:
d5200e72 3625# - multiple outputs
3626# - normalize
c7ea851d 3627# - lowdisk algorithm
d5200e72 3628# - anything else?
c7ea851d 3629if [ X"$USEPIPES" = "Xy" ]; then
b82e2042 3630 if [ $(echo "$OUTPUTTYPE" | tr , \ | wc -w ) -gt 1 ]; then
535944df 3631 log error "Unix pipes not compatible with multiple outputs"
d5200e72 3632 exit 1
3633 fi
c7ea851d 3634 if [ X"$DONORMALIZE" = "Xy" ]; then
535944df 3635 log error "Unix pipes not compatible with normalizer"
d5200e72 3636 # FIXME # Do we need to exit or shall we just disable the mode?
3637 exit 1
3638 fi
0709e644 3639 if [ X"$BATCHNORM" = "Xy" ]; then
535944df 3640 log error "Unix pipes not compatible with BATCHNORM encoding"
0709e644 3641 exit 1
3642 fi
3643 if [ X"$NOGAP" = "Xy" ]; then
535944df 3644 log error "Unix pipes not compatible with NOGAP encoding"
0709e644