@splice/shabushabu
v0.1.7
Published
Web Audio engine
Downloads
10
Readme
Shabu Shabu audio engine
Setup
The code for this "audio engine" is written in TypeScript, you therefore need the typescript compiler installed.
Make sure you have yarn installed.
Install dependencies:
$ yarn
Compile / start server
$ yarn demo
This will compile the files, start a local server and monitor file changes and reload whatever page you're on.
The demo/index.html
contains demo code.
Open your browser to:
http://localhost:8080/webpack-dev-server/demo/index.html
The code will auto-transpile and reload.
Goals
The main goal of this library is to focus on audio tasks and not do any UI rendering. Features:
- step sequencer
- audio dynamics
- MIDI support
- synthesizer modules
The idea is that various kind of graphic interfaces can be hooked to the engine to do different things. The first implementation of such a concept would be a drum step sequencer.
API
You need an instance of the engine to start, and from there, you can add a sequencer with a given tempo:
var engine = NewEngine()
var bpm = 120
var seq = new Sequencer(engine, bpm);
The engine can load remote sounds and cache them so they can be used by the sequencer:
engine.loadSample("kick", "sounds/kick.wav", function() {
seq.addSampleTrack("kick", engine.samples["kick"], [1,0,0,0, 0,0,0,0, 1,0,0.5,0, 0,0,0,0])
})
A loaded sample becomes available via its key (first argument of the loadSample
function)
The second argument is the URL of the sound source and finally the
third one is a closure that gets called once the remote sound was processed
and is available.
Cached samples are available in engine.samples[keyName]
.
The sequencer's addSampleTrack
method is a convenient way to create a stepper sequencer track
using a given audio sample. it takes 3 arguments, the track name, the audio buffer to use
and the step sequence.
Note that the step sequence can contain values between 0 and 1 where 0 is a muted sound (noop) and 1 plays the sound at full volume.
A sequencer can be started using the start()
and stop()
methods and patterns can be changed live.
You can access the pattern tracks (to modify them for instance) via the patternTracks
attribute on the sequence.
This would return an array of tracks and each track has:
- a name
- steps
- an optional sample (audio buffer)
If a sample isn't attached, the default synth gets triggered on each step.
Modules
The audio engine provides multiple modules to do different things.
Sequencer
The sequencer role is to trigger connections in a scheduled manner. A sequencer instance doesn't generate any sound instead it sends triggers coming from its pattern tracks.
The sequencer has a tempo/bpm and multiple pattern tracks. Each pattern track contains the pattern (steps to play and when to play them).
The connect
method allows a sequencer to drive a receiver (like the pad sampler).
Controls are basic: start()
and stop()
.
The sequencer has a convenient addSampleTrack
helper method allowing the quick addition of a pattern track.
Like other module, this module has a debugger that can be turned on. Pattern tracks and BPM can be changed while playing the sequencer is supposed to handle such changes without any issues.
PatternTrack
A pattern track represents a track within the sequence. It has a reference to the audio sample being triggered, an overall volume, a name and a list of steps.
PadSampler
The Pad sampler is a sampler with basic playback features. Each pad contains an audio buffer and attributes to play it.
A pad can be triggered by user inputs via keyboard keys, notes or programmatically.
A pad can be triggered multiple times in a row, if the cutSelf
attribute is on, new triggers will stop currently playing sounds, otherwise a new
instance will be started in conjunction with the existing one.