npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webaudio

v1.0.1

Published

write audio streams with javascript functions in the browser

Downloads

141

Readme

webaudio

Generate audio streams (DSP) with javascript functions in the browser. Compatible with baudio functions. Works on webkits only, including iOS 6 mobile safari (see note below).

var  A0 = 440
,    tau = Math.PI * 2

function sineWave(t, i){
	return Math.sin( t * tau * A0 )
}
...

Your function will be called with the following arguments:

  • Time (float)
  • Sample index (integer)
  • Input sample (float), this will be zero if there is no input. See "advanced" use below.
  • // Midi commands: this is not implemented.
  • // Fundamental Frequency: this is not implemented either, just an idea for cases such as effects node chains
  • // Data Object : also not implemented.

As a helper, this module will write _SAMPLERATE on the window. You cannot change sampleRates in the web audio api as of yet. I tried writing a mock downsampler into webaudio, but it did not sound good, nor bad, enough.

You function should return a float between [-1, 1]. See examples below.

To play around with it, do

npm install -g browserify opa
git clone https://github.com/NHQ/webaudio
cd webaudio
opa

Open your browser to http://localhost:11001 Edit entry.js and refresh

see also opa

usage

npm install webaudio

Simple use:

var webaudio = require('webaudio')
  , tau = Math.PI * 2
  , frequency = 555
;

function sine(time, i){
  return Math.sin(time * tau * frequency)
}

var channel = webaudio(sine);

channel.play()
// later, channel.stop()

For more advanced use, pass an audioContext as the first argument. You then use the same audio context to connect and use other webaudio functions, or other HTML5 Web Audio API nodes. This module (webaudio) returns a finished ScriptProcessorNode, which you can connect to other nodes, or, to the final audioContext.destination:

var webaudio = require('webaudio')
  , tau = Math.PI * 2
  , frequency = 555
  , context = new webkitAudioContext();

function sine(time, i){
  return Math.sin(time * tau * frequency)
}

function gain(time, i, inputSample){
  return inputSample * 1 / 4 
}

var signal = webaudio(context, sine)
var gain = webaudio(context, gain)

signal.connect(gain);

path.connect(context.destination)

/*
setTimeout(function(){
  path.disconnect(); 	// path.stop()
  path.connect(recorder);
  recorder.connect(repeater);
  repeater.connect(audio.destination)
}, 1000)
*/

Mobile Safari

On mobile safari webkit (iOS), you can only initiate web audio API sounds from within a user event context (at least the fist time...?). Ergo, channel.play(), or channel.connect(context.destination) must be called from inside an event callback.

iOS 5.x does not support the webkit that supports the web audio api.

#Methods

channel.play()

Start playing the sound. This connects the node to the master contexts destination for you.

channel.stop()

Disconnects the node from the master

channel.createSample(duration)

This helper method will run your function for as long as duration, in samples.

ie, 48000 =~ one second, depending on your sound card

This returns an audioBuffer node, which are what you use for precision timed samples and loops. So you could for instance write a script that creates 1 second samples for every note value, through your synth function.