1.1.0 • Published 8 years ago

regl-audio v1.1.0

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

regl-audio

Tools for working with audio in regl. This module has the following components:

  • An analyser
  • A microphone connection

Examples:

Examples

Here is a simple beat detector:

const regl = require('regl')()

const drawBeats = regl({
  vert: `
  precision highp float;

  attribute vec2 position;
  varying vec2 uv;

  void main () {
    uv = position;
    gl_Position = vec4(position, 0, 1);
  }
  `,

  frag: `
  precision highp float;

  varying vec2 uv;

  uniform float beats[16];

  void main () {
    float intensity = 0.0;

    float bin = floor(8.0 * (1.0 + uv.x));

    for (int i = 0; i < 16; ++i) {
      if (abs(float(i) - bin) < 0.25) {
        intensity += step(0.25 * abs(uv.y), beats[i]);
      }
    }
    gl_FragColor = vec4(intensity, 0, 0, 1);
  }
  `,

  attributes: {
    position: [
      -4, 0,
      4, 4,
      4, -4
    ]
  },

  count: 3
})

require('regl-audio/microphone')({
  regl,
  beats: 16,
  name: '',
  done: (microphone) => {
    regl.frame(() => {
      microphone(({beats}) => {
        drawBeats()
      })
    })
  }
})

regl-audio/analyser

This module takes a WebAudio analyser node and returns a scope command giving convenient access to stats from the analyser.

  • PCM time domain data
  • STFT frequency domain data
  • Beats
  • Pitch

API

const audio = require('regl-audio/analyser')(options)

The constructor for the analyser takes the following arguments:

ParameterDescriptionDefault
reglA handle to a regl instanceRequired
analyserA WebAudio analyser nodeRequired
nameA prefix for the analyser output.''
sampleRateThe sample rate of the audio source in Hz44100
beatsThe number of beats to detect grouped by pitch16
beatTimeDuration of moving average for beats in seconds1
beatThresholdCutoff for beat detection (must be between 0.5 and 1)0.8
pitchesNumber of pitches to detect4
maxPitchMaximum detectable pitch in Hz10000
pitchTimeDuration of moving average for pitch in seconds0.25
tempoInitial guess of tempo for song in beats per second100/60
maxTempoNumber of tempo bins to count up tobinSize / 4
tempoBufferSizeNumber of samples to compute tempo from4096

audio(block)

The result is a regl scope command with the following properties:

ContextDescription
sampleCountNumber of samples
freqArray of frequencies
timeArray of PCM time samples
cepstrumThe cepstrum of the signal
timeTextureCurrent time information in texture
freqTextureCurrent frequency information in texture
volumeVolume of the current signal
beatsArray of detected beats sorted from low to high pitch. Each beat is a scalar in [0, 1]
pitchesArray of detected pitches sorted from loudest to softest in Hz
UniformTypeDescription
sampleCountfloatNumber of samples in texture
timesampler2DA sampler storing the PCM time data
freqsampler2DA sampler storing the frequency data
volumefloatVolume of the signal
beatsfloat[NUM_PITCHES]An array of beats
pitchesfloat[NUM_PITCHES]The array of detected pitches
tempofloatTempo of song

Note that these context variables are optionally prefixed depending on the name parameter.

regl-audio/microphone

A short cut which gives you an analyser node connected to the microphone input from the computer. Note that this must be run on a secure domain.

API

const mic = require('regl-audio/microphone')(options)

The options are the same as above, except that it takes a webaudio context via the options.audioContext parameter instead of an analyser node. By default everything is prefixed with mic_ though this can be changed by passing some alternative to name.

License

(c) 2016 Mikola Lysenko. MIT License