/ music / woodblock2
Sign in
music / woodblock2
woodblock2 / js / physics.js
38 lines · 1.4 KBBlameHistoryRaw
1// Physics laws shared by the modal model and the SoundFont export.
3// Damping law of the modal model: Rayleigh damping matched to loss factor eta at F1 and
4// F2, with the stiffness term rolled off above F_TAU (a Zener element).
5export const F1 = 400, F2 = 4000, F_TAU = 40000;
6export const TAU = 1 / (2 * Math.PI * F_TAU);
8export function rayleigh(eta) {
9 const w1 = 2 * Math.PI * F1, w2 = 2 * Math.PI * F2;
10 return { alpha: (eta * w1 * w2) / (w1 + w2), beta: eta / (w1 + w2) };
13// Amplitude decay rate (1/s) of a mode at angular frequency w.
14export function decayRate(w, eta) {
15 const { alpha, beta } = rayleigh(eta);
16 return alpha / 2 + (beta * w * w) / (2 * (1 + (w * TAU) ** 2));
19// Hertz contact (F = K d^1.5) of mass m at speed v0 against a rigid surface.
20export function hertzPulse(K, m, v0) {
21 const dmax = Math.pow((5 * m * v0 * v0) / (4 * K), 0.4);
22 return { T: (2.9432 * dmax) / v0, Fm: K * Math.pow(dmax, 1.5) };
25// Fourier transform of a half-sine pulse A sin(pi t/T), 0<t<T, at angular frequency w.
26export function halfSineFT(A, T, w) {
27 const a = Math.PI / T;
28 const den = a * a - w * w;
29 if (Math.abs(den) < 1e-6 * a * a) {
30 // limit w -> a: integral = T/2 * (-i) * A (approximately)
31 return [0, -A * T / 2];
32 }
33 const re = (A * a * (1 + Math.cos(w * T))) / den;
34 const im = (-A * a * Math.sin(w * T)) / den;
35 return [re, im];
38export const pitchScale = (semitones) => Math.pow(2, semitones / 12);
↑↓ move↵openescclose