Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Memory and Performance in waveform.js #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions audiogram/waveform.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,45 @@ var probe = require("../lib/probe.js"),

function getWaveform(filename, options, cb) {

// Pre-allocate samples array if possible
let expectedSize = 100000; // Replace with an accurate estimate if possible
let samples = new Float64Array(expectedSize);
let sampleCount = 0;

var stream = pcmStream(filename, {
channels: options.channels
}),
samples = [];
channels: options.channels
});

stream.on("data",function(sample, channel){
stream.on("data", function(sample, channel){
let lastSampleIndex = sampleCount - 1;
let nextChannel = channel + 1;

// Average multiple channels
if (channel > 0) {
samples[samples.length - 1] = ((samples[samples.length - 1] * channel) + sample) / (channel + 1);
samples[lastSampleIndex] = ((samples[lastSampleIndex] * channel) + sample) / nextChannel;
} else {
samples.push(sample);
samples[sampleCount] = sample;
sampleCount++;
}

});

stream.on("error", cb);

stream.on("end", function(output){
var processed = processSamples(samples, options.numFrames, options.samplesPerFrame);
stream.on("end", function(){
// Trim the pre-allocated array to the actual size
let trimmedSamples = samples.subarray(0, sampleCount);
var processed = processSamples(trimmedSamples, options.numFrames, options.samplesPerFrame);
return cb(null, processed);
});

}

function processSamples(samples, numFrames, samplesPerFrame) {

// TODO spread out slop across frames
var perFrame = Math.floor(samples.length / numFrames),
perPoint = Math.floor(perFrame / samplesPerFrame),
range = d3.range(samplesPerFrame),
maxFrame,
maxRms = maxMid = 0;

var unadjusted = d3.range(numFrames).map(function(frame){

var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame),
points = range.map(function(point){

points = d3.range(samplesPerFrame).map(function(point){
var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
midpoint = pointSamples[Math.floor(pointSamples.length / 2)];

Expand All @@ -52,20 +52,16 @@ function processSamples(samples, numFrames, samplesPerFrame) {

if (rms > maxRms) {
maxRms = rms;
maxFrame = frame;
}

if (Math.abs(midpoint) > maxMid) {
maxMid = Math.abs(midpoint);
}

// Min value, max value, and midpoint value
return [rms, midpoint];

});

return points;

});

var adjusted = unadjusted.map(function(frame){
Expand All @@ -78,10 +74,19 @@ function processSamples(samples, numFrames, samplesPerFrame) {
});

// Make first and last frame peaky
adjusted[0] = adjusted[numFrames - 1] = adjusted[maxFrame];
// adjusted[0] = adjusted[numFrames - 1] = adjusted[d3.maxIndex(unadjusted, d => d[0])];
let maxIndex = 0;
let maxValue = -Infinity;
for(let i = 0; i < unadjusted.length; i++) {
let currMax = Math.max(...unadjusted[i].map(d => d[0]));
if(currMax > maxValue) {
maxValue = currMax;
maxIndex = i;
}
}
adjusted[0] = adjusted[numFrames - 1] = adjusted[maxIndex];

return adjusted;

}

module.exports = getWaveform;