Audio Signal Generation
About Audio Signal Generation
Audio signal generation refers to the process of creating sound waves that can be heard by the human ear. This can be achieved through various methods such as using electronic devices, software programs, or even physical instruments. The generated audio signal can be used for a variety of purposes such as music production, sound effects for movies or video games, or even for scientific research.
Sine Wave Generator
Switchboard Editor example
This example generates a sine wave to the audio output using the SineGeneratorNode
. Frequency and amplitude can be adjusted by the setFrequency
and setAmplitude
methods.
Code Example
- JSON
- Swift
- Kotlin
- C++
- JavaScript
{
"nodes": {
{ "id": "sineGeneratorNode", "type": "Switchboard.SineGenerator" }
},
"connections": {
{ "sourceNode": "sineGeneratorNode", "destinationNode": "outputNode" }
},
}
import SwitchboardSDK
class SineWaveGeneratorExample {
let audioEngine = SBAudioEngine()
let audioGraph = SBAudioGraph()
let sineGeneratorNode = SBSineGeneratorNode()
init() {
audioGraph.addNode(sineGeneratorNode)
audioGraph.connect(sineGeneratorNode, to: audioGraph.outputNode)
audioEngine.start(audioGraph)
}
func setFrequency(_ newValue: Float) {
sineGeneratorNode.frequency = newValue
}
func setAmplitude(_ newValue: Float) {
sineGeneratorNode.amplitude = newValue
}
}
import com.synervoz.switchboard.sdk.AudioEngine
import com.synervoz.switchboard.sdk.audiograph.AudioGraph
import com.synervoz.switchboard.sdk.audiographnodes.SineGeneratorNode
class SineWaveGeneratorExample(context: Context) {
val audioEngine = AudioEngine(context)
val audioGraph = AudioGraph()
val sineGeneratorNode = SineGeneratorNode()
init {
audioGraph.addNode(sineGeneratorNode)
audioGraph.connect(sineGeneratorNode, audioGraph.outputNode)
audioEngine.start(audioGraph)
}
fun setFrequency(frequency: Float) {
sineGeneratorNode.frequency = frequency
}
fun setAmplitude(amplitude: Float) {
sineGeneratorNode.amplitude = amplitude
}
}
#include "AudioGraph.hpp"
#include "SineGeneratorNode.hpp"
using namespace switchboard;
class SineWaveGeneratorExample {
public:
SineWaveGeneratorExample() {
// Adding nodes to audio graph
audioGraph.addNode(sineGeneratorNode);
// Connecting audio nodes
audioGraph.connect(sineGeneratorNode, audioGraph.getOutputNode());
// Starting the graph
audioGraph.start();
}
void setFrequency(float frequency) {
sineGeneratorNode.setFrequency(frequency);
}
void setAmplitude(float amplitude) {
sineGeneratorNode.setAmplitude(amplitude);
}
// Example method called by the audio processing pipeline on each buffer
bool process(float** buffers, const uint numberOfChannels, const uint numberOfFrames, const uint sampleRate) {
AudioBuffer<float> outBuffer = AudioBuffer<float>(numberOfChannels, numberOfFrames, false, sampleRate, buffers);
const bool result = audioGraph->processBuffer(nullptr, &outBuffer);
return result;
}
private:
AudioGraph audioGraph;
SineGeneratorNode sineGeneratorNode;
};
// Change the import to reflect the location of library
// relative to this publically accessible AudioWorklet
import { SwitchboardSDK } from '/libs/switchboard-sdk/index.js'
class SineWaveWorkletProcessor extends AudioWorkletProcessor {
constructor(options) {
super()
this.sampleRate = options.processorOptions.sampleRate
this.port.onmessage = (event) => this.onMessage(event.data)
}
onMessage(message) {
if (message.wasmArrayBuffer) {
const switchboardSdkConfigObject = {
extensions: [],
wasmBytes: message.wasmArrayBuffer,
}
this.configure(switchboardSdkConfigObject)
}
if (typeof message.frequency !== 'undefined')
this.setFrequency(message.frequency)
if (typeof message.amplitude !== 'undefined')
this.setAmplitude(message.amplitude)
}
configure(sdkConfig) {
this.switchboard = new SwitchboardSDK()
this.switchboard.configure(sdkConfig).then((response) => {
this.constructAudioGraph()
})
}
constructAudioGraph() {
const inputChannelLayout = []
const outputChannelLayout = [1]
const maxNumFrames = 128
let sineNode = new this.switchboard.classes.SineGeneratorNode()
sineNode.setFrequency(220.0)
sineNode.setAmplitude(1.0)
let gainNode = new this.switchboard.classes.GainNode()
gainNode.setGain(0.8)
let audioGraph = this.switchboard.createAudioGraph(
inputChannelLayout,
outputChannelLayout,
maxNumFrames,
this.sampleRate
)
let audioGraphOutputNode = audioGraph.getOutputNode()
audioGraph.addNode(sineNode)
audioGraph.addNode(gainNode)
audioGraph.connect(sineNode, gainNode)
audioGraph.connect(gainNode, audioGraphOutputNode)
audioGraph.start()
this.sineNode = sineNode
this.gainNode = gainNode
this.audioGraph = audioGraph
}
destruct() {
this.audioGraph.destruct()
this.gainNode.destruct()
this.sineNode.destruct()
}
setFrequency(newFrequency) {
if (this.sineNode) this.sineNode.setFrequency(newFrequency)
}
setAmplitude(newAmplitude) {
if (this.sineNode) this.sineNode.setAmplitude(newAmplitude)
}
process(inputs, outputs, parameters) {
if (this.audioGraph) return this.audioGraph.processGraph(inputs, outputs)
return true
}
}
registerProcessor('SwitchboardSineGeneratorProcessor', SineWaveWorkletProcessor)
White Noise Generator
Switchboard Editor example
This example generates white noise to the audio output using the WhiteNoiseGeneratorNode
. This class has no properties or methods.
Code Example
- JSON
- Swift
- Kotlin
- C++
- JavaScript
{
"nodes": {
{ "id": "whiteNoiseGeneratorNode", "type": "Switchboard.WhiteNoiseGenerator" }
},
"connections": {
{ "sourceNode": "whiteNoiseGeneratorNode", "destinationNode": "outputNode" }
},
}
import SwitchboardSDK
class WhiteNoiseGeneratorExample {
let audioEngine = SBAudioEngine()
let audioGraph = SBAudioGraph()
let whiteNoiseGeneratorNode = SBWhiteNoiseGeneratorNode()
init() {
audioGraph.addNode(whiteNoiseGeneratorNode)
audioGraph.connect(whiteNoiseGeneratorNode, to: audioGraph.outputNode)
audioEngine.start(audioGraph)
}
}
import com.synervoz.switchboard.sdk.AudioEngine
import com.synervoz.switchboard.sdk.audiograph.AudioGraph
import com.synervoz.switchboard.sdk.audiographnodes.WhiteNoiseGeneratorNode
class WhiteNoiseGeneratorExample(context: Context) {
val audioEngine = AudioEngine(context)
val audioGraph = AudioGraph()
val whiteNoiseGeneratorNode = WhiteNoiseGeneratorNode()
init {
audioGraph.addNode(whiteNoiseGeneratorNode)
audioGraph.connect(whiteNoiseGeneratorNode, audioGraph.outputNode)
audioEngine.start(audioGraph)
}
}
#include "AudioGraph.hpp"
#include "WhiteNoiseGeneratorNode.hpp"
using namespace switchboard;
class WhiteNoiseGeneratorExample {
public:
WhiteNoiseGeneratorExample() {
// Adding nodes to audio graph
audioGraph.addNode(whiteNoiseGeneratorNode);
// Connecting audio nodes
audioGraph.connect(whiteNoiseGeneratorNode, audioGraph.getOutputNode());
// Starting the graph
audioGraph.start();
}
// Example method called by the audio processing pipeline on each buffer
bool process(float** buffers, const uint numberOfChannels, const uint numberOfFrames, const uint sampleRate) {
AudioBuffer<float> outBuffer = AudioBuffer<float>(numberOfChannels, numberOfFrames, false, sampleRate, buffers);
const bool result = audioGraph->processBuffer(nullptr, &outBuffer);
return result;
}
private:
AudioGraph audioGraph;
WhiteNoiseGeneratorNode whiteNoiseGeneratorNode;
};
// Change the import to reflect the location of library
// relative to this publically accessible AudioWorklet
import { SwitchboardSDK } from '/libs/switchboard-sdk/index.js'
class SwitchboardWhiteNoiseGeneratorProcessor extends AudioWorkletProcessor {
constructor(options) {
super()
this.sampleRate = options.processorOptions.sampleRate
this.port.onmessage = (event) => this.onMessage(event.data)
}
onMessage(message) {
if (message.wasmArrayBuffer) {
const switchboardSdkConfigObject = {
extensions: [],
wasmBytes: message.wasmArrayBuffer,
}
this.configure(switchboardSdkConfigObject)
}
}
configure(sdkConfig) {
this.switchboard = new SwitchboardSDK()
this.switchboard.configure(sdkConfig).then((response) => {
this.constructAudioGraph()
})
}
constructAudioGraph() {
const inputChannelLayout = []
const outputChannelLayout = [1]
const maxNumFrames = 128
let noiseNode = new this.switchboard.classes.WhiteNoiseGeneratorNode()
let gainNode = new this.switchboard.classes.GainNode()
gainNode.setGain(1)
let audioGraph = this.switchboard.createAudioGraph(
inputChannelLayout,
outputChannelLayout,
maxNumFrames,
this.sampleRate
)
let audioGraphOutputNode = audioGraph.getOutputNode()
audioGraph.addNode(noiseNode)
audioGraph.addNode(gainNode)
audioGraph.connect(noiseNode, gainNode)
audioGraph.connect(gainNode, audioGraphOutputNode)
audioGraph.start()
this.noiseNode = noiseNode
this.gainNode = gainNode
this.audioGraph = audioGraph
}
destruct() {
this.audioGraph.destruct()
this.gainNode.destruct()
this.noiseNode.destruct()
}
process(inputs, outputs, parameters) {
if (this.audioGraph) return this.audioGraph.processGraph(inputs, outputs)
return true
}
}
registerProcessor(
'SwitchboardWhiteNoiseGeneratorProcessor',
SwitchboardWhiteNoiseGeneratorProcessor
)