Created
February 15, 2023 00:40
-
-
Save aynik/2c68fec2748c6bfa169d175cf51c5b4c to your computer and use it in GitHub Desktop.
Crossfade two audio tracks using sox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
fade_length="$1" | |
first_file="$2" | |
second_file="$3" | |
fade_first="auto" | |
if [ "$4" != "" ]; then | |
fade_first="$4" | |
fi | |
fade_second="auto" | |
if [ "$5" != "" ]; then | |
fade_second="$5" | |
fi | |
fade_first_opts= | |
if [ "$fade_first" != "no" ]; then | |
fade_first_opts="fade t 0 0:0:$fade_length 0:0:$fade_length" | |
fi | |
fade_second_opts= | |
if [ "$fade_second" != "no" ]; then | |
fade_second_opts="fade t 0:0:$fade_length" | |
fi | |
first_length=`sox --info -D "$first_file"` | |
trim_length=`echo "$first_length - $fade_length" | bc` | |
crossfade_split_length=`echo "scale=2; $fade_length / 2.0" | bc` | |
fadeout1=$(mktemp).wav | |
fadeout2=$(mktemp).wav | |
fadein1=$(mktemp).wav | |
fadein2=$(mktemp).wav | |
crossfade=$(mktemp).wav | |
crossfade1=$(mktemp).wav | |
crossfade2=$(mktemp).wav | |
song1=$(mktemp).wav | |
song2=$(mktemp).wav | |
sox -D -G "$first_file" -e signed-integer -b 16 "$fadeout1" trim $trim_length $fade_first_opts | |
if [ "$fade_first" == "auto" ]; then | |
RMS=`sox "$fadeout1" 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1` | |
should_fade=`echo "$RMS > 0.1" | bc` | |
if [ $should_fade == 0 ]; then | |
fade_first_opts="" | |
fi | |
fi | |
sox "$fadeout1" "$fadeout2" $fade_first_opts | |
sox -D -G "$second_file" -e signed-integer -b 16 "$fadein1" trim 0 $fade_length | |
if [ "$fade_second" == "auto" ]; then | |
RMS=`sox "$fadein1" 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1` | |
should_fade=`echo "$RMS > 0.1" | bc` | |
if [ $should_fade == 0 ]; then | |
fade_second_opts="" | |
fi | |
fi | |
sox "$fadein1" "$fadein2" $fade_second_opts | |
sox -m -D -G -v 1.0 "$fadeout2" -v 1.0 "$fadein2" "$crossfade" | |
sox "$crossfade" "$crossfade1" trim 0 $crossfade_split_length | |
sox "$crossfade" "$crossfade2" trim $crossfade_split_length | |
sox -D -G "$first_file" -e signed-integer -b 16 "$song1" trim 0 $trim_length | |
sox -D -G "$second_file" -e signed-integer -b 16 "$song2" trim $fade_length | |
sox "$song1" "$crossfade1" "$first_file" | |
sox "$crossfade2" "$song2" "$second_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment