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.

            
              <script id="shader-vs" type="x-shader/x-vertex">
  
  precision mediump float;
 
  varying vec2 vUv;
	
  void main() {
		gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
		vUv = uv; 
	}
</script>

<script  id="shader-fs" type="x-shader/x-fragment">
	    
  precision mediump float;
 
  #define pi 3.14159265358979323846
  #define pi2 pi * 2
        	
  /* three */
  
	varying vec2 vUv;
  varying vec3 vNormal;
  
	/* red-moon*/
		   		
 uniform vec2 size;        	
 uniform float time;
        	
  void main() {
				
    vec2 pos = -1.0 + 2.0 * vUv;	
	   
	  vec2 r = vec2( size.x/ size.y, 1);
		vec2 p = ( gl_FragCoord.xy / size.y ) - r / 2. ;
      
		p *= 2.;
    
    float sin_t = 0.99 * sin(time), cos_t = 0.99 * cos(time);
    
    vec3 n = normalize( vec3(p, sqrt( p.x*p.x + p.y*p.y) ) ), light = vec3( cos_t, cos_t * sin_t, sin_t ) ;
    
    float red = max(0.0, dot( light, n ) );
   
    gl_FragColor = vec4( vec3(red, .0, .0), 1.0);
    
	}
  
</script>

<canvas id="canvas"></canvas>
<button id="button">CLICK TO PLAY SOUND</button>
            
          
!
            
              body {
  margin: 0;
  padding: 0;
  background: #000;
  overflow: hidden;
  height: 100%;
  width: 100%;
}

canvas {
  display: block;
  width: 100%;
  top: 0;
  left: 0;
  
  position: absolute;
}

#canvas{
  z-index:0;
  height: 100%;
  width: 100%;
  z-index: 0;
}

#button{
  position: absolute;
  top: 80%;
  left: 50%;
  margin-left: -150px;
  z-index: 666;
  color: white;
  background: transparent;
  height: 20px;
  letter-spacing: 2px;
  width: 300px;
  outline: none;
  cursor: pointer;
  border: 0;
}
            
          
!
            
              var gui,
    scene,
    camera,
    render,
    container,
    orbit,
    mesh,
    material,
    renderer,
    stats,
    clock,
    canvas;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

const MIN_FQ = 0.1;
const MAX_FQ = 1000;
const VOL_MAX = 0.4

var ui = {
  volume: 0.25,
  binaural: 5,
  balance: 0.5,
  frequency: MAX_FQ * 0.75
};

var audio, bbeat, volume;

var rightNode, leftNode, volumeNode, compressorNode, leftRightNode, pannerNode, listener, audioContext;

var time, delta, t = 0;

var isPlaying = false;

// resonance audio 
let resonanceAudioScene;

/* */

