summaryrefslogtreecommitdiff
path: root/tools/mq_editor/mq_extract.js
blob: c7ead4da28b0252e2d9f3913400dcef9ed677516 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// MQ Extraction Algorithm
// McAulay-Quatieri sinusoidal analysis

// Extract partials from audio buffer
function extractPartials(audioBuffer, params) {
  const {fftSize, hopSize, threshold, sampleRate} = params;

  // Get mono channel (mix to mono if stereo)
  const signal = getMono(audioBuffer);
  const numFrames = Math.floor((signal.length - fftSize) / hopSize);

  // Analyze frames
  const frames = [];
  for (let i = 0; i < numFrames; ++i) {
    const offset = i * hopSize;
    const frame = signal.slice(offset, offset + fftSize);
    const peaks = detectPeaks(frame, fftSize, sampleRate, threshold);
    const time = offset / sampleRate;
    frames.push({time, peaks});
  }

  // Track trajectories
  const partials = trackPartials(frames, sampleRate);

  // Fit bezier curves
  for (const partial of partials) {
    partial.freqCurve = fitBezier(partial.times, partial.freqs);
    partial.ampCurve = fitBezier(partial.times, partial.amps);
  }

  return partials;
}

// Get mono signal
function getMono(audioBuffer) {
  const data = audioBuffer.getChannelData(0);
  if (audioBuffer.numberOfChannels === 1) {
    return data;
  }

  // Mix to mono
  const left = audioBuffer.getChannelData(0);
  const right = audioBuffer.getChannelData(1);
  const mono = new Float32Array(left.length);
  for (let i = 0; i < left.length; ++i) {
    mono[i] = (left[i] + right[i]) * 0.5;
  }
  return mono;
}

// Detect peaks in FFT frame
function detectPeaks(frame, fftSize, sampleRate, thresholdDB) {
  // Apply Hann window
  const windowed = new Float32Array(fftSize);
  for (let i = 0; i < fftSize; ++i) {
    const w = 0.5 - 0.5 * Math.cos(2 * Math.PI * i / fftSize);
    windowed[i] = frame[i] * w;
  }

  // FFT (using built-in)
  const spectrum = realFFT(windowed);

  // Convert to magnitude dB
  const mag = new Float32Array(fftSize / 2);
  for (let i = 0; i < fftSize / 2; ++i) {
    const re = spectrum[i * 2];
    const im = spectrum[i * 2 + 1];
    const magLin = Math.sqrt(re * re + im * im);
    mag[i] = 20 * Math.log10(Math.max(magLin, 1e-10));
  }

  // Find local maxima above threshold
  const peaks = [];
  for (let i = 2; i < mag.length - 2; ++i) {
    if (mag[i] > thresholdDB &&
        mag[i] > mag[i-1] && mag[i] > mag[i-2] &&
        mag[i] > mag[i+1] && mag[i] > mag[i+2]) {

      // Parabolic interpolation for sub-bin accuracy
      const alpha = mag[i-1];
      const beta = mag[i];
      const gamma = mag[i+1];
      const p = 0.5 * (alpha - gamma) / (alpha - 2*beta + gamma);

      const binFreq = (i + p) * sampleRate / fftSize;
      const ampDB = beta - 0.25 * (alpha - gamma) * p;
      const ampLin = Math.pow(10, ampDB / 20);

      peaks.push({freq: binFreq, amp: ampLin});
    }
  }

  return peaks;
}

