Stereo Splitting & Panning

From SoundDB
Jump to: navigation, search
/*
  Stereo Splitting & Panning
  Using the Mozilla Audio Data API
  by F1LT3R @ http://Bocoup.com
*/

// This example is illustrative and not enhanced for performance

// Create a Global pan variable visible to Processing.js Dial Element
var pan = 0;

// Define event in global/"window" scope
var mozAudioAvailable = function(){};

// When the DOM has loaded...
function init(){
  
  // Variable declarations
  var inputSamples, outputSamples, i, len,
      debug = document.getElementById('debug'),
      output = document.createElement('audio'),
      panElem = document.getElementById('panFormInput');
  
  // Pull the input signal from the mix
  document.getElementById('input').volume = 0;
    
  // Initiate the 
  output.mozSetup(2, 44100, 1);
  
  // WHEN SOUND HAPPENS.........>
  mozAudioAvailable = function mozAudioAvailable(event){
  
    // Alias buffer samples to a more conveinient name
    inputSamples = event.frameBuffer;
        
    // Set new length and clear channel data
    len = inputSamples.length, outputSamples = [];
     
    // Loop thougt the stereo input data...
    for(i=0;i < len; i+=2){
      
      // Copy left channel down via pan value
      outputSamples[i] = inputSamples[i] * (1-pan);
      
      // Copy right channel down via pan value
      outputSamples[i+1] = inputSamples[i+1] * (1- -pan);
    }
   
    // Display 5 samples from each buffer-written-(event at C level)
    debug.innerHTML = "";
    for(i=0;i<5;i++){
      debug.innerHTML += "<b>"+event.time.toFixed(2) + ":</b>  "+ outputSamples[i]+"<br>";
    }
    
    // Output mixed samples to <audio> element
    output.mozWriteAudio(outputSamples);
   
  };    

  var input = document.getElementById('input');
  //input.volume = 0;
  input.addEventListener("MozAudioAvailable", mozAudioAvailable, false);
      
};

// Call ready event when DOM is loaded
addEventListener('DOMContentLoaded', function(){ init(); }, false);