Skip to content

Incorrect window size calculation in waveFormDiff.ts #2

Description

@Zyam-1

Bug: Incorrect Millisecond to Sample Conversion in waveFormDiff.ts

Summary

The window size calculation in src/audio/waveFormDiff.ts incorrectly converts milliseconds to samples by dividing the duration by 100 instead of 1000.

This results in the computed window size being 10 times larger than intended, affecting the accuracy of waveform comparison and any downstream processing that relies on the configured analysis window.


Affected File

src/audio/waveFormDiff.ts

Problem

Current implementation:

let windowSize = Math.floor((windowSizeMs / 100) * sampleRate);

The conversion from milliseconds to seconds is incorrect.

To convert milliseconds to seconds, the value must be divided by 1000, not 100.

Example

Given:

  • windowSizeMs = 50
  • sampleRate = 44,100 Hz

Current calculation:

Math.floor((50 / 100) * 44100)

Result:

22,050 samples

This corresponds to approximately 500 ms, which is 10× larger than the requested window.

Expected calculation:

Math.floor((50 / 1000) * 44100)

Result:

2,205 samples

This correctly represents a 50 ms analysis window.


Expected Behavior

The window size should accurately represent the configured duration in milliseconds by converting milliseconds to seconds before multiplying by the sample rate.


Suggested Fix

Replace:

let windowSize = Math.floor((windowSizeMs / 100) * sampleRate);

with:

let windowSize = Math.floor((windowSizeMs / 1000) * sampleRate);

Acceptance Criteria

  • Replace the incorrect conversion factor (100) with 1000.
  • Ensure a 50 ms window at a 44.1 kHz sample rate produces 2,205 samples.
  • Verify other window durations produce the expected sample counts.
  • Add or update unit tests to validate the millisecond-to-sample conversion logic.
  • Confirm existing waveform comparison behavior remains correct after the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions