5 # Concatenates two files together with a crossfade of $1 seconds.
6 # Filenames are specified as $2 and $3.
8 # $4 is optional and specifies if a fadeout should be performed on
10 # $5 is optional and specifies if a fadein should be performed on
13 # Example: crossfade_cat.sh 10 infile1.wav infile2.wav auto auto
15 # By default, the script attempts to guess if the audio files
16 # already have a fadein/out on them or if they just have really
17 # low volumes that won't cause clipping when mixxing. If this
18 # is not detected then the script will perform a fade in/out to
21 # The user may specify "yes" or "no" to force the fade in/out
22 # to occur. They can also specify "auto" which is the default.
24 # Crossfaded file is created as "mix.wav".
26 # Original script from Kester Clegg. Mods by Chris Bagwell to show
27 # more examples of sox features.
33 if [ "$3" == "" ]; then
34 echo "Usage: $0 crossfade_seconds first_file second_file [ fadeout ] [ fadein ]"
36 echo "If a fadeout or fadein is not desired then specify \"no\" for that option. \"yes\" will force a fade and \"auto\" will try to detect if a fade should occur."
38 echo "Example: $0 10 infile1.wav infile2.wav auto auto"
47 if [ "$4" != "" ]; then
52 if [ "$5" != "" ]; then
57 if [ "$fade_first" != "no" ]; then
58 fade_first_opts
="fade t 0 0:0:$fade_length 0:0:$fade_length"
62 if [ "$fade_second" != "no" ]; then
63 fade_second_opts
="fade t 0:0:$fade_length"
66 echo "crossfade and concatenate files"
68 echo "Finding length of $first_file..."
69 first_length
=`$SOX --info -D "$first_file"`
70 echo "Length is $first_length seconds"
72 trim_length
=`echo "$first_length - $fade_length" | bc`
74 # Get crossfade section from first file and optionally do the fade out
75 echo "Obtaining $fade_length seconds of fade out portion from $first_file..."
76 $SOX "$first_file" -e signed-integer
-b 16 fadeout1.wav trim
$trim_length
78 # When user specifies "auto" try to guess if a fadeout is needed.
79 # "RMS amplitude" from the stat effect is effectively an average
80 # value of samples for the whole fade length file. If it seems
81 # quiet then assume a fadeout has already been done. An RMS value
82 # of 0.1 was just obtained from trial and error.
83 if [ "$fade_first" == "auto" ]; then
84 RMS
=`$SOX fadeout1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
85 should_fade
=`echo "$RMS > 0.1" | bc`
86 if [ $should_fade == 0 ]; then
87 echo "Auto mode decided not to fadeout with RMS of $RMS"
90 echo "Auto mode will fadeout"
94 $SOX fadeout1.wav fadeout2.wav
$fade_first_opts
96 # Get the crossfade section from the second file and optionally do the fade in
97 echo "Obtaining $fade_length seconds of fade in portion from $second_file..."
98 $SOX "$second_file" -e signed-integer
-b 16 fadein1.wav trim
0 $fade_length
100 # For auto, do similar thing as for fadeout.
101 if [ "$fade_second" == "auto" ]; then
102 RMS
=`$SOX fadein1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
103 should_fade
=`echo "$RMS > 0.1" | bc`
104 if [ $should_fade == 0 ]; then
105 echo "Auto mode decided not to fadein with RMS of $RMS"
108 echo "Auto mode will fadein"
112 $SOX fadein1.wav fadein2.wav
$fade_second_opts
114 # Mix the crossfade files together at full volume
115 echo "Crossfading..."
116 $SOX -m -v 1.0 fadeout2.wav
-v 1.0 fadein2.wav crossfade.wav
118 echo "Trimming off crossfade sections from original files..."
120 $SOX "$first_file" -e signed-integer
-b 16 song1.wav trim
0 $trim_length
121 $SOX "$second_file" -e signed-integer
-b 16 song2.wav trim
$fade_length
122 $SOX song1.wav crossfade.wav song2.wav mix.wav
124 echo -e "Removing temporary files...\n"
125 rm fadeout1.wav fadeout2.wav fadein1.wav fadein2.wav crossfade.wav song1.wav song2.wav
126 mins
=`echo "$trim_length / 60" | bc`
127 secs
=`echo "$trim_length % 60" | bc`
128 echo "The crossfade in mix.wav occurs at around $mins mins $secs secs"