Skip to content
Permalink
Browse files
Attempt to use single buffer as ring buffer
Chrome seems to hate this, but recording for posterity's sake just in case I got something wrong. Will be reverting immediately afterward.
  • Loading branch information
magcius committed Sep 14, 2017
1 parent 97746e8 commit 730e49810538fa2613acf7a2b956daec37fd7020
Showing 2 changed files with 13 additions and 13 deletions.
4 dsp.js
@@ -442,9 +442,9 @@ window.$moduleDSP = (function(exports) {
this.wp = wp;
};

DSP.prototype.resetBuffer = function(buffer) {
DSP.prototype.resetBuffer = function(buffer, offset) {
this._buffer = buffer;
this.wp = 0;
this.wp = offset;
};

DSP.prototype.CLOCKS_PER_SAMPLE = CLOCKS_PER_SAMPLE;
22 spc.js
@@ -116,34 +116,34 @@

function Driver(state) {
this._bufferSize = 8192;
this._freeBuffers = [];

this._ctx = new AudioContext();
// Hold around 12 buffers in memory.
this._numBuffers = 12;
this._buffer = this._ctx.createBuffer(2, this._bufferSize * this._numBuffers, 32000);
this._bufferIdx = 0;
this._dsp = new SPC_DSP(state, this._buffer);
this._cpu = new SPC_CPU(state, this._dsp);

this._playTime = this._ctx.currentTime;
this._pumpAudio();
}
Driver.prototype._runCPU = function() {
var buffer;
if (this._freeBuffers.length) {
buffer = this._freeBuffers.pop();
} else {
buffer = this._ctx.createBuffer(2, this._bufferSize, 32000);
}
this._dsp.resetBuffer(buffer);
this._dsp.resetBuffer(this._buffer, this._bufferIdx * this._bufferSize);
this._cpu.runUntilSamples(this._bufferSize);

var bs = this._ctx.createBufferSource();
bs.buffer = buffer;
bs.buffer = this._buffer;
bs.connect(this._ctx.destination);
bs.start(this._playTime);
var bufferLength = this._bufferSize / this._buffer.sampleRate;
bs.start(this._playTime, bufferLength * this._bufferIdx, bufferLength);
bs.onended = function() {
this._freeBuffers.push(buffer);
this._pumpAudio();
}.bind(this);
this._playTime += (this._bufferSize / 32000);
this._bufferIdx++;
if (this._bufferIdx === this._numBuffers)
this._bufferIdx = 0;
};
Driver.prototype._pumpAudio = function() {
// Schedule 300ms or so in advance.

0 comments on commit 730e498

Please sign in to comment.