0.1.1 • Published 8 years ago

sample-loader v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

sample-loader npm

Samplr

A powerful and easy audio buffer loader for browser:

var ac = new AudioContext()
var load = require('sample-loader')(ac)

// a simple audio buffer player (use `sample-player` instead)
function play(buffer) {
  var source = ac.createBufferSource()
  source.buffer = buffer
  source.connect(ac.destinaton)
  source.start(ac.currentTime)
}

load('@drum-machines/maestro').then(function (buffers) {
  play(buffers['snare'])
})

Features

  • Load individual audio files or collection of them
  • Load base64 encoded audio strings
  • Compatile with midi.js soundfonts
  • Ready to use instruments with no setup

## Install

Via npm: npm i --save sample-loader or grab the browser ready file (4kb) which exports loader as window globals.

User guide

sample-loader is a flexible function to load samples from server. You can create a loader with an AudioContext instance and an (optional) options hash map:

var loader = require('sample-loader')
var ac = new AudioContext()
var load = loader(ac, { /* options (can be null) */ })

The returned load function receives only one parameter: the samples to load and returns always a Promise.

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)
})

load(['samples/snare.mp3', 'samples/kick.mp3']).then(function (buffers) {
  // buffers is an array of AudioBuffers
  play(buffers[0])
})

load({ snare: 'samples/snare.mp3', kick: 'samples/kick.mp3' }).then(function (buffers) {
  // buffers is a hash of names to AudioBuffers
  play(buffers['snare'])
})

Load soundfont files

You can load midi.js soundfont files, and works out of the box with Benjamin Gleitzman's package of pre-rendered sound fonts. No server setup, just prepend @soundfont before the instrument name:

load('@soundfont/acoustic_grand_piano').then(function(buffers) {
  play(buffers['C2'])
})

Other instruments

Can load drum-machines by prepending @drum-machines before the instrument name:

load('@drum-machines/CR-78').then(function (buffers) {
  play(buffers['snare'])
})

Add instrument sources

You can add you own server samples repositories with the options parameter:

var load = loader(ac, { repositories: {
  '@my-repo': 'http://myserver.com/samples'
}})

and then:

load('@my-repo/file.mp3')

License

MIT License