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 frequency
and amplitude
properties.
Code Example
{
"nodes": {
{
"id": "sineGeneratorNode",
"type": "Switchboard.SineGenerator",
"config": {
"frequency": 440,
"amplitude": 0.5
}
}
},
"connections": {
{ "sourceNode": "sineGeneratorNode", "destinationNode": "outputNode" }
},
}
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
)