Public Solving: Creating a song with JavaScript

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today's puzzle is really cool, I enjoyed every step of it, and the outcome is super fun!
The elves have provided us with a piece of paper with letters on them. They say it should be some kind of music.
It's up to us to find and create sound waves for each letter and see what song they send to us.
You can find the complete puzzle here.
If you can't wait, try out the outcome of today's puzzle on this CodePen. (Hint: turn up your volume)
This challenge has multiple parts to it.
In the first part, we have to build a metronome function. This function will execute a function every x time.
I plan to use an interval to solve this.
This would work best as we have a separate function that should stop whatever happens.
The second part consists of finding the right frequency for a note, for which we will use a simple lookup table.
And then, we should play the melody based on a string. I'll split the string into pieces and call our metronome function to play and remove the letter it played from the string.
We will play the whole song and then stop the metronome function.
Alright, let's get to it.
As mentioned, we'll have to work on the metronome function first.
Let's start with the stopMetronome function. This function should simply erase the current interval that's looping.
export const stopMetronome = () => {
clearInterval(metronomeInterval);
};
As for the start function, we should start by clearing any existing intervals.
clearInterval(metronomeInterval);
Then we need to convert the bpm property into a millisecond interval.
For this, we can use the following formula.
const interval = (60 / bpm) * 1000;
And then we can start our interval. Each loop should execute the function that's being passed to it.
metronomeInterval = setInterval(() => fn(), interval);
And that's it, we created a metronome function.
We then have to make a lookup table to find the frequencies for each letter. I've decided to make a simple lookup table.
const noteToFrequency = {
C: 261.6,
D: 293.7,
E: 329.6,
F: 349.2,
G: 392.0,
A: 440.0,
B: 493.9,
};
And as for the function that should get the frequency for a letter, it's as easy as this:
export const getFrequency = (note) => noteToFrequency[note];
Then we can start on the stopMelody function. This should simply call the stopMetronome function.
export const stopMelody = () => stopMetronome();
Now on to the exciting part, playing the melody. The first thing we need to do is split the string into an array for each character.
const notes = input.split('');
Then we can call our metronome function and pass the bpm and a new function we'll call play.
startMetronome(bpm, () => play(notes));
This play function is where we will actually play the audio and modifications of the notes array.
We evaluate if the array is empty because then we have to stop playing.
if (!notes.length) stopMelody();
Then we need to retrieve the first letter in the array, which we'll be playing.
const note = notes[0];
This could be a letter or an empty space. Only if it's a letter should we play it.
if (note !== ' ') {
playNote(getFrequency(note));
}
And then, we can simply remove the first element from this array.
notes.shift();
That's it! We build our own custom music player in JavaScript.
Let's test it to see if we succeeded.

I enjoyed doing this assignment and would love to hear what you would do differently.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter