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 audioonReady() {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 scopethis.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
PROPERTYThe number of bytes in the peak waveform.
Type | Min | Max | Default | Number |
---|
waveformSize
PROPERTYThe number of bytes in the peak waveform.
Type | Min | Max | Default | int |
---|
Methods
constructor
METHODCreates a Waveform instance.ParametersReturnsName Type Description samplerate Number
The sample rate of the audio input. lengthSeconds Number
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. Type Description Superpowered.Waveform
The constructed instance. constructor
METHODCreates a Waveform instance.ParametersReturnsName Type Default Description samplerate unsigned int
The sample rate of the audio input. lengthSeconds int
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. Type Description Superpowered::Waveform
The constructed instance. destruct
METHODDestructor to free Linear Memory.ParametersNoneReturnsNonegetPeakWaveform
METHODReturns 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().ParametersReturnsName Type Description takeOwnership Boolean
If true, you take ownership on the data, so don't forget to free() the memory to prevent memory leaks. Type Description Superpowered.Uint8Buffer
150 points/sec waveform data getPeakWaveform
METHODReturns 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().ParametersReturnsName Type Default Description takeOwnership bool
If true, you take ownership on the data, so don't forget to free() the memory to prevent memory leaks. Type Description unsigned char *
150 points/sec waveform data makeResult
METHODMakes the result from the collected data. This method should NOT be used in a real-time audio thread.ParametersNoneReturnsNonemakeResult
METHODMakes the result from the collected data. This method should NOT be used in a real-time audio thread.ParametersNoneReturnsNoneprocess
METHODProcesses some audio. This method can be used in a real-time audio thread if lengthSeconds is -1.ParametersReturnsName Type Description input Number (pointer in Linear Memory)
32-bit interleaved stereo input. numberOfFrames Number
Number of frames to process. lengthSeconds Number
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. Noneprocess
METHODProcesses some audio. This method can be used in a real-time audio thread if lengthSeconds is -1.ParametersReturnsName Type Default Description input float *
32-bit interleaved stereo input. numberOfFrames unsigned int
Number of frames to process. lengthSeconds int
-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. None