audio-loader
An simple and flexible audio buffer loader for browser and node:
var load = // load one file // load a collection of files
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:
Usage
Load audio files
You can load individual or collection of files:
// apply a prefix using options.from // the options.from can be a function { return 'http://server.com/samples' + name + '?key=secret' }
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: 02 audio: 'samples/piano.mp3'
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:
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 installnpm test
To run the browser example:
npm i -g budonpm run example-browser
To run the node (audiojs) example:
node example/node.js
License
MIT License