Third party cookies may be stored when visiting this site. Please see the cookie information.

Home Family Days Out Raspberry Pi & Electronics Blog

Stewart's Blog

Stewart Watkiss website to the world ...

(Children, computing, first aid and other ramblings ...)

JLL Piano – Raspberry Pi Music Clip player

This project is called the JLL piano. This is a school project that my daughter created for her music homework. She needed to create a project that related to Jerry Lee Lewis and she created a mini piano that plays a short clip for a different song depending upon the key that is pressed.

Raspberry Pi Piano Hat

It uses a Raspberry Pi with a Pimoroni Piano Hat.

You first need to setup the Piano Hat using the Pimoroni Piano Hat install instructions. Then create the following in a text editor.
The program will work in Python 2 or Python 3.

You can of course change the music clips to be any music you prefer.

import pianohat
import time
from pygame import mixer

# Change key numbering (black keys = -1)
keys = [0,-1,1,-1,2,3,-1,4,-1,5,-1,6,7]

sound_files = [
"/home/pi/MusicClips/jll-wholelotofshaking.wav",
"/home/pi/MusicClips/jll-greatballsoffire.wav",
"/home/pi/MusicClips/jll-milkshake.wav",
"/home/pi/MusicClips/jll-mynameisjll.wav",
"/home/pi/MusicClips/jll-itllbeme.wav",
"/home/pi/MusicClips/jll-highschoolrocking.wav",
"/home/pi/MusicClips/jll-waitinforatrain.wav",
"/home/pi/MusicClips/jll-whenimshaking.wav"
]

def inc_volume(ch,evt):
if evt:
change_volume(1)

def dec_volume(ch,evt):
if evt:
change_volume(-1)

# Change volume by amount specified 0 to 10 (+1, -1 etc.)
def change_volume (change):
#print ("Change volume "+str(change))
if change == 0 : return
# volume is 0 to 1 so divide by 10
delta = change / 10
new_volume = sounds[0].get_volume() + delta
if new_volume > 1:
new_volume = 1
if new_volume < 0: new_volume = 0 for this_sound in sounds: this_sound.set_volume(new_volume) def handle_note(channel, pressed): if pressed: channel = keys[channel] if (channel < 0): return mixer.stop() sounds[channel].play(loops=0) mixer.init(22050, -16, 2, 512) mixer.set_num_channels(13) sounds = [mixer.Sound(sound_file) for sound_file in sound_files] pianohat.on_note(handle_note) pianohat.on_octave_up(inc_volume) pianohat.on_octave_down(dec_volume) while True: time.sleep(0.001)

Please leave comments using the facebook comments below.


UK days out, children and holiday information is also available on the Days Out Diary web site
Linux, LPI and the Quiz / Test Program posts are also available on the Penguin Tutor website
First Aid Information, first aid games and first aid practice test / quiz entries are also available on the First Aid Quiz Web site