Noise Gate
About Noise Gate
A noise gate is a tool used in voice communications applications to reduce unwanted background noise. It works by setting a threshold level below which any sound is muted or reduced in volume. This is particularly useful in situations where there is a lot of ambient noise, such as in a busy office or on a noisy street. By using a noise gate, the user's voice can be isolated and transmitted clearly, without interference from other sounds. This can improve the quality of communication and make it easier for the listener to understand what is being said. Noise gates are commonly used in teleconferencing, podcasting, and other voice recording applications.
Switchboard Editor example
This example plays a voice recording which has a fixed -25dB pink noise generated underneath. The Switchboard NoiseGateNode openThresholdDB property is set just above the generated noise floor, opening only when the speech occurs.
Code Example
- JSON
- Swift
- Kotlin
- C++
- JavaScript
{
"nodes": {
{ "id": "noiseGateNode", "type": "NoiseGateNode" }
},
"connections": {
{ "sourceNode": "inputNode", "destinationNode": "noiseGateNode" },
{ "sourceNode": "noiseGateNode", "destinationNode": "outputNode" }
}
}
import SwitchboardSDK
class NoiseGateExample {
let audioEngine = SBAudioEngine()
let audioGraph = SBAudioGraph()
let noiseGateNode = SBNoiseGateNode()
init() {
audioGraph.addNode(noiseGateNode)
audioGraph.connect(audioGraph.inputNode, to: noiseGateNode)
audioGraph.connect(noiseGateNode, to: audioGraph.outputNode)
audioEngine.start(audioGraph)
}
var openThresholdDb: Float {
get {
return noiseGateNode.openThresholdDB
}
set {
noiseGateNode.openThresholdDB = threshold
}
}
}
import com.synervoz.switchboard.sdk.AudioEngine
import com.synervoz.switchboard.sdk.audiograph.AudioGraph
import com.synervoz.switchboard.sdk.audiographnodes.NoiseGateNode
class NoiseGateExample(context: Context) {
val audioEngine = AudioEngine(context)
val audioGraph = AudioGraph()
val noiseGateNode = NoiseGateNode()
init {
audioGraph.addNode(noiseGateNode)
audioGraph.connect(audioGraph.inputNode, noiseGateNode)
audioGraph.connect(noiseGateNode, audioGraph.outputNode)
audioEngine.start(audioGraph)
}
// example setter
fun setOpenThresholdDb(threshold: Float) {
noiseGateNode.setOpenThresholdDB(threshold)
}
// example getter
fun getOpenThresholdDb() {
return noiseGateNode.getOpenThresholdDB();
}
}
#include "AudioGraph.hpp"
#include "NoiseGateNode.hpp"
using namespace switchboard;
class NoiseGateExample {
public:
NoiseGateExample() {
// Adding nodes to audio graph
audioGraph.addNode(noiseGateNode);
// Connecting audio nodes
audioGraph.connect(audioGraph.getInputNode(), noiseGateNode);
audioGraph.connect(noiseGateNode, 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> inBuffer = AudioBuffer<float>(numberOfChannels, numberOfFrames, false, sampleRate, buffers);
AudioBuffer<float> outBuffer = AudioBuffer<float>(numberOfChannels, numberOfFrames, false, sampleRate, buffers);
const bool result = audioGraph->process(&inBuffer, &outBuffer);
return result;
}
private:
AudioGraph audioGraph;
NoiseGateNode noiseGateNode;
};
// Change the import to reflect the location of library
// relative to this publically accessible AudioWorklet
import SwitchboardSDK from '../../libs/switchboard-sdk/SwitchboardSDK.js'
class NoiseGateProcessor extends AudioWorkletProcessor {
constructor(options) {
super()
this.sampleRate = options.processorOptions.sampleRate
this.port.onmessage = (event) => this.onMessage(event.data)
}
sendMessage(message, transfer = []) {
this.port.postMessage(message, transfer)
}
onMessage(message) {
if (message.command === 'requestUiDefinitions') {
this.sendMessage({ uiDefinitions: uiDefinitions })
} else if (message.wasmArrayBuffer) {
const switchboardSdkConfigObject = {
extensions: [],
wasmBytes: message.wasmArrayBuffer,
}
this.configure(switchboardSdkConfigObject)
} else {
// Must be a param change message
if (this.noiseGateNode) this.applyChangeObject(message)
}
}
configure(sdkConfig) {
this.switchboard = new SwitchboardSDK()
this.switchboard.configure(sdkConfig).then((response) => {
this.constructAudioGraph()
})
}
setupNoiseGate() {
// Apply the default parameters to the noisegate instance
const intitalChangeObject = {}
for (const param of uiDefinitions.parameters) {
intitalChangeObject[param.id] = param.defaultValue
}
this.applyChangeObject(intitalChangeObject)
}
applyChangeObject(changeObject) {
if (typeof changeObject.openThresholdDb !== 'undefined')
this.noiseGateNode.setOpenThresholdDB(changeObject.openThresholdDb)
if (typeof changeObject.closeThresholdDb !== 'undefined')
this.noiseGateNode.setCloseThresholdDB(changeObject.closeThresholdDb)
if (typeof changeObject.attackTimeSeconds !== 'undefined')
this.noiseGateNode.setAttackTimeSeconds(changeObject.attackTimeSeconds)
if (typeof changeObject.holdTimeSeconds !== 'undefined')
this.noiseGateNode.setHoldTimeSeconds(changeObject.holdTimeSeconds)
if (typeof changeObject.releaseTimeSeconds !== 'undefined')
this.noiseGateNode.setReleaseTimeSeconds(changeObject.releaseTimeSeconds)
if (typeof changeObject.enabled !== 'undefined')
this.noiseGateNode.setEnabled(changeObject.enabled)
}
constructAudioGraph() {
const inputChannelLayout = [2]
const outputChannelLayout = [2]
const maxNumFrames = 128
let audioGraph = this.switchboard.createAudioGraph(
inputChannelLayout,
outputChannelLayout,
maxNumFrames,
this.sampleRate
)
let noiseGateNode = new this.switchboard.classes.NoiseGateNode()
let audioGraphInputNode = audioGraph.getInputNode()
let audioGraphOutputNode = audioGraph.getOutputNode()
audioGraph.addNode(noiseGateNode)
audioGraph.connect(audioGraphInputNode, noiseGateNode)
audioGraph.connect(noiseGateNode, audioGraphOutputNode)
audioGraph.start()
this.noiseGateNode = noiseGateNode
this.audioGraph = audioGraph
this.setupNoiseGate()
}
destruct() {
this.audioGraph.destruct()
this.noiseGateNode.destruct()
}
process(inputs, outputs, parameters) {
return this.audioGraph.processGraph(inputs, outputs)
}
}
registerProcessor('NoiseGateProcessor', NoiseGateProcessor)