2017-04-02 18:54:51 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-08-21 22:32:07 +00:00
|
|
|
// from TSS
|
2018-07-11 00:58:46 +00:00
|
|
|
declare var MasterChannel, AudioLooper, PsgDeviceChannel;
|
|
|
|
|
2018-08-28 22:11:22 +00:00
|
|
|
export class MasterAudio {
|
|
|
|
master = new MasterChannel();
|
|
|
|
looper = new AudioLooper(512);
|
|
|
|
start() {
|
2017-04-02 18:54:51 +00:00
|
|
|
this.looper.setChannel(this.master);
|
|
|
|
this.looper.activate();
|
|
|
|
}
|
2018-08-28 22:11:22 +00:00
|
|
|
stop() {
|
2017-04-02 18:54:51 +00:00
|
|
|
this.looper.setChannel(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 22:11:22 +00:00
|
|
|
export class AY38910_Audio {
|
|
|
|
master : MasterAudio;
|
|
|
|
psg = new PsgDeviceChannel();
|
|
|
|
curreg = 0;
|
2018-11-19 14:10:13 +00:00
|
|
|
|
2018-08-28 22:11:22 +00:00
|
|
|
constructor(master : MasterAudio) {
|
|
|
|
this.master = master;
|
|
|
|
this.psg.setMode(PsgDeviceChannel.MODE_SIGNED);
|
|
|
|
this.psg.setDevice(PsgDeviceChannel.DEVICE_AY_3_8910);
|
|
|
|
master.master.addChannel(this.psg);
|
|
|
|
}
|
2017-04-02 18:54:51 +00:00
|
|
|
|
2018-08-28 22:11:22 +00:00
|
|
|
reset() {
|
2017-04-02 18:54:51 +00:00
|
|
|
for (var i=15; i>=0; i--) {
|
|
|
|
this.selectRegister(i);
|
|
|
|
this.setData(0);
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 22:11:22 +00:00
|
|
|
selectRegister(val : number) {
|
|
|
|
this.curreg = val & 0xf;
|
2017-04-02 18:54:51 +00:00
|
|
|
}
|
2018-08-28 22:11:22 +00:00
|
|
|
setData(val : number) {
|
|
|
|
this.psg.writeRegisterAY(this.curreg, val & 0xff);
|
2017-04-02 18:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 14:10:13 +00:00
|
|
|
export class SN76489_Audio {
|
|
|
|
master : MasterAudio;
|
|
|
|
psg = new PsgDeviceChannel();
|
|
|
|
|
|
|
|
constructor(master : MasterAudio) {
|
|
|
|
this.master = master;
|
|
|
|
this.psg.setMode(PsgDeviceChannel.MODE_SIGNED);
|
|
|
|
this.psg.setDevice(PsgDeviceChannel.DEVICE_SN76489);
|
|
|
|
master.master.addChannel(this.psg);
|
|
|
|
}
|
2018-11-21 17:54:59 +00:00
|
|
|
reset() {
|
|
|
|
// TODO
|
|
|
|
}
|
2018-11-19 14:10:13 +00:00
|
|
|
setData(val : number) {
|
|
|
|
this.psg.writeRegisterSN(0, val & 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 18:54:51 +00:00
|
|
|
// https://en.wikipedia.org/wiki/POKEY
|
|
|
|
// https://user.xmission.com/~trevin/atari/pokey_regs.html
|
|
|
|
// http://krap.pl/mirrorz/atari/homepage.ntlworld.com/kryten_droid/Atari/800XL/atari_hw/pokey.htm
|
|
|
|
|
2018-08-21 22:32:07 +00:00
|
|
|
export var POKEYDeviceChannel = function() {
|
2017-04-02 18:54:51 +00:00
|
|
|
|
|
|
|
/* definitions for AUDCx (D201, D203, D205, D207) */
|
|
|
|
var NOTPOLY5 = 0x80 /* selects POLY5 or direct CLOCK */
|
|
|
|
var POLY4 = 0x40 /* selects POLY4 or POLY17 */
|
|
|
|
var PURE = 0x20 /* selects POLY4/17 or PURE tone */
|
|
|
|
var VOL_ONLY = 0x10 /* selects VOLUME OUTPUT ONLY */
|
|
|
|
var VOLUME_MASK = 0x0f /* volume mask */
|
|
|
|
|
|
|
|
/* definitions for AUDCTL (D208) */
|
|
|
|
var POLY9 = 0x80 /* selects POLY9 or POLY17 */
|
|
|
|
var CH1_179 = 0x40 /* selects 1.78979 MHz for Ch 1 */
|
|
|
|
var CH3_179 = 0x20 /* selects 1.78979 MHz for Ch 3 */
|
|
|
|
var CH1_CH2 = 0x10 /* clocks channel 1 w/channel 2 */
|
|
|
|
var CH3_CH4 = 0x08 /* clocks channel 3 w/channel 4 */
|
|
|
|
var CH1_FILTER = 0x04 /* selects channel 1 high pass filter */
|
|
|
|
var CH2_FILTER = 0x02 /* selects channel 2 high pass filter */
|
|
|
|
var CLOCK_15 = 0x01 /* selects 15.6999kHz or 63.9210kHz */
|
|
|
|
|
|
|
|
/* for accuracy, the 64kHz and 15kHz clocks are exact divisions of
|
|
|
|
the 1.79MHz clock */
|
|
|
|
var DIV_64 = 28 /* divisor for 1.79MHz clock to 64 kHz */
|
|
|
|
var DIV_15 = 114 /* divisor for 1.79MHz clock to 15 kHz */
|
|
|
|
|
|
|
|
/* the size (in entries) of the 4 polynomial tables */
|
|
|
|
var POLY4_SIZE = 0x000f
|
|
|
|
var POLY5_SIZE = 0x001f
|
|
|
|
var POLY9_SIZE = 0x01ff
|
|
|
|
|
|
|
|
var POLY17_SIZE = 0x0001ffff /* else use the full 17 bits */
|
|
|
|
|
|
|
|
/* channel/chip definitions */
|
|
|
|
var CHAN1 = 0
|
|
|
|
var CHAN2 = 1
|
|
|
|
var CHAN3 = 2
|
|
|
|
var CHAN4 = 3
|
|
|
|
var CHIP1 = 0
|
|
|
|
var CHIP2 = 4
|
|
|
|
var CHIP3 = 8
|
|
|
|
var CHIP4 = 12
|
|
|
|
var SAMPLE = 127
|
|
|
|
|
|
|
|
var FREQ_17_EXACT = 1789790.0 /* exact 1.79 MHz clock freq */
|
|
|
|
var FREQ_17_APPROX = 1787520.0 /* approximate 1.79 MHz clock freq */
|
|
|
|
|
|
|
|
// LFSR sequences
|
2018-07-11 00:58:46 +00:00
|
|
|
var bit1 = new Uint8Array( [ 0,1 ] );
|
|
|
|
var bit4 = new Uint8Array( [ 1,1,0,1,1,1,0,0,0,0,1,0,1,0,0 ] );
|
|
|
|
var bit5 = new Uint8Array( [ 0,0,1,1,0,0,0,1,1,1,1,0,0,1,0,1,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1 ] );
|
2017-04-02 18:54:51 +00:00
|
|
|
var bit17 = new Uint8Array(1<<17);
|
|
|
|
var bit17_5 = new Uint8Array(1<<17);
|
|
|
|
var bit5_4 = new Uint8Array(1<<17);
|
|
|
|
for (var i=0; i<bit17.length; i++) {
|
2018-07-11 00:58:46 +00:00
|
|
|
bit17[i] = Math.random() > 0.5 ? 1 : 0;
|
2017-04-02 18:54:51 +00:00
|
|
|
bit17_5[i] = bit17[i] & bit5[i % bit5.length];
|
|
|
|
bit5_4[i] = bit5[i % bit5.length] & bit4[i % bit4.length];
|
|
|
|
}
|
|
|
|
var wavetones = [
|
|
|
|
bit17_5, bit5, bit5_4, bit5,
|
|
|
|
bit17, bit1, bit4, bit1
|
|
|
|
];
|
|
|
|
|
|
|
|
// registers
|
|
|
|
var regs = new Uint8Array(16);
|
|
|
|
var counters = new Float32Array(4);
|
|
|
|
var deltas = new Float32Array(4);
|
|
|
|
var volume = new Float32Array(4);
|
|
|
|
var audc = new Uint8Array(4);
|
|
|
|
var waveforms = [bit1, bit1, bit1, bit1];
|
|
|
|
var buffer;
|
|
|
|
var sampleRate;
|
|
|
|
var clock, baseDelta;
|
|
|
|
var dirty = true;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
this.setBufferLength = function (length) {
|
|
|
|
buffer = new Int32Array(length);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getBuffer = function () {
|
|
|
|
return buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setSampleRate = function (rate) {
|
|
|
|
sampleRate = rate;
|
|
|
|
baseDelta = FREQ_17_EXACT / rate / 1.2; // TODO?
|
|
|
|
};
|
|
|
|
|
|
|
|
function updateValues(addr) {
|
|
|
|
var ctrl = regs[8];
|
|
|
|
var base = (ctrl & CLOCK_15) ? DIV_15 : DIV_64;
|
|
|
|
var div;
|
|
|
|
var i = addr & 4;
|
|
|
|
var j = i>>1;
|
|
|
|
var k = i>>2;
|
|
|
|
if (ctrl & (CH1_CH2>>k)) {
|
|
|
|
if (ctrl & (CH1_179>>k))
|
|
|
|
div = regs[i+2] * 256 + regs[i+0] + 7;
|
|
|
|
else
|
|
|
|
div = (regs[i+2] * 256 + regs[i+0] + 1) * base;
|
|
|
|
deltas[j+1] = baseDelta / div;
|
|
|
|
deltas[j+0] = 0;
|
|
|
|
} else {
|
|
|
|
if (ctrl & (CH1_179>>k)) {
|
|
|
|
div = regs[i+0] + 4;
|
|
|
|
} else {
|
|
|
|
div = (regs[i+0] + 1) * base;
|
|
|
|
}
|
|
|
|
deltas[j+0] = baseDelta / div;
|
|
|
|
div = (regs[i+2] + 1) * base;
|
|
|
|
deltas[j+1] = baseDelta / div;
|
|
|
|
}
|
|
|
|
//console.log(addr, ctrl.toString(16), div, deltas[j+0], deltas[j+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setRegister = function(addr, value) {
|
|
|
|
addr &= 0xf;
|
|
|
|
value &= 0xff;
|
|
|
|
if (regs[addr] != value) {
|
|
|
|
regs[addr] = value;
|
|
|
|
switch (addr) {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 6: // AUDF
|
|
|
|
case 8: // ctrl
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
case 5:
|
|
|
|
case 7: // AUDC
|
|
|
|
volume[addr>>1] = value & 0xf;
|
|
|
|
waveforms[addr>>1] = wavetones[value>>5];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.generate = function (length) {
|
|
|
|
if (dirty) {
|
|
|
|
updateValues(0);
|
|
|
|
updateValues(4);
|
|
|
|
dirty = false;
|
|
|
|
}
|
|
|
|
for (var s=0; s<length; s+=2) {
|
|
|
|
var sample = 0;
|
|
|
|
for (var i=0; i<4; i++) {
|
|
|
|
var d = deltas[i];
|
|
|
|
var v = volume[i];
|
|
|
|
if (d > 0 && d < 1 && v > 0) {
|
|
|
|
var wav = waveforms[i];
|
|
|
|
var cnt = counters[i] += d;
|
2017-04-20 13:45:35 +00:00
|
|
|
if (cnt > wav.length) {
|
|
|
|
cnt = counters[i] = cnt - Math.floor(cnt / wav.length) * wav.length;
|
2017-04-02 18:54:51 +00:00
|
|
|
}
|
2017-04-20 13:45:35 +00:00
|
|
|
var on = wav[Math.floor(cnt)];
|
2017-04-02 18:54:51 +00:00
|
|
|
if (on) {
|
|
|
|
sample += v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-20 13:45:35 +00:00
|
|
|
sample *= 128;
|
2017-04-02 18:54:51 +00:00
|
|
|
buffer[s] = sample;
|
|
|
|
buffer[s+1] = sample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////// Worker sound
|
|
|
|
|
2018-08-21 22:32:07 +00:00
|
|
|
export var WorkerSoundChannel = function(worker) {
|
2017-04-02 18:54:51 +00:00
|
|
|
var sampleRate;
|
|
|
|
var output;
|
|
|
|
var pending = [];
|
|
|
|
var pendingLength = 0;
|
|
|
|
|
|
|
|
worker.onmessage = function(e) {
|
|
|
|
if (e && e.data && e.data.samples && output) {
|
|
|
|
pending.push(e.data.samples);
|
|
|
|
pendingLength += e.data.samples.length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setBufferLength = function (length) {
|
|
|
|
output = new Int16Array(length);
|
|
|
|
//worker.postMessage({bufferLength:length,numChannels:2});
|
|
|
|
pendingLength = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getBuffer = function () {
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setSampleRate = function (rate) {
|
|
|
|
sampleRate = rate;
|
|
|
|
worker.postMessage({sampleRate:rate});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.generate = function (length) {
|
|
|
|
if (pendingLength < length*3) {
|
|
|
|
//console.log(length, pendingLength);
|
|
|
|
output.fill(0);
|
|
|
|
return; // TODO: send sync msg?
|
|
|
|
}
|
|
|
|
for (var i=0; i<output.length;) {
|
|
|
|
if (pending.length == 0) break; // TODO?
|
|
|
|
var buf = pending.shift();
|
|
|
|
pendingLength -= buf.length;
|
|
|
|
var l = output.length-i;
|
|
|
|
if (buf.length < l) {
|
|
|
|
output.set(buf, i);
|
|
|
|
} else {
|
|
|
|
output.set(buf.slice(0, l), i);
|
|
|
|
pending.unshift(buf.slice(l));
|
|
|
|
pendingLength += buf.length-l;
|
|
|
|
}
|
|
|
|
i += buf.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-04-15 04:12:21 +00:00
|
|
|
|
|
|
|
// SampleAudio
|
|
|
|
|
2018-08-21 22:32:07 +00:00
|
|
|
export var SampleAudio = function(clockfreq) {
|
2017-04-15 04:12:21 +00:00
|
|
|
var self = this;
|
|
|
|
var sfrac, sinc, accum;
|
|
|
|
var buffer, bufpos, bufferlist;
|
2018-02-12 20:02:55 +00:00
|
|
|
var idrain, ifill;
|
2018-08-15 04:43:52 +00:00
|
|
|
var nbuffers = 4;
|
2017-04-15 04:12:21 +00:00
|
|
|
|
|
|
|
function mix(ape) {
|
|
|
|
var buflen=ape.outputBuffer.length;
|
|
|
|
var lbuf = ape.outputBuffer.getChannelData(0);
|
|
|
|
var m = this.module;
|
|
|
|
if (!m) m = ape.srcElement.module;
|
|
|
|
if (!m) return;
|
|
|
|
if (m.callback) {
|
|
|
|
m.callback(lbuf);
|
|
|
|
return;
|
|
|
|
} else {
|
2018-02-12 20:02:55 +00:00
|
|
|
var buf = bufferlist[idrain];
|
2017-04-15 04:12:21 +00:00
|
|
|
for (var i=0; i<lbuf.length; i++) {
|
|
|
|
lbuf[i] = buf[i];
|
2018-02-12 20:02:55 +00:00
|
|
|
//lbuf[i] = (i&128) ? 1.0 : 0.33;
|
2017-04-15 04:12:21 +00:00
|
|
|
}
|
2018-02-12 20:02:55 +00:00
|
|
|
idrain = (idrain + 1) % bufferlist.length;
|
2017-04-15 04:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createContext() {
|
2018-07-11 00:58:46 +00:00
|
|
|
var AudioContext = window['AudioContext'] || window['webkitAudioContext'] || window['mozAudioContext'];
|
2017-04-15 04:12:21 +00:00
|
|
|
if (! AudioContext) {
|
|
|
|
console.log("no web audio context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.context = new AudioContext();
|
|
|
|
self.sr=self.context.sampleRate;
|
2018-08-16 14:13:09 +00:00
|
|
|
self.bufferlen=2048;
|
2017-04-15 04:12:21 +00:00
|
|
|
|
|
|
|
// Amiga 500 fixed filter at 6kHz. WebAudio lowpass is 12dB/oct, whereas
|
|
|
|
// older Amigas had a 6dB/oct filter at 4900Hz.
|
|
|
|
self.filterNode=self.context.createBiquadFilter();
|
2018-02-12 20:02:55 +00:00
|
|
|
self.filterNode.frequency.value=6000;
|
2017-04-15 04:12:21 +00:00
|
|
|
|
|
|
|
// mixer
|
|
|
|
if ( typeof self.context.createScriptProcessor === 'function') {
|
|
|
|
self.mixerNode=self.context.createScriptProcessor(self.bufferlen, 1, 1);
|
|
|
|
} else {
|
|
|
|
self.mixerNode=self.context.createJavaScriptNode(self.bufferlen, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.mixerNode.module=self;
|
|
|
|
self.mixerNode.onaudioprocess=mix;
|
|
|
|
|
|
|
|
// compressor for a bit of volume boost, helps with multich tunes
|
|
|
|
self.compressorNode=self.context.createDynamicsCompressor();
|
|
|
|
|
|
|
|
// patch up some cables :)
|
|
|
|
self.mixerNode.connect(self.filterNode);
|
2018-08-18 00:46:55 +00:00
|
|
|
self.filterNode.connect(self.compressorNode);
|
2017-04-15 04:12:21 +00:00
|
|
|
self.compressorNode.connect(self.context.destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.start = function() {
|
|
|
|
if (!this.context) createContext();
|
|
|
|
sinc = this.sr * 1.0 / clockfreq;
|
|
|
|
sfrac = 0;
|
|
|
|
accum = 0;
|
|
|
|
bufpos = 0;
|
|
|
|
bufferlist = [];
|
2018-02-12 20:02:55 +00:00
|
|
|
idrain = 1;
|
|
|
|
ifill = 0;
|
2018-02-14 01:04:52 +00:00
|
|
|
for (var i=0; i<nbuffers; i++) {
|
2017-04-15 04:12:21 +00:00
|
|
|
var arrbuf = new ArrayBuffer(self.bufferlen*4);
|
|
|
|
bufferlist[i] = new Float32Array(arrbuf);
|
|
|
|
}
|
|
|
|
buffer = bufferlist[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stop = function() {
|
|
|
|
if (this.context) {
|
|
|
|
this.context.close();
|
|
|
|
this.context = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 05:24:19 +00:00
|
|
|
this.addSingleSample = function(value) {
|
2017-11-20 01:32:58 +00:00
|
|
|
if (!buffer) return;
|
2017-11-13 05:24:19 +00:00
|
|
|
buffer[bufpos++] = value;
|
|
|
|
if (bufpos >= buffer.length) {
|
|
|
|
bufpos = 0;
|
2018-02-12 20:02:55 +00:00
|
|
|
bufferlist[ifill] = buffer;
|
|
|
|
var inext = (ifill + 1) % bufferlist.length;
|
2018-08-15 04:43:52 +00:00
|
|
|
if (inext == idrain) {
|
2018-08-16 14:13:09 +00:00
|
|
|
ifill = Math.floor(idrain + nbuffers/2) % bufferlist.length;
|
|
|
|
//console.log('audio skipped', idrain, ifill);
|
2018-08-15 04:43:52 +00:00
|
|
|
} else {
|
|
|
|
ifill = inext;
|
|
|
|
}
|
2018-02-12 20:02:55 +00:00
|
|
|
buffer = bufferlist[ifill];
|
2017-11-13 05:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 04:12:21 +00:00
|
|
|
this.feedSample = function(value, count) {
|
|
|
|
while (count-- > 0) {
|
|
|
|
accum += value;
|
|
|
|
sfrac += sinc;
|
2018-08-16 14:13:09 +00:00
|
|
|
while (sfrac >= 1) {
|
2017-04-15 04:12:21 +00:00
|
|
|
sfrac -= 1;
|
2018-02-12 20:02:55 +00:00
|
|
|
value *= sfrac;
|
|
|
|
this.addSingleSample(accum - value);
|
|
|
|
accum = value;
|
2017-04-15 04:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|