// Track partials across frames (birth/death/continuation)
function trackPartials(frames, sampleRate) {
  const partials = [];
  const activePartials = [];
  const candidatePartials = []; // Pre-birth candidates
  const trackingThresholdRatio = 0.05; // 5% frequency tolerance
  const minTrackingHz = 20; // Minimum 20 Hz
  const birthPersistence = 3; // Require 3 consecutive frames to birth
  const deathAge = 5; // Allow 5 frame gap before death
  const minPartialLength = 10; // Minimum 10 frames for valid partial

  for (const frame of frames) {
    const matched = new Set();

    // Match peaks to existing partials
    for (const partial of activePartials) {
      const lastFreq = partial.freqs[partial.freqs.length - 1];
      const threshold = Math.max(lastFreq * trackingThresholdRatio, minTrackingHz);

      let bestPeak = null;
      let bestDist = Infinity;

      for (let i = 0; i < frame.peaks.length; ++i) {
        if (matched.has(i)) continue;

        const peak = frame.peaks[i];
        const dist = Math.abs(peak.freq - lastFreq);

        if (dist < threshold && dist < bestDist) {
          bestPeak = peak;
          bestDist = dist;
          partial.matchIdx = i;
        }
      }

      if (bestPeak) {
        // Continuation
        partial.times.push(frame.time);
        partial.freqs.push(bestPeak.freq);
        partial.amps.push(bestPeak.amp);
        partial.age = 0;
        matched.add(partial.matchIdx);
      } else {
        // No match
        partial.age++;
      }
    }

    // Update candidate partials (pre-birth)
    for (let i = candidatePartials.length - 1; i >= 0; --i) {
      const candidate = candidatePartials[i];
      const lastFreq = candidate.freqs[candidate.freqs.length - 1];
      const threshold = Math.max(lastFreq * trackingThresholdRatio, minTrackingHz);

      let bestPeak = null;
      let bestDist = Infinity;

      for (let i = 0; i < frame.peaks.length; ++i) {
        if (matched.has(i)) continue;

        const peak = frame.peaks[i];
        const dist = Math.abs(peak.freq - lastFreq);

        if (dist < threshold && dist < bestDist) {
          bestPeak = peak;
          bestDist = dist;
          candidate.matchIdx = i;
        }
      }

      if (bestPeak) {
        candidate.times.push(frame.time);
        candidate.freqs.push(bestPeak.freq);
        candidate.amps.push(bestPeak.amp);
        matched.add(candidate.matchIdx);

        // Birth if persistent enough
        if (candidate.times.length >= birthPersistence) {
          activePartials.push(candidate);
          candidatePartials.splice(i, 1);
        }
      } else {
        // Candidate died, remove
        candidatePartials.splice(i, 1);
      }
    }

    // Create new candidate partials from unmatched peaks
    for (let i = 0; i < frame.peaks.length; ++i) {
      if (matched.has(i)) continue;

      const peak = frame.peaks[i];
      candidatePartials.push({
        times: [frame.time],
        freqs: [peak.freq],
        amps: [peak.amp],
        age: 0,
        matchIdx: -1
      });
    }

    // Death old partials
    for (let i = activePartials.length - 1; i >= 0; --i) {
      if (activePartials[i].age > deathAge) {
        // Move to finished if long enough
        if (activePartials[i].times.length >= minPartialLength) {
          partials.push(activePartials[i]);
        }
        activePartials.splice(i, 1);
      }
    }
  }

  // Finish remaining active partials
  for (const partial of activePartials) {
    if (partial.times.length >= minPartialLength) {
      partials.push(partial);
    }
  }

  return partials;
}

// Fit cubic bezier curve to trajectory
function fitBezier(times, values) {
  if (times.length < 4) {
    // Not enough points, just use linear segments
    return {
      t0: times[0], v0: values[0],
      t1: times[0], v1: values[0],
      t2: times[times.length-1], v2: values[values.length-1],
      t3: times[times.length-1], v3: values[values.length-1]
    };
  }

  // Fix endpoints
  const t0 = times[0];
  const t3 = times[times.length - 1];
  const v0 = values[0];
  const v3 = values[values.length - 1];

  // Solve for interior control points via least squares
  // Simplification: place at 1/3 and 2/3 positions
  const t1 = t0 + (t3 - t0) / 3;
  const t2 = t0 + 2 * (t3 - t0) / 3;

  // Find v1, v2 by evaluating at nearest data points
  let v1 = v0, v2 = v3;
  let minDist1 = Infinity, minDist2 = Infinity;

  for (let i = 0; i < times.length; ++i) {
    const dist1 = Math.abs(times[i] - t1);
    const dist2 = Math.abs(times[i] - t2);

    if (dist1 < minDist1) {
      minDist1 = dist1;
      v1 = values[i];
    }
    if (dist2 < minDist2) {
      minDist2 = dist2;
      v2 = values[i];
    }
  }

  return {t0, v0, t1, v1, t2, v2, t3, v3};
}