Skip to content

Instantly share code, notes, and snippets.

@tinpark
Created October 15, 2018 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinpark/07f7d6a18eb62e0b05cbaa8030c71ca3 to your computer and use it in GitHub Desktop.
Save tinpark/07f7d6a18eb62e0b05cbaa8030c71ca3 to your computer and use it in GitHub Desktop.
Batch extract audio files from a single audio file based on segmentation using Ben Hackbarth's audioguide.
#!/bin/sh
# analyseExtractFade.sh or aef.sh for short
#
#
# Created by PARKER Martin on 11/10/2018.
#
# arguments are path of the audio file you want to segment, file format of the files you want to break things up into, fade parameters (fade in time fade out time and fade type and ultimately a text file from which to inject Metadata into the asstes you've generated.
# e.g. bash analyseExtractFade.sh ~/Dropbox/sounds/magicSound.wav aif 100s 560s q (this will analyse the audio file with agSegementSf.py and dump all of the slices it detects into a directory named magicSound. The slices will have fade ins of 100 samples fade outs of 560 samples using a quadratic fade function.
# It's useful to batch inject metadata into the files in this folder so you can do some further ace stuff if you have the command line version bwfmetaedit installed
agPath="/Applications/audioguide" #sets the path of audioguide software so you can use this script from anywhere, I'm keeping audioguide in my applications directory, you'll have to set this path up inside the script manually if your audioguide location is different.
sourceFile=$1 #we might need sourceFile later OK.
VAR=$1 # this creates a variable from the input argument
DIR=$(dirname "${VAR}") # this uses the terminal's ability to strip the directory name from the file supplied to the script
FILE=$(basename "${VAR}") # this uses the terminal's ability to strip the filename name from the file supplied to the script
SLICEPATH="${FILE%.*}_sliced" #Use this to make a folder name from the file name input, removing the file extension
python $agPath/agSegmentSf.py $sourceFile; #run agSegmentSf.py to generate a text file
cd "${DIR}" #navigate to the directory where the input file resides
mkdir $SLICEPATH; #make a folder to keep the slices in based on the file name input
index=0 #set a starting index of 0
while read line; do #read through the lines of the text file generated by audioguide, it's specified in the line below
if [ $index -gt 0 ]; then
sox $sourceFile ${DIR}/$SLICEPATH/${FILE%.*}_$index.$2 trim $start =$end #create the SoX call with file name based on slice number
# print to the console while rendering
# echo extracting $SLICEPATH/${1%.*}_$index.$2 $length ms long
fi
((index+=1))
start="$(awk '{print $1}' <<<$line)" #use awk to get the first float from the current line
end="$(awk '{print $2}' <<<$line)" #use awk to get the second float from the current line
length=$(bc <<< "$end - $start") #get the length of the extract if you want to make some fades with SoX
done < $sourceFile.txt
cd "${DIR}"/$SLICEPATH
mkdir faded # directory to place faded files in, prevents destructive conversion
for file in *.$2; do # for all the files of type specified in argument 2 in the directory
outfile="${file%.*}.$2" # create an output filename
FADE_IN_L=$3 #define the FadeIn time from arg 3
FADE_OUT_L=$4 #define the fadeOUt time from arg 4
# LENGTH=`soxi -d $file` # find out the length of the file using soxi (-d returns the duration), don't think I needed this in the end.
sox "$file" faded/"$outfile" fade $5 $FADE_IN_L -0 $FADE_OUT_L # run the sox command
echo writing faded files to faded/"$outfile" #tell the terminal what you're doing
done #finish up
# delete the slices files
rm "${DIR}"/$SLICEPATH/*.wav
# move the faded files folder up a level
ditto "${DIR}"/$SLICEPATH/faded/*.wav "${DIR}"/$SLICEPATH
#delete the directory called faded
rm -R "${DIR}"/$SLICEPATH/faded/
# move the text file made by audioguide to the folder you've created
mv $sourceFile.txt "${DIR}"/$SLICEPATH
# inject metatdata into extracted wav files using bwfmetaedit this is still todo, yawn.
# DATE=`date +%Y-%m-%d`
# TIME=`date +%H:%M:%S`
# YEAR=`date +%Y`
# echo bwfmetaedit --OriginationDate=$DATE --OriginationTime=$TIME --ICOP=$YEAR "${DIR}"/$SLICEPATH/*.wav # to do `awk '{print $0}' $6`
# bwfmetaedit --out-core="${DIR}"/$SLICEPATH/$SLICEPATH.csv
# open "${DIR}"/$SLICEPATH/$SLICEPATH.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment