Coder Social home page Coder Social logo

goertzel-node's Introduction

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

goertzel-node's People

Contributors

notthetup avatar dependabot[bot] avatar

Stargazers

SpectraSynq avatar Peter avatar Tobias Heinz avatar ◬ avatar

Watchers

◬ avatar  avatar James Cloos avatar Tobias Heinz avatar  avatar

goertzel-node's Issues

Feature-Request: Processing multiple frequencies.

Firstly, I'm pretty new to open source contribution so please excuse when doing false assumptions.

Many use cases need to apply Goertzel to various frequencies. Due to the fact that a asm.js module is a singleton, multiple instances aren't supported.
I created an approve with an array of frequencies as initialization parameter which result in an array of energies. If you're interested, I'm going to create a pull request with a more optimized code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.