获取网络音频的音量速度

Volume velocity to gain web audio

我正在尝试设置力度值,这是一个在 MIDI 信号中获得的值。力度范围从0到127.

web audio api 上的文档虽然做得很好,但并没有真正说明这一点。

目前我有这个来播放声音:

  play(key, startTime) {    
    this.audioContext.decodeAudioData(this.soundContainer[key], (buffer) => {
      let source = this.audioContext.createBufferSource(); 
      source.buffer = buffer;                   
      source.connect(this.audioContext.destination);
      source.start(startTime);
     });
  }

我没有找到任何可以使用范围从 0 到 127 的速度值的东西。但是我发现 gain node 应用了增益。

所以我的函数现在是这样的:

  play(key:string, startTime, velocity) {    
    this.audioContext.decodeAudioData(this.soundContainer[key], (buffer) => {
      let source = this.audioContext.createBufferSource(); 
      source.buffer = buffer;                   
      source.connect(this.gainNode);
      this.gainNode.connect(this.audioContext.destination);
      this.gainNode.gain.value = velocity;
      source.start(startTime);
     });
  }

Eehhh...如果我将 MIDI 速度值应用于增益,我显然会听到非常响亮的声音。所以我想知道这两个问题中的任何一个:

我不知道它是否正确,因为我对声音不太了解,但这似乎有效:

this.gainNode.gain.value = 速度 / 100 ;

所以 127 的速度 = 1.27 的增益

最终我认为更好的方法是将 1 除以 127 个值,每个值对应于它们各自的 midi 值。然而,这样代码更容易,所以是的,它有效。

MIDI specification 说:

Interpretation of the Velocity byte is left up to the receiving instrument. Generally, the larger the numeric value of the message, the stronger the velocity-controlled effect. If velocity is applied to volume (output level) for instance, then higher Velocity values will generate louder notes. A value of 64 (40H) would correspond to a mezzo-forte note […] Preferably, application of velocity to volume should be an exponential function.

通用 MIDI 规范不再具体。

DLS Level 1 specification 说:

The MIDI Note Velocity value is converted to attenuation in dB by the Concave Transform according to the following formula:

attendB = 20 × log10(1272 / Velocity2)

and fed to control either the volume or envelope generator peak level.

然后您必须将此衰减映射到增益因子,即 gain = velocity² / 127²

许多硬件合成器允许 select 不同的曲线将速度映射到音量。