My Music (Listen with quality headphones)

Tuesday, 26 March 2013

How to extract "metadata" from soundfont sf2 file using fluidsynth

This command will save the list of the instruments in a soundfont to a text file:
fluidsynth -i -f shell-commands.txt soundfont.sf2|grep ^000 > instruments.txt
fluidsynth -i -f shell-commands.txt soundfont.sf2 |grep "^[0-9]\{3\}" > instruments.txt

-i: start in non-interactive mode
-f: the file containing shell commands you want to send to fluidsynth
grep "^[0-9]\{3\}": only export the instruments lines of the output--lines beginning with 3 digits

The contents of shell-commands.txt:
inst 1

The contents of instruments.txt will look something like this:
000-000 Kick
000-001 Snare
000-002 Hihat
000-003 Crash

If you only want the actual instrument name text and not the numeric prefix, use this:
fluidsynth -i -f shell-commands.txt soundfont.sf2|grep ^000|cut -c 9- > out.txt
fluidsynth -i -f shell-commands.txt soundfont.sf2|grep "^[0-9]\{3\}"|cut -c 9- > out.txt

cut -c 9-: remove the first 9 characters of each output line.

So the resulting contents of instruments.txt would look something like this:

UPDATE 20130411: having "grep ^000" was a mistake because it was only outputting bank 0, whereas many soundfonts have more than 1 bank, so I changed the regex to grab any line beginning with 3 numbers: ^[0-9]{3}

Here's for-loop code to extract information from all sf2 files in a folder:
for f in *.[sS][fF]2; do echo "$f" >> out.txt;fluidsynth -a pulseaudio -i -f /path/to/shell-commands.txt "$f"|grep "^[0-9]\{3\}" >> instruments.txt; done

Thanks to this page for helping me figure out that I needed to escape the { and }, in the grep sentence, with a backslash:


Here are the important steps and commands I use to generate data for my sf2 soundfont list (google docs spreadsheet):
cd to the folder containing sf2 files

for f in *.[sS][fF]2; do echo "$f" >> out.txt;fluidsynth -a pulseaudio -i -f /path/to/music/lmms/sf2/shell-commands.txt "$f"|grep "^[0-9]\{3\}" >> out.txt; done

# formatting using regex
sed -r -i.bak s/\ +$//g out.txt
sed -r -i.bak s/^\([0-9]{3}\)\-\([0-9]{3}\)/\\1\\t\\2/g out.txt
sed -r -i.bak s/^\([0-9]{3}\\t[0-9]{3}\)\ /\\1\\t/g out.txt
sed -r -i.bak s/^\([0-9]{3}\\t[0-9]{3}\)/\\t\\1/g out.txt
sed -r -i.bak s/^\([^\\t]\)/\\n\\1/g out.txt

Open out.txt in geany
regex search:

regex replace:

Now the first instrument line is on the same line as the soundfont filename.

Copy the contents of out.txt and paste it into the spreadsheet.

No comments:

Post a Comment

My content is under the Creative Commons Attribution 3.0 license. I work hard to publish relevant and useful information in order to help people learn difficult things and find solutions to their problems, so please attribute me if you reuse my content elsewhere.