danigb / smplr

A web audio sampler instrument
https://danigb.github.io/smplr/
188 stars 19 forks source link

Controlling how velocity impacts note volume #92

Open iplanwebsites opened 1 month ago

iplanwebsites commented 1 month ago

Thanks for yet another fantastic library. I really like the variety samples used for the different velocities, it's especially great on the SFZ piano. I was wondering how we could control the note velocity -> volume function. Currently, the volume delta is very big and low velocity notes are often almost silent. A basic, easy to implement , approach could be to set min and max volume thresholds. Normalizing input velocity might also have a similar effect in practice, but it wouldn't benefit from those multiple velocity samples.

danigb commented 1 month ago

Hey, thanks for the kind words.

I'm not sure if this is what are you looking for, but there's this volumeToGain option that accepts a function that receives a volume level and return a gain value. See https://github.com/danigb/smplr?tab=readme-ov-file#shared-configuration-options

By default it uses MIDI recommendation on how to convert 127 level value to gain

iplanwebsites commented 1 month ago

Thank you! I see how volumeToGain is replacing the default midiVelToGain function. I'll have a closer look.

iplanwebsites commented 1 month ago

Ok, it's not in the docs, but there's the function velocityToGain can also be passed as an option to the sampler and the piano. That's the one that matters to adjust volume based on velocity.

 // ugly example of a custom curve that sounds decent on my midi device.
 function velocityToGain(vel) {
  const classicCurve = (vel * vel) / 16129; // 16129 = 127 * 127  ///official one.
  const RATIO_VEL = 0.9;
  const MIN = 0.3;
  const MAX = 1.0;
  const computer = (vel / 127) * RATIO_VEL + 1 - RATIO_VEL;
  return Math.max(MIN, Math.min(computer, MAX));
  }
danigb commented 1 month ago

What you mean is not in the docs? Is this issue fixed?

iplanwebsites commented 1 month ago

I can clarify the docs a bit and send a PR when I get some time. If I understand correctly, there are two similar functions that affects the output loudnesses. The data flow seems to be: velocity-> volume -> gain.

On Sun, 6 Oct 2024 at 08:42, danigb @.***> wrote:

What you mean is not in the docs? Is this issue fixed?

— Reply to this email directly, view it on GitHub https://github.com/danigb/smplr/issues/92#issuecomment-2395426556, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGAKEKGKXB4MKMYLK27W2DZ2EV2NAVCNFSM6AAAAABPBFOTG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJVGQZDMNJVGY . You are receiving this because you authored the thread.Message ID: @.***>