fix: 修复偷声后 note-off 认错对象导致的卡音;缩短混响脉冲响应
This commit is contained in:
+34
-14
@@ -16,7 +16,7 @@
|
||||
'use strict';
|
||||
|
||||
/* ---------------------------------------------------------------- */
|
||||
/* 音色参数表:w=[相对音量, 波形, 八度偏移, 失谐(音分)] */
|
||||
/* 音色参数表:w = [相对音量, 波形, 八度偏移, 失谐(音分)] */
|
||||
/* a 起音 / d 衰减 / s 保持 / r 释放 (秒) */
|
||||
/* cut 低通截止 / q 品质因数 / rev 混响发送量 / gain 总增益 */
|
||||
/* ---------------------------------------------------------------- */
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user