mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
add missing docs: SoundPlayer, AudioEngine, Effect.name
This commit is contained in:
parent
a79684a720
commit
97e2996090
5 changed files with 81 additions and 7 deletions
|
@ -248,12 +248,21 @@ class AudioEngine {
|
||||||
log.warn('the createPlayer method is no longer available, please use createBank');
|
log.warn('the createPlayer method is no longer available, please use createBank');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an effect chain.
|
||||||
|
* @returns {EffectChain} chain of effects defined by this AudioEngine
|
||||||
|
*/
|
||||||
createEffectChain () {
|
createEffectChain () {
|
||||||
const effects = new EffectChain(this, this.effects);
|
const effects = new EffectChain(this, this.effects);
|
||||||
effects.connect(this);
|
effects.connect(this);
|
||||||
return effects;
|
return effects;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a sound bank and effect chain.
|
||||||
|
* @returns {SoundBank} a sound bank configured with an effect chain
|
||||||
|
* defined by this AudioEngine
|
||||||
|
*/
|
||||||
createBank () {
|
createBank () {
|
||||||
return new SoundBank(this, this.createEffectChain());
|
return new SoundBank(this, this.createEffectChain());
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,23 +21,75 @@ class SoundPlayer extends EventEmitter {
|
||||||
constructor (audioEngine, {id, buffer}) {
|
constructor (audioEngine, {id, buffer}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unique sound identifier set by AudioEngine.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AudioEngine creating this sound player.
|
||||||
|
* @type {AudioEngine}
|
||||||
|
*/
|
||||||
this.audioEngine = audioEngine;
|
this.audioEngine = audioEngine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decoded audio buffer from audio engine for playback.
|
||||||
|
* @type {AudioBuffer}
|
||||||
|
*/
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output audio node.
|
||||||
|
* @type {AudioNode}
|
||||||
|
*/
|
||||||
this.outputNode = null;
|
this.outputNode = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VolumeEffect used to fade out playing sounds when stopping them.
|
||||||
|
* @type {VolumeEffect}
|
||||||
|
*/
|
||||||
this.volumeEffect = null;
|
this.volumeEffect = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Target engine, effect, or chain this player directly connects to.
|
||||||
|
* @type {AudioEngine|Effect|EffectChain}
|
||||||
|
*/
|
||||||
this.target = null;
|
this.target = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internally is the SoundPlayer initialized with at least its buffer
|
||||||
|
* source node and output node.
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the sound playing or starting to play?
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
this.isPlaying = false;
|
this.isPlaying = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timestamp sound is expected to be starting playback until. Once the
|
||||||
|
* future timestamp is reached the sound is considered to be playing
|
||||||
|
* through the audio hardware and stopping should fade out instead of
|
||||||
|
* cutting off playback.
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
this.startingUntil = 0;
|
this.startingUntil = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rate to play back the audio at.
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
this.playbackRate = 1;
|
this.playbackRate = 1;
|
||||||
|
|
||||||
// handleEvent is a EventTarget api for the DOM, however the web-audio-test-api we use
|
// handleEvent is a EventTarget api for the DOM, however the
|
||||||
// uses an addEventListener that isn't compatable with object and requires us to pass
|
// web-audio-test-api we use uses an addEventListener that isn't
|
||||||
// this bound function instead
|
// compatable with object and requires us to pass this bound function
|
||||||
|
// instead
|
||||||
this.handleEvent = this.handleEvent.bind(this);
|
this.handleEvent = this.handleEvent.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,14 +284,15 @@ class SoundPlayer extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// always do a manual stop on a taken / volume effect fade out sound player
|
// always do a manual stop on a taken / volume effect fade out sound
|
||||||
// take will emit "stop" as well as reset all of our playing statuses / remove our
|
// player take will emit "stop" as well as reset all of our playing
|
||||||
// nodes / etc
|
// statuses / remove our nodes / etc
|
||||||
const taken = this.take();
|
const taken = this.take();
|
||||||
taken.volumeEffect = new VolumeEffect(taken.audioEngine, taken, null);
|
taken.volumeEffect = new VolumeEffect(taken.audioEngine, taken, null);
|
||||||
|
|
||||||
taken.volumeEffect.connect(taken.target);
|
taken.volumeEffect.connect(taken.target);
|
||||||
// volumeEffect will recursively connect to us if it needs to, so this happens too:
|
// volumeEffect will recursively connect to us if it needs to, so this
|
||||||
|
// happens too:
|
||||||
// taken.connect(taken.volumeEffect);
|
// taken.connect(taken.volumeEffect);
|
||||||
|
|
||||||
taken.finished().then(() => taken.dispose());
|
taken.finished().then(() => taken.dispose());
|
||||||
|
|
|
@ -20,6 +20,10 @@ class PanEffect extends Effect {
|
||||||
this.channelMerger = null;
|
this.channelMerger = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the name of the effect.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
get name () {
|
get name () {
|
||||||
return 'pan';
|
return 'pan';
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,10 @@ class PitchEffect extends Effect {
|
||||||
this.ratio = 1;
|
this.ratio = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the name of the effect.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
get name () {
|
get name () {
|
||||||
return 'pitch';
|
return 'pitch';
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,10 @@ class VolumeEffect extends Effect {
|
||||||
return 100;
|
return 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the name of the effect.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
get name () {
|
get name () {
|
||||||
return 'volume';
|
return 'volume';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue