Skip to content

Commit

Permalink
Minor audio fixes, including capping FIFO sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
endrift committed Mar 31, 2013
1 parent a132124 commit 51a05d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 14 additions & 4 deletions js/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function GameBoyAdvanceAudio() {
this.context = new webkitAudioContext();
this.bufferSize = 0;
this.bufferSize = 4096;
this.maxSamples = this.bufferSize << 4;
this.maxSamples = this.bufferSize << 2;
this.buffers = [new Float32Array(this.maxSamples), new Float32Array(this.maxSamples)];
this.sampleMask = this.maxSamples - 1;
if (this.context.createScriptProcessor) {
Expand Down Expand Up @@ -568,6 +568,9 @@ GameBoyAdvanceAudio.prototype.updateEnvelope = function(channel, cycles) {

GameBoyAdvanceAudio.prototype.appendToFifoA = function(value) {
var b;
if (this.fifoA.length > 28) {
this.fifoA = this.fifoA.slice(-28);
}
for (var i = 0; i < 4; ++i) {
b = (value & 0xFF) << 24;
value >>= 8;
Expand All @@ -577,6 +580,9 @@ GameBoyAdvanceAudio.prototype.appendToFifoA = function(value) {

GameBoyAdvanceAudio.prototype.appendToFifoB = function(value) {
var b;
if (this.fifoB.length > 28) {
this.fifoB = this.fifoB.slice(-28);
}
for (var i = 0; i < 4; ++i) {
b = (value & 0xFF) << 24;
value >>= 8;
Expand All @@ -585,7 +591,7 @@ GameBoyAdvanceAudio.prototype.appendToFifoB = function(value) {
};

GameBoyAdvanceAudio.prototype.sampleFifoA = function() {
if (!this.fifoA.length) {
if (this.fifoA.length <= 16) {
var dma = this.core.irq.dma[this.dmaA];
dma.nextCount = 4;
this.core.mmu.serviceDma(this.dmaA, dma);
Expand All @@ -594,7 +600,7 @@ GameBoyAdvanceAudio.prototype.sampleFifoA = function() {
};

GameBoyAdvanceAudio.prototype.sampleFifoB = function() {
if (!this.fifoB.length) {
if (this.fifoB.length <= 16) {
var dma = this.core.irq.dma[this.dmaB];
dma.nextCount = 4;
this.core.mmu.serviceDma(this.dmaB, dma);
Expand Down Expand Up @@ -707,7 +713,7 @@ GameBoyAdvanceAudio.prototype.audioProcess = function(audioProcessingEvent) {
var i;
var o = this.outputPointer;
for (i = 0; i < this.bufferSize; ++i, o += this.resampleRatio) {
if (o > this.maxSamples) {
if (o >= this.maxSamples) {
o -= this.maxSamples;
}
if ((o | 0) == this.samplePointer) {
Expand All @@ -717,6 +723,10 @@ GameBoyAdvanceAudio.prototype.audioProcess = function(audioProcessingEvent) {
left[i] = this.buffers[0][o | 0];
right[i] = this.buffers[1][o | 0];
}
for (; i < this.bufferSize; ++i) {
left[i] = 0;
right[i] = 0;
}
this.outputPointer = o;
++this.totalSamples;
} else {
Expand Down
1 change: 0 additions & 1 deletion js/irq.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ GameBoyAdvanceInterruptHandler.prototype.updateTimers = function() {
this.video.updateTimers(this.cpu);
this.audio.updateTimers();
if (this.timersEnabled) {
// TODO: ensure incrementing only on read and overflow
var timer = this.timers[0];
if (timer.enable) {
if (this.cpu.cycles >= timer.nextEvent) {
Expand Down

0 comments on commit 51a05d4

Please sign in to comment.