Very simple and performant web audio library designed for games. Better than howler!
npm install not-shit-web-audiojavascript
const PLAY_NOT_SEEK = true;
// you can set the global volume of all audio by accessing the library methods
NSWA.setVolume(0.5);
// construct a source to automatically load from a file
const source = new NSWA.Source('/test/cinematic-music.mp3');
// by default an instance is the most optimal simple web audio node, with no unnecessary connections
const instance = source.create();
// by setting the volume, even to 1, we initialize the creation of a gain (volume) node
instance.setVolume(1);
// by setting the panner position, or any panner property, we initialize the creation of a panner (3d audio) node
// it's fine to do this rapidly, probably even every frame
instance.setPannerPosition(1, 0, 0);
// if you have 3d audio, you will also need to update the global position and orientation of the listener
// it's also fine to do this rapidly, probably even every frame
NSWA.setListenerOrientation(1, 0, 0, 0, 1, 0);
NSWA.setListenerPosition(0, 0, 0);
if (PLAY_NOT_SEEK) {
// if you want to play, calling play() will start the audio instance from 0
instance.play();
} else {
// if you want to seek to a specific point, calling seek(1.5) will skip to 1.5 seconds into the audio clip
// as a performance note, seek will first stop any existing audio, then reconstruct the
// node (which is relatively efficient) starting at the specified time
// as such, calling play then seek is redundant and unnecessary
instance.seek(1.5);
}
// this library also handles rate updating nicely. if you call setRate(0.5) the audio will play at 50% speed
instance.setRate(0.5);
// and a corresponding call to getCurrentTime() will return the current seconds into the current audio clip, while
// respecting every rate update call you've done up to this point
const time = instance.getCurrentTime();
// calling instance.stop() will deconstruct the node so you can release your reference
instance.stop();
if (PLAY_NOT_SEEK) {
// it's also perfectly fine to call play again after deconstructing the node
instance.play();
} else {
// or seek
instance.seek(time);
}
``