The Other Side of JavaScript - Audio Signal Scope
If you are currently new to learning JavaScript, I suggest you to learn the basics of JavaScripts before reading this article.
Because in this article I will explain some built-in JavaScript functions that you rarely heard before.
Introduction
Previously I apologize if any of my writing is wrong, because I am also not a person experienced in this field.
Are you a person who likes music or sound? when you enjoy the sound of the sound sometimes your limbs will follow the beat and rhythm that is heard by your ears. And there are times when you yourself want to know visually how the rhythms of the rhythm are actually shaped wave frequency.
![]() |
| Image from Google Images. |
Quoted from Wikipedia, Sound or sound is mechanical compression or longitudinal waves propagating through the medium. This medium or intermediate agent may be a liquid, solid, gas. Thus, sound waves can propagate for example in water, coal, or air.
Geeting Deeper
If you are a web developer, almost every day you are dealing with a programming language called JavaScript but the question is how do you know about JavaScript? If I explain here it may be too long with the development of technology in JavaScript is so fast every day.
And as a note in this article I will explain some built-in JavaScript functions in ES5 format (ECMA Script 5), maybe you already know about React, Angular, Vue, Meteor or other framework or also the most popular (but soon abandoned) jQuery.
But does JavaScript only serve as a library? plugin? or we call it as a framework? which in some cases only takes care of the DOM (Document Object Model) interaction and manipulation. Of course not, JavaScript is more than just those things JavaScript also has some other functions that you may rarely hear but you've used them.
Let's Write Your Code
And now we will start to write the codes that we will use to eksplore Audio on the web with JavaScript, first of course we prepare first a file. Html which will be the main page of this project. We call index.html and its structure as below:
As in the file. Html generally we start with <html> to </ html>, in the code above you can see I added the element canvasyang will be where we draw the frequency in the form of lines. Furthermore, the audio tag into the songplayer as well as the source that we will be again into frequency arrays.
And the input file that will insert the song file into the audio tag that we have prepared, if the open file above will produce a display like this:
![]() |
| The initial view when the index.html file is opened in the browser. |
Next on line 32 and beyond in the index.html file we will begin to create a process that will convert sound into frequency lines.
And first we write down some code as follows, which serves as a variable containing source, player, canvas, color & blob URL that we will use again later.
![]() |
| The code snippet containing the required variables. |
On line 11-13 we will check if the browser does not support window.URL it will appear warning.
Then after checking condition we add event to detect when user enter (upload) file through input element then we process the file and we convert to ObjectURL so that we can input into audio tag which we have prepared. Finally we play songs that have been input by the user.
![]() |
| The first function when it detects any changes to the input file. |
In line 13 in the code above there is a code that calls the function stream () that serves to process the Audio that is rotated into AudioContextuntuk later in the analysis into the Analyzer.
Here's the code snippet:
![]() |
| The stream () function that connects the Audio tag with the AudioContext function. |
In the above stream function we start by initializing AudioContext into the audio variable and Analyzer initialization into the variable analyzer, then we detect when the player (audio tag) plays the music (addEventListener 'showplay' event) we input the player into AudioContext.
Then we connect again AudioContext with Analyzer then we connect Analyzer with AudioContext destination.
And after that we set fftSize (line 12) to 128, this is the number of frequencies we will monitor in the form of a point (node) the higher the number the more points will be displayed, more about Fast Fourier Transform (fftSize ) you can read here.
Next reopen this project in the browser and try input a song on the input file, then the console tab will bring up the array as follows:
![]() |
| Initial results when the stream () function is executed. |
Then, why not come out yet on the canvas? Patient used to live a little longer. Next we will add a function that will change the dots of the array into a line that will be displayed into the canvas.
The function is as follows, you can add this script under console.log (frequency) code.
![]() |
| Function to draw a line in real-time based on frequency in Audio player. |
The function above is quite long because in this function we will convert the frequency we have got from the previous code into a line.
On line 2 we initialize canvas with 2D context then next in draw () we do request animation frame which will redial the draw () function every second.
Then on the bottom line we just write the code to draw canvas based on the frequency of each second that we get from analyser.getByteFrequencyData (frequency) function, last we add one more function to change the color of the line random.
![]() |
| Additional function to change the color of the line randomly. |
And the end result is as follows:
![]() |
| The final result of this article. |
And the complete code from the above code snippet (please uncomment console.log to display the frequency array).
![]() |
| The index.html file is complete. |
It's not been a few minutes since you read this article and I think it's time I retreat. Thank you very much in advance for reading this article.
Labels: Techno














0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home