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

audio-loader

v1.0.3

Published

A flexible web audio sample loader for browser and node

Downloads

3,210

Readme

audio-loader

Build Status Code Climate js-standard-style npm Greenkeeper badge license

An simple and flexible audio buffer loader for browser and node:

var load = require('audio-loader')

// load one file
load('http://example.net/audio/file.mp3').then(function (buffer) {
  console.log(buffer) // => <AudioBuffer>
})

// load a collection of files
load({ snare: 'samples/snare.wav', kick: 'samples/kick.wav' },
  { from: 'http://example.net/'} ).then(function (audio) {
  console.log(audio) // => { snare: <AudioBuffer>, kick: <AudioBuffer> }
})

Features

  • Load single audio files or collection of them (either using arrays or data objects)
  • Load base64 encoded audio strings
  • Compatible with midi.js pre-rendered soundfonts packages like midi-js-soundfonts
  • Compatible with json encoded audio like sampled

Install

Npm

Via npm: npm i --save audio-loader

Browser

Download the minified distribution (4kb) which exports loadAudio as window global:

<script src="audio-loader.min.js"></script>
<script>
  loadAudio('file.wav').then(..)
</script>

Usage

Load audio files

You can load individual or collection of files:

load('http://path/to/file.mp3').then(function (buffer) {
  // buffer is an AudioBuffer
  play(buffer)
})

// apply a prefix using options.from
load(['snare.mp3', 'kick.mp3'], { from: 'http://server.com/audio/' }).then(function (buffers) {
  // buffers is an array of AudioBuffers
  play(buffers[0])
})

// the options.from can be a function
function toUrl (name) { return 'http://server.com/samples' + name + '?key=secret' }
load({ snare: 'snare.mp3', kick: 'kick.mp3' }, { from: toUrl }).then(function (buffers) {
  // buffers is a hash of names to AudioBuffers
  play(buffers['snare'])
})

Recursive loading

audio-loader will detect if some of the values of an object is an audio file name and try to fetch it:

var inst = { name: 'piano', gain: 0.2, audio: 'samples/piano.mp3' }
load(inst).then(function (piano) {
  console.log(piano.name) // => 'piano' (it's not an audio file)
  console.log(piano.gain) // => 0.2 (it's not an audio file)
  console.log(piano.audio) // => <AudioBuffer> (it loaded the file)
})

Load soundfont files

If you provide a .js file, audio-loader will interpret it as a midi.js soundfont file and try to load it:

load('acoustic_grand_piano-ogg.js').then(function (buffers) {
  buffers['C2'] // => <AudioBuffer>
})

This is a repository of them: https://github.com/gleitz/midi-js-soundfonts

API

load(source, [options])

| Param | Type | Description | | --- | --- | --- | | source | Object | the object to be loaded: can be an URL string, ArrayBuffer with encoded data or an array/map of sources | | options | Object | (Optional) the load options for that source |

Possible options keys are:

  • from {Function|String}: a function or string to convert from file names to urls. If is a string it will be prefixed to the name: load('snare.mp3', { from: 'http://audio.net/samples/' }) If it's a function it receives the file name and should return the url as string.
  • only {Array} - when loading objects, if provided, only the given keys will be included in the decoded object: load('piano.json', { only: ['C2', 'D2'] })
  • context {AudioContext}: (browser only) The audio context to use. By default uses audio-context
  • decode {Function}: a function to decode audio. It receives a buffer and must return a promise to an audio buffer.
  • fetch {Function}: a function to fetch files. It receives an url and a response type (one of 'arraybuffer' or 'text') and must return a promise to the contents

Run tests and examples

To run the test, clone this repo and:

npm install
npm test

To run the browser example:

npm i -g budo
npm run example-browser

To run the node (audiojs) example:

node example/node.js

License

MIT License