-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixstf.py
executable file
·298 lines (227 loc) · 7.74 KB
/
fixstf.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
#!/usr/bin/env python
import sys
class Voice(object):
__id = 0;
def __init__(self, note_on ):
self._setNoteOn( note_on )
self._setSilent( 0 )
self._setDead( False )
self._id = Voice.__id
Voice.__id += 1
def _getNoteOn(self):
return self._note_on
def _setNoteOn(self,note_on):
if isinstance(note_on,Record):
if note_on.isNoteOn:
self._note_on = note_on
else:
raise TypeError('Event is not a note_on event')
else:
raise TypeError('Object is not a MIDI event')
noteOn = property( _getNoteOn, _setNoteOn, None,
"Previous note_on for a Voice" )
def _getDead(self):
return self._dead
def _setDead(self,dead):
if isinstance(dead,bool):
self._dead = dead
else:
raise TypeError('Value is not a boolean')
dead = property( _getDead, _setDead, None, "Voice has ended" )
def _getSilent(self):
return self._silent
def _setSilent(self,silent):
if isinstance(silent,int):
self._silent = silent
else:
raise TypeError('Value is not an int')
silent = property( _getSilent, _setSilent, None,
"The tick at which the voice was silenced" )
def _getId(self):
return self._id
id = property( _getId, None, None, "A unique integer id for each Voice")
class Record(object):
def __init__(self, line ):
if isinstance(line,str):
rec = [x.strip() for x in line.split(',')]
self._setTrack( rec[0] )
self._setTime( rec[1] )
self._setType( rec[2] )
self._setData( rec[3:] )
self._setVoice( None )
elif isinstance(line,Record):
self._setTrack( line.track )
self._setTime( line.time )
self._setType( line.type )
self._setData( line.data )
self._setVoice( None )
else:
raise TypeError('Line is not a string or a Record')
def _getTrack(self):
return self._track
def _setTrack(self,track):
self._track = int( track )
track = property( _getTrack, _setTrack, None, "The track number" )
def _getVoice(self):
return self._voice
def _setVoice(self,voice):
if voice:
if isinstance(voice,Voice):
self._voice = voice
else:
raise TypeError('Value is not a Voice')
else:
self._voice = None
voice = property( _getVoice, _setVoice, None, "The event voice" )
def _getTime(self):
return self._time
def _setTime(self,time):
self._time = int( time )
time = property( _getTime, _setTime, None, "The event time" )
def _getType(self):
return self._type
def _setType(self,type):
self._type = type.strip()
type = property( _getType, _setType, None, "The event type" )
def _getData(self):
return self._data
def _setData(self,data):
self._data = data
data = property( _getData, _setData, None, "The event data values" )
def _getFormattedData(self):
data = self._getData()
if data:
data = ', '.join(map(str,data))
return data
def __str__(self):
data = self._getFormattedData()
if data:
return "{0}, {1}, {2}, {3}".format(self._getTrack(),self._getTime(),
self._getType(),data)
else:
return "{0}, {1}, {2}".format(self._getTrack(),self._getTime(),
self._getType() )
def _isNoteOn(self):
result = False
if self.isType("note_on_c"):
if int( self._data[2] ) > 0:
result = True
return result
isNoteOn = property( _isNoteOn, None, None, "Is this a note on event?" )
def _isNoteOff(self):
result = False
if self.isType("note_on_c"):
if int( self._data[2] ) == 0:
result = True
elif self.isType("note_off_c"):
result = True
return result
isNoteOff = property( _isNoteOff, None, None, "Is this a note off event?" )
def isType(self,rtype):
return self.type.lower() == rtype.lower()
def _getPitch(self):
if self.isType("note_on_c") or self.isType("note_on_c"):
return int( self._data[1] )
else:
raise TypeError('Event is not a note_on or a note_off')
def _setPitch(self,pitch):
if self.isType("note_on_c") or self.isType("note_on_c"):
self._data[1] = str( time )
else:
raise TypeError('Event is not a note_on or a note_off')
pitch = property( _getPitch, _setPitch, None, "The note event pitch" )
class Midifile(list):
def __init__(self, file ):
super(Midifile, self).__init__()
if isinstance( file, str ):
self._tpb = 0
mspb = 0
with open( file ) as f:
for line in f:
rec = Record( line )
self.append( rec )
if rec.isType( "header" ):
self._tpb = int( rec.data[2] )
elif rec.isType( "tempo" ):
mspb = int( rec.data[0] )
if self._tpb == 0:
raise TypeError('No ticks per beat value found in {0}'.format(file))
elif mspb == 0:
raise TypeError('No tempo value found in {0}'.format(file))
self._tps = 1.0E6*float(self._tpb)/float(mspb)
elif isinstance( file, Midifile ):
self._tpb = file._tpb
self._tps = file._tps
else:
raise TypeError("file is not a string or a Midifile")
def _getTpb(self):
return self._tpb
tpb = property( _getTpb, None, None, "The number of ticks per beat" )
def _getTps(self):
return self._tps
tps = property( _getTps, None, None, "The number of ticks per second" )
def write(self,file):
fd = open( file, "w" )
for rec in self:
fd.write( "{0}\n".format(rec) )
fd.close()
def appendNoteOff(self, note, time ):
irec = len(self) - 1
while( self[irec].time > time and irec > 0 ):
irec -= 1
newrec = Record( note )
newrec.time = time
newrec.type = "Note_off_c"
self.insert( irec + 1, newrec )
def append(self,record):
if isinstance(record,Record):
super(Midifile, self).append(record)
else:
raise TypeError("record is not a Record")
def organise( oldmf ):
mf = Midifile( oldmf )
voices = []
slimit = mf.tpb*4
gap = int( mf.tps*0.05 )
print("Gap is {0}/{1} = {2} beats".format(gap,oldmf.tpb,float(gap)/oldmf.tpb))
for event in oldmf:
if event.isNoteOn:
pitch = event.pitch
minint = 15
minvoice = None
for voice in voices:
if voice.silent > event.time - slimit:
thisint = abs( pitch - voice.noteOn.pitch )
if thisint < minint:
minvoice = voice
minint = thisint
elif voice.silent > 0:
voice.dead = True
for voice in voices:
if voice.dead:
mf.appendNoteOff( voice.noteOn, event.time - slimit )
voices.remove( voice )
if minvoice:
stopTime = event.time - gap
if stopTime < minvoice.silent:
stopTime = minvoice.silent
mf.appendNoteOff( minvoice.noteOn, stopTime )
minvoice.noteOn = event
minvoice.silent = 0
else:
voice = Voice( event )
voices.append( voice )
mf.append( event )
elif event.isNoteOff:
pitch = event.pitch
for voice in voices:
if voice.silent == 0:
if voice.noteOn.pitch == pitch:
voice.silent = event.time
break
else:
mf.append( event )
return mf
oldmf = Midifile( sys.argv[1] )
mf = organise( oldmf )
mf.write("New.csv")