Created
February 20, 2020 03:11
-
-
Save varlen/fb81e20253b4518ea9143b4dc76310fb to your computer and use it in GitHub Desktop.
MIDI file mapper using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mido import MidiFile, MidiFile, MidiTrack | |
# Abrindo o arquivo de origem | |
input_midi = MidiFile('./Murundu.mid') | |
# Criando o arquivo de destino e a nova faixa MIDI | |
output_midi = MidiFile() | |
# Mantendo o tempo (BPM) entre os arquivos | |
output_midi.ticks_per_beat = input_midi.ticks_per_beat | |
note_map = {} | |
with open('note_map.csv') as map_text: | |
for line in map_text: | |
elements = line.replace('\n','').split(',') | |
# Cada linha do arquivo de mapeamento será um elemento | |
# num dicionário cuja chave é o número da nota MIDI de origem | |
# e o valor é também um dicionario com a nota destino e a descrição | |
note_map[int(elements[0])] = { 'target_note': int(elements[1]), 'description': elements[2] } | |
# Agora precisamos iterar no arquivo MIDI de origem | |
# e escrever as notas modificadas no arquivo de destino | |
# As notas MIDI são determinadas pelas mensagens dos tipos note_on e note_off | |
# Mensagens de outros tipos serão copiadas para o novo arquivo sem alterações | |
# Notas que não existem no mapeamento não serão copiadas | |
for original_track in input_midi.tracks: | |
new_track = MidiTrack() | |
for msg in original_track: | |
if msg.type in ['note_on','note_off']: | |
# A API da mido permite copiar uma mensagem MIDI alterando parametros da mensagem original | |
# Aqui usamos o dicionario com os mapeamentos para criar a cópia com o valor da nota alterado | |
# https://mido.readthedocs.io/en/latest/messages.html | |
origin_note = msg.note | |
if origin_note in note_map: | |
new_track.append( msg.copy( note = note_map[origin_note]['target_note'] )) | |
print(note_map[origin_note]['description']) | |
else: | |
print("Origin note",origin_note,"not mapped") | |
else: | |
print(msg.type) | |
new_track.append(msg) | |
output_midi.tracks.append(new_track) | |
output_midi.save('./Murundu-remap.mid') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 | 44 | Hihat Pedal | |
---|---|---|---|
63 | 68 | Hihat Edge Tight | |
62 | 69 | Hihat Edge Closed | |
61 | 71 | Hihat Edge Semi-open | |
60 | 72 | Hihat Edge Open | |
58 | 54 | Crash 1 Choke | |
57 | 55 | Crash 1 | |
55 | 55 | Splash -> Crash | |
53 | 53 | Ride Bell | |
52 | 35 | China | |
51 | 51 | Ride | |
50 | 50 | Tom 1 - Hi | |
49 | 52 | Crash 2 | |
48 | 50 | Tom 1 - Hi | |
47 | 48 | Tom 2 - Mid | |
46 | 72 | Hihat Edge Open | |
45 | 48 | Tom 2 - Mid | |
44 | 71 | Hihat Edge Semi-open | |
43 | 47 | Tom 3 - Low | |
42 | 69 | Hihat Edge Closed | |
41 | 47 | Tom 3 - Low | |
40 | 38 | Snare | |
39 | 38 | Snare | |
38 | 38 | Snare | |
37 | 37 | Sidestick | |
36 | 36 | Kick | |
35 | 36 | Kick |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment