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

tiny-audio

v0.0.4

Published

A simple, performance-focused audio library with global audio handling and individual sound controls.

Downloads

9

Readme

Tiny Audio

## Overview

Tiny Audio is a simple, ultra-optimized library for handling sound on the web. It is designed with a focus on memory efficiency and performance. The library consists of a Master class, Audio class, Spatial class, and shared utility functions.

## Initialization

To use Tiny Audio, import the required classes from their respective files, and create instances of the classes as needed. The Master class will automatically attach itself to the window object and wait for the audio context to be allowed to start.

Master Class

The Master class holds the audio context and can be created only once. It attaches itself to the window with the window.audioMaster name. It provides global controls for all the initialized Audio instances, such as controlling global volume, playing, and pausing all sounds.

Audio Class

The Audio class represents a single Audio item, which can have a single src or an array of different audio formats. An instance of the Audio class can play, pause, and set the volume of the sound, as well as fade the volume over time.

Every Audio instance attaches itself automatically to the Master class and creates it if it doesn't exist.

| Function | Description | | ----------------------------------------- | -------------------------------------------------------------------------------------------------------------- | | constructor(src, name, options, master) | Instantiates the Audio class with the specified src, name, options, and master reference. | | loadAudio(url, callback) | Loads the audio file from the specified url and calls the callback function with the decoded audio buffer. | | play() | Plays the audio instance. | | pause() | Pauses the audio instance. | | setVolume(volume) | Sets the volume for the audio instance. | | fadeVolume(targetVolume, duration) | Fades the volume of the audio instance to the target volume over the specified duration. |

Spatial Class

The Spatial class extends the Audio class, adding the ability to spatialize the audio with controls for setting the position. You can set the spatial position from a given point in space or use the default position (0, 0, 0).

| Function | Description | | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | | constructor(src, name, options, master) | Instantiates the Spatial class with the specified src, name, options, and master reference, extending the Audio class. | | setPosition(x, y, z) | Sets the position of the Spatial audio instance with the specified x, y, and z coordinates. | | setPositionFromPoint(point) | Sets the position of the Spatial audio instance using the provided point object with x, y, and z properties. |

Examples

import { Master, Audio, Spatial } from "./audio.js";

Single Audio Instance

import { Audio } from "./audio.js";

const mySound = new Audio("path/to/your/audio/file.mp3", "mySound");

// Play the sound
mySound.play();

// Pause the sound
mySound.pause();

// Set the volume to 50%
mySound.setVolume(0.5);

// Fade the volume to 20% over 2 seconds
mySound.fadeVolume(0.2, 2);

Multiple Audio Instance

import { Master } from "./master.js";

const audioMaster = new Master();

const sound1 = audioMaster.initAudios({
  src: "path/to/sound1.mp3",
  name: "sound1",
});
const sound2 = audioMaster.initAudios({
  src: "path/to/sound2.mp3",
  name: "sound2",
});

// Play sound1
audioMaster.play("sound1");

// Pause sound2
audioMaster.pause("sound2");

// Set the volume of sound1 to 30%
audioMaster.setVolume("sound1", 0.3);

Initializing multiple Audio instances from an array

import { Master } from "./master.js";

const audioMaster = new Master();

const sounds = [
  { src: "path/to/sound1.mp3", name: "sound1" },
  { src: "path/to/sound2.mp3", name: "sound2" },
];

audioMaster.initAudios(sounds);

// Play sound1
audioMaster.play("sound1");

// Pause sound2
audioMaster.pause("sound2");

Initializing and controlling Spatial Audio instances

import { Spatial } from "./spatial.js";

const mySpatialSound = new Spatial(
  "path/to/your/spatial/audio/file.mp3",
  "mySpatialSound"
);

// Play the spatial sound
mySpatialSound.play();

// Set the spatial sound position to (x: 10, y: 5, z: -3)
mySpatialSound.setPosition(10, 5, -3);

// Set the spatial sound position using a point object
const point = { x: 2, y: 0, z: 5 };
mySpatialSound.setPositionFromPoint(point);

Initializing multiple Spatial Audio instances using the Master class

import { Master } from "./master.js";

const audioMaster = new Master();

const spatialSound1 = audioMaster.initSpatialAudios({
  src: "path/to/spatial1.mp3",
  name: "spatial1",
});
const spatialSound2 = audioMaster.initSpatialAudios({
  src: "path/to/spatial2.mp3",
  name: "spatial2",
});

// Set the position of spatialSound1
audioMaster.setSpatialPosition("spatial1", 5, 10, -2);

// Update the position of all active spatial sounds with a reference position
const referencePosition = { x: 0, y: 0, z: 0 };
audioMaster.updateAllSpatialPositions(referencePosition);