-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbs2audio.py
238 lines (196 loc) · 9.02 KB
/
nbs2audio.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# 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 typing import Sequence
from nbswave import SongRenderer, audio, nbs
from nbswave.main import MissingInstrumentException
from pydub import AudioSegment
from pynbs import File, Header, Instrument, Layer, Note
from audio_common import load_sound
from common import SOUND_FOLDER
from nbsio import NbsSong
audio.load_sound = load_sound
def convert(data: NbsSong) -> File:
oldHeader = data.header
header = Header(
version=oldHeader.file_version,
default_instruments=oldHeader.vani_inst,
song_length=oldHeader.length,
song_layers=oldHeader.height,
song_name=oldHeader.name,
song_author=oldHeader.author,
original_author=oldHeader.orig_author,
description=oldHeader.description,
auto_save=oldHeader.auto_save,
auto_save_duration=oldHeader.auto_save_time,
tempo=oldHeader.tempo,
time_signature=oldHeader.time_sign,
minutes_spent=oldHeader.minutes_spent,
left_clicks=oldHeader.left_clicks,
right_clicks=oldHeader.right_clicks,
blocks_added=oldHeader.block_added,
blocks_removed=oldHeader.block_removed,
song_origin=oldHeader.import_name,
loop=oldHeader.loop,
max_loop_count=oldHeader.loop_max,
loop_start=oldHeader.loop_start
)
notes = [Note(tick=note.tick, layer=note.layer, instrument=note.inst, key=note.key,
velocity=note.vel, panning=note.pan, pitch=note.pitch) for note in data.notes]
layers = [Layer(id=i, name=layer.name, lock=layer.lock, volume=layer.volume,
panning=layer.pan) for i, layer in enumerate(data.layers)]
while len(layers) < data.maxLayer+1:
layers.append(Layer(id=len(layers)))
instruments = [Instrument(id=i, name=inst.name, file=inst.filePath, pitch=inst.pitch,
press_key=inst.pressKeys) for i, inst in enumerate(data.customInsts)]
return File(header, notes, layers, instruments)
class Renderer(SongRenderer):
def __init__(self, song, dialog):
super().__init__(song, SOUND_FOLDER)
self.dialog = dialog
async def mix_song(
self,
ignore_missing_instruments=False,
exclude_locked_layers=False,
**kwargs,
):
if exclude_locked_layers:
notes_to_mix = list(self._song.get_unlocked_notes())
else:
notes_to_mix = list(self._song.weighted_notes())
mixed = await self._mix(
notes_to_mix,
ignore_missing_instruments=ignore_missing_instruments,
**kwargs,
)
return mixed
async def _mix(
self,
notes: Sequence[nbs.Note],
ignore_missing_instruments: bool = False,
sample_rate: int = 44100,
channels: int = 2,
bit_depth: int = 16,
) -> audio.Track:
tempo_segments = self._song.tempo_segments
track_length = self.get_length(self._song.weighted_notes(), tempo_segments) # type: ignore
mixer = audio.Mixer(
sample_width=bit_depth // 8,
frame_rate=sample_rate,
channels=channels,
length=track_length,
)
sorted_notes = nbs.sorted_notes(notes)
last_ins = None
last_key = None
last_vol = None
last_pan = None
note_count = len(sorted_notes)
sound = sound1 = sound2 = sound3 = sound4 = AudioSegment.empty()
for i, note in enumerate(sorted_notes):
ins = note.instrument
key = note.key
vol = note.velocity
pan = note.panning
if ins != last_ins:
last_key = None
last_vol = None
last_pan = None
try:
sound1 = self._instruments[note.instrument]
except KeyError: # Sound file missing
if not ignore_missing_instruments:
custom_ins_id = ins - self._song.header.default_instruments
instrument_data = self._song.instruments[custom_ins_id]
ins_name = instrument_data.name
ins_file = instrument_data.file
raise MissingInstrumentException(
f"The sound file for instrument {ins_name} was not found: {ins_file}"
)
continue
if sound1 is None: # Sound file not assigned
continue
sound1 = audio.sync(sound1)
if key != last_key:
last_vol = None
last_pan = None
pitch = audio.key_to_pitch(key)
sound2 = audio.change_speed(sound1, pitch)
if vol != last_vol:
last_pan = None
gain = audio.vol_to_gain(vol)
sound3 = sound2.apply_gain(gain)
if pan != last_pan:
sound4 = sound3.pan(pan) # type: ignore
sound = sound4
last_ins = ins
last_key = key
last_vol = vol
last_pan = pan
pos = tempo_segments[note.tick]
mixer.overlay(sound, position=int(pos))
if self.dialog:
newPercent = 30 + i * 50 / note_count
if newPercent - self.dialog.currentProgress.get() >= 0.5:
self.dialog.currentProgress.set(newPercent)
await sleep(0.001)
return mixer.to_audio_segment()
async def nbs2audio(data: NbsSong, filepath: str, dialog=None,
fmt: str = 'wav',
sample_rate: int = 44100,
channels: int = 2,
bit_depth: int = 16,
target_bitrate: int = 320,
target_size = None,
ignore_missing_instruments: bool = False,
exclude_locked_layers: bool = False,
) -> None:
renderer = Renderer(convert(data), dialog)
if (not ignore_missing_instruments) and (missingInsts := renderer.missing_instruments()):
instFiles = '\n'.join(inst.file for inst in missingInsts)
raise MissingInstrumentException(
f"The following sound files are missing in the sounds/ folder:\n{instFiles}")
if dialog:
dialog.currentProgress.set(20) # 20%
await sleep(0.001)
renderer.load_instruments(SOUND_FOLDER)
if dialog:
dialog.currentProgress.set(30) # 30%
await sleep(0.001)
track = await renderer.mix_song(
ignore_missing_instruments,
exclude_locked_layers,
sample_rate=sample_rate,
bit_depth=bit_depth,
channels=channels,
)
if dialog:
dialog.currentProgress.set(80) # 80%
await sleep(0.001)
track.save(
filepath,
fmt,
bit_depth // 8,
sample_rate,
channels,
target_bitrate,
target_size,
)
if dialog:
dialog.currentProgress.set(90) # 90%
await sleep(0.001)