Haha. The short answer is that it’s laziness on my part. The long answer;
I’m loading the
sounds folder using a relative path, like:
Usually when you use a path like this Python only looks in the folder it’s currently running, the
cwd or “Current Working Directory”.
This works great when you’re in the right directory, but if you’re in
/home/pi and the sounds are in
/home/pi/Pimoroni/pianohat/sounds then Python has no chance of ever finding them since it’ll be looking in
The correct way to load these files would be to first identify where the Python script you’re calling actually lives. You can do this by passing
os.path.abspath like so:
In this instance,
__file__ is a variable Python sets up for us which contains the path we told it to run. If you ran
python ../simple-piano.py then
__file__ would equal
So the proper way to load sounds would be something like:
BANK_DIR = 'sounds'
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
PATCH_DIR = os.path.join(ROOT_DIR, BANK_DIR, '*')
patches = glob.glob(PATCH_DIR)
TLDR: I was lazy and I need to fix it!