123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #!/usr/bin/env python
- from midiutil import MIDIFile
- from mingus.core import chords
- NOTES = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B']
- OCTAVES = list(range(11))
- NOTES_IN_OCTAVE = len(NOTES)
- errors = {
- 'notes': 'Bad input please refer this spec-\n',
- }
- def swap_accidentals(note):
- match note:
- case 'Db':
- return 'C#'
- case 'D#':
- return 'Eb'
- case 'E#':
- return 'F'
- case 'Gb':
- return 'F#'
- case 'G#':
- return 'Ab'
- case 'A#':
- return 'Bb'
- case 'B#':
- return 'C'
- case _:
- return note
- def note_to_number(note: str, octave: int) -> int:
- note = swap_accidentals(note)
- assert note in NOTES, errors['notes']
- assert octave in OCTAVES, errors['notes']
- notenum = NOTES.index(note)
- notenum += (NOTES_IN_OCTAVE * octave)
- assert 0 <= notenum <= 127, errors['notes']
- return notenum
- chord_progression = ["Cmaj7", "Cmaj7", "Fmaj7", "Gdom7"]
- arpeggio_notes = []
- bass_notes = []
- for chord in chord_progression:
- arpeggio_notes.extend(chords.from_shorthand(chord))
- bass_notes.append(chords.from_shorthand(chord)[0])
- arpeggio_octave = 4
- arpeggio_nums = [note_to_number(x, arpeggio_octave) for x in arpeggio_notes]
- bass_octave = 3
- bass_nums = [note_to_number(x, bass_octave) for x in bass_notes]
- arpeggio_track = 0
- bass_track = 1
- channel = 0
- time = 0 # in beats
- arpeggio_duration = 1 # in beats
- bass_duration = 4 # in beats
- tempo = 120 # in BPM
- volume = 100 # 0-127
- MyMIDI = MIDIFile(2) # two tracks, defaults to format 1
- # tempo track is created automatically
- MyMIDI.addTempo(arpeggio_track, time, tempo)
- for i, pitch in enumerate(arpeggio_nums):
- MyMIDI.addNote(arpeggio_track, channel, pitch, time + i, arpeggio_duration, volume)
- for i, pitch in enumerate(bass_nums):
- MyMIDI.addNote(bass_track, channel, pitch, time + (i*4), bass_duration, volume)
- with open("pure-edm-fire-arpeggio.mid", "wb") as output_file:
- MyMIDI.writeFile(output_file)
- # print(arpeggio_notes)
|