Skip to content

Commit 13ba2a3

Browse files
committedJun 11, 2024
impl: Add logging to the program
1 parent 1c604bd commit 13ba2a3

9 files changed

+309
-182
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ Downloaded song/
343343

344344
*build/
345345
dist/
346+
logs/
346347
main.dist/
347348
.mscbackup/
348349

‎audio_common.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from functools import lru_cache
2+
3+
from pydub import AudioSegment
4+
5+
6+
@lru_cache(maxsize=32)
7+
def load_sound(path: str) -> AudioSegment:
8+
"""A patched version of nbswave.audio.load_song() which caches loaded sounds"""
9+
if not path:
10+
return AudioSegment.empty()
11+
else:
12+
return AudioSegment.from_file(path)

‎common.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
import sys
2121
from collections import namedtuple
2222
from os.path import abspath, dirname, join, normpath
23-
from functools import lru_cache
24-
from typing import Optional
25-
26-
from pydub import AudioSegment
2723

2824
# from main import __file__ as __mainfile__
2925

@@ -232,7 +228,7 @@
232228
SOUND_FOLDER = "sounds"
233229

234230
BASE_RESOURCE_PATH = ''
235-
if getattr(sys, 'frozen', False): # PyInstaller
231+
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): # PyInstaller
236232
BASE_RESOURCE_PATH = sys._MEIPASS # type: ignore
237233
elif '__compiled__' in globals(): # Nuitka
238234
BASE_RESOURCE_PATH = dirname(__file__)
@@ -241,12 +237,4 @@
241237
assert BASE_RESOURCE_PATH != ''
242238

243239
def resource_path(*args: str):
244-
return normpath(join(BASE_RESOURCE_PATH, *args))
245-
246-
@lru_cache(maxsize=32)
247-
def load_sound(path: str) -> AudioSegment:
248-
"""A patched version of nbswave.audio.load_song() which caches loaded sounds"""
249-
if not path:
250-
return AudioSegment.empty()
251-
else:
252-
return AudioSegment.from_file(path)
240+
return normpath(join(BASE_RESOURCE_PATH, *args))

‎customwidgets/builder.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
except ImportError as e:
2626
from pygubu import BuilderObject, register_widget # type: ignore
2727

28-
from wrapmessage import WrapMessage
29-
from checkablelabelframe import CheckableLabelFrame
28+
try:
29+
from wrapmessage import WrapMessage
30+
from checkablelabelframe import CheckableLabelFrame
31+
except ModuleNotFoundError:
32+
from .wrapmessage import WrapMessage
33+
from .checkablelabelframe import CheckableLabelFrame
3034

3135
class WrapMessageBuilder(BuilderObject): # type: ignore
3236
class_ = WrapMessage

‎main.py

+221-150
Large diffs are not rendered by default.

‎nbs2audio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
from pydub import AudioSegment
2626
from pynbs import File, Header, Instrument, Layer, Note
2727

28-
from common import load_sound
29-
3028
from nbswave import SongRenderer, audio, nbs
3129
from nbswave.main import MissingInstrumentException
3230

33-
from common import resource_path, SOUND_FOLDER
3431
from nbsio import VANILLA_INSTS, NbsSong
3532

33+
from audio_common import load_sound
34+
from common import resource_path, SOUND_FOLDER
35+
36+
3637
audio.load_sound = load_sound
3738

3839
def convert(data: NbsSong) -> File:

‎nbs2impulsetracker.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,25 @@
2828
from functools import total_ordering
2929
from os import path
3030
from asyncio import sleep
31+
from warnings import warn
3132

3233
from pydub import AudioSegment
3334
from pydub.effects import normalize
3435

3536
from nbsio import Note as NbsNote
3637
from nbsio import NbsSong, Layer, Instrument, VANILLA_INSTS
3738

38-
from common import load_sound, SOUND_FOLDER
39+
from common import SOUND_FOLDER
40+
from audio_common import load_sound
41+
3942

4043
DEFAULT_PATTERN_LENGTH = 64
4144
TRACKER_VERSION = 0xa00a
4245
SAMPLE_PITCH_SHIFT = -10
4346
LAST_SAMPLE_PITCH_SHIFT = 6 # The last need special treatment
4447
DEFAULT_SPEED = 6
48+
MAX_LENGTH = 40000
49+
MAX_CHANNEL = 127
4550

4651
BYTE = Struct("<b")
4752
UBYTE = Struct("<B")
@@ -227,7 +232,7 @@ def delete_missing_instruments(song: NbsSong, insts: list):
227232
for i in reversed(range(len(insts))):
228233
inst = insts[i]
229234
if not inst.filePath or get_audio_segment(inst.filePath) is None:
230-
print(f"Missing instrument: {inst.filePath}")
235+
warn(f"Missing instrument: {inst.filePath}")
231236
missing_indexes.add(i)
232237
del insts[i]
233238

@@ -248,11 +253,15 @@ def delete_missing_instruments(song: NbsSong, insts: list):
248253
async def nbs2it(song: NbsSong, fn: str, dialog=None) -> None:
249254
song.correctData()
250255
header = song.header
251-
layers = song.layers
252256

253257
if not fn.endswith('.it'):
254258
fn += '.it'
255259

260+
song.notes = list(
261+
filter(lambda note: note.tick < MAX_LENGTH and note.layer < MAX_CHANNEL, song.notes))
262+
song.correctData()
263+
layers = song.layers
264+
256265
all_instruments = VANILLA_INSTS[:header.vani_inst] + song.customInsts
257266
delete_missing_instruments(song, all_instruments)
258267

@@ -269,20 +278,13 @@ async def nbs2it(song: NbsSong, fn: str, dialog=None) -> None:
269278

270279
instrument_mapping = {index: i for i,
271280
index in enumerate(instrument_indexes)}
272-
273281
instrument_count = len(instruments)
282+
274283
pattern_length = DEFAULT_PATTERN_LENGTH
275284
pattern_count = ceil(header.length / pattern_length)
276285
while pattern_count > 200 and pattern_length <= 200:
277286
pattern_length += 16
278287
pattern_count = ceil(header.length / pattern_length)
279-
if pattern_length > 200:
280-
pattern_length = 200
281-
max_length = 40000
282-
song.notes = list(
283-
filter(lambda note: note.tick < max_length, song.notes))
284-
song.correctData()
285-
pattern_count = ceil(header.length / pattern_length)
286288

287289
order_count = pattern_count + 1
288290
time_sign = 4
@@ -456,7 +458,11 @@ async def nbs2it(song: NbsSong, fn: str, dialog=None) -> None:
456458
dialog.currentProgress.set(60)
457459
await sleep(0.001)
458460

459-
channels = tuple(map(Channel.from_instance, layers))
461+
channels = list(map(Channel.from_instance, layers))
462+
dummy_channel = Channel("")
463+
if len(channels) < song.maxLayer + 1:
464+
for _ in range(song.maxLayer + 1 - len(channels)):
465+
channels.append(dummy_channel)
460466

461467
def note_converter(note): return Note.from_instance(
462468
note, pattern_length)

‎poetry.lock

+44-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ lxml = ">=4.9.1"
1616
lark = ">=1.0.0"
1717
jsonschema = ">=4.17.3"
1818
nbswave = {git = "https://github.com/Bentroen/nbswave.git"}
19+
loguru = "^0.7.2"
1920

2021
[tool.poetry.dev-dependencies]
2122

0 commit comments

Comments
 (0)
Please sign in to comment.