We are very used to generating visual results with code but are still often surprised to hear that we can create music in the same way. Oh, you didn’t know that? You are not alone. Even though the idea of music programming is older than all of us this concept is still pretty unfamiliar among many developers. Not for long! Let's shed some light on music coding in our daily bases web applications using Angular.
What Does The Angular Say? 🦊
AI Generated Video Summary
Today's Talk is about using Angular, Web Audio API, and ToneJS to create custom sounds and instruments in a web application. The speaker demonstrates how to create an audio context, connect sources, and add filters to modify the sounds. They explore different elements in the ToneJS library, such as synthesizers, parts, noise, and audio tracks, to enhance the user experience. The Talk also showcases instruments that play cat and dog samples according to the pitch. Overall, it highlights the possibilities of using these technologies to create unique and interactive audio experiences in web applications.
1. Introduction to Angular Sounds and Web Audio API
Hello, everybody. Today, I want to show you the sounds of Angular using Web Audio API and ToneJS. We can create our own sounds, modify them, and use browsers as synthesizers. To do this, we need to create an audio context, connect sources like microphones or oscillators, and add filters if needed. ToneJS is a recommended library for this. I will demonstrate how to use ToneJS in an Angular web application to create our own instrument.
Hello, everybody. I'm Laura. I am a front-end developer at Swedbank, and today I want to show you the sounds of Angular. Or in other words, to answer, what does the Angular say? What do I mean here? We are very used to generate visuals for the code but are still often surprised that we can do the same thing for the music as well. We can code music.
And if we are looking into all your possibility in the web, that is nothing new. We have different ways how we can handle music in the web as well. But today I want to focus on the most sophisticated way to do that nowadays, which is Web Audio API. And why it is so great? Web Audio API not only enables us to replay some audio track. Web Audio API enables us to create our own sounds to modify them and analyze as well. So basically it enables us to use browsers as synthesizers.
How does that work? If we have some application for that, we would need to create the audio context to store the state of audio. And everything going to happen there. We would create the sources. That can be your microphone, that can be your oscillators, or it can be audio tracks as well. And then we would need to connect that to the destination, which is usually just speakers. If you would like to add some modifications as filters, that would happen in the middle. And of course there are helpers for that. Because we developers, we love libraries. And there are libraries for that as well. One of them is ToneJS. ToneJS is even recommended if you are reading about the web or the API in MDM documentation. And today I'm going to show you a quick demo of how we can use ToneJS in our web application using Angular. What do I have here so far? I just installed the Tone library symbol with npm install. I as well have the instrument component. So as you understand, we will create our own instrument today. In HTML, I'm just iterating through a bunch of notes. And on click event, I'm calling playNode method. In the component itself, I am taking symf element from the Tone library. And in playNode method, I'm just triggering that exact note with the length of 1 eighth.
2. Exploring Modifications and Different Elements
Amazingly, with just a few lines of code, we created an instrument capable of playing any pitch. But we're not stopping there. Let's explore some modifications to make our instrument more unique. We have the Katiana instrument that plays cat samples according to the right pitch. Surprisingly, it was much easier than expected with the help of the ToneJS library. We also have the sampler element, which allows us to map samples to the right note. And don't judge too soon, because I've created something special: the doggyana instrument, which plays dog samples. There are many different elements in this library, including synthesizers, parts, noise, and audio tracks, that can enhance the user experience.
How does that look and sound like? Amazingly, those few lines of code, we created the instrument which is capable to play any pitch you want. But we are not stopping here because so far, we're using just default sounds. And for that, I thought to add some a bit modifications to make our instrument a bit more unique.
We have this is Katiana instrument which plays cat samples but still according the right pitch. And honestly, I was surprised how easy that is to make the ToneJS library, because in my imagination, I thought that I will need to change every sample by myself and then just to map it to the help of Tone library. But that was way easier than this. Let's look into.
We have one more element from Tone library, it is called the sampler. And for sampler initialization, I had to provide the original cat sample, which is here. And then to map it to the right note. So to understand what cat was singing, that was the tough part. But nothing more was needed to provide. All other adjustments were done by the library itself. And you see that in the play note now I'm triggering sampler and synthesizer together.
Now, some of you may think, cat sound instrument, super useful information. But don't judge me too soon. Because for those few of you, I went an extra mile. And I created something else, something special. This is doggyana instrument, which plays dog samples according to the right pitch. But not only this. There are many different elements and I do not want to overhelm you with many different details but just quick overview what you can use in this library. Let's start with different synthesizers, before I was using synth now you see that I have polysynth as well. I can provide different parameters as different oscillators, squares, sines and so on and so on. Then there is the part, part is element which helps you to play predefined notes with predefined instrument. I'm taking for example here chords even from separate constant files and then I have the loop on top of everything and so on. Then different element is noise, noise here I'm not just playing straightly I'm adding the filter, auto filter which makes some waving effect and then back to basics I have some audio tracks I'm playing them randomly and the general parameters is changing tempo, the speed of the microphone, the speed of the mic and speed up a bit so there are many different elements which you can use according to different users actions and usually when we are thinking about front end development our full focus is only on visual parts sometimes we totally forget that we have audio possibility in the dev, but please know that sometimes the right audio can really enrich our visuals and in this way we can create the next level user experience in the dev. And with this, I want to finish up.