css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <head>
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">

</head>
<div id="root"></div>

            
          
!
            
              body{
  margin:0;
  overflow:hidden;
  position:absolute;
  width:100%;
  height:100%;
  display:flex;
  justify-content:center;
  align-items:center;
  background-color: #FF51A6;
  font-family:'Inconsolata', monospace;
}

#root{
  max-height:95%;
  min-height:60%;
  width:700px;
  display:flex;
  flex-wrap:wrap;
  position:relative;
  
}

#drum-machine {
  border-style:solid;
  width:100%;
  background:#FF5351;
}

#display{
  padding:5px;
  display:flex;
  background:ORANGE;
  align-self:center;
  justify-content:center;
  font-size:20px;
}
/*div where bank button lies*/
#bankContainer{
  justify-content:center;
  display:flex;
}
/*Bank Button*/
#bank{
  font-size:15px;
  background-color: #F0D264;
  margin-top:5px;
  cursor:pointer;
  border-radius: 6px;
}
  button:focus {outline:0;}

#pads{
  display:flex;
  flex-wrap:wrap;
  justify-content:center;
}
.drum-pad{
  box-sizing:border-box;
  margin:20px;
  background:#51FF53;
  text-align:center;
  position:relative;
  width:150px;
  height:3em;
  cursor:pointer;
  box-shadow: 0 0 5px -1px rgba(0,0,0.2);
  border-radius: 6px;
}

.drum-pad:active {
  background:#A6FF51;
  box-shadow: 0 0 5px -1px rgba(0,0,0,0.6);
}

#power{
  border-radius:50%;
  height:50px;
  width:50px;
}
#bottomControl{
  display:flex;
  justify-content:space-around;
}

#vol{
  margin-top:17px;
}

/*below is to toggle input button*/
#power{
  background-color:white;
  border-style:white;
}

.footerControl{
  text-align:center;
  width:120px;
  display:inline-block;
  padding-bottom:10px;
}
.caption{
  display:block;
}
            
          
!
            
              const bankOne = [
  {
    name:"kick_n_Hat",
    key:"Q",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3"
  },
  {
    name:"Heater-1",
    key:"W",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"
 },
  {
    name:"Heater-2",
    key:"E",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"
  },
  {
    name:"Heater-3",
    key:"A",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3"
  },
  {
    name:"Heater-4_1",
    key:"S",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"
  },
  {
    name:"Heater-6",
    key:"D",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"
  },
  {
    name:"Dsc_Oh",
    key:"Z",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"
  },
  {
    name:"Cev_H2",
    key:"X",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"
  },
  {
    name:"RP4_KICK_1",
    key:"C",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"
  }
];

const bankTwo = [
  {
    name:"Chord_3",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3"  
  },
  {
    name:"Chord_1",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3",
  },
  {
    name:"Chord_2",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3"
  },
  {
    name:"Give_us_a_light",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3"
  },
  {
    name:"Dry_Ohh",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3"
  },
  {
    name:"Bld_H1",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3"
  },
  {
    name:"punchy_kick_1",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3"
  },
  {
    name:"side_stick_1",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3"
  },
  {
    name:"Brk_Snr",
    sound:"https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3"
  }
  ];
// ‘https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3 ’


class DrumMachine extends React.Component {
  
  constructor(props) {
    super(props);
    this.state = {
      power:false,
      display: 'Turn the Power On',
      volume:0.4,
      bank:true
    };
    
     this.playSound = this.playSound.bind(this);
     this.keyPlaySound = this.keyPlaySound.bind(this);
     this.onOrOff = this.onOrOff.bind(this);
     this.changeVolume = this.changeVolume.bind(this);
    this.changeBank = this.changeBank.bind(this);
    
  };
 
  

  
  //the below function is for playing the audio. 
 playSound(event){
   if(this.state.power === true){
       const sound = event.target.lastChild //Grabs the last element in the Drum component which is the audio element.
       sound.parentElement.style.backgroundColor = "#51FF53";
       sound.volume = this.state.volume;
        setTimeout(() => { sound.parentElement.style.backgroundColor = '#A6FF51';}, 100);
        sound.currentTime = 0; 
        sound.play(); // plays the actual event after it has been clicked. 
       //Below changes the display. 
       this.setState({
         display: event.target.id
       })  
      };
   }
  
  keyPlaySound(event){
      const audioRec = document.getElementById(event.key.toUpperCase()); 
      if(audioRec !== null){ //If any of the keys that are pressed matches a div I.D proceed to second step
        audioRec.parentElement.click();// gets the parent element of audioRec which will be the Drum <input>.
      }; 
  }
  
  //Should toggle the Drum Machine between Power on and Power off.
  onOrOff(){
   if(this.state.power){
    this.setState({
      power:!this.state.power,
      display:"OFF"
    });
   }else{
     this.setState({
       display:"Initializing..."
     });
     //Below gives the illusion of a boot-up process when drum pad is powerd on.
     setTimeout(function(){
       this.setState({
       power:!this.state.power,
       display:"ON"
     });
    }.bind(this),1000)
   }
  }
  
      //Below function should control the volume
  changeVolume(event) {
    this.setState({
      volume: event.target.value
    });
  }
  changeBank(event) {
    this.setState({
      bank:!this.state.bank
    });
  }

  
  
    componentDidMount() {
    document.addEventListener("keydown", this.keyPlaySound);
  };
  
  componentWillUnmount() {
    document.removeEventListener("keydown", this.keyPlaySound);
  };
  
  render() {
    return(
    <div id="drum-machine">
      <div>
        <Display power={this.state.power} disp={this.state.display} />
      </div>
      <div>
        <Bank switchBank={this.state.bank} changeUrl={this.changeBank}/>
      </div>
      <div id="pads">
       {bankOne.map((word,index) => <Drum clicker={this.playSound} uniId={this.state.bank ? bankOne[index].name : bankTwo[index].name} kyPress={bankOne[index].key} sound={this.state.bank ? bankOne[index].sound : bankTwo[index].sound} />)}
      </div>
        <div id="bottomControl">
        <div class="footerControl">
          <Switch setPower={this.onOrOff} power={this.state.power} />
          <span class="caption">Power</span>
        </div>
        <div class="footerControl">
          <Volume  vol={this.state.volume} change={this.changeVolume} />
          <span class="caption">Volume</span>
        </div>
        </div>
 
    </div>
    )
  };
}
//On or Off button on the bottom of the screen
function Switch(props) {
  return (
    <button id="power" onClick={props.setPower}>
    {props.power ? "OFF" : "ON"}
    </button>
  );
}
//Displays Power on Or Off on the display.
function Display(props) {
  return (
    <div id="display">{props.disp}
      </div>
  );
}

function Bank(props) {
  return (
    <div id="bankContainer">
      <button id="bank" onClick={props.changeUrl}>{props.switchBank ? 'Bank One' : "Bank Two"}</button>
    </div>
  )
}

//plays sound and created Div for each element. 
function Drum(props) {
    return(
       <div className="drum-pad" id = {props.uniId}  onClick={props.clicker}>
       {props.kyPress}
       <audio id={props.kyPress} src={props.sound} />
     </div>
 )   
}         

function Volume(props){
  return(
    <input id="vol" type="range" id="vol" max="1" min="0" step="0.01" value={props.vol} onChange={props.change} />
  )
}


ReactDOM.render(
<DrumMachine />,document.getElementById('root'))
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console