Flexible audio player for browser
var ac = new AudioContext()
var player = require('audio-player')
var kick = player(ac, <AudioBuffer>)
kick.play()
## Features
var drums = player(ac, {
kick: <AudioBuffer>,
snare: <AudioBuffer>,
hihat: <AudioBuffer>
})
drums.play('kick')
drums.play('snare')
var samples = { 'C2': <AudioBuffer>, 'Db2': <AudioBuffer>, ... }
var piano = player(ac, samples, { midi: true })
piano.play(69) // => Plays 'A4'
piano.play('C#2') // => Plays 'Db2'
Unlike AudioBufferSourceNode, you can call start
several times. Also, the stop
function stops all the sounds.
var longSound = player(ac, <AudioBuffer>)
longSound.start() // one sample playing
longSound.start() // other sample playing (at same time)
longSound.stop() // stop both sounds
var drums = player(ac, { kick: ..., snare: ..., hihat ... })
drums.onstarted = function (e) {
console.log('start', e.name)
}
drums.onended = function (e) {
console.log('ended', e.name)
}
drums.start('kick') // console logs 'started kick'
// console.logs 'ended kick' when sound stops
## Install
Via npm: npm i --save audio-player
or grab the browser ready file (4kb) which exports loadAudio
as window global.
The API is very simple: player(ac, source, options)
Param | Type | Description |
---|---|---|
ac | AudioContext |
the audio context |
source | Object |
the object to be loaded |
options | Object |
(Optional) the load options for that object |
To run the test, clone this repo and:
npm install
npm test
To run the example:
npm i -g beefy
beefy example/example.js
MIT License