log++ qq bugfixes.
[minwii.git] / src / controllers / Wiimote.py
1 '''
2 Created on 15 juil. 2009
3
4 @author: Samuel Benveniste
5 '''
6 from gui.constants import *
7 import fluidsynth
8
9 class Wiimote:
10 '''
11 Object representing a Wiimote
12
13 number:
14 The number of the Wiimote
15 port:
16 The pypm object representing the MIDI port on which the Wiimote emits
17 instrument:
18 The instrument associated with the Wiimote
19 cursor:
20 The cursor associated with the Wiimote
21 '''
22
23 def __init__(self, number, portNumber, port, instrument, cursor):
24 '''
25 Constructor
26
27 number:
28 The number of the Wiimote
29 portNumber:
30 The number of the port (as numbered by pypm) on which the wiimote emits
31 port:
32 The pypm object representing the MIDI port on which the Wiimote emits
33 instrument:
34 The instrument associated with the Wiimote
35 cursor:
36 The cursor associated with the Wiimote
37 '''
38 self.number = number
39 self.portNumber = portNumber
40 self.port = port
41 self.instrument = instrument
42 self.cursor = cursor
43 self.numberPlayed = 0
44
45 self.fs = fs = fluidsynth.Synth()
46 fs.start()
47 soundFont = r"C:\Documents and Settings\Administrator\My Documents\Telechargements\fluid-soundfont-3.1\FluidR3_GM.sf2"
48 self.fsid = fsid = fs.sfload(soundFont)
49 bank = 0
50 preset = 0
51 fs.program_select(0, fsid, bank, preset)
52
53 def __del__(self) :
54 self.fs.delete()
55
56 def getNoteOnHexCode(self):
57 return (0x90 + self.instrument.channel - 1)
58
59 def getAftertouchHexCode(self):
60 return (0xA0 + self.instrument.channel - 1)
61
62 def getCCHexCode(self):
63 return (0xB0 + self.instrument.channel - 1)
64
65 # def playNote(self, note, velocity):
66 # print 'playNote'
67 # noteNumber = self.instrument.getNote(note)
68 #
69 # if noteNumber != None :
70 # noteOnHexCode = self.getNoteOnHexCode()
71 # CCHexCode = self.getCCHexCode()
72 # else :
73 # noteNumber = defaultInstrumentNote
74 # noteOnHexCode = defaultNoteOnHexCode
75 # CCHexCode = defaultCCHexCode
76 #
77 # self.port.write_short(noteOnHexCode, noteNumber , 127)
78 # self.port.write_short(CCHexCode, 07, velocity)
79
80 def playNoteByNoteNumber(self, midiNoteNumber, velocity):
81 print 'playNoteByNumber'
82 noteNumber = self.instrument.getNoteByNoteNumber(midiNoteNumber)
83 fs = self.fs
84 fs.noteon(0, noteNumber, velocity)
85
86 return
87 noteNumber = self.instrument.getNoteByNoteNumber(midiNoteNumber)
88
89 if noteNumber != None :
90 noteOnHexCode = self.getNoteOnHexCode()
91 CCHexCode = self.getCCHexCode()
92 else :
93 noteNumber = defaultInstrumentNote
94 noteOnHexCode = defaultNoteOnHexCode
95 CCHexCode = defaultCCHexCode
96
97 self.port.write_short(noteOnHexCode, noteNumber , 127)
98 self.port.write_short(CCHexCode, 07, velocity)
99
100 self.numberPlayed += 1
101
102 # def stopNote(self, note):
103 # print 'stopNote'
104 # noteNumber = self.instrument.getNote(note)
105 # if noteNumber != None :
106 # noteOnHexCode = self.getNoteOnHexCode()
107 # else :
108 # noteNumber = defaultInstrumentNote
109 # noteOnHexCode = defaultNoteOnHexCode
110 #
111 # self.port.write_short(noteOnHexCode, noteNumber, 0)
112
113 def stopNoteByNoteNumber(self, midiNoteNumber):
114 print 'stopNoteByNoteNumber'
115
116 noteNumber = self.instrument.getNoteByNoteNumber(midiNoteNumber)
117 fs = self.fs
118 fs.noteoff(0, noteNumber)
119
120 return
121 noteNumber = self.instrument.getNoteByNoteNumber(midiNoteNumber)
122 if noteNumber != None :
123 noteOnHexCode = self.getNoteOnHexCode()
124 else :
125 noteNumber = defaultInstrumentNote
126 noteOnHexCode = defaultNoteOnHexCode
127
128 self.port.write_short(noteOnHexCode, noteNumber, 0)
129
130 # def allNotesOff(self):
131 # print 'allNoteOff'
132 # CCHexCode = self.getCCHexCode()
133 # self.port.write_short(CCHexCode,123,0)