WebAudio Node that implements a goertzel filter
npm install goertzel-node##### A WebAudio Node that implements the goertzel algorithm.
> The Goertzel algorithm is a Digital Signal Processing (DSP) technique that provides a means for __efficient__ evaluation of individual terms of the Discrete Fourier Transform (DFT),
This package implement a WebAudio node which detects if a specified frequency is present in the audio stream using the goertzel algorithm.
- Exposes a (settable) threshold based simple boolean detected.
- Runs in real-time in WebAudio using a ScriptProcessorNode.
- Efficient and performant.
- Uses asm.js if available in the browser.
- Passes through the audio stream for further processing and output.
- Allows specification of which channel is to be analyzed.
- Move to upcoming AudioWorkletNode when available.
GoertzelNode uses the ScriptProcessorNode and performs the Goertzel algorithm on every chunk of data that comes through the ScriptProcessorNode. The calculations are performed on a chunk by chunk bases and the outputs (power, and detected) are updated on every chunk as well.
The package exposes a node.js style API, and is designed to be used tools such as browserify.
A standalone browserified file is also available here, which creates a global named GoertzelNode when included in the html.
npm install goertzel-node
``js
var GoertzelNode = require('goertzel-node'); // only if using browserify.
var audioContext = new AudioContext();
var osc = audioContext.createOscillator();
var gn = new GoertzelNode(audioContext);
gn.targetFrequency = 440; // 440Hz
osc.connect(gn);
gn.connect(audioContext.destination);
osc.start(0);
var result = gn.detected; //boolean true/false
`
- GoertzelNode : Creates a new GoertzelNode.`
- eg :
js`
var gn = new GoertzelNode(audioContext);
- arguments:
- audioContext : __AudioContext__ - The AudioContext within which the GoertzelNode is to be created.
- connect : Connects the GoertzelNode to other WebAudio Nodes. This is exactly the same as the connect method of ScriptProcessorNode.disconnect
- : Disconnects the GoertzelNode from other WebAudio Nodes. This is exactly the same as the disconnect method of ScriptProcessorNode.
- targetFrequency: __Number__ - The value of the frequency (in Hertz) that is to be detected by the GoertzelNode. It defaults to 440.
eg:
``
gn.targetFrequency = 440; // Set the frequency to be detected to be 440.
targetFrequency
- can be set at any time. Once set, the GoertzelNode will start calculating and outputting the values for that frequency at every chunk.
- passthrough: __Boolean__ - Boolean value defining if the Node passes the audio through to the destination or not. Default value is true.
eg:
``
osc.connect(gn);
gn.connect(context.destination);
gn.passthrough = false; // Ensures that audio from the oscillator doesn't get played out.
- channel: __Number__ - The channel of the input audio stream to be used for analysis. The default value is 1.
eg:
``
gn.channel = 1; // Set the channel of the input audio stream to be analyzed.
- Since WebAudio streams can have multiple channels and the Goertzel algorithm run on individual channels, this property allows one to choose which channel to run the Goertzel algorithm on.
- power: __Number__ - Returns the power of the audio of the input audio stream at the targetFrequency. This value is normalised to chunkSize and should be a maximum of 0.25 for a perfect match.
eg:
``
var power = gn.power; // Get the power.
targetFrequency
- This is the result of Goertzel algorithm. It can be used to decide if enough energy is detected at the .
- threshold: __Number__ - Sets a threshold of power used to decide if the targetFrequency was detected.
eg:
``
gn.threshold = 0.22; // Set the threshold to 0.22.
power
- If calculated power is higher than threshold then detected is set as true.
- detected: __Boolean__ - Returns if the targetFrequency was detected in the input audio stream.
eg:
```
var detected = gn.detected; // If the frequency was detected in the input audio stream.
MIT
See License file