Skip to content

Array of peaks #2 #5

@drzajwo

Description

@drzajwo

Hi,
Sorry for "reopening" already closed issue. I'm implementing waveform to project I'm working on.
@Jchinonso In article you provided images/code gists for most important part for me are missing...

For now I'm using bbc/audiowaveform data and scale it like below:

  const clamp = (num, min, max) => {
    return num <= min ? min : num >= max ? max : num;
  };

  const scaleData = () => {
    // song can be 8 or 16 bits
    const max = Math.pow(2, bits - 1);
    const scaled = data.map((val, index) => {
      let scaledVal = val / max;
      // Not sure how to handle negative values...
      scaledVal = scaledVal < 0 ? scaledVal * -1 : scaledVal;
      return clamp(scaledVal, 0, 1);
    });
    return scaled;
  };

or filtering data from negative values

  const scaleData = () => {
    // song can be 8 or 16 bits
    const max = Math.pow(2, bits - 1);
    const scaled = data.filter((val) => val >= 0).map((val) => {
      let scaledVal = val / max;
      return clamp(scaledVal, 0, 1);
    });
    return scaled;
  };

Where data and bits are fetched from bbc data. This helps me to easily get values in rage [-1, 1] however you guys support values in range [0, 1] so in my code I'm converting negative values to positive (I'm pretty sure it's wrong :D ).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions