/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

  1. from MidiOutFile import MidiOutFile
  2. from MidiInFile import MidiInFile
  3. """
  4. This is an example of the smallest possible type 0 midi file, where
  5. all the midi events are in the same track.
  6. """
  7. class Transposer(MidiOutFile):
  8. "Transposes all notes by 1 octave"
  9. def _transp(self, ch, note):
  10. if ch != 9: # not the drums!
  11. note += 12
  12. if note > 127:
  13. note = 127
  14. return note
  15. def note_on(self, channel=0, note=0x40, velocity=0x40):
  16. note = self._transp(channel, note)
  17. MidiOutFile.note_on(self, channel, note, velocity)
  18. def note_off(self, channel=0, note=0x40, velocity=0x40):
  19. note = self._transp(channel, note)
  20. MidiOutFile.note_off(self, channel, note, velocity)
  21. out_file = 'midiout/transposed.mid'
  22. midi_out = Transposer(out_file)
  23. #in_file = 'midiout/minimal_type0.mid'
  24. #in_file = 'test/midifiles/Lola.mid'
  25. in_file = 'test/midifiles/tennessee_waltz.mid'
  26. midi_in = MidiInFile(midi_out, in_file)
  27. midi_in.read()