def compose(self): notes = self.create_melody() tempo = int(1000000 / (self.bpm / 60.0)) elapsed = 0 with mido.MidiFile(ticks_per_beat=48, charset='utf-8') as midi: melody = mido.MidiTrack() melody.append(mido.MetaMessage('set_tempo', tempo=tempo)) for t in range(len(self.beats.pairs)): time = int(self.beats.pairs[t][1] * 48 * 4 * self.rhythm.simple) note_off = int(48 * 4 * self.rhythm.simple) note_off = ((time + note_off) / note_off) * note_off - time if t + 1 < len(self.beats.pairs): note_off = min(note_off, int(self.beats.pairs[t + 1][1] * 48 * 4 * self.rhythm.simple - time)) melody.append(mido.Message('note_on', note=notes[t], time=time - elapsed)) melody.append(mido.Message('note_off', note=notes[t], time=note_off)) elapsed = time + note_off midi.tracks.append(melody) accom_track = Accom.create_midi_track(self.rhythm, self.beats.time, self.chords, self.accoms) midi.tracks.append(accom_track) return midi
0.125: { 'length': 0.125, 'notes': [0] }, 0.25: { 'length': 0.25, 'notes': [1, 2] }, 0.5: { 'length': 0.25, 'notes': [2] }, 0.75: { 'length': 0.25, 'notes': [0, 100] }, } with mido.MidiFile(ticks_per_beat=48, charset='utf-8') as midi: track = Accom.create_midi_track(ts, 8, prog, pattern) midi.tracks.append(track) savepath = ''.join( [random.choice(string.ascii_letters + string.digits) for i in range(16)]) + '.mid' if not os.path.exists('log'): os.makedirs('log') midi.save('log/' + savepath)
import melete.accom as Accom import melete.rhythm as Rhythm import melete.chord as Chord ts = Rhythm.TimeSignature(4, 2) c = Chord.Chord.from_name('C') f = Chord.Chord.from_name('F') f.inversion(1) gsus4 = Chord.Chord.from_name('Gsus4') gsus4.inversion(1) prog = Chord.ChordProg(48, 4, [(c, 0), (f, 192), (gsus4, 384), (c, 576)]) pattern = { 0.125: {'length': 0.125, 'notes': [0]}, 0.25: {'length': 0.25, 'notes': [1, 2]}, 0.5: {'length': 0.25, 'notes': [2]}, 0.75: {'length': 0.25, 'notes': [0, 100]}, } with mido.MidiFile(ticks_per_beat=48, charset='utf-8') as midi: track = Accom.create_midi_track(ts, 8, prog, pattern) midi.tracks.append(track) savepath = ''.join([random.choice(string.ascii_letters + string.digits) for i in range(16)]) + '.mid' if not os.path.exists('log'): os.makedirs('log') midi.save('log/' + savepath)