173 lines
4.9 KiB
JavaScript
173 lines
4.9 KiB
JavaScript
/*
|
|
* Synthesised phone-line + modem sounds (Web Audio, no sample files).
|
|
* Not a faithful V.32bis handshake - just close enough to bring back memories.
|
|
*/
|
|
(function () {
|
|
let ctx = null;
|
|
let master = null;
|
|
let noiseBuf = null;
|
|
let skipping = false;
|
|
let pending = [];
|
|
const live = new Set();
|
|
|
|
const MA = { enabled: true };
|
|
|
|
function init() {
|
|
if (!MA.enabled) return;
|
|
if (!ctx) {
|
|
const AC = window.AudioContext || window.webkitAudioContext;
|
|
if (!AC) { MA.enabled = false; return; }
|
|
ctx = new AC();
|
|
master = ctx.createGain();
|
|
master.gain.value = 0.22;
|
|
master.connect(ctx.destination);
|
|
noiseBuf = ctx.createBuffer(1, ctx.sampleRate * 2, ctx.sampleRate);
|
|
const d = noiseBuf.getChannelData(0);
|
|
for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1;
|
|
}
|
|
if (ctx.state === 'suspended') ctx.resume();
|
|
}
|
|
|
|
function on() { return MA.enabled && ctx && !skipping; }
|
|
|
|
function wait(ms) {
|
|
if (skipping) return Promise.resolve();
|
|
return new Promise((resolve) => {
|
|
const done = () => { clearTimeout(t); pending = pending.filter((p) => p !== done); resolve(); };
|
|
const t = setTimeout(done, ms);
|
|
pending.push(done);
|
|
});
|
|
}
|
|
|
|
function track(node, stopAt) {
|
|
live.add(node);
|
|
node.onended = () => live.delete(node);
|
|
node.start(ctx.currentTime);
|
|
node.stop(stopAt);
|
|
return node;
|
|
}
|
|
|
|
function envGain(start, dur, level) {
|
|
const g = ctx.createGain();
|
|
g.gain.setValueAtTime(0, start);
|
|
g.gain.linearRampToValueAtTime(level, start + 0.006);
|
|
g.gain.setValueAtTime(level, Math.max(start + 0.006, start + dur - 0.006));
|
|
g.gain.linearRampToValueAtTime(0, start + dur);
|
|
g.connect(master);
|
|
return g;
|
|
}
|
|
|
|
/** Sum of steady sine tones. */
|
|
function tones(freqs, ms, level = 0.5) {
|
|
if (!on()) return;
|
|
const t = ctx.currentTime;
|
|
const g = envGain(t, ms / 1000, level / freqs.length);
|
|
for (const f of freqs) {
|
|
const o = ctx.createOscillator();
|
|
o.frequency.value = f;
|
|
o.connect(g);
|
|
track(o, t + ms / 1000 + 0.02);
|
|
}
|
|
}
|
|
|
|
/** Frequency-shift keying: random hops between the given frequencies. */
|
|
function fsk(freqSets, ms, baud, level = 0.4) {
|
|
if (!on()) return;
|
|
const t = ctx.currentTime;
|
|
const dur = ms / 1000;
|
|
const g = envGain(t, dur, level / freqSets.length);
|
|
for (const [f0, f1] of freqSets) {
|
|
const o = ctx.createOscillator();
|
|
for (let x = 0; x < dur; x += 1 / baud) {
|
|
o.frequency.setValueAtTime(Math.random() < 0.5 ? f0 : f1, t + x);
|
|
}
|
|
o.connect(g);
|
|
track(o, t + dur + 0.02);
|
|
}
|
|
}
|
|
|
|
/** Band-passed white noise - the "screech" of a fast carrier. */
|
|
function noise(ms, centre, q, level = 0.6) {
|
|
if (!on()) return;
|
|
const t = ctx.currentTime;
|
|
const src = ctx.createBufferSource();
|
|
src.buffer = noiseBuf;
|
|
src.loop = true;
|
|
const bp = ctx.createBiquadFilter();
|
|
bp.type = 'bandpass';
|
|
bp.frequency.value = centre;
|
|
bp.Q.value = q;
|
|
src.connect(bp).connect(envGain(t, ms / 1000, level));
|
|
track(src, t + ms / 1000 + 0.02);
|
|
}
|
|
|
|
const DTMF = {
|
|
1: [697, 1209], 2: [697, 1336], 3: [697, 1477],
|
|
4: [770, 1209], 5: [770, 1336], 6: [770, 1477],
|
|
7: [852, 1209], 8: [852, 1336], 9: [852, 1477],
|
|
0: [941, 1336], '*': [941, 1209], '#': [941, 1477],
|
|
};
|
|
|
|
const pace = (withSound, silent) => (MA.enabled ? withSound : silent);
|
|
|
|
MA.init = init;
|
|
MA.wait = wait;
|
|
|
|
MA.dialTone = async () => {
|
|
const ms = pace(900, 200);
|
|
tones([350, 440], ms, 0.5);
|
|
await wait(ms);
|
|
};
|
|
|
|
MA.dial = async (digits) => {
|
|
for (const d of digits) {
|
|
if (skipping) return;
|
|
if (DTMF[d]) tones(DTMF[d], 85, 0.55);
|
|
await wait(pace(150, 40));
|
|
}
|
|
};
|
|
|
|
MA.ringback = async () => {
|
|
tones([440, 480], 1600, 0.4);
|
|
await wait(pace(2300, 400));
|
|
};
|
|
|
|
MA.busy = async () => {
|
|
for (let i = 0; i < 3; i++) {
|
|
tones([480, 620], 480, 0.5);
|
|
await wait(pace(950, 200));
|
|
}
|
|
};
|
|
|
|
MA.handshake = async () => {
|
|
if (!MA.enabled) return wait(700);
|
|
tones([2100], 1100, 0.35); // answer tone
|
|
await wait(1180);
|
|
fsk([[980, 1180], [1650, 1850]], 520, 300, 0.4); // V.21 negotiation
|
|
await wait(620);
|
|
tones([150, 300, 450, 600, 900, 1200, 1800, 2400, 3000, 3600], 320, 0.6); // line probe "bong"
|
|
await wait(360);
|
|
fsk([[1800, 2400]], 380, 600, 0.3);
|
|
await wait(420);
|
|
noise(520, 1800, 0.9, 0.7); // training
|
|
tones([1800], 520, 0.12);
|
|
await wait(640);
|
|
noise(1500, 1900, 0.6, 0.55); // data carrier
|
|
tones([1800], 1500, 0.08);
|
|
await wait(1550);
|
|
};
|
|
|
|
/** Cut all sound and fast-forward any pending waits. */
|
|
MA.skip = () => {
|
|
skipping = true;
|
|
for (const n of live) { try { n.stop(); } catch (e) { /* already stopped */ } }
|
|
live.clear();
|
|
pending.slice().forEach((done) => done());
|
|
};
|
|
|
|
MA.reset = () => { skipping = false; };
|
|
MA.isSkipping = () => skipping;
|
|
|
|
window.ModemAudio = MA;
|
|
})();
|