4 @author: Samuel Benveniste
7 from mxmMidi
.MidiOutStream
import MidiOutStream
9 class MidiToSong(MidiOutStream
):
11 Creates songs from midi files using mxmMidi package
14 self
.midiNoteNumbers
= []
16 self
.quarterNoteLength
= 500
17 self
.firstNoteOn
= True
18 self
._lastNoteOnTime
= 0
20 def header(self
, format
=0, nTracks
=1, division
=96):
21 print 'format: %s, nTracks: %s, division: %s' % (format
, nTracks
, division
)
22 print '----------------------------------'
24 self
.division
= division
26 def tempo(self
,value
):
27 self
.quarterNoteLength
= value
/1000
28 print "quarterNoteLength in ms :" + str(self
.quarterNoteLength
)
30 def note_on(self
, channel
=0, note
=0x40, velocity
=0x40):
31 self
.midiNoteNumbers
.append(note
)
32 # if it's the first note_on take note of time (the song begins here)
33 # from the second note_on and after, mark the length of the preceding note
35 self
.firstNoteOn
= False
36 self
._lastNoteOnTime
= self
.abs_time()
38 self
.noteLengths
.append(float(self
.abs_time()-self
._lastNoteOnTime
)/float(self
.division
))
39 self
._lastNoteOnTime
= self
.abs_time()
42 self
.noteLengths
.append(4)
43 for i
in range(len(self
.midiNoteNumbers
)):
44 print "note number :" + str(self
.midiNoteNumbers
[i
]) + ", length in quarter Notes :" + str(self
.noteLengths
[i
])
48 if __name__
== '__main__':
51 test_file
= '../songs/midis/test.mid'
52 f
= open(test_file
, 'rb')
55 from mxmMidi
.MidiInFile
import MidiInFile
57 midiIn
= MidiInFile(mts
, f
)