Skip to content

Instantly share code, notes, and snippets.

@sym3tri
Last active January 21, 2021 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sym3tri/636befdaae22f0c4b1f7f4b448dad113 to your computer and use it in GitHub Desktop.
Save sym3tri/636befdaae22f0c4b1f7f4b448dad113 to your computer and use it in GitHub Desktop.
A simple script to convert a directory of audio files into MakeNoise Morphagene compatible format.

mg-convert

This simple script converts all audio files in a provided directory into MakeNoise Morphagene compatible files (48KHz, floating-point, 32-bit, stereo WAV). It auotmatically names the files in the proper format too.

Requirements

You must be comfortable with the comand-line, and have sox installed.

Copy this script to somewhere in your PATH, and make it executable (chmod +x mg-convert).

Example

ls /some/direcotry/of/sounds

├── foo.wav
├── bar.wav
└── baz.wav
mg-convert /some/directory/of/sounds

will produce:

├── foo.wav
├── bar.wav
├── baz.wav
└── converted
    ├── mg1.wav
    ├── mg2.wav
    └── mg3.wav

Everything in the converted subdirectory will be in Morphagene compatible format. All original input files are preserved. Subsequent runs will override anything in the converted directory.

NOTE

I've only tried this with a handful of input file formats, but in theory should work with any file format sox can process.

Use at your own risk, I'm not able to provide support for this.

#!/bin/bash
set -e
INPUTDIR=$(pwd)
if [ "$#" -eq 1 ]; then
INPUTDIR=$1
if [ ! -d $INPUTDIR ];then
echo "The directory you provided does not exist."
exit 1
fi
fi
echo -e "Processing files in: $INPUTDIR\n"
mkdir -p $INPUTDIR/converted
names="123456789abcdefghijklmnopqrstuvw"
function suffix() {
echo ${names:$1:1}
}
COUNTER=0
for filename in $INPUTDIR/*.wav; do
if [ "$COUNTER" -eq 32 ]; then
echo "WARNING: Too many files. Morphagene's max is 32. Exiting without processing the rest."
exit 0
fi
echo -e "Converting: $filename...\n"
outfile=mg$(suffix $COUNTER).wav
# this is where the magic happens
# somehow couldn't get ffmpeg to work :(
# ffmpeg -i "$filename" -ar 48000 -c:a pcm_f32le -ac 2 -y "$INPUTDIR/converted/$outfile"
# VLC worked, but can't normalize during conversion :(
# eval /Applications/VLC.app/Contents/MacOS/VLC -I dummy -vvv \""$filename"\" --audio-filter normvol --norm-max-level 5 --sout \''#transcode{vcodec=none,acodec=fl33,ab=128,channels=2,samplerate=48000,scodec=none,soverlay}:standard{mux=wav,access=file{no-overwrite},dst='"$INPUTDIR/converted/$outfile"'}'\' vlc://quit
# sox FTW!
sox --norm "$filename" -c 2 -r 48k -e float -b 32 "$INPUTDIR/converted/$outfile"
echo -e "\nDone!\n\n"
let COUNTER=COUNTER+1
done
echo "All converted files are located in: $INPUTDIR/converted"
@capital-G
Copy link

capital-G commented Oct 19, 2020

Thanks for the script!
It seems that ffmpeg does not work b/c the channel routing seems to be stereo rather than 2 channel (who knew that there seems to be a difference)

A working file from sox/reaper looks like this w/ ffprobe

Stream #0:0: Audio: pcm_f32le ([3][0][0][0] / 0x0003), 48000 Hz, 2 channels, flt, 3072 kb/s

A non-working file from ffmpeg looks like this

Stream #0:0: Audio: pcm_f32le ([3][0][0][0] / 0x0003), 48000 Hz, stereo, flt, 3072 kb/s

One addition: You could change

sox --norm "$filename" -c 2 -r 48k -e float -b 32 "$INPUTDIR/converted/$outfile"

to

sox --norm "$filename" -c 2 -r 48k -e float -b 32 "$INPUTDIR/converted/$outfile" trim 0 02:54

so the files get automatically trimmed if they exceed the maximum size.

@sym3tri
Copy link
Author

sym3tri commented Jan 21, 2021

@capital-G Thanks so much. That really helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment