BB.AudioSampler

A module for creating an audio sampler, an object that can load, sample and play back sound files

Constructor

BB.AudioSampler
(
  • config
  • [callback]
)

Parameters:

  • config Object

    A config object to initialize the Sampler, can contain the following:  {
        context: BB.Audio.context[2], // choose specific context
        connect: fft, // overide default destination
        autoload: false, // don't autoload ( sampler.load() later )
        rate: 2, // double the playback rate
        // then as many additional keys for samples...
        soundA: 'path/to/file.ogg',
        soundB: 'path/to/file.ogg'
     }

  • [callback] Function optional

    A callback, with a buffer Object Array ( see full example below )

Example:

in the example below instantiating the BB.AudioSampler creates a GainNode ( essentially the Sampler's output ) connected to the default BB.Audio.context ( ie. AudioDestination )

everytime an individual sample is played, for example: drum.play('kick'), the corresponding AudioBuffer ( from the URL provided in the config ) is created and connected to the sampler's GainNode ( the image below is an example of the graph when two samples are played )


 BB.Audio.init();

 var drum = new BB.AudioSampler({
    kick: 'audio/808/kick.ogg',
    snare: 'audio/808/snare.ogg',
    hat: 'audio/808/hat.ogg'
 }, function( bufferObj ){
    console.log( "loaded: " + bufferObj )
    run();
 });

 function run(){
     drum.play('kick');
 }

 // a more complex config example...
 // overrides default context ( BB.Audio.context )
 // overrides default connect ( BB.Audio.context.destination )
 BB.Audio.init(3);

 var drum = new BB.AudioSampler({
    context: BB.Audio.context[2],
    connect: ExampleNode,
    autoload: false,
    kick: 'audio/808/kick.ogg',
 });

 drum.load();

view basic BB.AudioSampler example

Properties

buffers Object

collection of sample buffers

ctx AudioContext default:BB.Audio.context

the Audio Context this derived from

detune Number protected default:0

changes the pitch (-1200 to 1200 )

gain GainNode private

the "output" gain node ( use .volume, .setGain() to interface with this )

keys Array

sample names, ex:['kick','snare']

loaded Boolean

whether or not the file(s) have loaded

node AudioBufferSourceNode

source node for last played instance

paths Array

array of paths to sample audio files

rate Number

changes the playback rate ( pitch and speed ), (reference )

volume Number default:1

the master volume (of output gain node)

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

connect
(
  • destination
  • output
  • input
)

connects the Noise to a particular AudioNode or AudioDestinationNode

Parameters:

  • destination AudioNode

    the AudioNode or AudioDestinationNode to connect to

  • output Number

    which output of the the Noise do you want to connect to the destination

  • input Number

    which input of the destination you want to connect the Noise to

Example:


 BB.Audio.init();

 var node = new BB.AudioBase({
    volume: 0.75,
 });

  node.connect( exampleNode );
  // connected to both default BB.Audio.context && exampleNode
  // so if exampleNode is also connected to BB.Audio.context by default,
  // ...then you've got node connected to BB.Audio.context twice

...which looks like this ( where the first Gain is the Noise and the second is the exampleNode )

disconnect
(
  • destination
  • output
  • input
)

diconnects the Noise from the node it's connected to

Parameters:

  • destination AudioNode

    what it's connected to

  • output Number

    the particular output number

  • input Number

    the particular input number
     BB.Audio.init();

     var node = new BB.AudioBase({
        volume: 0.75,
     });

     node.disconnect(); // disconnected from default BB.Audio.context
     node.connect( exampleNode ); // connected to exampleNode only

    ...which looks like this ( where the first Gain is the node and the second is the exampleNode )

load ()

creates buffers from url paths using BB.AudioBufferLoader, this automatically runs in constructor ( and thus no need to ever call it ) unless autoload is set to false in the config in the constructor

play
(
  • key
  • [when]
  • [offset]
  • [duration]
)

schedules an audio buffer to be played

Parameters:

  • key String

    name of particular sample ( declared in constructor )

  • [when] Number optional

    scheduled time in the AudioContext's timeline/clock (ie. currentTime) to play the file ( default 0, ie. automatically )

  • [offset] Number optional

    default is 0 (ie. beggining of the sample ) but can be offset (seconds) to start at another point in the sample

  • [duration] Number optional

    default is the duration of the entire sample (seconds) can be shortened to a lesser amount

Example:


 // plays the sample "fireworks"
 // starts playing it when AudioContext.currentTime == 10
 // starts the sample 30 seconds into the track
 // plays for half a second, then stops
 sampler.play('fireworks', 10, 30, 0.5);

setGain
(
  • num
  • ramp
)

sets the gain level of the node ( in a sense, master volume control )

Parameters:

  • num Number

    a float value, 1 being the default volume, below 1 decreses the volume, above one pushes the gain

  • ramp Number

    value in seconds for how quickly/slowly to ramp to the new value (num) specified

Example:


 BB.Audio.init();

 var node = new BB.AudioBase({
    volume: 0.75
 });

  node.setGain( 0.25, 2 ); // lower's volume from 0.75 to 0.25 in 2 seconds
// if no ramp value is needed, you could alternatively do
  node.volume = 0.5; // immediately jumps from 0.25 to 0.5

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index