1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| from mido import MidiFile, MidiTrack, Message, MetaMessage, bpm2tempo import os import subprocess import tempfile
TEMPO_BPM = 90 VELOCITY_MELODY = 70 VELOCITY_CHORDS = 50 VELOCITY_DRUMS = 80 GROUPS = 5 INTRO_MEAS = 4 REST_MEAS = 1
PENTA_DEGREES = {0, 2, 4, 7, 9} BASE_NOTE = 60
MOTIF = [ [60, 62, 64, 67], [67, 64, 62, 60], [60, 64, 67, 69], [69, 67, 64, 62], ]
KICK = 36 SNARE = 38 HIHAT = 42
def in_scale(note: int) -> bool: """Return True if note belongs to C pentatonic.""" return note % 12 in PENTA_DEGREES
def shift_up(note: int, steps: int = 1) -> int: """Shift note up by `steps` pentatonic degrees without leaving the scale.""" moved, n = 0, note while moved < steps: n += 1 if in_scale(n): moved += 1 return n
def transpose_motif(motif, steps: int): if steps == 0: return motif return [[shift_up(n, steps) for n in bar] for bar in motif]
accompaniment_mid = MidiFile()
vocal_mid = MidiFile()
acc_meta_tr = MidiTrack(); accompaniment_mid.tracks.append(acc_meta_tr) acc_meta_tr.append(MetaMessage('set_tempo', tempo=bpm2tempo(TEMPO_BPM), time=0)) acc_meta_tr.append(MetaMessage('time_signature', numerator=4, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=0))
chd_tr = MidiTrack(); accompaniment_mid.tracks.append(chd_tr) chd_tr.append(Message('program_change', channel=1, program=48, time=0))
intro_mel_tr = MidiTrack(); accompaniment_mid.tracks.append(intro_mel_tr) intro_mel_tr.append(Message('program_change', channel=2, program=73, time=0))
per_tr = MidiTrack(); accompaniment_mid.tracks.append(per_tr)
voc_meta_tr = MidiTrack(); vocal_mid.tracks.append(voc_meta_tr) voc_meta_tr.append(MetaMessage('set_tempo', tempo=bpm2tempo(TEMPO_BPM), time=0)) voc_meta_tr.append(MetaMessage('time_signature', numerator=4, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=0))
mel_tr = MidiTrack(); vocal_mid.tracks.append(mel_tr) mel_tr.append(Message('program_change', channel=0, program=0, time=0))
PPQ = accompaniment_mid.ticks_per_beat BEAT = PPQ BAR = 4 * BEAT
intro_melody = [ [60, 64, 67, 69], [67, 64, 62, 60], [62, 64, 67, 72], [69, 67, 64, 60] ]
for m in range(INTRO_MEAS): root = BASE_NOTE chd_tr.append(Message('note_on', channel=1, note=root, velocity=VELOCITY_CHORDS, time=0)) chd_tr.append(Message('note_on', channel=1, note=root+7, velocity=VELOCITY_CHORDS, time=0)) chd_tr.append(Message('note_on', channel=1, note=root+12,velocity=VELOCITY_CHORDS, time=0)) chd_tr.append(Message('note_off',channel=1, note=root, velocity=0, time=BAR)) chd_tr.append(Message('note_off',channel=1, note=root+7, velocity=0, time=0)) chd_tr.append(Message('note_off',channel=1, note=root+12,velocity=0, time=0)) for note_idx, note in enumerate(intro_melody[m]): intro_mel_tr.append(Message('note_on', channel=2, note=note, velocity=VELOCITY_MELODY-10, time=0)) intro_mel_tr.append(Message('note_off', channel=2, note=note, velocity=0, time=BEAT))
for beat in range(4): if beat == 0 or beat == 2: per_tr.append(Message('note_on', channel=9, note=KICK, velocity=VELOCITY_DRUMS, time=0)) per_tr.append(Message('note_off',channel=9, note=KICK, velocity=0, time=BEAT//2)) else: per_tr.append(Message('note_on', channel=9, note=SNARE, velocity=VELOCITY_DRUMS, time=0)) per_tr.append(Message('note_off',channel=9, note=SNARE, velocity=0, time=BEAT//2)) per_tr.append(Message('note_on', channel=9, note=HIHAT, velocity=VELOCITY_DRUMS//2, time=0)) per_tr.append(Message('note_off',channel=9, note=HIHAT, velocity=0, time=BEAT//2))
mel_tr.append(Message('note_off', channel=0, note=0, velocity=0, time=INTRO_MEAS * BAR))
for g in range(GROUPS): steps = 0 if (g == 0 or g == GROUPS - 1) else g motif = transpose_motif(MOTIF, steps)
for bar_idx, bar_notes in enumerate(motif): for n in bar_notes: mel_tr.append(Message('note_on', channel=0, note=n, velocity=VELOCITY_MELODY, time=0)) mel_tr.append(Message('note_off',channel=0, note=n, velocity=0, time=BEAT))
root = bar_notes[0] chd_tr.append(Message('note_on', channel=1, note=root, velocity=VELOCITY_CHORDS, time=0)) chd_tr.append(Message('note_on', channel=1, note=root+7, velocity=VELOCITY_CHORDS, time=0)) chd_tr.append(Message('note_on', channel=1, note=root+12,velocity=VELOCITY_CHORDS, time=0)) chd_tr.append(Message('note_off',channel=1, note=root, velocity=0, time=BAR)) chd_tr.append(Message('note_off',channel=1, note=root+7, velocity=0, time=0)) chd_tr.append(Message('note_off',channel=1, note=root+12,velocity=0, time=0))
for beat in range(4): drum_note = KICK if beat in (0, 2) else SNARE per_tr.append(Message('note_on', channel=9, note=drum_note, velocity=VELOCITY_DRUMS, time=0)) per_tr.append(Message('note_off',channel=9, note=drum_note, velocity=0, time=BEAT//2)) per_tr.append(Message('note_on', channel=9, note=HIHAT, velocity=VELOCITY_DRUMS//2, time=0)) per_tr.append(Message('note_off',channel=9, note=HIHAT, velocity=0, time=BEAT//2))
if g < GROUPS - 1: mel_tr.append(Message('note_off', channel=0, note=0, velocity=0, time=REST_MEAS * BAR)) last_note = motif[-1][-1] breath_melody = [] if g % 2 == 0: breath_melody = [last_note-5, last_note-2, last_note, last_note+2] else: breath_melody = [last_note+2, last_note, last_note-2, last_note-5] breath_melody = [note for note in breath_melody if in_scale(note)] for i, note in enumerate(breath_melody): intro_mel_tr.append(Message('note_on', channel=2, note=note, velocity=VELOCITY_MELODY-5, time=0)) intro_mel_tr.append(Message('note_off', channel=2, note=note, velocity=0, time=BEAT//2)) chd_tr.append(Message('note_on', channel=1, note=root-12, velocity=VELOCITY_CHORDS-5, time=0)) chd_tr.append(Message('note_on', channel=1, note=root-5, velocity=VELOCITY_CHORDS-5, time=0)) chd_tr.append(Message('note_on', channel=1, note=root+4, velocity=VELOCITY_CHORDS-5, time=0)) chd_tr.append(Message('note_off',channel=1, note=root-12, velocity=0, time=REST_MEAS * BAR)) chd_tr.append(Message('note_off',channel=1, note=root-5, velocity=0, time=0)) chd_tr.append(Message('note_off',channel=1, note=root+4, velocity=0, time=0)) for beat in range(4 * REST_MEAS): if beat % 4 == 0: drum_note = KICK velocity = VELOCITY_DRUMS-5 elif beat % 4 == 2: drum_note = SNARE velocity = VELOCITY_DRUMS-10 else: drum_note = HIHAT velocity = VELOCITY_DRUMS-15 per_tr.append(Message('note_on', channel=9, note=drum_note, velocity=velocity, time=0)) per_tr.append(Message('note_off',channel=9, note=drum_note, velocity=0, time=BEAT//2))
ACCOMPANIMENT_MIDI = "poem_accompaniment.mid" VOCAL_MIDI = "poem_vocal.mid"
accompaniment_mid.save(ACCOMPANIMENT_MIDI) vocal_mid.save(VOCAL_MIDI) print(f"✅ 伴奏MIDI文件已生成: {ACCOMPANIMENT_MIDI}") print(f"✅ 人声MIDI文件已生成: {VOCAL_MIDI}")
|