Audio cleanup.

This commit is contained in:
Will Scullin 2020-07-20 17:04:55 -07:00
parent e41e8d2bf2
commit eb6b5620a5
No known key found for this signature in database
GPG Key ID: 26DCD1042C6638CD
2 changed files with 22 additions and 13 deletions

View File

@ -29,6 +29,7 @@ export default function Apple2IO(cpu, callbacks)
var _paddle = [0.0, 0.0, 0.0, 0,0];
var _phase = -1;
var _sample = [];
var _sampleIdx = 0;
var _sampleTime = 0;
var _didAudio = false;
@ -103,17 +104,18 @@ export default function Apple2IO(cpu, callbacks)
function _tick() {
var now = cpu.cycles();
var phase = _phase > 0 ? _high : _low;
var phase = _didAudio ? (_phase > 0 ? _high : _low) : 0.0;
for (; _sampleTime < now; _sampleTime += _cycles_per_sample) {
_sample.push(phase);
if (_sample.length >= _sample_size) {
_sample[_sampleIdx++] = phase;
if (_sampleIdx === _sample_size) {
if (_audioListener) {
_audioListener(_didAudio ? _sample : []);
_audioListener(_sample);
}
_sample = [];
_didAudio = false;
_sample = new Array(_sample_size);
_sampleIdx = 0;
}
}
_didAudio = false;
}
function _calcSampleRate() {
@ -491,8 +493,11 @@ export default function Apple2IO(cpu, callbacks)
_tapeOffset = -1;
},
sampleRate: function sampleRate(rate) {
sampleRate: function sampleRate(rate, sample_size) {
_rate = rate;
_sample_size = sample_size
_sample = new Array(_sample_size);
_sampleIdx = 0;
_calcSampleRate();
},

View File

@ -15,6 +15,9 @@ import { debug } from '../util';
* Audio Handling
*/
var SAMPLE_SIZE = 1024
var SAMPLE_RATE = 44000
export default function Audio(io) {
var sound = true;
var _samples = [];
@ -25,8 +28,10 @@ export default function Audio(io) {
var started = false;
if (AudioContext) {
audioContext = new AudioContext();
audioNode = audioContext.createScriptProcessor(4096, 1, 1);
audioContext = new AudioContext({
sampleRate: SAMPLE_RATE
});
audioNode = audioContext.createScriptProcessor(SAMPLE_SIZE, 1, 1);
audioNode.onaudioprocess = function(event) {
var data = event.outputBuffer.getChannelData(0);
@ -52,12 +57,11 @@ export default function Audio(io) {
function _initAudio(io) {
if (audioContext) {
debug('Using Webkit Audio');
io.sampleRate(audioContext.sampleRate);
io.sampleRate(audioContext.sampleRate, SAMPLE_SIZE);
io.addSampleListener(function(sample) {
if (sound) {
_samples.push(sample);
while (_samples.length > 5) {
_samples.shift();
if (_samples.length < 5) {
_samples.push(sample);
}
}
});