-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbs2midi.py
180 lines (150 loc) · 7.3 KB
/
nbs2midi.py
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
# This file is a part of:
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# ███▄▄▄▄ ▀█████████▄ ▄████████ ███ ▄██████▄ ▄██████▄ ▄█
# ███▀▀▀██▄ ███ ███ ███ ███ ▀█████████▄ ███ ███ ███ ███ ███
# ███ ███ ███ ███ ███ █▀ ▀███▀▀██ ███ ███ ███ ███ ███
# ███ ███ ▄███▄▄▄██▀ ███ ███ ▀ ███ ███ ███ ███ ███
# ███ ███ ▀▀███▀▀▀██▄ ▀███████████ ███ ███ ███ ███ ███ ███
# ███ ███ ███ ██▄ ███ ███ ███ ███ ███ ███ ███
# ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ ███ ███▌ ▄
# ▀█ █▀ ▄█████████▀ ▄████████▀ ▄████▀ ▀██████▀ ▀██████▀ █████▄▄██
# __________________________________________________________________________________
# NBSTool is a tool to work with .nbs (Note Block Studio) files.
# Author: IoeCmcomc (https://github.com/IoeCmcomc)
# Programming language: Python
# License: MIT license
# Source codes are hosted on: GitHub (https://github.com/IoeCmcomc/NBSTool)
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
from asyncio import sleep
from itertools import repeat
from mido import Message, MetaMessage, MidiFile, MidiTrack, bpm2tempo
from numpy import interp
from common import MIDI_DRUMS, MIDI_INSTRUMENTS
from nbsio import NbsSong, Layer
MIDI_DRUMS_BY_NBS_KEY_INST = {
(obj.nbs_pitch, obj.nbs_instrument): obj.pitch for obj in MIDI_DRUMS}
INST_PROGRAMS = {
0: 1, # Harp: Piano
1: 32, # Double bass: Acoustic bass
5: 28, # Guitar: Acoustic guitar (steel)
6: 73, # Flute: Flute
7: 10, # Bell: Music box
8: 112, # Chime: Tinkle bell (wind chime)
9: 13, # Xylophone: Xylophone
10: 11, # Iron Xylophone: Vibraphone
11: 12, # Cow Bell: Marimba
12: 43, # Didgeridoo: Contrabass
13: 80, # Bit: Lead 1 (square)
14: 105, # Banjo: Banjo
15: 16, # Pling: Drawbar organ
}
INST2PITCH = {
2: 36,
3: 44,
4: 81,
}
def firstInstInLayer(nbs: NbsSong, layer: int) -> int:
'''It returns the instrument of the first note in a layer
Parameters
----------
nbs : NbsSong
The NBS file to be converted.
layer : int
The layer to check for the first instrument.
Returns
-------
The instrument of the first note in the layer.
'''
if layer > nbs.maxLayer:
return 0
for note in nbs.notes:
if note.layer == layer:
return note.inst
return 0
class MsgComparator:
def __init__(self, msg: Message) -> None:
self.msg = msg
def __lt__(self, other: 'MsgComparator') -> bool:
if (not self.msg.is_meta) and other.msg.is_meta:
return self.msg.time < other.msg.time # type: ignore
return False
def absTrack2DeltaTrack(track) -> MidiTrack:
'''It takes a track of absolute time messages and returns a track of delta
time messages'''
track.sort(key=MsgComparator)
ret = MidiTrack()
ret.append(track[0])
for i in range(1, len(track)):
msg = track[i]
# print(msg.time - track[i-1].time)
ret.append(msg.copy(time=msg.time - track[i-1].time))
return ret
async def nbs2midi(data: NbsSong, filepath: str, dialog = None):
'''It converts a NBS song to a MIDI file
Parameters
----------
data : NbsSong
NbsSong
filepath : str
The path to the file you want to save to.
dialog
the dialog that shows the progress bar
'''
headers, notes, layers = data.header, data.notes, data.layers
default_layer = Layer("")
if (data.maxLayer >= headers.height):
layers.extend(repeat(default_layer, data.maxLayer+1 - headers.height))
# The time signature upper number in ONBS
# doesn't affect the overall tempo at all.
# timeSign = headers.time_sign
timeSign = 4
tempo = headers.tempo * 60 / timeSign # BPM
mid = MidiFile(type=1)
tpb = mid.ticks_per_beat
note_tpb = int(tpb / timeSign)
tracks: list[MidiTrack] = []
for i in range(data.maxLayer+1):
layer = layers[i]
programCode = INST_PROGRAMS.get(firstInstInLayer(data, i), 1) - 1
track = MidiTrack()
track.append(MetaMessage(
'time_signature', numerator=headers.time_sign, denominator=4, time=0))
track.append(MetaMessage('set_tempo', tempo=bpm2tempo(tempo), time=0))
track.append(Message('program_change', program=programCode, time=0))
track.append(Message(
'control_change', control=7, value=int(layer.volume * 127 / 100), time=0))
pan = int(interp(layer.pan, (-100, 100), (0, 127)))
track.append(Message('control_change', control=10, value=pan, time=0))
tracks.append(track)
if dialog:
dialog.currentProgress.set(25) # 25%
await sleep(0.001)
for note in notes:
abs_time = int(note.tick / timeSign * tpb)
pitch = note.key + 21
trackIndex = note.layer
velocity = int(note.vel * 127 / 100)
# pan = int(interp(layers[trackIndex].pan * note.pan, (-10000, 10000), (0, 127)))
pan = int(interp((layers[trackIndex].pan + note.pan) / 2, (-100, 100), (0, 127)))
inst: int = note.inst
tracks[trackIndex].append(Message(
'control_change', control=10, value=pan, time=abs_time))
if note.isPerc:
pitch = MIDI_DRUMS_BY_NBS_KEY_INST.get((pitch, inst), INST2PITCH[inst])
tracks[trackIndex].append(Message('note_on', channel=9, note=pitch, velocity=velocity, time=abs_time))
tracks[trackIndex].append(Message('note_off', channel=9, note=pitch, velocity=velocity, time=abs_time + note_tpb))
else:
# keyShift = MIDI_INSTRUMENTS[INST_PROGRAMS.get(inst, 1)-1].octave_shift * 12
# pitch += keyShift
tracks[trackIndex].append(Message('note_on', note=pitch, velocity=velocity, time=abs_time))
tracks[trackIndex].append(Message('note_off', note=pitch, velocity=velocity, time=abs_time + note_tpb))
if dialog:
dialog.currentProgress.set(50) # 50%
await sleep(0.001)
mid.tracks = [absTrack2DeltaTrack(track) for track in tracks]
if dialog:
dialog.currentProgress.set(75) # 75%
await sleep(0.001)
if not filepath.endswith('.mid'):
filepath += '.mid'
mid.save(filepath)