Script for using Timidity with moc

Forums:

Hi,
I haven't managed to add midi support to MOC so I made a bash script that converts midi to wav (with Timidity) and adds them to /tmp/timiditym before automatically adding them to MOC's playlist.
The advantage over a simple integration of MIDI in MOC is that you can specify any timidity options you like (config file, reverb,...).
For instance, I have a directory full of Bach organ pieces specifically made for a specialized organ soundfont so I can simply add the right options to the timidity command.

Here is the script if any of you are interested:

#!/bin/bash
#timiditym.bash
#This script comes with no warranty of any kind.
#Feel free to share it as you like.
#read *.mid's with moc
#
#timidity -rm : remove all temp files
#
#timidity TIMIDITY_OPTIONS : makes wav files in /tmp/timiditym and adds
# them to moc playlist

mids=''
opts=''

if [ "$1" = '-rm' ]; then
rm -Rv /tmp/timiditym/
shift
fi

for i in "$@"; do
if ( echo "$i" | grep '\.[mM][iI][dD]$' ); then
mids=$(echo -e "$mids\n$i")
else
opts+=" $i"
fi
done

mkdir /tmp/timiditym

timiditycmd="timidity$opts"

oldIFS="$IFS"
IFS='
'

for i in $(echo "$mids"); do
of="/tmp/timiditym/$(basename "$i").wav";
eval "nice -n 5 $timiditycmd \"$i\" -Ow -o \"$of\""
mocp -a "$of";
done

IFS="$oldIFS"

I'm just wondering:
I guess you know that there is a (lib)timidity plugin for MOC and that you can specify a configuration file for it in MOCs options. You also can specifiy MOCs configuration file to run multiple / different instances (and by this, for the plugin).
Options on a per file/directory basis should also be easy to merge into the plugin I think.

As I wrote that plugin I would like to know what 'turns people off' when using it. :-)

Well, I never managed to install it. Even though at compile time it says timidity is available, MIDI files never show up in MOC.
Do you have to do something special to get it working?

EDIT: Ok, I managed to install it by installing libtimidity from source. But for some weird reason it looks for timidity.cfg in /etc/ instead of /etc/timidity (I managed to figure that out with 'strace') causing MOC to crash on startup. Ok, so I 'ln -s' /etc/timidty/timidity.cfg in /etc/ but now MIDI files show up in MOC and play (the elapsed time bar scrolls and everything) but don't make any sound. (The other audio files work)

I'd be glad to use your plugin if you or anyone else has any ideas how I can make it work.

Okay, thats really wierd.
Looking for etc/timidity.cfg is the default behaviour of libtimidity - I would expect package maintainers to change this to meet their distro but that is no help when compiling from source (on my system it is in /etc/timidity.cfg).

The bad thing is, that the plugin cannot ask libtimidity where is is searching for as default and cannot check if the file exists...

But there are still the options you can set in MOCs config (listed with their default):

TiMidity_Volume = 100
TiMidity_Rate = 44100
TiMidity_Bits = 16
TiMidity_Channels = 2
TiMidity_Config = NULL

So instead of symlinking you can also specify the path in TiMidity_Config.

I have no idea, why there is no sound when there is progress. My only guess is, that libtimidty messes with the patches for timidity (I had a similar issue, when I forgot to add patches to my soundcard for hardware-MIDI playback (no timidity)).

What system are you using ? I'll setup a VM and see if I can reproduce.

Maybe you could try a different set of patches and see if the problem remains.

If you really want to know what system I'm using it's:
Ubuntu 9.04 (Jaunty) AMD64.
I tried it with Alsa and with Jack but I still get the same result.

Okay, I just finished setting up Ubuntu 9.04 (Desktop) AMD 64 inside a VirtualBox session.

Now I know again why I do not use Ubuntu - but that would bring us off track...

The bad news is that it works perfectly for me here...

I just installed timidity which pulled in the freepats patches. Then I compiled and installed libtimidity system-wide as root.
After pulling in some other libs (sndfile, modplug, avformat...) I configured, compiled and installed moc-2.5.0-alpha4 (download from here) and set the TiMidity_Config option to /etc/timidity/timidity.cfg

Testing was done with MIDIs from the game Descent 2 (that can be easily found on the internet if you want to reproduce this).

Are you using any special patch set ?

edit:
To prevent possible misinterpretation: I wanted to say that the MIDI files can be found easily not the game...

Thanks a lot for trying to help. I managed to make it work by restoring the default soudfont (freepats). It works but I would like to use another soundfont in sf2 format. Is that possible?

