-
Notifications
You must be signed in to change notification settings - Fork 2
Performance: optimize AudioEngine getVisualizationData #237
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,33 +868,38 @@ export class AudioEngine implements IAudioEngine { | |
| ? outBuffer | ||
| : new Float32Array(outSize); | ||
| const samplesPerTarget = this.VIS_SUMMARY_SIZE / width; | ||
| const pos = this.visualizationSummaryPosition; | ||
|
|
||
| // Optimization: Pre-calculate bounds and sequential pointers to eliminate Math.floor/multiplication per sample | ||
| let outIdx = 0; | ||
| let startIdx = 0; | ||
| let floatEnd = samplesPerTarget; | ||
|
|
||
| for (let i = 0; i < width; i++) { | ||
| const rangeStart = i * samplesPerTarget; | ||
| const rangeEnd = (i + 1) * samplesPerTarget; | ||
| const endIdx = Math.floor(floatEnd); | ||
|
|
||
| let minVal = 0; | ||
| let maxVal = 0; | ||
| let first = true; | ||
|
|
||
| for (let s = Math.floor(rangeStart); s < Math.floor(rangeEnd); s++) { | ||
| // Use shadow buffer property to read linearly without modulo | ||
| const idx = (this.visualizationSummaryPosition + s) * 2; | ||
| const vMin = this.visualizationSummary[idx]; | ||
| const vMax = this.visualizationSummary[idx + 1]; | ||
|
|
||
| if (first) { | ||
| minVal = vMin; | ||
| maxVal = vMax; | ||
| first = false; | ||
| } else { | ||
|
|
||
| if (startIdx < endIdx) { | ||
| let idx = (pos + startIdx) * 2; | ||
| minVal = this.visualizationSummary[idx]; | ||
| maxVal = this.visualizationSummary[idx + 1]; | ||
|
|
||
| for (let s = startIdx + 1; s < endIdx; s++) { | ||
| idx += 2; | ||
| const vMin = this.visualizationSummary[idx]; | ||
| const vMax = this.visualizationSummary[idx + 1]; | ||
| if (vMin < minVal) minVal = vMin; | ||
| if (vMax > maxVal) maxVal = vMax; | ||
| } | ||
| } | ||
|
|
||
| subsampledBuffer[i * 2] = minVal; | ||
| subsampledBuffer[i * 2 + 1] = maxVal; | ||
| subsampledBuffer[outIdx++] = minVal; | ||
| subsampledBuffer[outIdx++] = maxVal; | ||
|
|
||
| startIdx = endIdx; | ||
| floatEnd += samplesPerTarget; | ||
| } | ||
|
Comment on lines
+876
to
903
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While replacing multiplication with repeated addition ( It's safer to recalculate the end bound in each iteration, similar to the original implementation. The performance impact on the outer loop is negligible compared to the gains in the inner loop, and it ensures correctness. for (let i = 0; i < width; i++) {
const endIdx = Math.floor((i + 1) * samplesPerTarget);
let minVal = 0;
let maxVal = 0;
if (startIdx < endIdx) {
let idx = (pos + startIdx) * 2;
minVal = this.visualizationSummary[idx];
maxVal = this.visualizationSummary[idx + 1];
for (let s = startIdx + 1; s < endIdx; s++) {
idx += 2;
const vMin = this.visualizationSummary[idx];
const vMax = this.visualizationSummary[idx + 1];
if (vMin < minVal) minVal = vMin;
if (vMax > maxVal) maxVal = vMax;
}
}
subsampledBuffer[outIdx++] = minVal;
subsampledBuffer[outIdx++] = maxVal;
startIdx = endIdx;
} |
||
| return subsampledBuffer; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: ysdede/keet
Length of output: 688
Use integer arithmetic for bucket boundary calculation to avoid floating-point drift.
The cumulative approach
floatEnd += samplesPerTargetshifts bucket boundaries compared to the old per-iteration methodMath.floor((i + 1) * step). Analysis shows 1,380 out of ~2,000 display widths produce different partitions, with mismatches occurring at boundary indices where floating-point rounding accumulates. For example, width=6 now ends bucket 5 at index 1999 instead of 2000; width=12 ends bucket 5 at 999 instead of 1000.Replace the cumulative float calculation with integer-based bucket splitting to ensure partitions match the original algorithm and preserve visualization accuracy across all widths.
🤖 Prompt for AI Agents