No sound Piano Hat ( fixed)

No Sound after install of your script everything looked like it went ok the leds come on when the keys are touched but no
sound I do get a comment after I sent the command for the simple piano like (you need some patches ./sounds/ )can you put some light this type of problem as I have tried too times new o/s and loading your scripted and on two different Pi’s and both have same problem,I have checked my HDMI and its ok sound is working ,and there are files in the sounds dir.??

Thanks
skippyone

How are you trying to run the simple-piano.py example file?

Might be that you’re not in the right directory for the example to pick up the sounds folder:

cd ~/Pimoroni/pianohat
python simple-piano.py

Hello and Thanks it is now working I went to Pimoroni/pianohat$ sudo python
simple-piano.py and it worked like a dream so why don’t this command work for me sudo python Pimoroni/pianohat/simple-piano.py l mean it works the keys light up as you touch them but no sound at all but now using the way you have pointed out everything works great it took 2 minutes to put in on a Raspberry Pi half an hour to update/upgrade and load Pimoroni’s Script and 8 hours to get it run ??? thanks

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: ./sounds/ or sounds/

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 /home/pi/sounds

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 __file__ into os.path.abspath like so:

import os

print(os.path.abspath(__file__))

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 ../simple-piano.py

So the proper way to load sounds would be something like:

import os
import glob

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)

print(patches)

TLDR: I was lazy and I need to fix it!