cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="row">
  
  <button  id=play title="play"></button>
<div class="c6 tc l">
  
  <h2>L</h2>
  <div class="waveform tc">
  <label class="sine">
    <input type="radio" name="waveform_l" value="sine" checked>
    <span></span>
  </label>
  <label class="triangle">
    <input type="radio" name="waveform_l" value="triangle">
    <span></span>
  </label>
  <label class="square">
    <input type="radio" name="waveform_l" value="square">
    <span></span>
  </label>
  <label class="sawtooth">
    <input type="radio" name="waveform_l" value="sawtooth">
    <span></span>
  </label>
</div>
  <label>
  <input type="range" id="range_freq_l" min=30 max=2000 value=240>
  </label>
  <label>
    freq
      <input type="number" id="input_freq_l" value=240 min=30 max=2000 step=.1>
  </label>
  
  <label>
  <input type="range" id="range_amp_l" min=0 max=1 step="0.1">
    </label>
  <label>
    amp
    <input type="number" id="input_amp_l" value="0.5" step="0.1" min=0 max=1>
  </label>
  
  
  
</div>

<div class="c6 tc r">
  <h2>R</h2>
  <div class="waveform tc">
  <label class="sine">
    <input type="radio" name="waveform_r" value="sine" checked>
    <span></span>
  </label>
  <label class="triangle">
    <input type="radio" name="waveform_r" value="triangle">
    <span></span>
  </label>
  <label class="square">
    <input type="radio" name="waveform_r" value="square">
    <span></span>
  </label>
  <label class="sawtooth">
    <input type="radio" name="waveform_r" value="sawtooth">
    <span></span>
  </label>
</div>
  <label>
  <input type="range" id="range_freq_r" min=30 max=2000 value=238>
    </label>
  <label>
    freq
    <input type="number" id="input_freq_r" value=238 min=30 max=2000 step=.1>
  </label>
  
  <label>
  <input type="range" id="range_amp_r" min=0 max=1 step="0.1">
    </label>
  <label>
    amp
    <input type="number" id="input_amp_r" value="0.5" step="0.1" min=0 max=1>
  </label>
