topical media & game development

talk show tell print

graphic-player-10-pixel-sound-shader.txt / txt



  
  // Create shader
  [Embed(source="mixer.pbj", mimeType="application/octet-stream")]
  var MixerShader:Class;
  var mixerShader:Shader = new Shader(new MixerShader());
  
  // buffers will become shader inputs
  var buffer:Vector.<ByteArray> = new Vector.<ByteArray>(15);
  // volume control volume on each track, 1.0 is full volume
  var volume:Vector.<Number> = new Vector.<Number>(15);
  
  // initialize the shader inputs and volume values
  for (var j:int = 0; j < 15; j++) {
    volume[j]=1.0;
    buffer[j] = new ByteArray();
    // set so shader will always work even we have not enough tracks
    buffer[j].length = BUFFER_SIZE*4*2; 
    mixerShader.data["track"+j]["width"] = 1024;
    mixerShader.data["track"+j]["height"] = BUFFER_SIZE/1024;
    mixerShader.data["track"+j]["input"] = buffer[j];
  }
  
  function onSoundData(e:SampleDataEvent) : void
  {
    // extract the mp3 data into our shader inputs
    for (var i:int = 0; i < NUM_TRACKS; i++) {
      buffer[i].position = 0;
      sounds[i].extract(buffer[i], BUFFER_SIZE); 
      buffer[i].position = 0;
    }
    // update the volume value in the shader
    for (var k:int = 0; k < NUM_TRACKS; k++)  {
      mixerShader.data["volume"+k]["value"] = [ volume[k], volume[k] ];
    }
    // mix!
    var mixerJob:ShaderJob = new ShaderJob(mixerShader, e.data, 1024, BUFFER_SIZE/1024);
    mixerJob.start(true);
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.