PageRenderTime 320ms CodeModel.GetById 80ms app.highlight 17ms RepoModel.GetById 103ms app.codeStats 1ms

/src/echonest/support/midi/MidiOutStream.py

http://echo-nest-remix.googlecode.com/
Python | 471 lines | 426 code | 7 blank | 38 comment | 0 complexity | 4039d2767524f78e22adb5830e7fbbfc MD5 | raw file
  1# -*- coding: ISO-8859-1 -*-

  2
  3class MidiOutStream:
  4
  5
  6    """

  7

  8    MidiOutstream is Basically an eventhandler. It is the most central

  9    class in the Midi library. You use it both for writing events to

 10    an output stream, and as an event handler for an input stream.

 11

 12    This makes it extremely easy to take input from one stream and

 13    send it to another. Ie. if you want to read a Midi file, do some

 14    processing, and send it to a midiport.

 15

 16    All time values are in absolute values from the opening of a

 17    stream. To calculate time values, please use the MidiTime and

 18    MidiDeltaTime classes.

 19

 20    """
 21
 22    def __init__(self):
 23        
 24        # the time is rather global, so it needs to be stored 

 25        # here. Otherwise there would be no really simple way to 

 26        # calculate it. The alternative would be to have each event 

 27        # handler do it. That sucks even worse!

 28        self._absolute_time = 0
 29        self._relative_time = 0
 30        self._current_track = 0
 31        self._running_status = None
 32
 33    # time handling event handlers. They should be overwritten with care

 34
 35    def update_time(self, new_time=0, relative=1):
 36        """

 37        Updates the time, if relative is true, new_time is relative, 

 38        else it's absolute.

 39        """
 40        if relative:
 41            self._relative_time = new_time
 42            self._absolute_time += new_time
 43        else:
 44            self._relative_time = new_time - self._absolute_time
 45            self._absolute_time = new_time
 46
 47    def reset_time(self):
 48        """

 49        reset time to 0

 50        """
 51        self._relative_time = 0
 52        self._absolute_time = 0
 53        
 54    def rel_time(self):
 55        "Returns the relative time"
 56        return self._relative_time
 57
 58    def abs_time(self):
 59        "Returns the absolute time"
 60        return self._absolute_time
 61
 62    # running status methods

 63    
 64    def reset_run_stat(self):
 65        "Invalidates the running status"
 66        self._running_status = None
 67
 68    def set_run_stat(self, new_status):
 69        "Set the new running status"
 70        self._running_status = new_status
 71
 72    def get_run_stat(self):
 73        "Set the new running status"
 74        return self._running_status
 75
 76    # track handling event handlers

 77    
 78    def set_current_track(self, new_track):
 79        "Sets the current track number"
 80        self._current_track = new_track
 81    
 82    def get_current_track(self):
 83        "Returns the current track number"
 84        return self._current_track
 85    
 86    
 87    #####################

 88    ## Midi events

 89
 90
 91    def channel_message(self, message_type, channel, data):
 92        """The default event handler for channel messages"""
 93        pass
 94
 95
 96    def note_on(self, channel=0, note=0x40, velocity=0x40):
 97
 98        """

 99        channel: 0-15

100        note, velocity: 0-127

101        """
102        pass
103
104
105    def note_off(self, channel=0, note=0x40, velocity=0x40):
106
107        """

108        channel: 0-15

109        note, velocity: 0-127

110        """
111        pass
112
113
114    def aftertouch(self, channel=0, note=0x40, velocity=0x40):
115
116        """

117        channel: 0-15

118        note, velocity: 0-127

119        """
120        pass
121
122
123    def continuous_controller(self, channel, controller, value):
124
125        """

126        channel: 0-15

127        controller, value: 0-127

128        """
129        pass
130
131
132    def patch_change(self, channel, patch):
133
134        """

135        channel: 0-15

136        patch: 0-127

137        """
138        pass
139
140
141    def channel_pressure(self, channel, pressure):
142
143        """

144        channel: 0-15

145        pressure: 0-127

146        """
147        pass
148
149
150    def pitch_bend(self, channel, value):
151
152        """

153        channel: 0-15

154        value: 0-16383

155

156        """
157        pass
158
159
160
161
162    #####################

163    ## System Exclusive

164
165    def system_exclusive(self, data):
166
167        """

168        data: list of values in range(128)

169        """
170        pass
171
172
173    #####################

174    ## Common events

175
176    def song_position_pointer(self, value):
177
178        """

179        value: 0-16383

180        """
181        pass
182
183
184    def song_select(self, songNumber):
185
186        """

187        songNumber: 0-127

188        """
189        pass
190
191
192    def tuning_request(self):
193
194        """

195        No values passed

196        """
197        pass
198
199            
200    def midi_time_code(self, msg_type, values):
201        """

202        msg_type: 0-7

203        values: 0-15

204        """
205        pass
206
207
208    #########################

