-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyCompIOPort.py
executable file
·393 lines (309 loc) · 11.8 KB
/
pyCompIOPort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
try:
import rtmidi
except:
print('Failed to import rtmidi')
try:
import csnd6
except:
print('Failed to import csnd6.')
import time
import EventScheduler
import datetime
import platform
INPUT = 0
OUTPUT = 1
defaultCSD = '''<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
;-odac0 ;; -iadc ;;;RT audio I/O
</CsOptions>
<CsInstruments>
; Initialize the global variables.
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
; Instrument #1.
instr 1
kamp = 31129.60
kfreq = 440
krat = 0.127236
kvibf = 6.12723
ifn = 1
; Create an amplitude envelope for the vibrato.
kenv linseg 0, 0.15, 1, p3-0.4, 1, 0.25, 0
kv linseg 0, 0.5, 0, 1, 1, p3-0.5, 1
kvamp = kv * 0.01
a1 oscili kamp*kenv, kfreq, 1
out a1 * 0.5
endin
</CsInstruments>
<CsScore>
; Table #1, a sine wave.
f 1 0 128 10 1
f 0 3600 ;'DUMMY' SCORE EVENT KEEPS REALTIME PERFORMANCE GOING FOR 1 HOUR
; Play Instrument #1 for two seconds.
;i 1 0 5
e
</CsScore>
</CsoundSynthesizer>'''
def ms(interval):
''' convert sec to ms '''
return EventScheduler.sec2ms(interval)
# //////////////////////////////////////////////////////////////////////////////
# ENUMS
class eMidiCommands:
NOTE_OFF = 0x80
NOTE_ON = 0x90
AFTER_TOUCH = 0xA0
CNTRL = 0xB0 # Continuous controller
PATCH = 0xC0 # Patch change
CHAN_PRESSURE = 0xD0 # Channel Pressure
PITCH_BEND = 0xE0 # Pitch bend
MISC_COMMAND = 0xF0 # (non-musical commands)
class ePortType:
""" Enum class that enumerates our output ports """
NONE = object()
MIDI = object()
OSC = object()
DAC = object()
FILE = object()
CSOUND = object()
class eEventParams:
'''Enum of note parameters'''
TIME = 0
DUR = 1
NOTE_PARAMS = 2
# //////////////////////////////////////////////////////////////////////////////
class NoteParams:
''' encapsulates our note parameters '''
def __init__(self, key, vel, chan):
self.key = key
self.vel = vel
self.chan = chan
def Print(self):
return '(key: {0}, vel: {1}, chan: {2})'.format(self.key, self.vel, self.chan)
# //////////////////////////////////////////////////////////////////////////////
class ControlParams:
""" encapsulates our control parameters """
def __init__(self):
self.ctrl = 32
self.data = 127
self.chan = 1
# //////////////////////////////////////////////////////////////////////////////
class MIDIEventQueue():
''' Encapsulation of list containing MIDI events '''
def __init__(self):
self.theQ = list()
def AddEvent(self, eventParams):
''' adds note to end of queue '''
self.theQ.append(eventParams)
def AddEventTimeSorted(self, eventParams):
''' adds note to queue at proper place in time'''
bFound = False
# go through and find where in time we should insert this note
noteParams = eventParams[eEventParams.NOTE_PARAMS]
noteTime = eventParams[eEventParams.TIME]
for ev in self.theQ:
curTime = ev[eEventParams.TIME]
if (curTime > noteTime):
self.theQ.insert(0, eventParams) # insert in front of
bFound = True
break;
# Insert an item at a given position. The first argument is the index of the
# element before which to insert, so a.insert(0, x) inserts at the front of the list
if (bFound == False):
self.theQ.append(eventParams) # just add to end
# self.DumpEvents()
def GetFirstEvent(self):
''' will return the next note that needs to be played '''
if (self.isEmpty()):
return None
else:
return self.theQ[eEventParams.TIME]
def RemoveEvent(self):
''' will remove the first note (oldest) note in the queue '''
self.theQ.pop(0)
def Flush(self):
''' remove all notes from queue '''
for i in range(self.theQ.count()):
self.theQ.pop(0)
def SortEvents(self):
self.theQ = sorted(self.theQ, key=lambda param: param[eEventParams.TIME]) # sort on time
# convert absolute duration of note to relative duration of event
for i in range(0, len(self.theQ) - 1):
event1 = self.theQ[i]
event2 = self.theQ[i + 1]
time1 = event1[eEventParams.TIME]
time2 = event2[eEventParams.TIME]
event1[eEventParams.DUR] = time2 - time1
def isEmpty(self):
if (len(self.theQ) == 0):
return True
else:
return False
def Count(self):
return len(self.theQ)
def DumpEvents(self):
for params in self.theQ:
time = params[eEventParams.TIME]
dur = params[eEventParams.DUR]
event = params[eEventParams.NOTE_PARAMS]
# print "The sum of 1 + 2 is {0}".format(1+2)
# print 'We are the {} who say "{}!"'.format('knights', 'Ni')
print('time: {0}, dur:{1}, event: {2}'.format(time, dur, event.Print()))
# print 'time: ' + time ' event: ' + event
def PrintEvent(self, event=[]):
liststr = ''
for value in event:
liststr += (value + " ")
return liststr
# //////////////////////////////////////////////////////////////////////////////
class IOPort():
""" Base class of our output port """
def __init__(self, portType):
if (property == ePortType.MIDI):
self = MIDIPort()
elif (property == ePortType.CSOUND):
self = CSoundPort()
else:
raise Exception('Unknown i/o port specified')
pass
def Open(self):
print("OutputPort:Open")
def Close(self):
print("OutputPort:Close")
# //////////////////////////////////////////////////////////////////////////////
class MIDIPort(IOPort):
""" Represents our MIDI Port: uses portmidi"""
def __init__(self):
self.currentMidiPort = 0 # should be MS MIDI Mapper
self.latency = 0 # used by pypm to specify MIDI latecny, does not seem to have any effect
self.midiOut = rtmidi.MidiOut()
self.bOpen = False
self.availablePorts = self.midiOut.get_ports()
self.startTime = datetime.datetime.now()
def __del__(self):
del self.midiOut
# opens MIDI ports with specified name
def OpenNamed(self, portName):
portNum = 0
cleanPortName = ""
self.startTime = datetime.datetime.now()
for systemPort in self.availablePorts:
cleanPortName = systemPort
# windows appends an integer on the end of the midi portname, we will remove it
if (platform.system() == 'Windows'):
cleanPortName = systemPort[:systemPort.rfind(' ')]
if cleanPortName == portName:
self.currentMidiPort = portNum
self.midiOut.open_port(portNum)
print("MIDIPort:Open " + systemPort)
self.bOpen = True
break
portNum += 1
if (self.bOpen == False):
raise Exception("Could not open specified MIDI port")
def Open(self, portNum=0):
self.startTime = datetime.datetime.now()
if (portNum >= len(self.availablePorts)):
print("Specified midi port is beyond physical range.")
return
self.currentMidiPort = portNum
self.midiOut.open_port(portNum)
print("MIDIPort:Open")
self.bOpen = True
def Close(self):
if (self.midiOut != None):
print("MIDIPort:Close")
self.bOpen = False
def PrintDevices(self, InOrOut):
for systemPort in self.availablePorts:
print(systemPort)
def SetPortID(self, portID):
self.currentMidiPort = portID
def NoteOut(self, params):
'''' this will send a note out our midi port'''
timestamp = self.GetTime()
note_message = [0x90, (int)(params.key), (int)(params.vel)] # channel 1, note, velocity
self.midiOut.send_message(note_message)
def CtrlOut(self):
pass
def GetTime(self):
deltaTime = datetime.datetime.now() - self.startTime
return deltaTime.total_seconds() * 1000
# //////////////////////////////////////////////////////////////////////////////
class MaxPort(IOPort):
""" Represents a port that will send data to max"""
def __init__(self):
self.currentMidiPort = -1 # not used in MaxPort
self.latency = 1000 # used by pypm to specify MIDI latecny, does not seem to have any effect
self.midiOut = None
self.pyext = None # if we are in a Max environment, this will be set to pyext.MaxPyExt()
def Open(self):
print("MaxPort:Open")
def Close(self):
print("MaxPort:Close")
def PrintDevices(self, InOrOut):
pass
def SetPortID(self, portID):
pass
def NoteOut(self, params):
'''' this will send a note out our midi port'''
# timestamp = self.midiOut.Time()
# print 'NoteOut: {0}, {1}'.format(timestamp, params.Print())
# self.midiOut.Write([[[0x90, params.key, params.vel], timestamp]]) # send note
# self.midiOut.WriteShort(0x90, (int)(params.key), (int)(params.vel))
noteStr = 'note {0} {1} {2}'.format(params.key, params.vel, params.chan)
# self._outlet(1, noteStr)
if self.pyext is not None:
self.pyext.NoteOut(noteStr)
else:
print('note: {0}'.format(noteStr))
def CtrlOut(self):
pass
def GetTime(self):
pass
# //////////////////////////////////////////////////////////////////////////////
class CSoundPort(IOPort):
""" Represents a port that will send data to max"""
def __init__(self):
self.currentMidiPort = -1 # not used in MaxPort
self.latency = 1000 # used by pypm to specify MIDI latecny, does not seem to have any effect
self.midiOut = None
self.pyext = None # if we are in a Max environment, this will be set to pyext.MaxPyExt()
self.csound = csnd6.Csound()
self.startTime = datetime.datetime.now()
self.performance = None
def Open(self, csdFile):
print("CSoundPort:Open")
self.startTime = datetime.datetime.now()
if (csdFile is None):
res = self.csound.CompileCsdText(defaultCSD)
else:
res = self.csound.Compile(csdFile)
self.performance = csnd6.CsoundPerformanceThread(self.csound)
self.csound.Start()
self.performance.Play()
def Close(self):
print("CSoundPort:Close")
self.performance.Stop()
self.performance.Join()
self.csound.Stop()
def PrintDevices(self, InOrOut):
print(csnd6.version_info)
def NoteOut(self, params):
'''' this will send a note to our csound port'''
timestamp = self.GetTime()
# print 'CSoundPort:NoteOut {0}, {1}'.format(timestamp, params.Print())
self.csound.InputMessage("i1 0 .5")
def CtrlOut(self, params):
timestamp = self.port.GetTime() # use portMidi for timing
ctrlStr = 'note {0} {1} {2}'.format(params.ctrl, params.data, params.chan)
if (self.pyext != None):
self.pyext.Write([[[eMidiCommands.CNTRL, params.ctrl, params.data], timestamp]]) # send control
else:
print('control: {0}'.format(ctrlStr))
def GetTime(self):
deltaTime = datetime.datetime.now() - self.startTime
return deltaTime.total_seconds() * 1000