Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @depends AbstractAudioletDevice.js
  3.  */
  4.  
  5. var AudioDataAPIDevice = new Class({
  6.     Extends: AbstractAudioletDevice,
  7.     initialize: function(audiolet) {
  8.         AbstractAudioletDevice.prototype.initialize.apply(this, [audiolet]);
  9.         this.output = new Audio();
  10.         this.overflow = null;
  11.         this.writePosition = 0;
  12.  
  13.         this.output.mozSetup(this.numberOfChannels, this.sampleRate);
  14.        
  15.         this.tick.periodical(1000 * this.bufferSize / this.sampleRate, this);
  16.     },
  17.  
  18.     tick: function() {
  19.         // Check if some data was not written in previous attempts
  20.         var numSamplesWritten;
  21.         if (this.overflow) {
  22.             numSamplesWritten = this.output.mozWriteAudio(this.overflow);
  23.             this.writePosition += numSamplesWritten;
  24.             if (numSamplesWritten < this.overflow.length) {
  25.                 // Not all the data was written, saving the tail for writing
  26.                 // the next time fillBuffer is called
  27.                 this.overflow = this.overflow.subarray(numSamplesWritten);
  28.                 return;
  29.             }
  30.             this.overflow = null;
  31.         }
  32.  
  33.         var outputPosition = this.output.mozCurrentSampleOffset();
  34.         if (outputPosition == 0) {
  35.             // Until the output starts ticking we ignore any samples written
  36.             this.writePosition = 0;
  37.         }
  38.         var samplesNeeded = outputPosition +
  39.                             (this.bufferSize * this.numberOfChannels) -
  40.                             this.writePosition;
  41.         // Seems to help stability - lob some extra samples in if the internal
  42.         // buffer will become almost completely empty before we tick again
  43.         if (samplesNeeded <= 0 && samplesNeeded >= -128) {
  44.             samplesNeeded = this.bufferSize;
  45.         }
  46.         if (samplesNeeded > 0) {
  47.             // Request some sound data from the callback function.
  48.             AudioletNode.prototype.tick.apply(this, [samplesNeeded /
  49.                                                      this.numberOfChannels,
  50.                                                      this.getWriteTime()]);
  51.             this.buffer.interleave();
  52.             var buffer = this.buffer.data;
  53.  
  54.             // Writing the data.
  55.             numSamplesWritten = this.output.mozWriteAudio(buffer);
  56.             this.writePosition += numSamplesWritten;
  57.             if (numSamplesWritten < buffer.length) {
  58.                 // Not all the data was written, saving the tail.
  59.                 this.overflow = buffer.subarray(numSamplesWritten);
  60.             }
  61.         }
  62.     },
  63.  
  64.     getPlaybackTime: function() {
  65.         return this.output.mozCurrentSampleOffset() / this.numberOfChannels;
  66.     },
  67.  
  68.     getWriteTime: function() {
  69.         return this.writePosition / this.numberOfChannels;
  70.     }
  71. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement