IMP: waveform perf improvement
This commit is contained in:
parent
c44b30b55b
commit
62e0906b3a
1 changed files with 7 additions and 4 deletions
|
|
@ -27,16 +27,19 @@ export class WaveformGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
extractPeaks(audioBuffer) {
|
extractPeaks(audioBuffer) {
|
||||||
const { numberOfChannels, length, sampleRate } = audioBuffer;
|
const { length, duration } = audioBuffer;
|
||||||
const numPeaks = Math.floor(4*length/sampleRate);
|
const numPeaks = Math.min(Math.floor(4*duration), 1000);
|
||||||
const peaks = new Float32Array(numPeaks);
|
const peaks = new Float32Array(numPeaks);
|
||||||
const chanData = audioBuffer.getChannelData(0); // Use first channel
|
const chanData = audioBuffer.getChannelData(0); // Use first channel
|
||||||
const step = Math.floor(length / numPeaks);
|
const step = Math.floor(length / numPeaks);
|
||||||
|
const stride = 8; // Check every 8th sample for speed
|
||||||
|
|
||||||
for (let i = 0; i < numPeaks; i++) {
|
for (let i = 0; i < numPeaks; i++) {
|
||||||
let max = 0;
|
let max = 0;
|
||||||
for (let j = 0; j < step; j++) {
|
const start = i * step;
|
||||||
const datum = chanData[i * step + j];
|
const end = start + step;
|
||||||
|
for (let j = start; j < end; j += stride) {
|
||||||
|
const datum = chanData[j];
|
||||||
if (datum > max) {
|
if (datum > max) {
|
||||||
max = datum;
|
max = datum;
|
||||||
} else if (-datum > max) {
|
} else if (-datum > max) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue