User:Pailiaq/common.js: Difference between revisions
No edit summary |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
var ctx = null; | var ctx = null; | ||
var | var activeNodes = []; | ||
var activeBtn = null; | var activeBtn = null; | ||
var STORAGE_KEY = 'edoChordPlayTimbre'; | var STORAGE_KEY = 'edoChordPlayTimbre'; | ||
var TIMBRES = ['triangle', 'sawtooth', 'square', 'sine']; | |||
var TIMBRE_LABELS = { triangle: 'Triangle', sawtooth: 'Sawtooth', square: 'Square', sine: 'Sine' }; | |||
var LOOK_AHEAD = 0.00; // 50ms buffer so the audio thread can schedule | |||
var BASE_FREQ = 261.63; // middle C; pitch is set via detune (cents) on top | |||
var timbre = (function () { | |||
var | |||
try { | try { | ||
var saved = localStorage.getItem(STORAGE_KEY); | var saved = localStorage.getItem(STORAGE_KEY); | ||
return TIMBRES.indexOf(saved) >= 0 ? saved : 'triangle'; | |||
} catch (e) { | } catch (e) { return 'triangle'; } | ||
})(); | })(); | ||
function | function setTimbre(t) { | ||
if (TIMBRES.indexOf(t) < 0) return; | |||
timbre = t; | |||
try { localStorage.setItem(STORAGE_KEY, t); } catch (e) {} | |||
document.querySelectorAll('.edo-chord-timbre').forEach(function (sel) { | |||
sel.value = t; | |||
}); | |||
try { localStorage.setItem(STORAGE_KEY, | |||
document.querySelectorAll('.edo-chord-timbre').forEach(function ( | |||
} | } | ||
| Line 50: | Line 32: | ||
} | } | ||
function disposeNode(node) { | |||
try { node.osc.disconnect(); } catch (e) {} | |||
function | try { node.gain.disconnect(); } catch (e) {} | ||
var idx = activeNodes.indexOf(node); | |||
if (idx >= 0) activeNodes.splice(idx, 1); | |||
} | } | ||
function stopAll() { | function stopAll() { | ||
if (!ctx) return; | if (!ctx) return; | ||
var now = ctx.currentTime; | var now = ctx.currentTime + LOOK_AHEAD; | ||
// Snapshot and clear before stopping, so onended handlers find no matches and just dispose | |||
var toStop = activeNodes; | |||
activeNodes = []; | |||
toStop.forEach(function (n) { | |||
try { | try { | ||
n.gain.gain.cancelScheduledValues(now); | n.gain.gain.cancelScheduledValues(now); | ||
| Line 86: | Line 53: | ||
} catch (e) {} | } catch (e) {} | ||
}); | }); | ||
if (activeBtn) { activeBtn.classList.remove('playing'); activeBtn = null; } | if (activeBtn) { activeBtn.classList.remove('playing'); activeBtn = null; } | ||
} | } | ||
function playChord(centsArr, btn) { | |||
function | stopAll(); | ||
var c = getCtx(); | var c = getCtx(); | ||
if (c.state === 'suspended') c.resume(); | |||
var | var now = c.currentTime + LOOK_AHEAD; | ||
var attack = 0.02, sustain = 0.2, release = | var attack = 0.02, sustain = 0.2, release = 5; | ||
var totalDur = attack + sustain + release; | var totalDur = attack + sustain + release; | ||
var peak = 0.18 / Math.sqrt(centsArr.length); | var peak = 0.18 / Math.sqrt(centsArr.length); | ||
| Line 104: | Line 68: | ||
var osc = c.createOscillator(); | var osc = c.createOscillator(); | ||
var gain = c.createGain(); | var gain = c.createGain(); | ||
osc.type = | osc.type = timbre; | ||
osc.frequency.value = | osc.frequency.value = BASE_FREQ; | ||
osc.detune.value = cents; | |||
gain.gain.setValueAtTime(0, now); | gain.gain.setValueAtTime(0, now); | ||
gain.gain.linearRampToValueAtTime(peak, now + attack); | gain.gain.linearRampToValueAtTime(peak, now + attack); | ||
| Line 113: | Line 78: | ||
osc.start(now); | osc.start(now); | ||
osc.stop(now + totalDur + 0.1); | osc.stop(now + totalDur + 0.1); | ||
var node = { osc: osc, gain: gain }; | |||
activeNodes.push(node); | |||
osc.onended = function () { disposeNode(node); }; | |||
}); | }); | ||
if (btn) { | if (btn) { | ||
btn.classList.add('playing'); | btn.classList.add('playing'); | ||
| Line 158: | Line 89: | ||
setTimeout(function () { | setTimeout(function () { | ||
if (activeBtn === btn) { btn.classList.remove('playing'); activeBtn = null; } | if (activeBtn === btn) { btn.classList.remove('playing'); activeBtn = null; } | ||
}, | }, totalDur * 1000); | ||
} | } | ||
} | } | ||
function injectSelectors() { | function injectSelectors() { | ||
var tables = new Set(); | var tables = new Set(); | ||
| Line 174: | Line 104: | ||
var sel = document.createElement('select'); | var sel = document.createElement('select'); | ||
sel.className = 'edo-chord-timbre'; | sel.className = 'edo-chord-timbre'; | ||
sel.title = 'Synth | sel.title = 'Synth timbre'; | ||
TIMBRES.forEach(function (t) { | TIMBRES.forEach(function (t) { | ||
var opt = document.createElement('option'); | var opt = document.createElement('option'); | ||
opt.value = t | opt.value = t; | ||
opt.textContent = t | opt.textContent = TIMBRE_LABELS[t]; | ||
if (t | if (t === timbre) opt.selected = true; | ||
sel.appendChild(opt); | |||
}); | }); | ||
sel.addEventListener('change', function () { setTimbre(sel.value); }); | sel.addEventListener('change', function () { setTimbre(sel.value); }); | ||
| Line 201: | Line 124: | ||
e.preventDefault(); | e.preventDefault(); | ||
if (btn === activeBtn) { stopAll(); return; } | if (btn === activeBtn) { stopAll(); return; } | ||
var edo = parseInt(btn.dataset.edo, 10); | |||
var cents; | |||
if (btn.dataset.cents) { | |||
cents = btn.dataset.cents.split(',').map(Number); | |||
} else { | |||
var edo = parseInt(btn.dataset.edo, 10); | |||
var steps = (btn.dataset.steps || '').split(',').map(Number); | |||
if (!edo || !steps.length) return; | |||
var stepSize = 1200 / edo; | |||
cents = steps.map(function (s) { return s * stepSize; }); | |||
} | |||
if (!cents.length || cents.some(isNaN)) return; | |||
playChord(cents, btn); | |||
} | } | ||