PageRenderTime 101ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 19ms

/src/echonest/support/midi/example_transpose_octave.py

http://echo-nest-remix.googlecode.com/
Python | 40 lines | 21 code | 13 blank | 6 comment | 2 complexity | 180a293414a27687023236b9f7feeabd MD5 | raw file
 1from MidiOutFile import MidiOutFile
 2from MidiInFile import MidiInFile
 3
 4"""

 5This is an example of the smallest possible type 0 midi file, where 

 6all the midi events are in the same track.

 7"""
 8
 9
10class Transposer(MidiOutFile):
11    
12    "Transposes all notes by 1 octave"
13    
14    def _transp(self, ch, note):
15        if ch != 9: # not the drums!

16            note += 12
17            if note > 127:
18                note = 127
19        return note
20
21
22    def note_on(self, channel=0, note=0x40, velocity=0x40):
23        note = self._transp(channel, note)
24        MidiOutFile.note_on(self, channel, note, velocity)
25        
26        
27    def note_off(self, channel=0, note=0x40, velocity=0x40):
28        note = self._transp(channel, note)
29        MidiOutFile.note_off(self, channel, note, velocity)
30
31
32out_file = 'midiout/transposed.mid'
33midi_out = Transposer(out_file)
34
35#in_file = 'midiout/minimal_type0.mid'

36#in_file = 'test/midifiles/Lola.mid'

37in_file = 'test/midifiles/tennessee_waltz.mid'
38midi_in = MidiInFile(midi_out, in_file)
39midi_in.read()
40