1.0.1 • Published 6 years ago

spectro v1.0.1

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

A clustered nods.js module to create spectrograms from pcm audio data

Install

Spectro can be installed via npm:

npm install spectro

Usage

By using a cluster of workers to process the computational expensive stuff the execution time can be decreased dramatically. The spectrogram class is a writable stream where the data can be piped into. So the easiest way to use it is to pipe a wav file into it:

const Spectro = require('spectro')
const fs = require('fs')

var sp = new Spectro()
var audioFile = fs.createReadStream('file.wav', {start: 44}) // Note: The first 44 bytes are the wav-header

// The file stream can simply be piped into the Spectro instance
audioFile.pipe(sp)

// Check when the file stream completed
var fileRead = false
audioFile.on('end', () => fileRead = true)

// The data event can be used to work with recently processed data from the workers
spectro.on('data', (err, frame) => {
    if (err) console.error('Spectro data event has an error', err)
    // frame contains an index of the processed frame and a data section with the processed data
})

spectro.on('end', (err, data) => {
    if (err) return console.error('Spectro ended with an error', err)
    // The 'end' event always fires when spectro has reached the end of the currently processable data
    // Therefore we should check if the file was read completely before using the data
    if (fileRead !== true) return

    // Stop spectro from waiting for data and stop all of it's workers
    spectro.stop()

	// The spectrogram can e.g. be drawn with third party modules such as pngjs
	// Examples therefor can be found in examples/...
})

Methods

Constructor

The constructor can be called with an options object with these options:

OptionDefaultDescription
bps16Bits per second of the audio
channels1The channels count of the audio (allowed: 1)
wSize1024The window size being used for the dct
wFunc'Hamming'The window function to use
overlap0The overlap size: 0 <= overlap < 1
workers\<CPU cores>How many workers should be created to process the data

Instance methods

  • stop() - Stops all workers from further processing of incoming data
  • start() - Recreates the workers that compute the spectrogram on incoming data
    Note: start() will automatically be called with the constructor
  • clear() - Triggers stop() and resets all data and the time measuring
  • executionTime(): number - Returns the execution time in ms

Static methods

  • colorize(colorMap): Function - Returns colorization function for the amplitudes.
  • maxApplitude(): number - Returns the maximum applitude of a spectrogram
  • minApplitude(): number - Returns the minimum applitude of a spectrogram

Events

EventParametersDescription
dataerr, frameWhen a new frame was processed. frame has an index and a data array
enderr, dataWhen all possible windows where processed. This event can be called several times before the complete audio has been piped into the Spectro instance. data is a two-dimensional array with the amplitudes

Window functions

The following window functions are implemented:

  • Square
  • Hamming
  • VonHann (Hanning)
  • Blackman (default)
  • BlackmanHarris
  • BlackmanNuttall
  • Bartlett

Limitations

Only mono is supported at the moment.

1.0.1

6 years ago

1.0.0

8 years ago