Class: Waveform

Overview

Provides waveform data in 150 points/sec resolution (each number will represent 6.666 milliseconds regardless of the sample rate).


Implementation example

class SuperpoweredClipperProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
// Called when the AudioWorklet is ready to start processing audio
onReady() {
this.waveform = new this.Superpowered.Waveform(
48000, // The sample rate of the audio input.
60 // The length in seconds of the audio input. It will not be able to process more audio than this. You can change this value in the process() method.
);
}
onDestruct() {
this.waveform.destruct();
}
calculateAndSendWaveformData() {
//First instruct the waveform instance to run its calculations internally. processAudio should not run when this is called!
this.waveform.makeResult();
// Then get the peakWaveform data
// returns a Superpowered.Uint8Buffer. 150 points/sec waveform data displaying the peak volume. Uint8Array. Each byte represents one "pixel".
const wasmResult = waveform.getPeakWaveform();
// Result is backed by WebAssembly Linear Memory that can not be passed to another scope (thread).
// Let's clone it into "result".
const result = new Uint8Array();
result.set(wasmResult.array);
// Then send this back to the main scope from from this AudioWorklet scope
this.sendMessageToMainScope({
waveformData: result
});
}
onMessageFromMainScope(message) {
// handle an incoming instruction from the main thread to generate data
// We're using sendMessageTOAudioScope({requestWaveformData: true}) here, but you decide the object shape.
// We trigger this to be sent from the main scope on a window.requestAnimationFrame loop for example.
if (typeof message.requestWaveformData) this.calculateAndSendWaveformData();
}
processAudio(inputBuffer, outputBuffer, buffersize) {
this.waveform.process(
inputBuffer.pointer,
buffersize,
-1
);
}
}
#include "SuperpoweredWaveform.h"
void initWaveformAnalyzer() {
waveform = new Superpowered::Waveform(
48000, // The sample rate of the audio input.
60 // The length in seconds of the audio input. It will not be able to process more audio than this. You can change this value in the process() method.
);
}
unsigned char *getWaveformPeakResult(int *size) {
//First instruct the waveform instance to run its calculations internally. processAudio should not run when this is called!
waveform->makeResult();
*size = waveform->waveformSize;
return waveform->getPeakWaveform(); // Each byte represents one "pixel".
}
void processAudio(float *interleavedInput, float *interleavedOutput, unsigned int numberOfFrames) {
waveform->process(
interleavedInput, // Pointer to floating point numbers. 32-bit interleaved stereo input.
numberOfFrames, // Number of frames to process.
-1 // If the audio input length may change, set this to the current length. Use -1 otherwise. If this value is not -1, this method can NOT be used in a real-time audio thread.
);
}

Properties

waveformSize

PROPERTY
The number of bytes in the peak waveform.
TypeMinMaxDefault
Number

waveformSize

PROPERTY
The number of bytes in the peak waveform.
TypeMinMaxDefault
int

Methods

  • constructor

    METHOD
    Creates a Waveform instance.
    Parameters
    NameTypeDescription
    samplerateNumberThe sample rate of the audio input.
    lengthSecondsNumberThe length in seconds of the audio input. It will not be able to process more audio than this. You can change this value in the process() method.
    Returns
    TypeDescription
    Superpowered.WaveformThe constructed instance.
  • constructor

    METHOD
    Creates a Waveform instance.
    Parameters
    NameTypeDefaultDescription
    samplerateunsigned intThe sample rate of the audio input.
    lengthSecondsintThe length in seconds of the audio input. It will not be able to process more audio than this. You can change this value in the process() method.
    Returns
    TypeDescription
    Superpowered::WaveformThe constructed instance.
  • destruct

    METHOD
    Destructor to free Linear Memory.
    Parameters
    None
    Returns
    None
  • getPeakWaveform

    METHOD
    Returns with 150 points/sec waveform data displaying the peak volume. Each number is an unsigned byte (8-bits), representing one "pixel". Available after calling makeResults().
    Parameters
    NameTypeDescription
    takeOwnershipBooleanIf true, you take ownership on the data, so don't forget to free() the memory to prevent memory leaks.
    Returns
    TypeDescription
    Superpowered.Uint8Buffer150 points/sec waveform data
  • getPeakWaveform

    METHOD
    Returns with 150 points/sec waveform data displaying the peak volume. Each number is an unsigned byte (8-bits), representing one "pixel". Available after calling makeResults().
    Parameters
    NameTypeDefaultDescription
    takeOwnershipboolIf true, you take ownership on the data, so don't forget to free() the memory to prevent memory leaks.
    Returns
    TypeDescription
    unsigned char *150 points/sec waveform data
  • makeResult

    METHOD
    Makes the result from the collected data. This method should NOT be used in a real-time audio thread.
    Parameters
    None
    Returns
    None
  • makeResult

    METHOD
    Makes the result from the collected data. This method should NOT be used in a real-time audio thread.
    Parameters
    None
    Returns
    None
  • process

    METHOD
    Processes some audio. This method can be used in a real-time audio thread if lengthSeconds is -1.
    Parameters
    NameTypeDescription
    inputNumber (pointer in Linear Memory)32-bit interleaved stereo input.
    numberOfFramesNumberNumber of frames to process.
    lengthSecondsNumberIf the audio input length may change, set this to the current length. Use -1 otherwise. If this value is not -1, this method can NOT be used in a real-time audio thread.
    Returns
    None
  • process

    METHOD
    Processes some audio. This method can be used in a real-time audio thread if lengthSeconds is -1.
    Parameters
    NameTypeDefaultDescription
    inputfloat *32-bit interleaved stereo input.
    numberOfFramesunsigned intNumber of frames to process.
    lengthSecondsint-1If the audio input length may change, set this to the current length. Use -1 otherwise. If this value is not -1, this method can NOT be used in a real-time audio thread.
    Returns
    None

v1.0.6