-
Notifications
You must be signed in to change notification settings - Fork 2
Note Parser
This page includes some insight and documentation about the Note Parser module of the project
[TOC]
The initial goal was to play any music file on the speaker, but I realised that it was simply not possible given the capabilities of a typical micro-processor speaker. The speaker was only capable of playing 1 frequency at a time which introduced a hurdle.
Although generic music formats can't be played on the speaker, you can still replicate playing songs by writing the corresponding frequency of each note and looping through the set.
My first attempt was to manually read the musical notes from a sheet, and write down each note's corresponding frequency (translation). This was fine for a couple of songs, but is simply too time consuming for adding a larger library and would definitely not fulfil the requirement we had of easily maintaining songs. Surely there had to be a way of automating that time consuming step.
I knew that the translating step was only following simple logic which could be automated, you only need a few requirements:
- Consistent format which contained all the necessary sound data
- Efficient format since we can't have too much data due to the limited resources
- Easy enough so that anyone who has understanding of musical instruments can add their own songs
- Widely used so that anyone who does not have an understanding of musical instruments can add their favourite song
I eventually found a site which has lots of songs written as piano notes in a specific format, and after thinking whether to go with the MIDI format or a simple piano format, I just went with the piano one for the sole reason of being more readable to the non technical user.
After deciding the format, the only job left to do was to parse the notes to their corresponding frequency.
This was fairly simple, just a matter of iterating through the notes from left to right and swapping each note with its corresponding frequency. The only problem was when 2 notes were being played simultaneously, for this I just had to accept the quality loss and went with the higher note.
The missing piece of the puzzle was now to link the parser with the rest of the system, for this the plan was to use MQTT. This protocol depends on a simple publishing and subscribing mechanism were the communication channels are called topics
I wanted to have a consistent and super easy procedure for adding future subscriptions to the project.
- A subscription must be addable by simply adding a new class which includes it's corresponding topic
- The program shall load all the classes and automatically take care of subscribing
So the MQTTConnection part of the project was designed as follows
{width=50%}
The steps to add a new subscription are:
- Create a new class which extends
AbstractSubscription - Pass the subscription string to the parent constructor
- override the
messageArrivedmethod (optional)
And the program will then automatically load the listeners and take care of everything else for you
