diff --git a/js/synth.js b/js/synth.js index 257513a..9dfb268 100644 --- a/js/synth.js +++ b/js/synth.js @@ -16,27 +16,27 @@ 'use strict'; /* ---------------------------------------------------------------- */ - /* 音色参数表:w=[相对音量, 波形, 八度偏移, 失谐(音分)] */ + /* 音色参数表:w = [相对音量, 波形, 八度偏移, 失谐(音分)] */ /* a 起音 / d 衰减 / s 保持 / r 释放 (秒) */ /* cut 低通截止 / q 品质因数 / rev 混响发送量 / gain 总增益 */ /* ---------------------------------------------------------------- */ var FAMILIES = [ - /* 0 钢琴 */ { w: [[0.85, 'triangle'], [0.45, 'sine', 1], [0.12, 'sawtooth']], a: 0.004, d: 1.6, s: 0.16, r: 0.35, cut: 3200, q: 0.7, rev: 0.16, gain: 1.0 }, - /* 1 半音打击 */ { w: [[0.9, 'sine'], [0.35, 'triangle', 1]], a: 0.003, d: 0.9, s: 0.05, r: 0.5, cut: 4200, q: 0.7, rev: 0.30, gain: 0.95 }, - /* 2 风琴 */ { w: [[0.7, 'square'], [0.5, 'square', 1], [0.32, 'sine', -1]], a: 0.012, d: 0.05, s: 0.95, r: 0.12, cut: 2600, q: 0.5, rev: 0.20, gain: 0.75 }, - /* 3 吉他 */ { w: [[0.8, 'sawtooth'], [0.4, 'triangle', 1]], a: 0.006, d: 0.9, s: 0.12, r: 0.35, cut: 2800, q: 0.8, rev: 0.18, gain: 0.95 }, - /* 4 贝斯 */ { w: [[0.9, 'sawtooth'], [0.6, 'sine', -1]], a: 0.008, d: 0.6, s: 0.45, r: 0.18, cut: 1100, q: 1.0, rev: 0.05, gain: 1.15 }, - /* 5 弦乐 */ { w: [[0.8, 'sawtooth'], [0.8, 'sawtooth', 0, 7]], a: 0.18, d: 0.4, s: 0.85, r: 0.7, cut: 2400, q: 0.6, rev: 0.45, gain: 0.8 }, - /* 6 合奏 */ { w: [[0.7, 'sawtooth'], [0.6, 'square', 0, -6]], a: 0.12, d: 0.4, s: 0.85, r: 0.6, cut: 2200, q: 0.6, rev: 0.42, gain: 0.8 }, - /* 7 铜管 */ { w: [[0.9, 'sawtooth'], [0.4, 'square', 1]], a: 0.06, d: 0.3, s: 0.78, r: 0.3, cut: 3000, q: 0.9, rev: 0.28, gain: 0.85 }, - /* 8 簧管 */ { w: [[0.8, 'square'], [0.4, 'sawtooth', 0, 5]], a: 0.05, d: 0.3, s: 0.82, r: 0.3, cut: 2000, q: 0.8, rev: 0.30, gain: 0.75 }, - /* 9 吹管 */ { w: [[0.9, 'sine'], [0.35, 'triangle', 1], [0.08, 'sawtooth']], a: 0.08, d: 0.3, s: 0.88, r: 0.35, cut: 3400, q: 0.6, rev: 0.40, gain: 0.85 }, - /* 10 合成主音 */ { w: [[0.9, 'sawtooth'], [0.5, 'square', 0, 8]], a: 0.02, d: 0.35, s: 0.6, r: 0.3, cut: 2800, q: 1.2, rev: 0.25, gain: 0.8 }, - /* 11 合成铺底 */ { w: [[0.7, 'sawtooth'], [0.7, 'triangle', 0, -9], [0.4, 'sine', 1]], a: 0.6, d: 1.2, s: 0.7, r: 1.4, cut: 1600, q: 0.5, rev: 0.60, gain: 0.7 }, - /* 12 音效 */ { w: [[0.6, 'sawtooth'], [0.5, 'sine', 1]], a: 0.4, d: 1.5, s: 0.5, r: 1.4, cut: 1800, q: 0.7, rev: 0.60, gain: 0.7 }, - /* 13 民族 */ { w: [[0.8, 'triangle'], [0.4, 'sawtooth', 1]], a: 0.01, d: 0.8, s: 0.2, r: 0.4, cut: 2800, q: 0.8, rev: 0.25, gain: 0.9 }, - /* 14 打击 */ { w: [[0.8, 'triangle'], [0.4, 'square', 1]], a: 0.002, d: 0.5, s: 0.03, r: 0.3, cut: 4500, q: 0.7, rev: 0.30, gain: 0.9 }, - /* 15 特效 */ { w: [[0.7, 'sawtooth'], [0.5, 'square', 1]], a: 0.05, d: 1.2, s: 0.2, r: 0.8, cut: 2200, q: 0.7, rev: 0.50, gain: 0.7 } + /* 0 钢琴 */ { w: [[0.85, 'triangle'], [0.45, 'sine', 1], [0.12, 'sawtooth']], a: 0.004, d: 1.6, s: 0.16, r: 0.35, cut: 3200, q: 0.7, rev: 0.16, gain: 1.0 }, + /* 1 半音打击 */ { w: [[0.9, 'sine'], [0.35, 'triangle', 1]], a: 0.003, d: 0.9, s: 0.05, r: 0.5, cut: 4200, q: 0.7, rev: 0.30, gain: 0.95 }, + /* 2 风琴 */ { w: [[0.7, 'square'], [0.5, 'square', 1], [0.32, 'sine', -1]], a: 0.012, d: 0.05, s: 0.95, r: 0.12, cut: 2600, q: 0.5, rev: 0.20, gain: 0.75 }, + /* 3 吉他 */ { w: [[0.8, 'sawtooth'], [0.4, 'triangle', 1]], a: 0.006, d: 0.9, s: 0.12, r: 0.35, cut: 2800, q: 0.8, rev: 0.18, gain: 0.95 }, + /* 4 贝斯 */ { w: [[0.9, 'sawtooth'], [0.6, 'sine', -1]], a: 0.008, d: 0.6, s: 0.45, r: 0.18, cut: 1100, q: 1.0, rev: 0.05, gain: 1.15 }, + /* 5 弦乐 */ { w: [[0.8, 'sawtooth'], [0.8, 'sawtooth', 0, 7]], a: 0.18, d: 0.4, s: 0.85, r: 0.7, cut: 2400, q: 0.6, rev: 0.45, gain: 0.8 }, + /* 6 合奏 */ { w: [[0.7, 'sawtooth'], [0.6, 'square', 0, -6]], a: 0.12, d: 0.4, s: 0.85, r: 0.6, cut: 2200, q: 0.6, rev: 0.42, gain: 0.8 }, + /* 7 铜管 */ { w: [[0.9, 'sawtooth'], [0.4, 'square', 1]], a: 0.06, d: 0.3, s: 0.78, r: 0.3, cut: 3000, q: 0.9, rev: 0.28, gain: 0.85 }, + /* 8 簧管 */ { w: [[0.8, 'square'], [0.4, 'sawtooth', 0, 5]], a: 0.05, d: 0.3, s: 0.82, r: 0.3, cut: 2000, q: 0.8, rev: 0.30, gain: 0.75 }, + /* 9 吹管 */ { w: [[0.9, 'sine'], [0.35, 'triangle', 1], [0.08, 'sawtooth']], a: 0.08, d: 0.3, s: 0.88, r: 0.35, cut: 3400, q: 0.6, rev: 0.40, gain: 0.85 }, + /* 10 合成主音 */ { w: [[0.9, 'sawtooth'], [0.5, 'square', 0, 8]], a: 0.02, d: 0.35, s: 0.6, r: 0.3, cut: 2800, q: 1.2, rev: 0.25, gain: 0.8 }, + /* 11 合成铺底 */ { w: [[0.7, 'sawtooth'], [0.7, 'triangle', 0, -9], [0.4, 'sine', 1]], a: 0.6, d: 1.2, s: 0.7, r: 1.4, cut: 1600, q: 0.5, rev: 0.60, gain: 0.7 }, + /* 12 音效 */ { w: [[0.6, 'sawtooth'], [0.5, 'sine', 1]], a: 0.4, d: 1.5, s: 0.5, r: 1.4, cut: 1800, q: 0.7, rev: 0.60, gain: 0.7 }, + /* 13 民族 */ { w: [[0.8, 'triangle'], [0.4, 'sawtooth', 1]], a: 0.01, d: 0.8, s: 0.2, r: 0.4, cut: 2800, q: 0.8, rev: 0.25, gain: 0.9 }, + /* 14 打击 */ { w: [[0.8, 'triangle'], [0.4, 'square', 1]], a: 0.002, d: 0.5, s: 0.03, r: 0.3, cut: 4500, q: 0.7, rev: 0.30, gain: 0.9 }, + /* 15 特效 */ { w: [[0.7, 'sawtooth'], [0.5, 'square', 1]], a: 0.05, d: 1.2, s: 0.2, r: 0.8, cut: 2200, q: 0.7, rev: 0.50, gain: 0.7 } ]; function familyFor(program, channel) { @@ -76,7 +76,7 @@ return buf; } - /* 生成一段指数衰减的噪声作为混响脉冲响应 */ + /* 生成一段指数衰减的噪声作为混响脉冲响应(无需外部音频文件) */ function makeImpulse(ctx, seconds, decay) { var len = Math.floor(ctx.sampleRate * seconds); var buf = ctx.createBuffer(2, len, ctx.sampleRate); @@ -139,7 +139,8 @@ var cut = fam.cut * (0.55 + 0.45 * vel) + freq * 0.8; cut = Math.max(180, Math.min(cut, nyq - 800)); filter.frequency.setValueAtTime(cut, t0); - filter.frequency.exponentialRampToValueAtTime(Math.max(180, cut * 0.45), t0 + fam.a + fam.d + 0.05); + filter.frequency.exponentialRampToValueAtTime( + Math.max(180, cut * 0.45), t0 + fam.a + fam.d + 0.05); filter.Q.value = fam.q; filter.connect(out); @@ -210,7 +211,7 @@ this.midi = midi; this.key = 9 * 128 + midi; this.channel = 9; - this.released = true; // 打击乐不需要 note-off + this.released = true; // 打击乐自动衰减,不需要 note-off this.isDrum = true; var ctx = this.ctx; @@ -262,7 +263,7 @@ src.stop(t0 + dur + 0.05); this.sources.push(src); - if (spec.body) { // 军鼓加一点鼓皮基音 + if (spec.body) { // 军鼓:叠一点鼓皮基音 var bo = ctx.createOscillator(); bo.type = 'triangle'; bo.frequency.setValueAtTime(spec.body, t0); @@ -282,6 +283,7 @@ } DrumVoice.prototype.release = function () { /* 打击乐自动衰减 */ }; + DrumVoice.prototype.cut = function (when) { var t = clampTime(this.ctx, when); holdParam(this.out.gain, t); @@ -319,12 +321,12 @@ this.comp = comp; this.analyser = analyser; - /* 混响总线(生成式脉冲响应,失败则自动关闭) */ + /* 混响总线(生成式脉冲响应,构造失败则自动关闭) */ this.reverb = null; this.reverbGain = null; try { var conv = ctx.createConvolver(); - conv.buffer = makeImpulse(ctx, 2.2, 2.6); + conv.buffer = makeImpulse(ctx, 1.8, 2.6); var wet = ctx.createGain(); wet.gain.value = 0.9; conv.connect(wet); @@ -372,15 +374,14 @@ this._gc(t); if (this.voices.length >= this.maxVoices) this._steal(t); - var voice; if (channel === 9) { - voice = new DrumVoice(this, midi, velocity, t); + this.voices.push(new DrumVoice(this, midi, velocity, t)); } else { - voice = new Voice(this, channel, midi, velocity, t, program || 0); + var voice = new Voice(this, channel, midi, velocity, t, program || 0); var k = channel * 128 + midi; (this.pending[k] || (this.pending[k] = [])).push(voice); + this.voices.push(voice); } - this.voices.push(voice); }; Synth.prototype.noteOff = function (channel, midi, when) { @@ -388,6 +389,11 @@ var k = channel * 128 + midi; var arr = this.pending[k]; if (!arr || !arr.length) return; + + /* 跳过已被偷声/掐断的声部,避免把 note-off 用错对象导致卡音 */ + while (arr.length && arr[0].released) arr.shift(); + if (!arr.length) { delete this.pending[k]; return; } + var v = arr.shift(); if (!arr.length) delete this.pending[k]; v.release(clampTime(this.ctx, when)); @@ -412,6 +418,7 @@ /* 强制掐断所有正在发声的音符 */ Synth.prototype.panic = function (when) { + if (!this.ctx) return; var t = when == null ? this.ctx.currentTime : when; var list = this.voices.slice(); for (var i = 0; i < list.length; i++) { @@ -422,7 +429,7 @@ Synth.prototype.voiceCount = function () { return this.voices.length; }; - /* 当前正在发声的 音高集合(用于可视化) */ + /* 当前正在发声的音高(键 = channel*128 + midi),用于可视化 */ Synth.prototype.activeNotes = function () { var set = []; for (var i = 0; i < this.voices.length; i++) { @@ -437,7 +444,9 @@ } for (var k in this.pending) { var arr = this.pending[k]; - for (var j = arr.length - 1; j >= 0; j--) if (arr[j].released) arr.splice(j, 1); + for (var j = arr.length - 1; j >= 0; j--) { + if (arr[j].released) arr.splice(j, 1); + } if (!arr.length) delete this.pending[k]; } }; @@ -449,7 +458,18 @@ } if (idx === -1) idx = 0; var v = this.voices.splice(idx, 1)[0]; - if (v) { try { v.cut(t); } catch (e) { /* 忽略 */ } } + if (!v) return; + try { v.cut(t); } catch (e) { /* 忽略 */ } + /* 从待关闭表里摘掉,防止 note-off 认错对象 */ + for (var k in this.pending) { + var arr = this.pending[k]; + var pos = arr.indexOf(v); + if (pos >= 0) { + arr.splice(pos, 1); + if (!arr.length) delete this.pending[k]; + break; + } + } }; return {