(Why don't you use Ubuntu then? I would like to know because I'm open-minded to other distributions and am ready to try an alternative. Which one do you use and what's so good about it?)

I just checked with the libtimidity library: it only supports GUS-style patch-sets (freepats and some others).

This is quite annoying although I personally have only encountered patch-sets in this format.

Given that TiMidity++ seems to support other formats (and libtimidity is a fork of an old timidity version) it *could* be easy to port this support into libtimidty. I will try to find out more on this.

Another alternative I would even more like is to directly use timidity to generate WAV data - I tried this when I started the MIDI plugin but it did not work out that time. Maybe it's time to look at it again.

My soundcard (old SBLive!) supports soundfonts (SF2) in hardware and I have a set to test with for TiMidity++ - I'll see for far this goes. What soundfont are you using ? Something commercial of could I also use it for testing ?

The good side of this is that I could finally add XMI support to the plugin - something I wanted to do for a while... :-)

And now for the ultimate question: What is the best linux distribution ? :-)
In short - I do not believe that there exists a single distribution that is best for all users. Everyone needs to find the one that fits best.
But here comes my speech: :-)

There are many reasons why I do not use Ubuntu but the most prominent of them is that I like to keep control. Ubuntu tries to do everything for the user and thinks it is smart in doing so. I had a fight with automatic network setups that reverted my changes to communicate with some special other network and other setup-related-rivalries between me and the system.
The second-rated reason is 'sudo su'. No distribution should allow that kind of BS. It is just stupid... (convenient - yes - like the dark side of the force but not the right thing to teach linux newcomers).
I personally believe that Linux does not have to mimic Windows features and I also don't believe in the mouse. I really like the concept of shortcuts and terminal interaction (I use X but I do not use a 'desktop' to drop files). I think control & configuration are the concepts people should embrace when using linux - but these things are hard to teach.
So the distribution I like most is Gentoo as I see many of my believes shared in their concepts. But I would never tell a novice linux user to try that distribution because of its complexity. Setting up a Gentoo system might seem intimidating at first and applications are not installed but compiled for your system (so you need some patience). I think that is why there is the 'Slogan': 'Gentoo - Linux for human beings - with too much time on their hands' (A direct pun on the Ubuntu slogan and Gentoos time consuming compilation process). The nice thing about Gentoo is that you can finetune each application to match your needs. This affects required dependencies a lot (e.g. some packages can be compiled with or without KDE support - that makes a huge difference in requirements). There are also some nice features to switch between different GCC, Java, Python, etc. versions installed on your system to allow very much flexibility to the user (that might or might not give you a warm feeling ;-) ).
For an experienced linux-user Gentoo can be a fresh experience but you should have no fear when you need to boot from a rescue system to fix your broken boot process ;-)

My main soundfont is a 250MB free soundfont called Titanic. You can find it here: http://instrumentsvirtuels.free.fr/freeware6.htm#titanic (in 4 parts)
I also use a harpsichord soundfont (http://sonimusicae.free.fr/blanchet1-en.html) and an organ soundfont (http://www.hammersound.com/cgi-bin/soundlink.pl?action=view_download_page;ID=284;SoundFont_Location_Selected=Download%20USA;SoundFont_Filename_Selected=jeux14.rar).

Here are my Timidity configuration files: http://mxpgrm.free.fr/timidity/

(As I'm on holiday, I think I'll try Gentoo. I've been wanting to try it for some time now.)

Just to keep some information coming (and I'm replying to myself to make this entry a bit more readable - I think):
I had a good look at TiMidity++ in it's current state and at the libtimidity library.
As far as I can see, it makes a lot more sense to realize MIDI playback directly through TiMidity as there is still development (libtimidity seems to be abandoned some time now). But there is no real API to work with...

So I will try the alternative route and create a new plugin for MIDI playback.
It will start a TiMidity process and feed MIDI data into it while receiving raw wave data via a fifo or pipe - this is not very portable but all environments supporting MOC will be able to support this (e.g. we are getting further away from a windows port - I don't really mind).
Seeking is a problem as TiMidity just cannot do it (AFAIK). Currently I plan to enable seeking by rebuilding the MIDI data to start at a specific position. I just have to take care at this step to get all controller changes. Seeking will be the hard part of writing the new plugin but it could be fun in some way.

As always, I'll do this in free time so I'm not sure when it will be working. But I have a long trip on an airplane next week so there is a good chance to get things started.