Source Class: Generator
Interactive example
Single Generator demo
Superpowered WebAssembly Audio SDK v2.6.1
Overview
The Generator creates audio in various waveform shapes. Generators can be chained together to make complex audio sources that power synths, but are also useful as a tool for diagnostics. We're using a Superpowered Generator in our oscillator input stage you might have seen through the other audio effects examples on this site.
The Superpowered Generator supports 7 different shapes, these are stored as constants under the Generator namespace:
Superpowered.Generator.Sine
Superpowered.Generator.Triangle
Superpowered.Generator.Sawtooth
Superpowered.Generator.PWM
Superpowered.Generator.PinkNoise
Superpowered.Generator.WhiteNoise
Superpowered.Generator.SyncMaster
Superpowered::Generator::Sine
Superpowered::Generator::Triangle
Superpowered::Generator::Sawtooth
Superpowered::Generator::PWM
Superpowered::Generator::PinkNoise
Superpowered::Generator::WhiteNoise
Superpowered::Generator::SyncMaster
Implementation example
Let's start of with the most simple application of a Generator, a simple sine tone oscillator. We set the initial frequency and point to our output buffer in the generate
method call. Notice also that this class does not have a process
method.
class SuperpoweredSingleGeneratorStageProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
onReady() {
this.generator = new this.Superpowered.Generator(
this.samplerate,
this.Superpowered.Generator.Sine
);
this.generator.frequency = 440;
this.genOutputBuffer = new this.Superpowered.Float32Buffer(2048);
}
onDestruct() {
this.generator.destruct();
}
onMessageFromMainScope(message) {
if (typeof message.frequency !== 'undefined') this.generator.frequency = message.frequency;
}
processAudio(inputBuffer, outputBuffer, buffersize, parameters) {
this.generator.samplerate = this.samplerate;
this.generator.generate(
this.genOutputBuffer.pointer,
buffersize
);
this.Superpowered.Interleave(
this.genOutputBuffer.pointer,
this.genOutputBuffer.pointer,
outputBuffer.pointer,
buffersize
);
}
}
#include "SuperpoweredGenerator.h"
void initGeneratorSource() {
generator = new Superpowered::Generator(48000. Superpowered::Generator::Sine);
generator->frequency = 440;
}
void processAudio(float *interleavedInput, float *interleavedOutput, unsigned int numberOfFrames, unsigned int samplerate) {
generator->samplerate = samplerate;
generator->generate(interleavedOutput, numberOfFrames);
}
Multiple voices
We can create multiple Oscillators and sum the results together.
class SuperpoweredMultipleGeneratorStageProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
onReady() {
this.generator1 = new this.Superpowered.Generator(
this.samplerate,
this.Superpowered.Generator.Sine
);
this.generator2 = new this.Superpowered.Generator(
this.samplerate,
this.Superpowered.Generator.Sine
);
this.gen1OutputBuffer = new this.Superpowered.Float32Buffer(2048);
this.gen2OutputBuffer = new this.Superpowered.Float32Buffer(2048);
this.summedMonoOutputBuffer = new this.Superpowered.Float32Buffer(2048);
this.generator1.frequency = 440;
this.generator2.frequency = 880;
}
onDestruct() {
this.generator1.destruct();
this.generator2.destruct();
}
onMessageFromMainScope(message) {
if (typeof message.frequency1 !== 'undefined') this.generator1.frequency = message.frequency1;
if (typeof message.pulsewidth1 !== 'undefined') this.generator1.pulsewidth = message.pulsewidth1;
if (typeof message.shape1 !== 'undefined') this.generator1.shape = this.Superpowered.Generator[message.shape1];
if (typeof message.frequency2 !== 'undefined') this.generator2.frequency = message.frequency2;
if (typeof message.pulsewidth2 !== 'undefined') this.generator2.pulsewidth = message.pulsewidth2;
if (typeof message.shape2 !== 'undefined') this.generator2.shape = this.Superpowered.Generator[message.shape2];
}
processAudio(inputBuffer, outputBuffer, buffersize, parameters) {
this.generator1.samplerate = this.samplerate;
this.generator2.samplerate = this.samplerate;
this.generator1.generate(
this.gen1Pointer.pointer,
buffersize
);
this.generator2.generate(
this.gen2Pointer.pointer,
buffersize
);
this.Superpowered.Add2(
this.gen1Pointer.pointer,
this.gen2Pointer.pointer,
this.monoSumPointer.pointer,
buffersize
);
this.Superpowered.Interleave(
this.monoSumPointer.pointer,
this.monoSumPointer.pointer,
outputBuffer.pointer,
buffersize
);
}
#include "SuperpoweredGenerator.h"
void initGeneratorSources() {
generator1 = new Superpowered::Generator(48000. Superpowered::Generator::Sine);
generator2 = new Superpowered::Generator(48000. Superpowered::Generator::Sine);
generator1->frequency = 440;
generator2->frequency = 880;
}
void processAudio(float *interleavedInput, float *interleavedOutput, unsigned int numberOfFrames, unsigned int samplerate) {
generator1->samplerate = samplerate;
generator2->samplerate = samplerate
float gen1OutputBuffer[numberOfFrames];
float gen2OutputBuffer[numberOfFrames];
float monoSumBuffer[numberOfFrames];
generator1->generate(gen1OutputBuffer, numberOfFrames);
generator2->generate(gen2OutputBuffer, numberOfFrames);
Superpowered->Add2(
gen1OutputBuffer,
gen2OutputBuffer,
monoSumBuffer,
numberOfFrames
);
Superpowered->Interleave(
monoSumBuffer,
monoSumBuffer,
interleavedOutput,
buffersize
);
}
Here's how multiple oscillators sound:
Multiple Generators demo
Superpowered WebAssembly Audio SDK v2.6.1
FM synthesis
We can use the Superpowered synthesizer to create frequency modulated outputs from our Generators. For a guide on Frequency modulation, see the Wikipedia page.
class SuperpoweredFmGeneratorStageProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
onReady() {
this.carrier = new this.Superpowered.Generator(
this.samplerate,
this.Superpowered.Generator.Sine
);
this.modulator = new this.Superpowered.Generator(
this.samplerate,
this.Superpowered.Generator.Sine
);
this.carrierBuffer = new this.Superpowered.Float32Buffer(2048);
this.modulatorBuffer = new this.Superpowered.Float32Buffer(2048);
this.carrier.frequency = 440
this.modulator.frequency = 1.5;
this.modDepth = 250;
}
onDestruct() {
this.carrier.destruct();
this.modulator.destruct();
}
onMessageFromMainScope(message) {
if (typeof message.frequency1 !== 'undefined') this.carrier.frequency = Number(message.frequency1) ;
if (typeof message.pulsewidth1 !== 'undefined') this.carrier.pulsewidth = Number(message.pulsewidth1);
if (typeof message.shape1 !== 'undefined') this.carrier.shape = this.Superpowered.Generator[message.shape1];
if (typeof message.frequency2 !== 'undefined') this.modulator.frequency = Number(message.frequency2);
if (typeof message.modDepth !== 'undefined') this.modDepth = Number(message.modDepth);
if (typeof message.shape2 !== 'undefined') this.modulator.shape = this.Superpowered.Generator[message.shape2];
}
processAudio(inputBuffer, outputBuffer, buffersize, parameters) {
this.modulator.generate(
this.modulatorBuffer.pointer,
buffersize
);
this.carrier.generateFM(
this.carrierBuffer.pointer,
buffersize,
this.modulatorBuffer.pointer,
this.modDepth
);
this.Superpowered.Interleave(
this.carrierBuffer.pointer,
this.carrierBuffer.pointer,
outputBuffer.pointer,
buffersize
);
}
}
#include "SuperpoweredGenerator.h"
void initGeneratorSources() {
carrier = new Superpowered::Generator(48000. Superpowered::Generator::Sine);
modulator = new Superpowered::Generator(48000. Superpowered::Generator::Sine);
carrier->frequency = 440;
modulator->frequency = 880;
modDeph = 250;
}
void processAudio(float *interleavedInput, float *interleavedOutput, unsigned int numberOfFrames, unsigned int samplerate) {
float carrierBuffer[numberOfFrames];
float modulatorBuffer[numberOfFrames];
carrier->samplerate = samplerate;
modulator->samplerate = samplerate
modulator->generate(modulatorBuffer, numberOfFrames);
carrier->generateFM(carrierBuffer, numberOfFrames, modulatorBuffer, modDepth);
Superpowered->Interleave(
carrierBuffer,
carrierBuffer,
interleavedOutput,
numberOfFrames
);
}
Here's how FM synthesis sounds:
FM Synthesis Generators demo
Superpowered WebAssembly Audio SDK v2.6.1
Shapes
Superpowered.Generator.GeneratorShape
CONSTANTSSuperpowered generator shapes.
Value | Description |
---|
Sine | sine wave |
Triangle | triangle wave |
Sawtooth | sawtooth wave |
PWM | pulse wave with adjustable width |
PinkNoise | pink noise |
WhiteNoise | white noise |
SyncMaster | generates no sound, but sync data to use with generateSyncMaster |
Superpowered::Generator::GeneratorShape
CONSTANTSSuperpowered generator shapes.
Value | Description |
---|
Sine | sine wave |
Triangle | triangle wave |
Sawtooth | sawtooth wave |
PWM | pulse wave with adjustable width |
PinkNoise | pink noise |
WhiteNoise | white noise |
SyncMaster | generates no sound, but sync data to use with generateSyncMaster |
Properties
frequency
PROPERTYFrequency of generator output in Hz.
Type | Min | Max | Default |
---|
Number | 0.0001 | sample rate / 2 | 440 |
frequency
PROPERTYFrequency of generator output in Hz.
Type | Min | Max | Default |
---|
float | 0.0001 | sample rate / 2 | 440 |
pulsewidth
PROPERTYPulse Width for PWM shape. 0.5 results in a square wave.
Type | Min | Max | Default |
---|
Number | 0.0001 | 0.9999 | 0.5 |
pulsewidth
PROPERTYPulse Width for PWM shape. 0.5 results in a square wave.
Type | Min | Max | Default |
---|
float | 0.0001 | 0.9999 | 0.5 |
samplerate
PROPERTYThe sample rate of the audio context inHz, you must update if it changes.
samplerate
PROPERTYThe sample rate of the audio context inHz, you must update if it changes.
Type | Min | Max | Default |
---|
unsigned int | | | |
shape
PROPERTYWaveform shape of generator.
shape
PROPERTYWaveform shape of generator.
Type | Min | Max | Default |
---|
Superpowered::Generator::shape | | | |
Methods
constructor
METHODCreates a Generator instance.
ParametersName | Type | Description |
---|
samplerate | Number | The initial sample rate in Hz. |
GeneratorShape | Generator.GeneratorShape | The initial shape. |
ReturnsType | Description |
---|
Superpowered.Superpowered::Generator | The constructed instance. |
constructor
METHODCreates a Generator instance.
ParametersName | Type | Default | Description |
---|
samplerate | unsigned int | | The initial sample rate in Hz. |
GeneratorShape | Generator::GeneratorShape | | The initial shape. |
ReturnsType | Description |
---|
Superpowered::Superpowered::Generator | The constructed instance. |
generate
METHODGenerates (outputs) audio. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Description |
---|
output | Number (pointer in Linear Memory) | 32-bit MONO output |
numberOfSamples | Number | Number of samples to produce. |
ReturnsNone
generate
METHODGenerates (outputs) audio. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Default | Description |
---|
output | float * | | 32-bit MONO output |
numberOfSamples | unsigned int | | Number of samples to produce. |
ReturnsNone
generateAndCreateSync
METHODGenerates audio for an oscillator that also serves as synchronization source for another oscillator. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Description |
---|
output | Number (pointer in Linear Memory) | 32-bit MONO output |
syncdata | Number (pointer in Linear Memory) | A buffer to receive hard sync information for syncing oscillators. Should be numberOfSamples + 1 floats big minimum. |
numberOfSamples | Number | Number of samples to produce. |
ReturnsNone
generateAndCreateSync
METHODGenerates audio for an oscillator that also serves as synchronization source for another oscillator. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Default | Description |
---|
output | float * | | 32-bit MONO output |
syncdata | float * | | A buffer to receive hard sync information for syncing oscillators. Should be numberOfSamples + 1 floats big minimum. |
numberOfSamples | unsigned int | | Number of samples to produce. |
ReturnsNone
generateFM
METHODGenerates (outputs) audio for a frequency modulated (FM) oscillator. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Description |
---|
output | Number (pointer in Linear Memory) | 32-bit MONO output |
numberOfSamples | Number | Number of samples to produce. |
fmsource | Number (pointer in Linear Memory) | Source for FM modulation containing numberOfSamples samples, usually from a previous call to generate(). |
fmdepth | Number | Frequency modulation depth. 0 means no modulation, 1000 is a reasonable upper limit. |
ReturnsNone
generateFM
METHODGenerates (outputs) audio for a frequency modulated (FM) oscillator. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Default | Description |
---|
output | float * | | 32-bit MONO output |
numberOfSamples | unsigned int | | Number of samples to produce. |
fmsource | float * | | Source for FM modulation containing numberOfSamples samples, usually from a previous call to generate(). |
fmdepth | float | | Frequency modulation depth. 0 means no modulation, 1000 is a reasonable upper limit. |
ReturnsNone
generateSynchronized
METHODGenerates audio for an oscillator that is hard-synced to another oscillator. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Description |
---|
output | Number (pointer in Linear Memory) | 32-bit MONO output |
syncdata | Number (pointer in Linear Memory) | A buffer to receive hard sync information for syncing oscillators. Should be numberOfSamples + 1 floats big minimum. |
numberOfSamples | Number | Number of samples to produce. |
ReturnsNone
generateSynchronized
METHODGenerates audio for an oscillator that is hard-synced to another oscillator. It's never blocking for real-time usage. You can change all properties on any thread, concurrently with this function.
ParametersName | Type | Default | Description |
---|
output | float * | | 32-bit MONO output |
syncdata | float * | | A buffer to receive hard sync information for syncing oscillators. Should be numberOfSamples + 1 floats big minimum. |
numberOfSamples | unsigned int | | Number of samples to produce. |
ReturnsNone
reset
METHODStart oscillator with given phase angle. In a synthesizer, this should be called whenever a voice starts.
ParametersName | Type | Description |
---|
phase | Number | Start phase of the oscillator between 0.0 (0 degree) and 1.0 (180 degrees). |
ReturnsNone
reset
METHODStart oscillator with given phase angle. In a synthesizer, this should be called whenever a voice starts.
ParametersName | Type | Default | Description |
---|
phase | float | | Start phase of the oscillator between 0.0 (0 degree) and 1.0 (180 degrees). |
ReturnsNone