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

@ircam/sc-loader

v1.0.0

Published

Unified assets loaders between node and browser

Downloads

8

Readme

sc-loader

Load AudioBuffer in Node.js (cf. node-web-audio-api) and the Browser with a unified interface

Install

npm install --save @ircam/sc-loader

Usage

import { AudioBufferLoader } from '@ircam/sc-loader';

const audioContext = new AudioContext();
const loader = new AudioBufferLoader(audioContext);
// load one file
const buffer = await loader.load('path/to/file.wav');
const src = audioContext.createBufferSource();
src.buffer = buffer;
src.connect(audioContext.destination);
src.start();

API

Table of Contents

AudioBufferLoader

The AudioBufferLoader interface allows to load AudioBuffer in Node.js and the Browser with a unified interface.

Parameters

  • sampleRateOrAudioContext (AudioContext | number) An exisiting AudioContext instance or a valid sample rate. Loaded audio buffers will be resampled to the given sample rate or audio context sample rate
  • serverAddress string Optional server address URL to use for loading the audio files. (optional, default null)

Examples

import { AudioBufferLoader } from '@ircam/sc-loader';
// import { AudioContext } from 'node-web-audio-api';

const audioContext = new AudioContext()
const loader = new AudioBufferLoader(audioContext);
// load one file
const buffer = await loader.load('path/to/file.wav');

sampleRate

Sample rate of loader and decoded audio buffers

serverAddress

Optional server address URL to use for loading the audio files.

load

Load audio buffers.

In the browser, the given path to the audio files are resolved as following:

  • if serverAddress is null, rely on default fetch behavior
  • if serverAddress is not null, try to create an URL from pathname and serverAddress

In Node.js,

  • load from filesystem relative to process.cwd()
  • load from filesystem relative to caller site
  • load from network if an absolute URL is given
  • if serverAddress is given and all other strategies failed, try to build a valid URL from pathname and serverAddress, and try to load from network

Calling this function will erase the cache from previous load call.

Returns null if aborted

Parameters

  • requestInfos (string | array<string> | object<string, string>) List of sound file to load, the returned value structure will match the strcuture of the argument. If the sound file could not be load (e.g. file not found or decoding error) the slot will be set to null.

Examples

// load single file
const buffer = await loader.load('path/to/file.wav');
// load array
const buffers = await loader.load(['file1.wav', 'file2.mp3', 'ile3.wav']);
// load object
const buffers = await loader.load({ file1: 'file1.wav' });

Returns (AudioBuffer | array<AudioBuffer> | object<string, AudioBuffer>)

get

Get an AudioBuffer from cache according the its key or index.

If the cache is a single AudioBuffer, it will be returned disregarding the given key.

Parameters

Examples

// load and get single file
await loader.load('path/to/file.wav');
const buffer = loader.get();
// load array and get file at index
await loader.load(['file1.wav', 'file2.mp3', 'ile3.wav']);
const buffer = loader.get(0);
// load object and get file by key
await loader.load({ file1: 'file1.wav' });
const buffer = loader.get('file1');

Returns AudioBuffer

getValues

Get full cache of the loader.

Examples

const buffers = await loader.load(['file1.wav', 'file2.mp3', 'ile3.wav']);
const cache = loader.getValues(0);
console.log(buffers === cache);

Returns (AudioBuffer | array<AudioBuffer> | object<string, AudioBuffer>)

abort

Abort an ongoing load call.

The cache from previous load call will be preserved.

Examples

const promise = loader.load(['file1.wav', 'file2.mp3', 'ile3.wav']);
await true;
loader.abort();
const result = await promise;
console.log(result === null);

Returns (AudioBuffer | array<AudioBuffer> | object<string, AudioBuffer>)

clear

Clear the cache.

Examples

const buffers = await loader.load(['file1.wav', 'file2.mp3', 'ile3.wav']);
loader.clear();
const cache = loader.getValues();
console.log(cache === null);

Development notes

Running the tests

npm run test

TODOS

Provides loaders for other type of files?

  • [ ] AudioBuffers
  • [ ] JSON
  • [ ] raw text
  • [ ] binary format (i.e. midi files)

License

BSD-3-Clause