2.1.0 • Published 5 years ago

goertzel-node v2.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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),

-Wikipedia

This package implement a WebAudio node which detects if a specified frequency is present in the audio stream using the goertzel algorithm.

Features

  • 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.

ToDo

  • Move to upcoming AudioWorkletNode when available.

Internals

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.

Usage

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

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

API

Constructor

  • GoertzelNode : Creates a new GoertzelNode. - eg :

    var gn = new GoertzelNode(audioContext);
    • arguments: - audioContext : AudioContext - The AudioContext within which the GoertzelNode is to be created.

Methods

Properties

  • 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.
    	```
    	- This is the result of Goertzel algorithm. It can be used to decide if enough energy is detected at the `targetFrequency`.
  • 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.
    	```
    	- If calculated `power` 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.
    	```

License

MIT

See License file