Changeset 18744 for lang/python/mid2flmml/trunk
- Timestamp:
- 09/03/08 19:46:38 (4 months ago)
- Files:
-
- 1 modified
-
lang/python/mid2flmml/trunk/mid2flmml.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/python/mid2flmml/trunk/mid2flmml.py
r18700 r18744 1 FILE_NAME = 'tekito.mid' 2 1 3 import midi 2 4 3 5 notes = ['C', 'C+', 'D', 'D+', 'E', 'F', 'F+', 'G', 'G+', 'A', 'A+', 'B'] 4 6 globals = [] 5 channels = [[] for d in 1 6* [None]]7 channels = [[] for d in 17 * [None]] 6 8 channel_stats = [{ 7 9 'note': None, 8 10 'note_on_time': None, 11 'note_off_time': None, 9 12 'velocity': None, 10 13 'octave': None, 11 } for d in 1 6* [None]]14 } for d in 17 * [None]] 12 15 13 16 def pitch_to_octave_and_note(pitch): 14 17 return [pitch / 12, notes[pitch % 12]] 15 18 16 def add_octave_and_note(event, octave, pitch): 19 def add_length(e, note, from_time, tpq): 20 if not note: 21 raise 'note is none' 22 td = e.time - from_time 23 if td == 0: 24 if note != 'R': 25 raise 'no time diff' 26 return 27 while td > tpq: 28 channels[e.channel].append(note) 29 td -= tpq 30 if td > 0: 31 channels[e.channel].append('&') 32 if td > 0: 33 channels[e.channel].append(note) 34 channels[e.channel].append(str(int(((tpq / td) * 4)))) 35 36 def add_octave_and_note(event, octave, pitch, tpq): 37 if channel_stats[e.channel]['note_on_time']: 38 print 'WARNING!: detect waon.' 39 return 40 cmd = '' 41 if channel_stats[e.channel]['note_off_time']: 42 add_length(e, 'R', channel_stats[e.channel]['note_off_time'], tpq) 17 43 if channel_stats[e.channel]['velocity'] != e.velocity: 18 44 channels[e.channel].append('@V%d' % e.velocity) 45 channel_stats[e.channel]['velocity'] = e.velocity 19 46 octave, note = pitch_to_octave_and_note(e.pitch) 20 47 if channel_stats[e.channel]['octave']: … … 22 49 else: 23 50 od = None 51 channel_stats[e.channel]['octave'] = octave 24 52 25 53 if od == 1: … … 28 56 pass 29 57 elif od == -1: 30 channels[e.channel].append(' <')58 channels[e.channel].append('>') 31 59 else: 32 60 channels[e.channel].append('O%d' % octave) 33 channels[e.channel].append(note)34 61 channel_stats[e.channel]['note'] = note 35 62 channel_stats[e.channel]['note_on_time'] = e.time 63 channel_stats[e.channel]['note_off_time'] = None 64 65 def note_off(e, tpq): 66 add_length(e, channel_stats[e.channel]['note'], channel_stats[e.channel]['note_on_time'], tpq) 67 channel_stats[e.channel]['note'] = None 68 channel_stats[e.channel]['note_on_time'] = None 69 channel_stats[e.channel]['note_off_time'] = e.time 36 70 37 71 mid = midi.MidiFile() 38 mid.open( 'dora_h.mid')72 mid.open(FILE_NAME) 39 73 mid.read() 40 74 for track in mid.tracks: … … 47 81 pass 48 82 elif e.type == 'SET_TEMPO': 49 secPerQuarterNote = 1000000.0 / midi.getNumber(e.data, 3)[0]83 secPerQuarterNote = midi.getNumber(e.data, 3)[0] / 1000000.0 50 84 if mid.ticksPerQuarterNote: 51 tpq = mid.ticksPerQuarterNote 85 print "tpq: %d" % mid.ticksPerQuarterNote 86 tpq = float(mid.ticksPerQuarterNote) 52 87 else: 53 tpq = mid.ticksPerSecond / secPerQuarterNote 88 print "tps: %d" % mid.ticksPerSecond 89 tpq = mid.ticksPerSecond * secPerQuarterNote 54 90 tempo = 60.0 / secPerQuarterNote 91 print "tempo: %d" % tempo 55 92 globals.append('T%d' % tempo) 56 93 elif e.type == 'PROGRAM_CHANGE': 57 globals.append('/* program change ' + str(e.data) + ' */') 94 # TODO: http://noike.info/~kenzi/cgi-bin/mml2mp3/doc/FlMML_to_mml2mid.html 95 # globals.append('/* program change ' + str(e.data) + ' */') 96 pass 58 97 elif e.type == 'CONTROLLER_CHANGE': 59 # TODO: NOTE_ON tochu change taiou98 # TODO: like PITCH_BEND 60 99 pass 61 100 elif e.type == 'PITCH_BEND': 101 if channel_stats[e.channel]['note_on_time']: 102 if channel_stats[e.channel]['note_on_time'] != e.time: 103 note_off(e, tpq) 104 channels[e.channel].append('&') 62 105 octave, note = pitch_to_octave_and_note(e.pitch) 63 add_octave_and_note(e, octave, note )106 add_octave_and_note(e, octave, note, tpq) 64 107 elif e.type == 'NOTE_ON': 65 108 octave, note = pitch_to_octave_and_note(e.pitch) 66 add_octave_and_note(e, octave, note )109 add_octave_and_note(e, octave, note, tpq) 67 110 elif e.type == 'NOTE_OFF': 68 td = e.time - channel_stats[e.channel]['note_on_time'] 69 while td > tpq: 70 channels[e.channel].append('&') 71 channels[e.channel].append(channes_stats[e.channel]['note']) 72 td -= tpq 73 if td > 0: 74 channels[e.channel].append(str((tpq / td) * 4)) 111 note_off(e, tpq) 75 112 elif e.type == 'END_OF_TRACK': 76 113 pass
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)