OpenNBS / nbswave

A Python package to render note block songs to a variety of audio formats

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

statically typed Dict problem; and 'int' object has no attribute 'name'

4321ba opened this issue · comments

Hi! This will a much needed improvement for OpenNBS, and also I'm quite excited, as the solution I had bugged me quite a bit
so I've been doing synthesia videos of Minecraft noteblock songs on my youtube channel, and my previous solution was to render the song with fluidsynth and a custom soundfont, however that had quite a lot of drawbacks (especially with more than one of the same note playing)
its pretty cool that there will be this tool that I can automate
I may also try contributing some stuff to gain some experience in that

os: Ubuntu 20.04
python version: 3.8.10
numpy (1.20.1) (this is lower than in the pyproject.toml file :/ )
pydub (0.25.1)
pynbs (0.4.2)

1.

So I had a few errors while trying to get it working, first it said:

File ".../nbs-audio-main/nbsaudio/audio.py", line 139, in Track
    tags: Optional[dict[str, str]] = None,
TypeError: 'type' object is not subscriptable

this is the first time I see static typing in Python so I don't know exactly whats going on, but with
from typing import Optional, Dict importing Dict and changing all the dict to Dict fixed the problem
if this is the right solution, I'd gladly open a pull request with it!

2.

then I got an error in the test thing but that may be because I dont know how to setup the environment,

File "./test.py", line 15, in <module>
    os.environ["USERPROFILE"], "Minecraft Note Block Studio", "Data", "Sounds"
  File "/usr/lib/python3.8/os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'USERPROFILE'

anyway thats also installation dependent, so I just hardcoded the sounds path for testing purposes

3.

so when trying to render the test.nbs file, I got an error that something's not right with the instruments (though I indeed didn't provide any custom instruments, custom_sounds_path was just the absolute path of the normal sounds folder):

Sound file for instrument fizz.ogg couldn't be found; skipping
Sound file for instrument Villager_idle3.ogg couldn't be found; skipping
Sound file for instrument wood_click.ogg couldn't be found; skipping
Sound file for instrument entity.firework.blast_far.ogg couldn't be found; skipping
Sound file for instrument Wither_death.ogg couldn't be found; skipping
Sound file for instrument Villager_accept1.ogg couldn't be found; skipping
Sound file for instrument Custom instrument #7 wasn't assigned; skipping
Traceback (most recent call last):
  File ".../nbs-audio-main/nbsaudio/main.py", line 153, in _mix
    sound1 = self._instruments[note.instrument]
KeyError: 16

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./test.py", line 23, in <module>
    render_audio(song, output, custom_sound_path=custom_sound_path, format="mp3")
  File ".../nbs-audio-main/nbsaudio/main.py", line 222, in render_audio
    renderer.mix_song().save(
  File ".../nbs-audio-main/nbsaudio/main.py", line 194, in mix_song
    return self._mix(self._song.sorted_notes())
  File ".../nbs-audio-main/nbsaudio/main.py", line 157, in _mix
    f"The sound file for instrument f{ins.name} was not found: f{ins.file}"
AttributeError: 'int' object has no attribute 'name'

well idk about this one, but trying one of my nbs files it actually worked so pretty cool :D

1

So I searched a bit and found that I'd need python 3.9 or newer for that syntax: pep and stackoverflow
however I also saw that

from __future__ import annotations

also works, and that is the better way of doing it so I'll close my pr
actually it's already included in nbs.py, it's just not in from audio.py and main.py
but idk if it is acutally needed because it will fix itself after python 3.9 or +

Hi! Thank you for opening the issue, and sorry for taking so long to reply >.<

this is the first time I see static typing in Python so I don't know exactly whats going on, but with
from typing import Optional, Dict importing Dict and changing all the dict to Dict fixed the problem
if this is the right solution, I'd gladly open a pull request with it!

You are correct! I've used this syntax which is supported in Python 3.9 even without the import statement. As you mentioned, you need from __future__ import annotations for it to work on Python 3.7+, and since I'm aiming for compatibility with those versions, not including that statement in those modules (or rather, using typing.Dict instead of dict) was my fault. Your PR was correct, sorry for causing you the extra work to do it! I'll leave this issue open until I fix the problem.

then I got an error in the test thing but that may be because I dont know how to setup the environment,

Yeah... I just hardcoded the path to the NBS 'Sounds' folder in my system 🤦‍♂️

so when trying to render the test.nbs file, I got an error that something's not right with the instruments (though I indeed didn't provide any custom instruments, custom_sounds_path was just the absolute path of the normal sounds folder):

This was addressed in two ways: rendering a sound with custom instruments still missing will now raise a nicer MissingInstrumentException, which can be suppressed by calling the render_audio function with ignore_missing_instruments=True. Thanks for pointing it out :)

Well, this is what you get for making your package public before the first release hahaha :)
Other than 1, both issues were a result of me publishing a completely unfinished package to GitHub. It was naive of me to think no one would be using it at its current state, and I should either have made this more clear in the README, or waited a bit longer to publish the project. Though, I'm glad it has been useful to you even with all those problems :)

On that note, I have good news for ya: the package is now on PyPI, so you can do pip install nbswave to include it in your projects! Hooray! :D

Nicee! :D , I think it was pretty cool that you published it to github before it was ready (though these were pretty minor problems, it worked very well otherwise considering it wasn't stable yet), it was fun trying it out, and it was clear that without any release and without much documentation it was still work in progress

but nice to see that it is now available for wider use ;)