Flexible audio sample player for browser:
var player = var ac = var sample = samplestartsamplestart // can start several samples at the same timesample // stop all playing sounds
Features
Create multi-sample player
Pass a map of names to audio buffers to create a multi-sample player:
var player = var ac = var drums = drumsstart'kick'drumsstart'snare' accurrentTime gain: 05
Map note names to midi (and oposite)
If the buffers are mapped to note names, you can pass note names (including enharmonics) or midi numbers:
var samples = 'C2': <AudioBuffer> 'Db2': <AudioBuffer> ... var piano = pianostart69 // => Plays 'A4'pianostart'C#2' // => Plays 'Db2'
Decimal midi note numbers can be used to detune the notes:
pianostart695 // => Plays a note in the middle of 'A4' and 'Bb4'
Events
You can register event handlers with the on
function:
var drums = drumsdrumsdrumsstart'kick'// console logs 'start kick'// console.logs 'ended kick' when sound ends
To add a listener to all events use: player.on(function (eventName, when, obj, opts))
.
Currently it fires: start
, started
, stop
, ended
, scheduled
Amplitude envelope control
You can apply an amplitude envelope control player-wide or shot-wide. You can pass a signle adsr
option with an array of [attack, decay, sustain, release]
or add each parameter to the options object:
// using a single option to set all envelopvar longSound = longSoundstart// override only the attacklongSoundstartaccurrentTime + 10 attack: 3
Listen to midi inputs
Easily attach the player to a Web MIDI API MidiInput
:
var piano = windownavigator
Schedule to play buffers at given times
var buffers = 'C2': <AudioBuffer> 'Db2': <AudioBuffer> ... var marimba = marimba
Install
Via npm: npm i --save sample-player
or grab the browser ready file which exports SamplePlayer
as window global.
Options
The options can be passed to the SamplePlayer
function to apply to all buffers, or to start
function to apply to one shot.
gain
: float between 0 to 1attack
: the attack time of the amplitude envelopedecay
: the decay time of the amplitude envelopesustain
: the sustain gain value of the amplitude enveloperelease
: the release time of the amplitude envelopeadsr
: an array of[attack, decay, sustain, release]
. Overrides other parameters.duration
: set the playing duration in seconds of the buffer(s)loop
: set to true to loop the audio buffer
API
player
SamplePlayer(ac, source, options) ⇒ Create a sample player.
Returns: player
- the player
Param | Type | Description |
---|---|---|
ac | AudioContext |
the audio context |
source | ArrayBuffer | Object.<String, ArrayBuffer> |
|
options | Onject |
(Optional) an options object |
Example
var SamplePlayer = var ac = var snare = snarestart
- SamplePlayer(ac, source, options) ⇒
player
player.play
An alias for player.start
See: player.start
AudioNode
player.start(name, when, options) ⇒ Start a sample buffer. The returned object has a function stop(when)
to stop the sound.
Returns: AudioNode
- an audio node with a stop
function
Param | Type | Description |
---|---|---|
name | String |
the name of the buffer. If the source of the SamplePlayer is one sample buffer, this parameter is not required |
when | Float |
(Optional) when to start (current time if by default) |
options | Object |
additional sample playing options |
Example
// A single sample playervar sample = var first = samplestartaccurrentTime loop: true var second = samplestartaccurrentTime + 05 loop: true gain: 07 // name not required since is only one AudioBufferfirst // only stops first soundsample // stop all sounds
Example
// A multi-sample playervar drums = drumsstart'snare'drumsstart'snare' 0 gain: 03
Array
player.stop(when, nodes) ⇒ Stop some or all samples
Returns: Array
- an array of ids of the stoped samples
Param | Type | Description |
---|---|---|
when | Float |
(Optional) an absolute time in seconds (or currentTime if not specified) |
nodes | Array |
(Optional) an array of nodes or nodes ids to stop |
Example
var longSound = longSoundstartaccurrentTimelongSoundstartaccurrentTime + 1longSoundstartaccurrentTime + 2longSound // stop the three sounds
AudioPlayer
player.connect(destination) ⇒ Connect the player to a destination node
Chainable
Returns: AudioPlayer
- the player
Param | Type | Description |
---|---|---|
destination | AudioNode |
the destination node |
Example
var sample =
SamplePlayer
player.on(event, callback) ⇒ Adds a listener of an event
Chainable
Returns: SamplePlayer
- the player
Param | Type | Description |
---|---|---|
event | String |
the event name |
callback | function |
the event handler |
Example
player
Array
player.schedule(when, events) ⇒ Schedule a list of events to be played at specific time.
It supports two formats of events:
- An array with
[time, note]
- An array with objects
{ time: ?, [name|note|midi|key]: ? }
Returns: Array
- an array of ids
Param | Type | Description |
---|---|---|
when | Float |
an absolute time to start (or AudioContext's currentTime if it's less than currentTime) |
source | Array |
the events array |
Example
// Event format: [time, note]var piano = piano
Example
// Event format: object { time: , name: }var drums = drums
SamplePlayer
player.listenToMidi(input, options) ⇒ Connect a player to a midi input
The options accepts:
- channel: the channel to listen to. Listen to all channels by default.
Returns: SamplePlayer
- the player
Param | Type | Description |
---|---|---|
input | MIDIInput |
|
options | Object |
(Optional) |
Example
var piano = windownavigator
Run tests and examples
To run the test, clone this repo and:
npm installnpm test
To run the examples:
npm i -g budobudo examples/microtone.js
(Take a look into examples
folder)
License
MIT License