#!/usr/bin/env python
#
# MIDI controled playout system for jingles
# (c) 2004 <j@reboot.fm>
#
# this need to be running:
# alsaplayer -s jingle -o text 2>&1 > /dev/null &
# or use init.d script to start,stop
#
# using ap-control, it needs to be called like this
# (latest verion in svn.reboot.fm needed):
# ap-control -s jingle add_and_play audiofile
#
# you need PyMIDI from http://www.hyperreal.org/~est/python/MIDI/

import sys
import os
from os.path import abspath,exists,getsize,join,isdir,basename,split
import MIDI

import jingles_mapping

def play_jingle(audiofile):
	cmd="ap-control -s jingle add_and_play %s" % audiofile
	os.system(cmd)

def sysex(bs):
	print 'sysex', map(ord, bs)

def jingles_foo(msg, *args): 
	if msg=="note-on" and args[2]!=0 and jingles_mapping.jingles.has_key(args[1]):
		print join(jingles_mapping.basedir,jingles_mapping.jingles[args[1]])
		play_jingle(join(jingles_mapping.basedir,jingles_mapping.jingles[args[1]]))

m = MIDI.In(sysex)
m[...] = jingles_foo

if __name__ == "__main__":
	mididev=open("/dev/midi")
	while 1:
		bs = mididev.read(1)
		if not bs: break
		m.inbytes(bs)
	mididev.close()
