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