function init() {

  var h = window.innerHeight,
    w = window.innerWidth; //< max_width ? window.innerWidth : max_width;

  gui = new dat.GUI();

  canvas = document.querySelector('#canvas');

  scene = new THREE.Scene();

  camera = new THREE.PerspectiveCamera(45, w / h, 0.1, 250);
  camera.aspect = w / h;
  camera.lookAt(scene.position);

  clock = new THREE.Clock();

  renderer = new THREE.WebGLRenderer({ antialias: false, canvas: canvas});
  renderer.setSize(w, h);
  renderer.setViewport(0, 0, w, h);
  renderer.setClearColor(0x000000);

  /* mesh */

  material = new THREE.ShaderMaterial({
    uniforms: {
      time: {
        type: "f",
        value: 0.0
      },
      size: {
        type: "v2",
        value: new THREE.Vector2(w, h)
      }
    },
    vertexShader: document.getElementById("shader-vs").textContent,
    fragmentShader: document.getElementById("shader-fs").textContent
  });
  
  mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(50, 50, 1, 1), material);
  
  camera.position.set(0, 0, 25);

  scene.add(camera);
  scene.add(mesh);

  var uiFolder = gui.addFolder("binaural");
  //https://de.wikipedia.org/wiki/Planetent%C3%B6ne

  uiFolder
    .add(ui, "volume", 0.0, VOL_MAX )
    .step(0.01)
    .onChange(update_ui);
  //folder_audio.add(ui, 'balance', 0, 1).step(.01).onChange(update_ui);
  uiFolder
    .add(ui, "binaural", 0.1, 100)
    .step(0.01)
    .onChange(update_ui);
  uiFolder
    .add(ui, "frequency", MIN_FQ, MAX_FQ)
    .step(0.01)
    .onChange(update_ui);
  uiFolder
    .open();

  /* audio/binaural part */
  console.log("audio context exist", !!window.AudioContext, window.AudioContext)
  window.AudioContext = window.AudioContext || window.webkitAudioContext;
  if ( !(!!window.AudioContext) ) {
    console.log('no audio context');
    return;
  }

  audioContext = new AudioContext();

  // Create a (first-order Ambisonic) Resonance Audio scene and pass it
  // the AudioContext.
  //resonanceAudioScene = new ResonanceAudio( audioContext );
  
  // Connect the scene’s binaural output to stereo out.
  //resonanceAudioScene.output.connect(audioContext.destination);

  rightNode = audioContext.createOscillator();
  rightNode.waveType = "sine";
  rightNode.frequency.setValueAtTime(ui.frequency + ui.binaural,    audioContext.currentTime)

  leftNode = audioContext.createOscillator();
  leftNode.waveType = "sine";
  leftNode.frequency.setValueAtTime(ui.frequency, audioContext.currentTime);

  //panner node
  pannerNode = audioContext.createPanner();
  pannerNode.panningModel = "HRTF";
  pannerNode.distanceModel = "inverse";
  pannerNode.refDistance = 1;
  pannerNode.maxDistance = 1000;
  pannerNode.rolloffFactor = 1;
  pannerNode.coneInnerAngle = 360;
  pannerNode.coneOuterAngle = 0;
  pannerNode.coneOuterGain = 0;
  pannerNode.setPosition(w / 2, h / 2, 0);
  pannerNode.setOrientation(1, 0, 0);

  /**listener = audioContext.listener;
  listener.setOrientation(0, 0, -1, 0, 1, 0);
  listener.setPosition(w / 2, h / 2, 0);*/

  //gain node
  volumeNode = audioContext.createGain();
  volumeNode.gain.value = ui.volume;

  //compressor node Compression lowers the volume of the loudest parts of the signal and raises the volume of the softest parts
  compressorNode = audioContext.createDynamicsCompressor();
  
  //--> comp
  leftNode.connect(volumeNode);
  rightNode.connect(volumeNode);
  
  //--> gain
  //compressorNode.connect(volumeNode);
 
  //--> out
  volumeNode.connect(audioContext.destination);
  
  // --> out
  //pannerNode.connect(audioContext.destination);
  //resonanceAudioScene.output.connect(audioContext.destination);
  
  window.addEventListener("resize", resize);
  render();

  // One-liner to resume playback when user interacted with the page.
  document.querySelector("#button").addEventListener('click', () =>{
    audioContext.resume().then(()=> {
      
      const now = audioContext.currentTime
      if (!isPlaying){ 
          rightNode.start(now);
          leftNode.start(now);
        isPlaying = true;
      }
      else{
        rightNode.stop();
        leftNode.stop();
        isPlaying = false;
      }
    });
  });

}

function lerp(ratio, start, end) {
  return start + (end - start) * ratio;
}

function norm(val, start, end) {
  return (val - start) / (end - start);
}

function update_ui() {
  // material.uniforms.speed.value = data.speed;
  const now = audioContext.currentTime;
  leftNode.frequency.setValueAtTime(ui.frequency, now );
  rightNode.frequency.setValueAtTime(ui.frequency + ui.binaural, now );
  volumeNode.gain.setValueAtTime( ui.volume, now );
}

/* render */

function render() {
  
  requestAnimationFrame(render);

  delta = clock.getDelta();

  t += ( 1.25 * delta ) * norm(ui.binaural, 0.1, 100);

  material.uniforms.time.value = t;
  
  renderer.render(scene, camera);
}

function resize() {
  
  var h = window.innerHeight,
    w = window.innerWidth;

  material.uniforms.size.value = new THREE.Vector2(w, h);

  camera.aspect = w / h;
  camera.updateProjectionMatrix();
  
  renderer.setSize(w, h);
  renderer.setViewport(0, 0, w, h);

  renderer.render(scene, camera);
}

//
window.addEventListener("load", init);
            
          
!
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