The Sound class provides the core functionality for interacting with the Web Audio API's AudioContext, and is the base class for all other Audio types. It prepares an audio source, provides various methods for interacting with the audio source, creates AudioNodes from the connections array, sets up the necessary connections/routing between them, and provides some methods to play and Sound/stop:method the audio source.
Property Summary
Public Properties | |
---|---|
public |
The AudioBuffer instance that provides audio data to the bufferSource connection. |
public |
The parent AudioContext instance that all audio events are occurring within. It is useful for getting currentTime, as well as creating new AudioNodes. |
public |
|
public |
Computed property. Value is an object containing the duration of the audioBuffer in three formats. The three formats are |
public |
|
public |
When using the Audio-Service, The name that this Sound instance is registered as on it's parent register. |
public |
Computed property. Value is the amount of gain currently applied to the |
public |
When a Sound instance is played, this value is passed to the AudioBufferSourceNode.start() |
Private Properties | |
---|---|
private |
When a Sound instance plays, this is set to the |
Method Summary
Public Methods | |
---|---|
public |
changeGainTo(value): Object
Gets the |
public |
changePanTo(value)
Gets the |
public |
getConnection(name): Connection
|
public |
getNodeFrom(name): AudioNode
|
public |
play( )
|
public |
playAt(time)
|
public |
playFor(seconds)
|
public |
playIn(seconds)
|
public |
playInAndStopAfter(playIn, stopAfter)
|
public |
removeConnection(name)
|
public |
Gets the bufferSource and stops the audio, changes it's play position, and restarts the audio. |
public |
stop( )
|
public |
stopAt(time)
|
public |
stopIn(seconds)
|
public |
update(key, value)
|
Protected Methods | |
---|---|
protected |
|
protected |
|
Private Methods | |
---|---|
private |
_createNode(connection): Connection
|
private |
_play(time)
|
private |
_setAttrsOnNode(connection): Connection
|
private |
_stop(stopAt)
|
private |
|
private |
_wireConnection(connection, idx, connections): Connection
|
Public Properties
addon/classes/sound.js:45
public audioBuffer: AudioBuffer
The AudioBuffer instance that provides audio data to the bufferSource connection.
addon/classes/sound.js:54
public audioContext: AudioContext
The parent AudioContext instance that all audio events are occurring within. It is useful for getting currentTime, as well as creating new AudioNodes.
This is the object that facilitates and ties together all aspects of the Web Audio API.
addon/mixins/connectable.js:31
public connections: Ember.MutableArray
null
but set to an array on init
via the
{{#crossLink "Connectable/_initConnections:method"}}{{/crossLink}} method.addon/classes/sound.js:93
public duration: Object
Computed property. Value is an object containing the duration of the
audioBuffer in three formats. The three formats
are raw
, string
, and pojo
.
Duration of 6 minutes would be output as:
{
raw: 360, // seconds
string: '06:00',
pojo: {
minutes: 6,
seconds: 0
}
}
addon/mixins/playable.js:29
public isPlaying: Boolean
addon/classes/sound.js:35
public name: String
When using the Audio-Service, The name that this Sound instance is registered as on it's parent register.
addon/classes/sound.js:125
public percentGain: Number
Computed property. Value is the amount of gain currently applied to the
gain
connection from the connections
array, formatted as a percentage.
addon/classes/sound.js:81
public startOffset: Number
When a Sound instance is played, this value is passed to the
AudioBufferSourceNode.start()
offset
param. Determines where
(in seconds) the play will start, along
the duration of the audio source.
Private Properties
addon/classes/sound.js:70
private _startedPlayingAt: Number
When a Sound instance plays, this is set to the audioContext.currentTime
.
It will always reflect the start time of the most recent
Sound/_play:method.
Public Methods
addon/classes/sound.js:150
public changeGainTo(value): Object
Gets the gain
connection and changes it's gain value to the value passed in.
returns a pojo with the from
method that value
is curried to, allowing
one to specify which type of value is being provided.
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
value | Number |
|
The value that the |
Return:
Intermediary POJO containing the from
method which will
determine the type of value that gain.value
is being set to and make the
change accordingly.
Example:
// these all result in gainNode.gain.value === 0.9
soundInstance.changeGainTo(0.9).from('ratio');
soundInstance.changeGainTo(0.1).from('inverseRatio')
soundInstance.changeGainTo(90).from('percent');
addon/classes/sound.js:137
public changePanTo(value)
Gets the panner
connection and changes it's pan value to the value passed in.
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
value | Number |
|
The value, between -1 and 1 that the |
addon/mixins/connectable.js:62
public getConnection(name): Connection
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
name | String |
|
The name of the AudioNode that should be returned. |
Return:
addon/mixins/connectable.js:43
public getNodeFrom(name): AudioNode
name
.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
name | String |
|
The name of the AudioNode that should be returned. |
Return:
addon/mixins/playable.js:39
public play( )
addon/mixins/playable.js:49
public playAt(time)
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
time | Number |
|
The moment in time (in seconds, relative to the {{#crossLink "AudioContext"}}AudioContext's{{/crossLink}} "beginning of time") when the audio source should be played. |
addon/mixins/playable.js:80
public playFor(seconds)
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
seconds | Number |
|
The amount of time after which the audio source is stopped. |
addon/mixins/playable.js:67
public playIn(seconds)
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
seconds | Number |
|
Number of seconds from "now" that the audio source should be played. |
addon/mixins/playable.js:95
public playInAndStopAfter(playIn, stopAfter)
playIn
seconds have elapsed, then
stops the audio source stopAfter
seconds after it started playing.addon/mixins/connectable.js:76
public removeConnection(name)
name
and removes it.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
name | String |
|
The name of the connection that should be removed. |
addon/classes/sound.js:193
public seek(amount): Object
Gets the bufferSource and stops the audio, changes it's play position, and restarts the audio.
returns a pojo with the from
method that value
is curried to, allowing
one to specify which type of value is being provided.
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
amount | Number |
|
The new play position value. |
Return:
Intermediary POJO containing the from
method which will
determine the type of value that play position is being set to and make the
change accordingly.
Example:
// for a Sound instance with a duration of 100 seconds, these will all
// move the play position to 90 seconds.
soundInstance.seek(0.9).from('ratio');
soundInstance.seek(0.1).from('inverseRatio')
soundInstance.seek(90).from('percent');
soundInstance.seek(90).from('seconds');
addon/mixins/playable.js:113
public stop( )
addon/mixins/playable.js:136
public stopAt(time)
audioContext
.
Functionally equivalent to the _stop
method.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
time | Number |
|
The time that the audio source should be stopped. |
addon/mixins/playable.js:123
public stopIn(seconds)
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
seconds | Number |
|
Number of seconds from "now" that the audio source should be stopped. |
addon/mixins/connectable.js:88
public update(key, value)
connection.node
(which affects the audio in real time).
If a connection is pulling a property from a connectable object via
onPlaySetAttrsOnNode[index].relativePath
, this method will update that
property directly on the Connection's node
as well as on the connectable
object.
Important to note that this only works for properties that are set directly
on a connectable object and then proxied/set on a node via
onPlaySetAttrsOnNode
.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
key | String |
|
The name/path to a property on an Oscillator instance that should be updated. |
value | String |
|
The value that the property should be set to. |
Example:
// With set
const osc = audioService.createOscillator({ frequency: 440 });
osc.play(); // playing at 440Hz
osc.set('frequency', 1000); // still playing at 440Hz
osc.stop();
osc.play(); // now playing at 1000Hz
// With update
const osc = audioService.createOscillator({ frequency: 440 });
osc.play(); // playing at 440Hz
osc.update('frequency', 1000); // playing at 1000Hz
Protected Methods
addon/mixins/connectable.js:139
protected _initConnections( )
on('init')
.addon/mixins/connectable.js:197
protected wireConnections( ): Array | Connection
Return:
Private Methods
addon/mixins/connectable.js:218
private _createNode(connection): Connection
node
property. Unless the Connection instance's createdOnPlay
property
is true, does nothing if the AudioNode instance has already been created.
Also sets any properties from a connection's onPlaySetAttrsOnNode
array
on the node.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
connection | Connection |
|
A Connection instance that should have it's node created (if needed). |
Return:
addon/mixins/playable.js:179
private _play(time)
play
methods. Plays sound and
sets isPlaying
to true at specified time.
Functionally equivalent to playAt
.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
time | Number |
|
The moment in time (in seconds, relative to the {{#crossLink "AudioContext"}}AudioContext's{{/crossLink}} "beginning of time") when the audio source should be played. |
addon/mixins/connectable.js:253
private _setAttrsOnNode(connection): Connection
onPlaySetAttrsOnNode
and sets them on it's
node.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
connection | Connection |
|
The Connection instance that needs it's node's attrs set. |
Return:
addon/mixins/playable.js:151
private _stop(stopAt)
stop
methods. Stops sound and
set isPlaying
to false at specified time.
Functionally equivalent to the stopAt
method.Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
stopAt | Number |
|
The moment in time (in seconds, relative to the {{#crossLink "AudioContext"}}AudioContext's{{/crossLink}} "beginning of time") when the audio source should be stopped. |
addon/mixins/connectable.js:186
private _watchConnectionChanges( )
addon/mixins/connectable.js:300
private _wireConnection(connection, idx, connections): Connection
Parameters:
Name | Type | Attribute | Description |
---|---|---|---|
connection | Connection |
|
The current Connection instance in the iteration. |
idx | Number |
|
The index of the current iteration. |
connections | Array | Connection |
|
The original array of connections. |