This project is about simulating a piano via a web application that runs using Flask in Python. The web page has 4 pages: Home, Single Octave Piano, Three Octave Piano, and Samples which are constructed using Bootstrap 4 and Font Awesome 4. To make the piano more 'friendly' I used an RGB LED for different musical notes pressed on the piano. Also, I added 3 samples of songs: Coffin Dance, Smells Like Teen Spirits, and Sweet Dreams, which can be played either through the Samples page, either through those 3 push buttons on the breadboard.
ImagesPiano musical notes frequencies are stored in a JSON file. The program runs on 2 threads: one for the web application and another one for the 3 push buttons inputs. The Http requests are sent using ajax and jquery.
Each song sample has its own class. They extend Abstract_Song class and have methods that contain specific sequences of notes and delays. If the project is cloned, each user can add their own songs there if they want to.
Each musical note has its own color displayed on the RGB LED. The colors are as follows:
- C (Do) - red
- C# (Do Major) - pale cyan
- D (Re) - yellow
- D# (Re Major) - pale magenta
- E (Mi) - green
- F (Fa) - cyan
- F# (Fa major) - orange
- G (Sol) - blue
- G# (Sol major) - pale yellow
- A (La) - magenta
- A# (La major) - pale blue
- B (Si) - white
Note: The pale colors could be displayed a little bit differently, according to your RGB LED. You can change those colors in the raspberrypi_configurations.py file.
How to run the programBecauseof the PWM output from the Rpi.GPIO library is not stable enough, we are going to use the pigpio library to use the hardware PWM capability of the development board.
Pigpio is a library that allows control over all the GPIO pins on a raspberry pi, just as the Rpi.GPIO library. It usually comes pre-installed with the Raspbian.
If you don't have it, install it using the following commands:
wget abyz.me.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install
In order to start the application, you need to do the following steps:
1) Run a pigpiod daemon:
sudo pigpiod
Every time you start the terminal you need to start the pigpiod daemon if you want to use a script that uses the pigpio library.
2) Install flask web framework in python:
sudo pip3 install flask
3) Clone the Github repository:
git clone https://github.com/enaky/My_Little_Piano.git
4) Run the application using the following command:
python3 my_little_piano_app.py
The server runs on port 2014. You can change the port in the my_little_piano_app.py. The link for the website is:rasp_ip
:2014/
where rasp_ip - is your raspberry pi IP address. Example: 192.168.1.106:2014/
Note: You can find this IP address typing the following command in the terminal of your raspberry pi.
ifconfig
Project Structure and CodeNote: The total code contains 1870 lines including blank spaces.
So, please check the Github Page for full code.
Short Project Structure Modules Analysis:
- raspberrypi_configurations - contains all the configurations for raspberry Pi including buzzer and RGB led colors settings
- resources - contains the JSON which holds all 7 octaves of the piano with notes frequencies
- samples - includes the abstract class abstract_song.py which can be extended to implement a new song and three implemented song classes included in sweet_dreams.py, coffin_dance.py and smells_like_teen_spirit.py
- utilities - includes loading the JSON function into an array or JSON object
- web_services - contains all files for the web application
1) web_services.py holds the flask routes which provides to assign URLs in our app to functions easily
2) templates - contains 4 HTML files (HTML and js) for the 4 pages of the website (Bootstrap 4 and Font Awesome 4 are used)
3) static - contains the CSS for the piano octaves and the images used in the web pages (Home Page and Samples Page)
The main program is located in my_little_piano_app.py. Here are included all the modules specified above and also a function is constructed here (for playing the samples using the pushbuttons) which runs on a different thread.
Song Samples- Sample 1 - Coffin Dance
- Sample 2 - Smells Like Teen Spirits
- Sample 3 - Sweet Dreams
Comments