4/6/2023 0 Comments Morse decoder javascriptAs you may or may not know, the Foobar challenge is a secret hiring challenge from Google which you get when you match some search behavior on Google or check Google Documentation. The real magic is where the async/await blocks the main thread forcing a wait before the draw/redraw of the canvas will change the lights color.In the last few days, a friend obtained the invitation to the Google Foobar challenge. The flashlight method just colors the canvas element for a specified amount of time. We do this with the following: generateMorse(time: any, phrase: string) Think of this like literally turning volume on and off on a speaker. We do this by literally setting the gain value to “0” or “1” at specific intervals in time. Here we are going to “schedule” sounds by simulating turning the gain node “on” and “off”. With the Audio Graph setup, we next need to go about the business of creating the sounds. The oscillator connects to the gain which then creates output that is sent to the Speakers (Audio Destination). The three Nodes that are listed here are just part of the Audio Graph. In the graph here, all of the nodes exist within an AudioContext. The resulting Audio Graph should look somewhat like the following diagram: This is important because it becomes the value that we set to “schedule” sounds with the network. Then we set the frequency (pitch) of the oscillator, connect the oscillator to the gain node, and connect the gain node to the speakers (audio destination) with the following: = 0 = 750 (this.gain) () įinally, we set the rate at which the sounds will play and start the oscillator node. Next, we set the value for the gain node to be “0” to simulate it as being off (makes it not play a sound). First, we create an instance of the AudioContext, oscillator, and gain node with the following: dioContext = new AudioContext() this.oscillator = () this.gain = () The createContext method here creates the Audio Graph that we’ve been talking about. The app itself just takes in text, plays the text as Morse Code and also outputs the translated result as in the following screenshot. Please checkout the source code to see how the app was created at the GitHub link I provided above. I’m just going to walkthrough how the main component works with the Web APIs. Since the app follows the basic structure of Angular, I’m not going to do the standard “ng new” and app setup. You can also view the source code for the app on GitHub here. You can play with a running version of the app here. The App is built with Angular and is hosted on Firebase. To create the app, I consulted the blog post here and the associated Gist here to get a better understanding of how to hook all of this up. Now that you’ve had an introduction to Web Audio and Morse Code, let’s get to coding! In the process of learning about Morse Code, I created an app that uses the Web Audio API to generate the “dot” and “dash” sounds that you would normally hear with a telegraph or radio transmission. I’m going to walkthrough implementations using both the Web Audio API and Javascript’s setTimeout function. The rest of this post is going to cover some cool ways to create Morse Code with Javascript. Then I spent subsequent time reading (and coding) with both Javascript Timers and the Web Audio API playing with different methods of sending messages via light (colors) and sound.Īnd ultimately, since a lot of this was kinda cool, I decided to write a post on it. I Googled this for a little while, and found several people used the Web Audio API to generate Morse Code with Javascript. Since I was interested in this, I thought it would be cool to create Morse Code with a program if I could. This technology was ground-breaking when it was developed, and it’s still relevant today. We’ve probably also watched Navy movies where ship commanders are sending the famous “SOS” when a ship is in trouble. We’ve all watched movies about WWII where governments send telegraphs using Morse Code to transmit messages across huge distances. In the computer science world, Morse Code is one of those old-timey technologies that’s still cool.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |