Skip to content

ircam-ismm/isomorphic-web-audio-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Isomorphic Web Audio API

Provide an isomorphic re-export of the Web Audio API

This package aims at helping library authors to create components that can run seamlessly in Node.js and the browser.

Uses:

Usage

npm install isomorphic-web-audio-api
import { AudioContext, OscillatorNode, GainNode } from 'isomorphic-web-audio-api';
const audioContext = new AudioContext();
// This must be called on a user gesture in browsers
// await audioContext.resume();

setInterval(() => {
  const now = audioContext.currentTime;
  const frequency = 200 + Math.random() * 2800;

  const env = new GainNode(audioContext, { gain: 0 });
  env.connect(audioContext.destination);
  env.gain
    .setValueAtTime(0, now)
    .linearRampToValueAtTime(0.2, now + 0.02)
    .exponentialRampToValueAtTime(0.0001, now + 1);

  const osc = new OscillatorNode(audioContext, { frequency });
  osc.connect(env);
  osc.start(now);
  osc.stop(now + 1);
}, 80);

Caveats

  • AudioRenderCapacityEvent and AudioRenderCapacity are only available on Node.js
  • On node the AudioContext is automatically resumed at construction, make sure to call audioContext.resume() on a user gesture in browsers
  • Some small inconsistencies might still appear between the different implementations

Make sure to test your code everywhere!

License

BSD-3-Clause

Releases

No releases published

Packages

No packages published