</div>
</div>
            
          
!
            
              body {
  background-color:#121516;
  margin:0;
  padding:1em 2em;
  font:1.5em monospace;
  color:#def;
}
#play  {
  background:green;
  color:white;
  border:0;
  padding:1em;
  width: 1em;
  height: 1em;
  font-size: 2em;
  line-height: 1;
  border-radius:100%;
  position: absolute;
  left: 50%;
  top: 50%;
 transform:translate(-50%,-50%);
  
  &:before {
    position: absolute;
    left: 50%;top: 50%;
    transform:translate(-50%,-50%);
    content:'▶';
  }
  
  &.playing {
    background:red;
    &:before {
      content:'||';
      font-weight: bold;
    }
  }
}
canvas {
  display: block;
  margin:2em -2em;
}
label {
  display: block;
  margin: 1em 0;
}
.waveform > label {
  display: inline;
  
  > span {
    display: inline-block;
    vertical-align: middle;
    text-align: center;
    width: 2em;
    height: 2em;
    //margin: 0 -.125em;
    background:#424843;
    box-shadow: inset 0 -1px 4px -2px black, inset 0 1px 5px -3px white;
  }
  input {
    display: none;
  }
  input:checked + span {
    background:#323633;
    box-shadow: inset 0 1px 5px -3px black, inset 0 -1px 4px -2px white;
  }
  
  
  &.sine > span {
    &:before, &:after {
      content:'';
      display: inline-block;
      
      border-radius:100%;
      margin-top: .5em;
      width: .75em;
      height: .75em;
      vertical-align: middle;
    }
    &:before {
      border-top:1px solid white;
    }
    &:after {
      border-bottom:1px solid white; 
    }
    
  }
 
  &.triangle > span {
    &:before {
      content:'';
      display: inline-block;
      width: .75em;height: .75em;
      margin-top: .75em;
      border:solid rgba(#fff,.5);
      border-width:1px 1px 0 0;
      transform:rotate(-45deg);
    }
  }
  
  &.square > span {
    
    &:before, &:after {
      content:'';
      display: inline-block;
      margin-top: .5em;
      width: .5em;
      height: .75em;
      border:1px solid rgba(#fff,.5);
    }
    &:before {
      border-bottom: 0;
    }
    &:after {
      border-top: 0;
      border-left: 0;
    }
    
  }
  &.sawtooth > span {
    &:before {
      content:'';
      display: inline-block;
      width: 1em;
      height:1px;
      background:white;
      margin-top: .75em;
      vertical-align: top;
      transform-origin:top right;
      transform:rotate(-28deg);
      margin-left: -0.25em;
      opacity:.5;
    }
    &:after {
      content:'';
      display: inline-block;
      width: 1px;
      height: .5em;
      background:white;
      margin-top: .75em;
      opacity:.5;
      
    }
  }
}
.row {
  position: relative;
  overflow: hidden;
}

.c6 {
  float:left;
  width: 50%;
  
  &.l h2 {
    color:#0f0;
  }
  &.r h2 {
    color:#f00;
  }
}
.tc {
  text-align: center;
}
            
          
!
            
              var wave_left, wave_right;
var isPlaying = false;

function setup() {
  createCanvas(window.innerWidth,200);
  wave_left = new p5.Oscillator();
  wave_right = new p5.Oscillator();
  
  wave_left.start();
  wave_right.start();
  wave_left.freq(1);
  wave_left.amp(0);
  wave_left.pan(-1);
  wave_right.freq(1);
  wave_right.amp(0);
  wave_right.pan(1);
  
  
  update();
}
$("#range_freq_l").bind("input",  function() {
  $('#input_freq_l').val(this.value);   
  update();
});
$("#input_freq_l").bind("input",  function() {
  $('#range_freq_l').val(this.value);   
  update();           
});
$("#range_freq_r").bind("input",  function() {
  $('#input_freq_r').val(this.value); 
  update();              
});
$("#input_freq_r").bind("input",  function() {
  $('#range_freq_r').val(this.value);  
  update();
});
$("#range_amp_l").bind("input",  function() {
  $('#input_amp_l').val(this.value);   
  update();
});
$("#input_amp_l").bind("input",  function() {
  $('#range_amp_l').val(this.value);   
  update();           
});
$("#range_amp_r").bind("input",  function() {
  $('#input_amp_r').val(this.value);   
  update();
});
$("#input_amp_r").bind("input",  function() {
  $('#range_amp_r').val(this.value);   
  update();           
});
$(".waveform input").bind("click", function() {
  update();
})

$("#play").click(function() {
  $(this).toggleClass("playing");
  isPlaying = !isPlaying;
  
  update();
})
var previewTimeoutHandler;
var current_amp_l = 0, current_amp_r = 0;


function update() {
  

  
  if (isPlaying) {
    
    var type_l = $("[name=waveform_l]:checked").val(); 
    if (type_l !== wave_left.getType()) {
      wave_left.setType(type_l);
    }
    
    var type_r = $("[name=waveform_r]:checked").val()
    if (type_r !== wave_right.getType()) {
      wave_right.setType(type_r);
    }
    
    var freq_l = parseFloat(window.input_freq_l.value);
    
    if (freq_l !== wave_left.freq().value) {
      wave_left.freq(freq_l,1);
    }
    var freq_r = parseFloat(window.input_freq_r.value)
    if (freq_r !== wave_right.freq().value) {
     wave_right.freq(freq_r,1);
    }
    var amp_l = parseFloat(window.input_amp_l.value);
    
   if (amp_l !=current_amp_l) {
     current_amp_l = amp_l; 
     wave_left.amp(amp_l,1);
   }
    var amp_r = parseFloat(window.input_amp_r.value);
    
    if (amp_r != current_amp_r) {
      wave_right.amp(amp_r,1);
      current_amp_r = amp_r;
   }
  } else {
    if (current_amp_l > 0) {
      current_amp_l = 0;
      wave_left.amp(0,1);
    } 
    if (current_amp_r > 0) {
      current_amp_r = 0;
      wave_right.amp(0,1);
    }
  }
  
  if (previewTimeoutHandler) clearTimeout(previewTimeoutHandler);
  previewTimeoutHandler = setTimeout(function() {
    updatePreview(); 
  },100);
}

function updatePreview() {
  background(12);
  
  // lines
  var sec_x = Math.floor(width/2);
  
  strokeWeight(1);
  
  for (var x = 0; x <= 2; x++) {
    stroke(96);
    line(x*sec_x - sec_x/2, 0, x*sec_x -sec_x/2, height);
    
    stroke(128);
    line(x*sec_x, 0, x*sec_x, height);
    
    
    var prev_l_y_off = 0, 
        prev_r_y_off = 0, 
        prev_x_off = 0;
    
    var resolution = sec_x * 2;
    
    var freq_l = parseFloat(window.input_freq_l.value);
    var freq_r = parseFloat(window.input_freq_r.value);
    
    var amp_l = parseFloat(window.input_amp_l.value);
    var amp_r = parseFloat(window.input_amp_r.value);
    
    for(var y = 0; y < resolution; y++) {
      
      var x_off = x*sec_x + y/resolution*sec_x;
      
      
    // left
      var l_y_off = Math.sin((x+y/resolution)*freq_l*Math.PI*2);
      if (prev_l_y_off) {
        stroke(0,255,0,90);
        line(prev_x_off, prev_l_y_off * 50 * amp_l + height/2, x_off, l_y_off* 50 * amp_l + height/2)
      }
      //right 
      var r_y_off = Math.sin((x+y/resolution)*freq_r*Math.PI*2);
      if (prev_r_y_off) {
        stroke(255,0,0,90);
        line(prev_x_off, prev_r_y_off * 50 * amp_r + height/2, x_off, r_y_off* 50 * amp_r + height/2)
        
      }
      //combined  
      if (prev_r_y_off && prev_l_y_off) {
        strokeWeight(2);
        stroke(255,255,0,100);
        line(prev_x_off, (prev_r_y_off*amp_r+prev_l_y_off*amp_l) * 50 * (amp_l+amp_r) + height/2, x_off, (r_y_off*amp_r+l_y_off*amp_l)* 50 * (amp_l+amp_r) + height/2);
      }
      
      prev_l_y_off = l_y_off;
      prev_r_y_off = r_y_off;
      prev_x_off = x_off;
      
    }
    
  }
  
}

            
          
!
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