AudioNode

AudioNode class.

An AudioNode can be either a source a processor or a target.

  • A source delivers a stream, which can be decoded and played (mp3, wav, ogg, wma, aac).
  • A processor can change the stream of data (gain, reverb, delay, ...).
  • A target is the audio system output (speakers).
var dsp = Audio.getContext();
var source  = dsp.createNode("source", 2, 2);   // Source node
var delay   = dsp.createNode("delay", 2, 2);    // Processor node
var custom  = dsp.createNode("custom", 2, 2);   // Processor node
var target  = dsp.createNode("target", 2, 0);   // Target node

// Most processor and custom node allows you to set individual values for a node
delay.set("delay", 500);
delay.set("wet", 0.5);

// You can also pass an object with key/pair to set
delay.set({
    delay: 500,
    wet: 0.5
});

// Custom nodes also support the set()
// Any type of JS variables can be passed
custom.set("data", {
    name: "Hello world",
    anArray: [13, 37],
    theAnswer: 42
});

// Variables are copied to the audio context, not shared
var foo = 15;
custom.set("foo", foo);
// Changing local variable does not affect the value of "foo" in the audio context.
foo = 0;

// A setter is also called everytime a value is set
custom.assignSetter(function(key, value) {
    switch (key) {
        case "foo":
            console.log("foo variable set to ", value);
            break;
    }
});

// Variable passed to the node using set() are available on the node instance
custom.assignProcessor(function(frame, scope) {
    this.foo; // 15
    this.data.theAnswer // 42
    this.data.name // Hello world
});

assignInit(callback)

Assign a function to a custom-source or custom node called when the node is initialized.

  • Nodes are initialized when they are starting to process data.
Arguments:
callback (Function)

Function to execute when the node is initialized

scope (_globalaudiothread)

Global Object of the Audio thread

assignProcessor(processor)

Assign to the node a function to be called when the node needs to process audio data.

This can be used to fill the audio buffers of the node to generate sound. This callback runs is another thread than the main nidium UI. If you need to pass Variables you must explicitly set them with AudioNode.set.

Arguments:
processor (Function)

Callback function for generating sound

frame (Object)

An object with audio buffers

size (Integer)

The size of each audio frame in samples per buffer

data (Float32array[])

An array of Float32Array with the audio data. Each entry of the array represent a channel of the node

scope (_globalaudiothread)

Global Object of the Audio thread

assignSeek(callback)

Assign to the node a function to be called when the AudioNode.position is changed on a custom-source node.

Arguments:
callback (Function)

Function to execute when the node position is changed

position (Float)

The new position in seconds.

scope (_globalaudiothread)

Global Object of the Audio thread

assignSetter(callback)

Assign a function to the node called when a variable has been set on the node with AudioNode.set. This is very usefull if you need to do some extra processing on the variable being set.

Arguments:
callback (Function)

Function to execute when a variable is set on the AudioNode

key (String)

The key name

value (Any)

The value that has been set

scope (_globalaudiothread)

Global Object of the Audio thread

close() Slow Method

Close a stream. This method will free all associated resource with the node. You'll have to call AudioNode.open if you want to play a new file

This method is only available on source node.

get(key)

Get a value from a custom AudioNode object.

Variables passed to custom nodes are copied to the audio context (thread).

Arguments:
key (String)

Key to find the value for

Returns: Any

The value belonging to 'key'

open(src)

Opens an audio file. You can use any stream supported by Nidium.

Supported Audio Formats: mp3, wav, ogg, wma, aac.

This method is only available on source node.

Arguments:
src (String)

The file or stream to open

pause()

Pause playback.

This method is only available on source node.

play()

Start playback.

This method is only available on source node.

send()

Send a message from a custom-source node or a custom node, to the main thread.

The values are copied and can be accessed with the message event.

set(key, value)

Set a value on an AudioNode from the main thread.

For native processor node an error will be thrown if the name of the property is unknown. If the value has been set correctly, the function that was defined with assigneSetter will be called. Variables passed to custom nodes are copied to the audio context. Audio context runs in a different thread. Variables can then be accessed on the node from inside the audio thread using `this[key]|.

Arguments:
key (String)

Key

value (Any)

Value belonging to Key

stop()

Stop playback.

This method is only available on source node.

bitrate(readonly)

  • Type: Integer

The bitrate of the AudioNode in samples per buffer.

This property is only available on source node.

duration(readonly)

  • Type: Integer

The duration of the AudioNode in seconds.

This property is only available on source node.

input(readonly)

  • Type: Audionodelink[]

AudioNode Input array.

metadata(readonly)

  • Type: Object

An object with metadata that belong to this AudioNode stream.

This property is only available on source node.

var src = new Video(c);
src.open("test.ogg");
        for (var k in src.metadata) {
    console.log("Metadata : " + k + "=" + src.metadata[k]);
}

output(readonly)

  • Type: Audionodelink[]

AudioNode Output array.

position

  • Type: Integer

The current position in the AudioNode in seconds. Can also be used to set the position to a certain time.

This property is only available on source node.

var src = new Video(c);
src.open("test.ogg");
        console.log(src.position); // display the current position in the source
src.position += 5; // seek 5 seconds forwards

type(readonly)

  • Type: String

Node name.

Event: buffering(event)

Event fired when the source is buffering data.

Parameters:
event (Object)

Event object

filesize (Integer)

The size of the file in bytes

startByte (Integer)

The start position of the buffered data.

bufferedBytes (Integer)

The number of bytes buffered.

Event: end(dummy)

Event fired when the source reached the end of the file.

Parameters:
dummy (Any)

Empty parameter, set to 'undefined'

Event: error(event)

Event fired when the source fails to be opened or if a read error happens while playing the media file.

Parameters:
event (Object)

Event object

code (Integer)

The error code

error (String)

Description of the errorcode

Event: message(event)

Event fired when a custom node or custom-source node is sending a message from the audio thread to the main thread.

As the AudioContext has it's own thread, communication between threads must be done via messages.

Parameters:
event (Object)

The event object

data (Any)

Data of the message

Event: pause(dummy)

Event fired when the source is put on pause.

Parameters:
dummy (Any)

Empty parameter, set to 'undefined'

Event: play(dummy)

Event fired when the source is put on play.

Parameters:
dummy (Any)

Empty parameter, set to 'undefined'

Event: ready(dummy)

Event fired when the source is opened and ready to be played.

Parameters:
dummy (Any)

Empty parameter, set to 'undefined'


Caught a mistake or want to contribute to the documentation? Edit this page on GitHub!