summaryrefslogtreecommitdiff
path: root/tools/mq_editor/mq_synth.js
blob: 1eec709447cd4afdd187ef20d791472c3b16adbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// MQ Synthesizer
// Replica oscillator bank for sinusoidal synthesis

// Evaluate cubic bezier curve at time t
function evalBezier(curve, t) {
  const dt = curve.t3 - curve.t0;
  if (dt <= 0) return curve.v0;
  let u = (t - curve.t0) / dt;
  u = Math.max(0, Math.min(1, u));
  const u1 = 1.0 - u;
  return u1*u1*u1 * curve.v0 +
         3*u1*u1*u * curve.v1 +
         3*u1*u*u  * curve.v2 +
         u*u*u     * curve.v3;
}

// Deterministic LCG PRNG
function randFloat(seed, min, max) {
  seed = (1664525 * seed + 1013904223) % 0x100000000;
  return min + (seed / 0x100000000) * (max - min);
}

// Synthesize audio from MQ partials
// partials: array of {freqCurve, ampCurve, replicas?}
// replicas: {offsets, decay_alpha, jitter, spread_above, spread_below}
// integratePhase: true = accumulate 2π*f/SR per sample (correct for varying freq)
//                 false = 2π*f*t (simpler, only correct for constant freq)
function synthesizeMQ(partials, sampleRate, duration, integratePhase = true, options = {}) {
  const numSamples = Math.floor(sampleRate * duration);
  const pcm = new Float32Array(numSamples);

  const jitterMult = options.disableJitter ? 0 : 1;
  const spreadMult = options.disableSpread ? 0 : 1;

  const defaultReplicas = {
    offsets:       [1.0],
    decay_alpha:   0.1,
    jitter:        0.05,
    spread_above:  0.02,
    spread_below:  0.02
  };

  // Pre-build per-partial configs with fixed spread/jitter and phase accumulators
  const configs = [];
  for (let p = 0; p < partials.length; ++p) {
    const rep = partials[p].replicas != null ? partials[p].replicas : defaultReplicas;
    const offsets      = rep.offsets      != null ? rep.offsets      : [1.0];
    const decay_alpha  = rep.decay_alpha  != null ? rep.decay_alpha  : 0.0;
    const jitter       = rep.jitter       != null ? rep.jitter       : 0.0;
    const spread_above = rep.spread_above != null ? rep.spread_above : 0.0;
    const spread_below = rep.spread_below != null ? rep.spread_below : 0.0;

    const replicaData = [];
    for (let r = 0; r < offsets.length; ++r) {
      // Fixed per-replica spread (frequency detuning) and initial phase (jitter)
      const spread    = spreadMult * randFloat(p * 67890 + r * 999, -spread_below, spread_above);
      const initPhase = randFloat(p * 67890 + r, 0.0, 1.0) * (jitter * jitterMult) * 2.0 * Math.PI;
      replicaData.push({ratio: offsets[r], spread, phase: initPhase});
    }

    configs.push({
      fc: partials[p].freqCurve,
      ac: partials[p].ampCurve,
      decay_alpha,
      replicaData
    });
  }

  for (let i = 0; i < numSamples; ++i) {
    const t = i / sampleRate;
    let sample = 0.0;

    for (let p = 0; p < configs.length; ++p) {
      const {fc, ac, decay_alpha, replicaData} = configs[p];
      if (t < fc.t0 || t > fc.t3) continue;

      const f0 = evalBezier(fc, t);
      const A0 = evalBezier(ac, t);

      for (let r = 0; r < replicaData.length; ++r) {
        const rep = replicaData[r];
        const f = f0 * rep.ratio * (1.0 + rep.spread);
        const A = A0 * Math.exp(-decay_alpha * Math.abs(f - f0));

        let phase;
        if (integratePhase) {
          rep.phase += 2.0 * Math.PI * f / sampleRate;
          phase = rep.phase;
        } else {
          phase = 2.0 * Math.PI * f * t + rep.phase;
        }

        sample += A * Math.sin(phase);
      }
    }

    pcm[i] = sample;
  }

  // Normalize
  let maxAbs = 0;
  for (let i = 0; i < numSamples; ++i) maxAbs = Math.max(maxAbs, Math.abs(pcm[i]));
  if (maxAbs > 1.0) {
    for (let i = 0; i < numSamples; ++i) pcm[i] /= maxAbs;
  }

  return pcm;
}