209    # header does not really belong here. But anyhoo!!!

210    
211    def header(self, format=0, nTracks=1, division=96):
212
213        """

214        format: type of midi file in [1,2]

215        nTracks: number of tracks

216        division: timing division

217        """
218        pass
219
220
221    def eof(self):
222
223        """

224        End of file. No more events to be processed.

225        """
226        pass
227
228
229    #####################

230    ## meta events

231
232
233    def meta_event(self, meta_type, data):
234        
235        """

236        Handles any undefined meta events

237        """
238        pass
239
240
241    def start_of_track(self, n_track=0):
242
243        """

244        n_track: number of track

245        """
246        pass
247
248
249    def end_of_track(self):
250
251        """

252        n_track: number of track

253        """
254        pass
255
256
257    def sequence_number(self, value):
258
259        """

260        value: 0-16383

261        """
262        pass
263
264
265    def text(self, text):
266
267        """

268        Text event

269        text: string

270        """
271        pass
272
273
274    def copyright(self, text):
275
276        """

277        Copyright notice

278        text: string

279        """
280        pass
281
282
283    def sequence_name(self, text):
284
285        """

286        Sequence/track name

287        text: string

288        """
289        pass
290
291
292    def instrument_name(self, text):
293
294        """

295        text: string

296        """
297        pass
298
299
300    def lyric(self, text):
301
302        """

303        text: string

304        """
305        pass
306
307
308    def marker(self, text):
309
310        """

311        text: string

312        """
313        pass
314
315
316    def cuepoint(self, text):
317
318        """

319        text: string

320        """
321        pass
322
323
324    def midi_ch_prefix(self, channel):
325
326        """

327        channel: midi channel for subsequent data (deprecated in the spec)

328        """
329        pass
330
331
332    def midi_port(self, value):
333
334        """

335        value: Midi port (deprecated in the spec)

336        """
337        pass
338
339
340    def tempo(self, value):
341
342        """

343        value: 0-2097151

344        tempo in us/quarternote

345        (to calculate value from bpm: int(60,000,000.00 / BPM))

346        """
347        pass
348
349
350    def smtp_offset(self, hour, minute, second, frame, framePart):
351
352        """

353        hour,

354        minute,

355        second: 3 bytes specifying the hour (0-23), minutes (0-59) and 

356                seconds (0-59), respectively. The hour should be 

357                encoded with the SMPTE format, just as it is in MIDI 

358                Time Code.

359        frame: A byte specifying the number of frames per second (one 

360               of : 24, 25, 29, 30).

361        framePart: A byte specifying the number of fractional frames, 

362                   in 100ths of a frame (even in SMPTE-based tracks 

363                   using a different frame subdivision, defined in the 

364                   MThd chunk).

365        """
366        pass
367
368
369
370    def time_signature(self, nn, dd, cc, bb):
371
372        """

373        nn: Numerator of the signature as notated on sheet music

374        dd: Denominator of the signature as notated on sheet music

375            The denominator is a negative power of 2: 2 = quarter 

376            note, 3 = eighth, etc.

377        cc: The number of MIDI clocks in a metronome click

378        bb: The number of notated 32nd notes in a MIDI quarter note 

379            (24 MIDI clocks)        

380        """
381        pass
382
383
384
385    def key_signature(self, sf, mi):
386
387        """

388        sf: is a byte specifying the number of flats (-ve) or sharps 

389            (+ve) that identifies the key signature (-7 = 7 flats, -1 

390            = 1 flat, 0 = key of C, 1 = 1 sharp, etc).

391        mi: is a byte specifying a major (0) or minor (1) key.

392        """
393        pass
394
395
396
397    def sequencer_specific(self, data):
398
399        """

400        data: The data as byte values

401        """
402        pass
403
404
405
406
407    #####################

408    ## realtime events

409
410    def timing_clock(self):
411
412        """

413        No values passed

414        """
415        pass
416
417
418
419    def song_start(self):
420
421        """

422        No values passed

423        """
424        pass
425
426
427
428    def song_stop(self):
429
430        """

431        No values passed

432        """
433        pass
434
435
436
437    def song_continue(self):
438
439        """

440        No values passed

441        """
442        pass
443
444
445
446    def active_sensing(self):
447
448        """

449        No values passed

450        """
451        pass
452
453
454
455    def system_reset(self):
456
457        """

458        No values passed

459        """
460        pass
461
462
463
464if __name__ == '__main__':
465
466    midiOut = MidiOutStream()
467    midiOut.update_time(0,0)
468    midiOut.note_on(0, 63, 127)
469    midiOut.note_off(0, 63, 127)
470
471