File talk:MIDI sample.mid

from midiutil import MIDIFile

  1. Buat file MIDI sederhana

track = 0 channel = 0 time = 0 tempo = 100 volume = 100

  1. Not angka ke nada MIDI

note_map = {

   '1': 60,  # C4
   '2': 62,  # D4
   '3': 64,  # E4
   '4': 65,  # F4
   '5': 67,  # G4
   '6': 69,  # A4
   '7': 71,  # B4
   '^1': 72,  # C5
   '^2': 74,  # D5

}

  1. Melodi chorus (versi sederhana)

chorus_melody = [

   ('1', 1), ('1', 1), ('2', 1), ('3', 1), ('5', 2),
   ('1', 1), ('1', 1), ('2', 1), ('3', 1), ('5', 2),
   ('5', 1), ('6', 1), ('^1', 1), ('^1', 1), ('^2', 2),
   ('^2', 1), ('^1', 1), ('6', 1), ('5', 1), ('5', 2),
   ('5', 1), ('6', 1), ('^1', 1), ('^1', 1), ('^2', 2),
   ('^2', 1), ('^1', 1), ('6', 1), ('5', 1), ('5', 2)

]

  1. Buat file MIDI

midi = MIDIFile(1) midi.addTempo(track, time, tempo)

current_time = time for note, duration in chorus_melody:

   if note in note_map:
       pitch = note_map[note]
       midi.addNote(track, channel, pitch, current_time, duration, volume)
       current_time += duration
  1. Simpan file

with open("Tanah_Air_Merdeka.mid", "wb") as output_file:

   midi.writeFile(output_file)

print("File MIDI berhasil dibuat: Tanah_Air_Merdeka.mid")

Start a discussion