1#!/usr/bin/env bash
2#
3# audio_menu.sh		- Ripping/encode script.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Library General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18#
19# $Id: audio_menu.sh 28 2007-01-06 17:05:14Z k0k $
20
21# Read in language file
22source ${BBROOTDIR}/lang/${BBLANG}/audio_menu.lang
23
24# Read in common functions
25source ${BBROOTDIR}/misc/commonfunctions.sh
26
27# Function: This function lets you swap cds if you only have one device. {{{1
28#-----------------------------------------------------------------------------
29#(CDwriter and CDreader is same device.)
30function insert_new_CD()
31{
32        while true; do
33                echo $bb_am_enter_2
34                read temp
35                if [[ "$temp" = "" ]]; then
36                        break
37                else
38                        continue
39                fi
40        done
41}
42
43# Function: Looks for mp3. {{{1
44#-----------------------------------------------------------------------------
45function check_for_mp3s()
46{
47	cd ${BBBURNDIR}
48	while read MPTHREE; do
49        	existing="yes"
50	done < <(find ${BBBURNDIR} -iname "*.[Mm][Pp]3" | sort)
51
52	if [[ "$existing" != "yes" ]]; then
53        	StatusBar "$bb_am_nomp3s${BBBURNDIR}"
54	else
55		${BBROOTDIR}/convert/convert_mp3s.sh
56	fi
57}
58
59# Function: Checks for ogg files. {{{1
60#-----------------------------------------------------------------------------
61function check_for_oggs()
62{
63	cd ${BBBURNDIR}
64	while read OGGS; do
65		existing="yup"
66	done < <(find ${BBBURNDIR} -iname "*.[Oo][Gg][Gg]" | sort)
67
68	if [[ "$existing" != "yup" ]]; then
69        	StatusBar "$bb_am_nooggs${BBBURNDIR}"
70	else
71		${BBROOTDIR}/convert/convert_oggs.sh
72	fi
73}
74
75# Function: Checks for flac files. {{{1
76#-----------------------------------------------------------------------------
77function check_for_flacs()
78{
79	cd ${BBBURNDIR}
80	while read FLACS; do
81		existing="aight"
82	done < <(find ${BBBURNDIR} -iname "*.[Ff][Ll][Aa][Cc]" | sort)
83
84	if [[ "$existing" != "aight" ]]; then
85		StatusBar "$bb_am_noflacs${BBBURNDIR}"
86	else
87		${BBROOTDIR}/convert/convert_flacs.sh
88	fi
89}
90
91# Function: Adjust the volume of wav audio files to a standard volume level. {{{1
92#-----------------------------------------------------------------------------
93function normalization()
94{
95        if [[ "$BBNORMALIZE" = "yes" ]]; then
96                cd ${BBBURNDIR}
97                for i in *.wav; do
98		   echo;echo -e "${BBTABLECOLOR}|>${BBSUBCOLOR}$bb_am_norm_1$i...${BBCOLOROFF}";
99                   ${BB_NORMCMD} -v -m $i;
100                done
101        fi
102}
103
104# Function: Valide the input of "y" or "n". {{{1
105#-----------------------------------------------------------------------------
106function conf_yes_no()
107{
108	unset ANSWER
109	while [ "${ANSWER}" != 'y' ] && [ "${ANSWER}" != 'n' ]
110	do
111		echo -n $bb_am_conf_2
112		read ANSWER
113	done
114
115}
116
117# Function: Control errors. {{{1
118#-----------------------------------------------------------------------------
119function conf_error()
120{
121	STDERROR=$?
122	# If there is any error return to main menu.
123	if [[ ${STDERROR} -ne 0 ]]; then
124		echo -e "${BBTABLECOLOR}$bb_am_err_1${BBCOLOROFF}"
125		sleep 3
126		exit
127	fi
128}
129
130# Function: Valide confirmation of song names. {{{1
131#-----------------------------------------------------------------------------
132function confirmation()
133{
134	echo
135	if [[ ! -f "${BBBURNDIR}/song_name.txt" ]]; then
136		exit
137	else
138		echo -e "${BBTABLECOLOR}|>${BBMAINCOLOR}$bb_am_conf_1${BBCOLOROFF}"
139		cat -n ${BBBURNDIR}/song_name.txt
140		echo -e "${BBSUBCOLOR}"
141		conf_yes_no		# Valid input.
142		echo -e "${BBCOLOROFF}"
143
144		if [[ ${ANSWER} = 'n' ]];
145		then
146			rm -f ${BBBURNDIR}/song_name.txt
147			rm -f ${BBBURNDIR}/tracks.txt
148			exit
149		fi
150	fi
151}
152
153# Function: Interactive naming of files. {{{1
154#-----------------------------------------------------------------------------
155function named()
156{
157	# Delete old lists of songs rip.
158	rm -f ${BBBURNDIR}/*.txt
159
160	# cdda2wav show information of tracks to rip.
161	# it's great for see which track would to be
162	# rip and the time of duration of the tracks.
163	echo -e "${BBSUBCOLOR}$bb_am_named_1${BBCOLOROFF}"
164	sleep 1s
165	${BB_CDAUDIORIP} -D ${BBCDROM} -H -J -v toc
166
167	# If there is any error return to main menu.
168	conf_error
169
170	TRACK=0
171	while [ "${TRACK}" != "" ]; do
172		echo;echo -en "${BBMAINCOLOR}$bb_am_named_2"
173		echo;echo -en "${BBMAINCOLOR}$bb_am_named_3${BBTABLECOLOR}|>${BBCOLOROFF} "
174		read TRACK
175
176			if [ "${TRACK}" != "" ];
177			then
178				# Only permit integer numbers standing the format in the numbers of back prompt.
179				number_track=`printf '%02d' ${TRACK}`
180
181				# This line puts track numbers of the input standard into tracks.txt.
182				echo "${number_track}" >> ${BBBURNDIR}/tracks.txt
183			else
184			# If nothing is entered at the prompt then exit loop.
185				continue
186			fi
187
188		echo
189		echo -e "${BBMAINCOLOR}$bb_am_named_4"
190		echo -e "${BBMAINCOLOR}$bb_am_named_5"
191		echo -en "${BBMAINCOLOR}$bb_am_named_6${number_track} ${BBTABLECOLOR}|>${BBCOLOROFF} "
192		read song_name
193
194		# If the song_name variable = space blank then, change
195		# fill that with the number of the track to ripped.
196		if [[ "${song_name}" = "" ]]; then
197			song_name=`echo "${number_track}.-Track"`
198		else
199
200		# If the song_name variable contained some signs and caracters specials,
201		# that difficulty the naming in bash shell, to be equal to nothing.
202		# Read sed man page to see how it work.
203		song_name=`echo "$song_name" | sed -e 's/(//g'		\
204					-e 's/)//g' -e 's/*//g' 	\
205					-e 's/?//g' -e 's/�//g' 	\
206					-e 's/\///g' -e 's/&//g'`
207		fi
208		# This line puts song name of the input standard into song_name.txt.
209	     	echo ${song_name} >> ${BBBURNDIR}/song_name.txt
210done
211}
212
213# Function: Rip the tracks or songs selects. {{{1
214#-----------------------------------------------------------------------------
215function rip()
216{
217	confirmation
218	cd ${BBBURNDIR}
219	track=0
220	while [ "${track}" != "" ]; do
221
222		# Read the track to rip of the files in temp directory.
223		track=`sed -ne '1p' ${BBBURNDIR}/tracks.txt`
224		if [[ "${track}" = "" ]]; then
225			continue
226		else
227                   	echo -e "${BBTABLECOLOR}|>${BBSUBCOLOR}$bb_am_rip_1${track}...${BBCOLOROFF}"
228
229			# Begin Rip.
230			 ${BB_CDAUDIORIP} -D ${BBCDROM} -x -t ${track} -O wav ${track} #Changed from cdparanoia to cdda2wav
231			sleep 2s
232
233			# This two lines add '.wav' to finished of the tracks/song_name variable for rename.
234			track=`sed -ne '1p' ${BBBURNDIR}/tracks.txt | sed -e 's/$/.wav/g'`
235			song_name=`sed -ne '1p' ${BBBURNDIR}/song_name.txt | sed -e 's/$/.wav/g'`
236
237			# Rename the tracks that has been ripped, by the name
238			# get back by users in prompt.
239			mv "${track}" "${song_name}"
240
241			# Remove the song that has been ripped.
242			sed -e '1d' ${BBBURNDIR}/song_name.txt >> ${BBBURNDIR}/temp_song.txt
243			mv ${BBBURNDIR}/temp_song.txt ${BBBURNDIR}/song_name.txt
244			sed -e '1d' ${BBBURNDIR}/tracks.txt >> ${BBBURNDIR}/temp_tracks.txt
245			mv ${BBBURNDIR}/temp_tracks.txt ${BBBURNDIR}/tracks.txt
246		fi
247	 done
248        # Remove temp files.
249	rm -f ${BBBURNDIR}/tracks.txt
250	rm -f ${BBBURNDIR}/song_name.txt
251	rm -f ${BBBURNDIR}/*.inf
252
253	eject ${BBCDROM}
254	echo -e "${BBSUBCOLOR}$bb_am_rip_2${BBCOLOROFF}"
255	sleep 2s
256}
257
258# Function: Encode Filter Command. {{{1
259#-----------------------------------------------------------------------------
260function encode_filter()
261{
262	if [[ "$ENCODEFILTER" != "" ]]; then
263                echo -e "${BBTABLECOLOR}|>${BBSUBCOLOR}$bb_am_encfilt(${ENCODEFILTER})${BBCOLOROFF}"
264		`${ENCODEFILTER} ${BBBURNDIR}/*.${format}`
265	fi
266}
267
268# Function: Copy an audio cd. {{{1
269#-----------------------------------------------------------------------------
270function copy_audio_cd()
271{
272    cd ${BBBURNDIR}
273    if ${BB_CDAUDIORIP} -D ${BBCDROM} -v all -B -Owav; then      #Changed from cdparanoia to cdda2wav
274
275	eject ${BBCDROM}
276	StatusBar "$bb_am_rip_2"
277	# Normalize WAV's.
278	normalization
279
280	if [[ ${BBNUMDEV} == 1 ]]; then                     #Check number of devices
281	    insert_new_CD
282	fi
283
284	if eval "${BB_CDBURNCMD} -v dev=${BBCDWRITER} speed=${BBSPEED} \
285	    ${BBOPT_ONE:+\"driveropts=$BBOPT_ONE\"} ${BBDTAO} \
286	    -useinfo ${BBBURNDIR}/*.[Ww][Aa][Vv]"; then
287	    StatusBar "$bb_am_ch3_1"
288	    ShowWarn && wait_for_enter
289	else
290	    StatusBar "$bb_am_ch3_2"
291	    ShowWarn && wait_for_enter
292	fi
293    else
294	StatusBar "$bb_am_ch3_3${BBCDROM}"
295	ShowWarn && wait_for_enter
296    fi
297}
298
299# Function: Copy an audio to HD. {{{1
300#-----------------------------------------------------------------------------
301function copy_cd_to_hd()
302{
303 MakeTempFile
304 cd ${BBBURNDIR}
305 ${BB_CDAUDIORIP} -D ${BBCDROM} -v all -B -Owav > ${TMPFILE} 2>&1 &
306 $DIALOG --backtitle " ${BACKTITLE} " --title " INFORMATION " \
307 --tailbox ${TMPFILE} 24 70
308
309 StatusBar "Eject ${BBCDROM}" 1.5
310 eject ${BBCDROM}
311
312 # Normalize WAV's.
313 normalization
314
315$DIALOG --backtitle " ${BACKTITLE} " --title " INFORMATION " \
316--msgbox "$bb_am_ch4_1${BBBURNDIR}.$bb_am_ch4_2 $bb_am_ch4_3" 0 0
317}
318
319# Function: Create Mp3s from Wavs in BURNDIR. {{{1
320#-----------------------------------------------------------------------------
321function create_mp3s_from_wavs()
322{
323 cd ${BBBURNDIR}
324
325 while read WAV; do
326   if ${BB_MP3ENC} --preset cd "${WAV}" "${WAV%%.wav}.mp3"; then
327     StatusBar "${WAV%%.wav}.mp3$bb_am_ch6_1"
328   else
329     StatusBar "${WAV}:$bb_am_ch6_2"
330   fi
331     existing="yes"
332   done < <(find "${BBBURNDIR}" -iname "*.[Ww][Aa][Vv]" | sort)
333
334   if [[ "$existing" != "yes" ]]; then
335     StatusBar "$bb_am_ch6_3${BBBURNDIR}"
336   else
337     # Encode Filter Command.
338     format=mp3
339     encode_filter
340   fi
341   sleep 2s
342   continue
343}
344
345# Function: Create Oggs from Wavs in BURNDIR. {{{1
346#-----------------------------------------------------------------------------
347function create_oggs_from_wavs()
348{
349    cd ${BBBURNDIR}
350
351    while read WAV; do
352	echo
353	if ${BB_OGGENC} -b ${BBBITRATE} "${WAV}"; then
354	    StatusBar "$bb_am_ch7_1"
355	else
356	    StatusBar "${WAV}:$bb_am_ch6_2"
357	fi
358	echo
359	existing="yes"
360    done < <(find "${BBBURNDIR}" -iname "*.[Ww][Aa][Vv]" | sort)
361
362    if [ "$existing" != "yes" ]; then
363	StatusBar "$bb_am_ch6_3${BBBURNDIR}"
364    else
365	# Encode Filter Command.
366	format=ogg
367	encode_filter
368    fi
369    sleep 2s
370    continue
371}
372
373# Function: Create flacs from Wavs in BURNDIR {{{1
374#-----------------------------------------------------------------------------
375function create_flacs_from_wavs()
376{
377
378    cd ${BBBURNDIR}
379
380    while read WAV; do
381	echo
382	if ${BB_FLACCMD} "${WAV}"; then
383	    echo $bb_am_ch7_1
384	else
385	    echo "${WAV}:$bb_am_ch6_2"
386	fi
387	echo
388	existing="yes"
389    done < <(find "${BBBURNDIR}" -iname "*.[Ww][Aa][Vv]" | sort)
390
391    if [[ "$existing" != "yes" ]]; then
392	StatusBar "$bb_am_ch6_3${BBBURNDIR}"
393    else
394	#Encode Filter command
395	format=flac
396	encode_filter
397    fi
398    sleep 2s
399    continue
400}
401
402# Function: Create Mp3s from an audio cd. {{{1
403#-----------------------------------------------------------------------------
404function create_mp3s_from_cd()
405{
406#First, name and rip the tracks
407# Give name to the tracks.
408    named
409    # Rip the tracks in wav audio file.
410    rip
411    # Normalize WAV's.
412    normalization
413    #Now create the Mp3s
414
415    while read WAV; do
416	echo;echo -e "${BBTABLECOLOR}|>${BBSUBCOLOR}$bb_am_ch9_1${BBCOLOROFF}"
417
418	if ${BB_MP3ENC} --preset cd ${WAV} ${WAV%%.wav}.mp3; then
419	    StatusBar "${WAV%%.wav}.mp3$bb_am_ch6_1"
420	else
421	    StatusBar "${WAV}:$bb_am_ch6_2"
422	fi
423	existing="yes"
424    done < <(find "$BURNDIR" -iname "*.[Ww][Aa][Vv]" | sort)
425
426    if [[ "$existing" != "yes" ]]; then
427	StatusBar "$bb_am_ch6_3${BBBURNDIR}" 2
428	continue
429    else
430	# Encode Filter Command.
431	format=mp3
432	encode_filter
433    fi
434
435    StatusBar "$bb_am_ch9_2${BBBURNDIR}"
436    ShowWarn && rm ${BBBURNDIR}/*.[Ww][Aa][Vv]
437    wait_for_enter
438}
439
440# Function: Create Oggs from an audio cd. {{{1
441#-----------------------------------------------------------------------------
442function create_oggs_from_cd()
443{
444#First, name and rip the tracks
445# Give name to the tracks.
446    named
447# Rip the tracks in wav audio file.
448    rip
449# Normalize WAV's.
450    normalization
451
452#Now create the Oggs.
453
454    while read WAV; do
455	echo;echo -e "${BBTABLECOLOR}|>${BBSUBCOLOR}$bb_am_ch10_1${BBCOLOROFF}"
456	if ${BB_OGGENC} -b ${BBBITRATE} "${WAV}"; then
457	    echo $bb_am_ch7_1
458	else
459	    echo "${WAV}:$bb_am_ch6_2"
460	fi
461	echo
462	existing="yes"
463    done < <(find "${BBBURNDIR}" -iname "*.[Ww][Aa][Vv]" | sort)
464
465    if [[ "$existing" != "yes" ]]; then
466	StatusBar "$bb_am_ch6_3${BBBURNDIR}" 2
467	continue
468    else
469	# Encode Filter Command.
470	format=ogg
471	encode_filter
472    fi
473
474    echo "$bb_am_ch10_2${BBBURNDIR}"
475    rm ${BBBURNDIR}/*.[Ww][Aa][Vv]
476    wait_for_enter
477}
478
479# Function: Create flacs from cd. {{{1
480#-----------------------------------------------------------------------------
481function create_flacs_from_cd()
482{
483
484# Give name to the tracks.
485    named
486# Rip the tracks in wav audio file.
487    rip
488# Normalize WAV's.
489    normalization
490
491# Now create Flacs
492
493    while read WAV; do
494	echo
495	if ${BB_FLACCMD} "${WAV}"; then
496	    echo $bb_am_ch7_1
497	else
498	    echo "${WAV}:$bb_am_ch6_2"
499	fi
500	echo
501	existing="yes"
502    done < <(find "${BBBURNDIR}" -iname "*.[Ww][Aa][Vv]" | sort)
503
504    if [[ "$existing" != "yes" ]]; then
505	StatusBar "$bb_am_ch6_3${BBBURNDIR}" 2
506	continue
507    else
508	# Function Filter Command.
509	format=flac
510	encode_filter
511    fi
512
513    echo "$bb_am_ch11_1${BBBURNDIR}"
514    rm ${BBBURNDIR}/*.[Ww][Aa][Vv]
515    wait_for_enter
516}
517
518# Run: Main part. {{{1
519#-----------------------------------------------------------------------------
520####PROGRAM START#####
521MakeTempFile
522while true; do
523# <menu>
524        $DIALOG $OPTS --help-label "$bb_help_button" \
525	  --backtitle "${BACKTITLE}" --begin 2 2 \
526	  --title " $bb_am_menu_title " \
527          --cancel-label $bb_return \
528          --menu "$bb_menu_input" 0 0 0 \
529        "1)" "$bb_am_menu_1" \
530        "2)" "$bb_am_menu_2" \
531        "3)" "$bb_am_menu_3" \
532        "4)" "$bb_am_menu_4" \
533        "5)" "$bb_am_menu_5" \
534        "6)" "$bb_am_menu_6" \
535        "7)" "$bb_am_menu_7" \
536        "8)" "$bb_am_menu_8" \
537        "9)" "$bb_am_menu_9" \
538        "10)" "$bb_am_menu_10" \
539        "11)" "$bb_am_menu_11" 2> ${TMPFILE}
540
541STDOUT=$?       # Return status
542EventButtons
543ReadAction
544 case $action in
545     1\))  # Burn Audio from Mp3s
546	 check_for_mp3s
547	 check_for_oggs
548	 check_for_flacs
549	 ${BBROOTDIR}/burning/burning.sh --audio
550	 ;;
551     2\)) # Burn Audio Directly
552	 ${BBROOTDIR}/burning/burning.sh --pipeline
553	 ;;
554     3\))
555	 copy_audio_cd
556	 ;;
557     4\))
558	 copy_cd_to_hd
559	 ;;
560     5\))	# Burn a xmms playlist
561	 if eval ${BBROOTDIR}/misc/xmmsread.sh; then
562	     ${BBROOTDIR}/burning/burning.sh --audio
563	 else
564	     echo $bb_am_ch5
565	     wait_for_enter
566	 fi
567	 ;;
568     6\))
569	 create_mp3s_from_wavs
570	 ;;
571     7\))
572	 create_oggs_from_wavs
573	 ;;
574     8\))
575	 create_flacs_from_wavs
576	 ;;
577     9\))
578	 create_mp3s_from_cd
579	 ;;
580     10\))
581	 create_oggs_from_cd
582	 ;;
583     11\))
584	 create_flacs_from_cd
585	 ;;
586 esac
587done
588
589# vim: set ft=sh nowrap nu foldmethod=marker:
590