<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Atharva Pingale </title>
    <description>The latest articles on DEV Community by Atharva Pingale  (@atharva0300).</description>
    <link>https://dev.to/atharva0300</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F676503%2Ff8b8542c-7c79-4816-b1ca-c66cb8cdb49a.png</url>
      <title>DEV Community: Atharva Pingale </title>
      <link>https://dev.to/atharva0300</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atharva0300"/>
    <language>en</language>
    <item>
      <title>Creating a Piano using Reactjs - Audio( ) WebAPI and Hooks</title>
      <dc:creator>Atharva Pingale </dc:creator>
      <pubDate>Tue, 12 Jul 2022 12:59:44 +0000</pubDate>
      <link>https://dev.to/atharva0300/creating-a-piano-using-reactjs-audio-webapi-and-hooks-1p2a</link>
      <guid>https://dev.to/atharva0300/creating-a-piano-using-reactjs-audio-webapi-and-hooks-1p2a</guid>
      <description>&lt;p&gt;I recently participated in the &lt;strong&gt;Hackbytes hackathon 2022 on Devfolio&lt;/strong&gt;, where I had created a piano using Reactjs. It was too good to be real. So, I thought of sharing the processs of actually creating one using Reactjs. Note that I will be ignoring the CSS Part and will only be focussing on Javascript.&lt;/p&gt;

