Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
notmasteryet edited this page Sep 14, 2010 · 6 revisions

Welcome to the Audio Data API Objects wiki!

See Audio Data API documentation for low level methods and operations with the audio on the web. This project will give you quick start and will be an example how to access and generate the sound.

View interfaces to get the main idea behind the objects. Two main objects you will start with: AudioDataSource and AudioDataDestination.

To generate sound:

 
      var currentSoundSample = 0;
      var sampleRate = 44100, gain = 0.8, frequency = 440;      
      function NoteSource() {
        this.audioParameters = new AudioParameters(1, sampleRate);      
        this.read = function(soundData) {
          var size = soundData.length;
          var k = 2* Math.PI * frequency / sampleRate;
          for (var i=0; i<size; i++) {
            soundData[i] = gain * Math.sin(k * currentSoundSample++);
          }
          return size;
        };
      }     
      var audioSource = new NoteSource();
      var audioDestination = new AudioDataDestination();
      audioDestination.writeAsync(audioSource);

To get (draw) the sound:

 
  function CanvasDrawListener(canvasElement) {
    var context = canvasElement.getContext("2d");
    var width = canvasElement.width, height = canvasElement.height;    
    var step;
    this.init = function(p) { step = p.channels; };    
    this.write = function(data) {
      context.strokeStyle = "#0000FF";
      context.fillStyle = "#808080";
      context.fillRect(0,0,width,height);
      context.beginPath();
      context.moveTo(0,height/2*(1-data[0]));
      for(var i=1,j=step;i<width;i++,j+=step) {
        context.lineTo(i,height/2*(1-data[j]));
      }
      context.stroke();
      context.closePath();
    };    
    this.shutdown = function() {};
  }
  ....  
    var audioSource = new AudioDataSource(audio);
    var canvasDestination = new CanvasDrawListener(canvas);
    audioSource.readAsync(canvasDestination);
Clone this wiki locally