Updating osxbuild to work with OS X 10.7+/XCode 4.x.
[sox.git] / scripts / crossfade.sh
blob8444219285e7cc4433f6399597733c586c318d47
1 #!/bin/bash
3 # crossfade.sh
5 # Creates two new files that when played together have a crossfade
6 # of $1 seconds. This means that $1/2 of the crossfade is in
7 # the 1st file and $1/2 in the second file. Can be used to
8 # make audio CD's with a 0 second pregap between songs to
9 # have one song flow into the next; yet still be able to skip
10 # around the CD with minimal loss of song.
12 # Filenames are specified as $2 and $3.
14 # $4 is optional and specifies if a fadeout should be performed on
15 # first file. Can be yes/no/auto.
16 # $5 is optional and specifies if a fadein should be performed on
17 # second file. Can be yes/no/auto
19 # Example: $0 10 infile1.wav infile2.wav auto auto
21 # By default, the script attempts to guess if the audio files
22 # already have a fadein/out on them or if they just have really
23 # low volumes that won't cause clipping when mixxing. If this
24 # is not detected then the script will perform a fade in/out to
25 # prevent clipping.
27 # The user may specify "yes" or "no" to force the fade in/out
28 # to occur. They can also specify "auto" which is the default.
30 # Crossfaded file $2 is prepended with cfo_ and $3 is prepended
31 # with cfi_.
33 # Original script from Kester Clegg. Mods by Chris Bagwell to show
34 # more examples of sox features.
37 SOX=../src/sox
39 if [ "$3" == "" ]; then
40 echo "Usage: $0 crossfade_seconds first_file second_file [ fadeout ] [ fadein ]"
41 echo
42 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."
43 echo
44 echo "Example: $0 10 infile1.wav infile2.wav auto auto"
45 exit 1
48 fade_length=$1
49 first_file=$2
50 second_file=$3
52 fade_first="auto"
53 if [ "$4" != "" ]; then
54 fade_first=$4
57 fade_second="auto"
58 if [ "$5" != "" ]; then
59 fade_second=$5
62 fade_first_opts=
63 if [ "$fade_first" != "no" ]; then
64 fade_first_opts="fade t 0 0:0:$fade_length 0:0:$fade_length"
67 fade_second_opts=
68 if [ "$fade_second" != "no" ]; then
69 fade_second_opts="fade t 0:0:$fade_length"
72 echo "crossfade and concatenate files"
73 echo
74 echo "Finding length of $first_file..."
75 first_length=`$SOX "$first_file" 2>&1 -n stat | grep Length | cut -d : -f 2 | cut -f 1`
76 echo "Length is $first_length seconds"
78 trim_length=`echo "$first_length - $fade_length" | bc`
79 crossfade_split_length=`echo "scale=2; $fade_length / 2.0" | bc`
81 # Get crossfade section from first file and optionally do the fade out
82 echo "Obtaining $fade_length seconds of fade out portion from $first_file..."
83 $SOX "$first_file" -s -b 16 fadeout1.wav trim $trim_length $fade_first_opts
85 # When user specifies "auto" try to guess if a fadeout is needed.
86 # "RMS amplitude" from the stat effect is effectively an average
87 # value of samples for the whole fade length file. If it seems
88 # quite then assume a fadeout has already been done. An RMS value
89 # of 0.1 was just obtained from trail and error.
90 if [ "$fade_first" == "auto" ]; then
91 RMS=`$SOX fadeout1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
92 should_fade=`echo "$RMS > 0.1" | bc`
93 if [ $should_fade == 0 ]; then
94 echo "Auto mode decided not to fadeout with RMS of $RMS"
95 fade_first_opts=""
96 else
97 echo "Auto mode will fadeout"
101 $SOX fadeout1.wav fadeout2.wav $fade_first_opts
103 # Get the crossfade section from the second file and optionally do the fade in
104 echo "Obtaining $fade_length seconds of fade in portion from $second_file..."
105 $SOX "$second_file" -s -b 16 fadein1.wav trim 0 $fade_length
107 # For auto, do similar thing as for fadeout.
108 if [ "$fade_second" == "auto" ]; then
109 RMS=`$SOX fadein1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
110 should_fade=`echo "$RMS > 0.1" | bc`
111 if [ $should_fade == 0 ]; then
112 echo "Auto mode decided not to fadein with RMS of $RMS"
113 fade_second_opts=""
114 else
115 echo "Auto mode will fadein"
119 $SOX fadein1.wav fadein2.wav $fade_second_opts
121 # Mix the crossfade files together at full volume
122 echo "Crossfading..."
123 $SOX -m -v 1.0 fadeout2.wav -v 1.0 fadein2.wav crossfade.wav
125 echo "Spliting crossfade into $crossfade_split_length lengths"
126 $SOX crossfade.wav crossfade1.wav trim 0 $crossfade_split_length
127 $SOX crossfade.wav crossfade2.wav trim $crossfade_split_length
129 echo "Trimming off crossfade sections from original files..."
131 $SOX "$first_file" -s -b 16 song1.wav trim 0 $trim_length
132 $SOX "$second_file" -s -b 16 song2.wav trim $fade_length
134 echo "Creating crossfade files"
135 $SOX song1.wav crossfade1.wav "cfo_${first_file}.wav"
136 $SOX crossfade2.wav song2.wav "cfi_${second_file}.wav"
138 echo -e "Removing temporary files...\n"
139 rm fadeout1.wav fadeout2.wav fadein1.wav fadein2.wav crossfade.wav crossfade1.wav crossfade2.wav song1.wav song2.wav
140 mins=`echo "$trim_length / 60" | bc`
141 secs=`echo "$trim_length % 60" | bc`
142 echo "The crossfade occurs at around $mins mins $secs secs in $first_file"