&lt;p&gt;So get Ready !&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Download all the key sounds in mp3 format&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing it to download all the key sounds. You must have a minimum of 7 white key sounds + 5 black key sounds = 12 key sounds ( 1 octave ) of any scale.&lt;br&gt;&lt;br&gt;
I recommend downloading all the mp3 files from here - &lt;a href="https://freesound.org/people/Tesabob2001/packs/12995/" rel="noopener noreferrer"&gt;https://freesound.org/people/Tesabob2001/packs/12995/&lt;/a&gt;&lt;br&gt;
OR you can download the zip file of my repo on Github and navigate to - &lt;em&gt;/client/src/Components/Music/Sounds&lt;/em&gt; and extract all of them.&lt;br&gt;&lt;br&gt;
Store all the mp3 files in a folder named 'Sounds'&lt;br&gt;
Rename all the files in the below format for simplicity.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqr31h9pqmmm43wxclot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqr31h9pqmmm43wxclot.png" alt="audio files name"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Javascript Code&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now comes the fun part! Which is the code. Create a component first ( a js/jsx file ). We will then have to create an object of the class ' &lt;em&gt;Audio&lt;/em&gt; ' and access its properties to play the sound.&lt;br&gt;
&lt;em&gt;Sample code :&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96vbhdimwkrt0kjy9i3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96vbhdimwkrt0kjy9i3m.png" alt="sample audio code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be creating an object for each and every mp3 file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const C4 = new Audio(require('./Sounds/C4.mp3'));
  const Db4 = new Audio(require("./Sounds/Db4.mp3"));
  const D4 = new Audio(require("./Sounds/D4.mp3"));
  const Eb4 = new Audio(require("./Sounds/Eb4.mp3"));
  const E4 = new Audio(require("./Sounds/E4.mp3"));
  const F4 = new Audio(require("./Sounds/F4.mp3"));
  const Gb4 = new Audio(require("./Sounds/Gb4.mp3"));
  const G4 = new Audio(require("./Sounds/G4.mp3"));
  const Ab4 = new Audio(require("./Sounds/Ab4.mp3"));
  const A4 = new Audio(require("./Sounds/A4.mp3"));
  const Bb4 = new Audio(require("./Sounds/Bb4.mp3"));
  const B4 = new Audio(require("./Sounds/B4.mp3"));
  const C5 = new Audio(require("./Sounds/C5.mp3"));
  const Db5 = new Audio(require("./Sounds/Db5.mp3"));
  const D5 = new Audio(require("./Sounds/D5.mp3"));
  const Eb5 = new Audio(require("./Sounds/Eb5.mp3"));
  const E5 = new Audio(require("./Sounds/E5.mp3"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to create a function named &lt;em&gt;playSound()&lt;/em&gt; which will accept audio object as an argument, create a cloned node and play the audio. &lt;br&gt;
More information about &lt;em&gt;cloneNode()&lt;/em&gt; can be found on the official docs - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode" rel="noopener noreferrer"&gt;cloneNode() MDN Docs&lt;/a&gt; | &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio" rel="noopener noreferrer"&gt;HTMLAudioElement MDN Docs&lt;/a&gt; | &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play" rel="noopener noreferrer"&gt;play() MDN Docs&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const playSound = audio =&amp;gt; {
    // creating a cloned node of the audio object
    const clone = audio.cloneNode()

    // calling the play() method to play the mp3 file
    clone.play()
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is importing the &lt;strong&gt;useState hook&lt;/strong&gt; and we will be creating a variable named the piano key and a modifier function which will have a boolean value. This will toggle the state of the variable when we click on a particular key ( meaning - that when we click on a button ). If the key is clicked, we set the toggle_Key_ to ' &lt;strong&gt;True&lt;/strong&gt; ' , else ' &lt;strong&gt;False&lt;/strong&gt; '.We will be creating useState hooks for each and every key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // keys 
  const [C4Key , toggleC4key] = useState(false);
  const [Db4Key , toggleDb4key] = useState(false);
  const [D4Key , toggleD4key] = useState(false);
  const [Eb4Key , toggleEb4key] = useState(false);
  const [E4Key , toggleE4key] = useState(false);
  const [F4Key , toggleF4key] = useState(false);
  const [Gb4Key , toggleGb4key] = useState(false);
  const [G4Key , toggleG4key] = useState(false);
  const [Ab4Key , toggleAb4key] = useState(false);
  const [A4Key , toggleA4key] = useState(false);
  const [Bb4Key , toggleBb4key] = useState(false);
  const [B4Key , toggleB4key] = useState(false);
  const [C5Key , toggleC5key] = useState(false);
  const [Db5Key , toggleDb5key] = useState(false);
  const [D5Key , toggleD5key] = useState(false);
  const [Eb5Key , toggleEb5key] = useState(false);
  const [E5Key , toggleE5key] = useState(false);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is Creating the onClick() function. This is the function which will be called when a button is clicked. An event object will be passed to the function which will invoke 2 functions - &lt;em&gt;playSound()&lt;/em&gt; and &lt;em&gt;toggleKey()&lt;/em&gt; functions. We will be creating onClick() functions for each and every key ( ie- 12 keys ).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const playC4 = (e) =&amp;gt; {
    playSound(C4);
    toggleC4key(true);
    setTimeout(() =&amp;gt; {
        toggleC4key(false);
    }, 200);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create similar functions named - playDb4, playD4, playEb4 .... and pass the respective audio objects to the &lt;em&gt;playSound()&lt;/em&gt; function. &lt;br&gt;
The &lt;strong&gt;setTimeout()&lt;/strong&gt; function here is used as a time Period for which the particular key will be in active ( toggled as true ) state ( ie - &lt;em&gt;for how long is it pressed&lt;/em&gt; ). I have set the value to 200ms, which after 200ms the key will be toggled off. &lt;/p&gt;

&lt;p&gt;Next, will be creating a button element and defining the &lt;strong&gt;onClick() handler&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button className='white-btn C4' onClick={(e)=&amp;gt; playC4(e)} /&amp;gt;
&amp;lt;button className='Db4' onClick = {(e) =&amp;gt; playDb4(e)} /&amp;gt;
&amp;lt;button className='white-btn D4' onClick={(e)=&amp;gt; playD4(e)} /&amp;gt;
&amp;lt;button className='white-btn E4' onClick={(e)=&amp;gt; playE4(e)} /&amp;gt;
&amp;lt;button className='white-btn F4' onClick={(e)=&amp;gt; playF4(e)} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Replicate the buttons for the rest of the keys as well and the piano should be done! You can check the code on my github for any help - &lt;a href="https://github.com/atharva0300/Hackbytes-2022/blob/main/client/src/Components/Music/TimeLine1.js" rel="noopener noreferrer"&gt;Code&lt;/a&gt; | all the mp3 audio files can be extracted from &lt;a href="https://github.com/atharva0300/Hackbytes-2022/tree/main/client/src/Components/Music/Sounds" rel="noopener noreferrer"&gt;Audio Files&lt;/a&gt; | Repo Link - &lt;a href="https://github.com/atharva0300/Hackbytes-2022" rel="noopener noreferrer"&gt;Github Repo&lt;/a&gt; | Use the app here &lt;a href="https://hackbytes-2022.herokuapp.com/music" rel="noopener noreferrer"&gt;Heroku App&lt;/a&gt;. Cover Image credits : &lt;a href="https://wallpaperaccess.com/react-js" rel="noopener noreferrer"&gt;Wallpaper Access&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>github</category>
    </item>
  </channel>
</rss>
