Ajout de la version refrain des feuilles mortes.
[minwii.git] / src / songs / MidiToSong.py
1 '''
2 Created on 8 dec. 2009
3
4 @author: Samuel Benveniste
5 '''
6
7 from mxmMidi.MidiOutStream import MidiOutStream
8
9 class MidiToSong(MidiOutStream):
10 '''
11 Creates songs from midi files using mxmMidi package
12 '''
13 def __init__(self):
14 self.midiNoteNumbers = []
15 self.noteLengths = []
16 self.quarterNoteLength = 500
17 self.firstNoteOn = True
18 self._lastNoteOnTime = 0
19
20 def header(self, format=0, nTracks=1, division=96):
21 print 'format: %s, nTracks: %s, division: %s' % (format, nTracks, division)
22 print '----------------------------------'
23 print ''
24 self.division = division
25
26 def tempo(self,value):
27 self.quarterNoteLength = value/1000
28 print "quarterNoteLength in ms :" + str(self.quarterNoteLength)
29
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
34 if self.firstNoteOn :
35 self.firstNoteOn = False
36 self._lastNoteOnTime = self.abs_time()
37 else :
38 self.noteLengths.append(float(self.abs_time()-self._lastNoteOnTime)/float(self.division))
39 self._lastNoteOnTime = self.abs_time()
40
41 def eof(self):
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])
45 print"--------------"
46 print "end of file"
47
48 if __name__ == '__main__':
49
50 # get data
51 test_file = '../songs/midis/test.mid'
52 f = open(test_file, 'rb')
53
54 # do parsing
55 from mxmMidi.MidiInFile import MidiInFile
56 mts = MidiToSong()
57 midiIn = MidiInFile(mts, f)
58 midiIn.read()
59 f.close()
60