/
/lightswitchmidi
1 #!/usr/bin/python
2
3 """pyalsamidi at http://infohost.nmt.edu/~dbaird/software/
4 --> depends on ubuntu pkg libasound2-dev
5
6 n3->rdf convertor at http://www.w3.org/DesignIssues/Notation3.html
7
8 almost related: http://lists.w3.org/Archives/Public/www-archive/2001Nov/0033
9
10 cd soundfont
11 fluidsynth
12 prog 0 86
13
14 alsa-patch-bay
15
16 more ubuntu pkg needed:
17 python2.4-alsaaudio
18 python2.4-librdf
19
20
21
22 """
23
24 import os, select, time, xmlrpclib, socket
25 from twisted.internet import reactor, tksupport, defer
26 from pyalsamidi import pyalsamidi as amidi
27 from logsetup import commonlogsetup
28 import RDF
29 from rdfaction import RoomAction
30
31 log = commonlogsetup(filename=None)
32
33 class NoteCode:
34 def __init__(self):
35 self.phrase = []
36 self.last_note_time = 0
37 self.max_pause_time = .5 # sec
38
39 self.room_action = RoomAction()
40
41 #self.action([60,65]) # tester
42
43 def event(self,ev):
44 if ev.data.__class__ != amidi.MidiEvent.NoteOn:
45 return
46
47 now = time.time()
48 if now - self.last_note_time > self.max_pause_time:
49 self.phrase[:] = []
50
51 self.last_note_time = now
52 self.phrase.append(ev.data.note)
53
54 try:
55 matched = self.action(self.phrase)
56 if matched:
57 self.phrase[:] = []
58 except Exception, e:
59 if isinstance(e, (KeyboardInterrupt, SystemExit)):
60 raise
61 log.error(e)
62
63
64 def action(self, phrase):
65 phrase_literal = RDF.Node(" ".join(map(str, phrase)))
66 phrase_quoted_literal = '"%s"' % phrase_literal #?
67 log.info("checking phrase %s" % phrase_quoted_literal)
68
69 return self.room_action.fire(
70 "midi:notes",#"<http:/projects.bigasterisk.com/midi/notes>",
71 phrase_quoted_literal)
72
73 seq = amidi.Sequencer('Test Sequencer')
74 inp = seq.create_midiin('Test Midi In')
75
76 outp = seq.create_midiout('Test Midi Out')
77 queue = seq.create_queue()
78 ppq = 120
79 queue.set_bpm(120, ppq)
80 queue.start()
81
82 # If you do not have a musical keyboard connected to 64:0, you might
83 # need to modify this line:
84 os.system('aconnect -t %d 64:0 %d:%d' % (queue.id, seq.id, inp.id))
85 os.system('aconnect -t %d %d:%d 64:0' % (queue.id, seq.id, outp.id))
86
87 nc = NoteCode()
88
89 while 1:
90 a, _, _ = select.select([inp], [], [], .1)
91 while (inp in a) or inp.events_pending():
92 a = []
93 ev = inp.read_event()
94 nc.event(ev)