cacophony
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "cacophony", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Typescript audio library with caching", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -166,3 +166,3 @@ import { AudioContext, IAudioBuffer, IAudioBufferSourceNode, IAudioListener, IBiquadFilterNode, IGainNode, IPannerNode } from 'standardized-audio-context'; | ||
const playback = this.preplay(); | ||
playback.forEach(p => p.source.start()); | ||
playback.forEach(p => p.source!.start()); | ||
return playback; | ||
@@ -197,3 +197,3 @@ } | ||
this.loopCount = loopCount; | ||
this.playbacks.forEach(p => p.source.loop = true); | ||
this.playbacks.forEach(p => p.source!.loop = true); | ||
return this.loopCount; | ||
@@ -224,5 +224,5 @@ } | ||
context: AudioContext; | ||
source: AudioBufferSourceNode; | ||
gainNode: GainNode; | ||
panner: PannerNode; | ||
source?: AudioBufferSourceNode; | ||
gainNode?: GainNode; | ||
panner?: PannerNode; | ||
loopCount: LoopCount = 0; | ||
@@ -256,2 +256,5 @@ currentLoop: number = 0; | ||
play() { | ||
if (!this.source) { | ||
throw new Error('Cannot play a sound that has been cleaned up'); | ||
} | ||
this.source.start(); | ||
@@ -262,2 +265,5 @@ return [this]; | ||
get volume(): number { | ||
if (!this.gainNode) { | ||
throw new Error('Cannot get volume of a sound that has been cleaned up'); | ||
} | ||
return this.gainNode.gain.value; | ||
@@ -267,17 +273,40 @@ } | ||
set volume(v: number) { | ||
if (!this.gainNode) { | ||
throw new Error('Cannot set volume of a sound that has been cleaned up'); | ||
} | ||
this.gainNode.gain.value = v; | ||
} | ||
fadeIn(time: number, FfadeType: FadeType = 'linear'): Promise<void> { | ||
fadeIn(time: number, fadeType: FadeType = 'linear'): Promise<void> { | ||
return new Promise(resolve => { | ||
let volume = 0; | ||
const increment = this.gainNode.gain.value / (time * 60); // Assuming time is in seconds | ||
const interval = setInterval(() => { | ||
volume += increment; | ||
this.gainNode.gain.value = volume; | ||
if (volume >= this.gainNode.gain.value) { | ||
clearInterval(interval); | ||
resolve(); | ||
if (!this.gainNode) { | ||
throw new Error('Cannot fade in a sound that has been cleaned up'); | ||
} | ||
const initialVolume = this.gainNode.gain.value; | ||
const targetVolume = 1; // Assuming the target volume after fade-in is 1 (full volume) | ||
// Reset volume to 0 to start the fade-in process | ||
this.gainNode.gain.value = 0; | ||
switch (fadeType) { | ||
case 'exponential': | ||
// Start at a low value (0.01) because exponentialRampToValueAtTime cannot ramp from 0 | ||
this.gainNode.gain.setValueAtTime(0.01, this.context.currentTime); | ||
this.gainNode.gain.exponentialRampToValueAtTime(targetVolume, this.context.currentTime + time); | ||
break; | ||
case 'linear': | ||
this.gainNode.gain.linearRampToValueAtTime(targetVolume, this.context.currentTime + time); | ||
break; | ||
} | ||
// Resolve the Promise after the fade-in time | ||
setTimeout(() => { | ||
// Ensure the final volume is set to the target volume | ||
if (!this.gainNode) { | ||
throw new Error('Cannot fade in a sound that has been cleaned up'); | ||
} | ||
}, 1000 / 60); // 60 times per second | ||
this.gainNode.gain.value = targetVolume; | ||
resolve(); | ||
}, time * 1000); | ||
}); | ||
@@ -289,2 +318,5 @@ } | ||
// Storing the current gain value | ||
if (!this.gainNode) { | ||
throw new Error('Cannot fade out a sound that has been cleaned up'); | ||
} | ||
const initialVolume = this.gainNode.gain.value; | ||
@@ -306,3 +338,19 @@ switch (fadeType) { | ||
cleanup(): void { | ||
if (this.source) { | ||
this.source.disconnect(); | ||
this.source = undefined; | ||
} | ||
if (this.gainNode) { | ||
this.gainNode.disconnect(); | ||
this.gainNode = undefined; | ||
} | ||
this.filters.forEach(filter => filter.disconnect()); | ||
this.filters = []; | ||
} | ||
loop(loopCount?: LoopCount): LoopCount { | ||
if (!this.source) { | ||
throw new Error('Cannot loop a sound that has been cleaned up'); | ||
} | ||
if (loopCount === undefined) { | ||
@@ -318,2 +366,5 @@ return this.source.loop === true ? 'infinite' : 0; | ||
stop(): void { | ||
if (!this.source) { | ||
throw new Error('Cannot stop a sound that has been cleaned up'); | ||
} | ||
this.source.stop(); | ||
@@ -323,2 +374,5 @@ } | ||
pause(): void { | ||
if (!this.source) { | ||
throw new Error('Cannot pause a sound that has been cleaned up'); | ||
} | ||
if ('suspend' in this.source.context) { | ||
@@ -330,2 +384,5 @@ this.source.context.suspend(); | ||
resume(): void { | ||
if (!this.source) { | ||
throw new Error('Cannot resume a sound that has been cleaned up'); | ||
} | ||
if ('resume' in this.source.context) { | ||
@@ -347,2 +404,5 @@ this.source.context.resume(); | ||
moveTo(x: number, y: number, z: number): void { | ||
if (!this.panner) { | ||
throw new Error('Cannot move a sound that has been cleaned up'); | ||
} | ||
this.panner.positionX.value = x; | ||
@@ -354,2 +414,5 @@ this.panner.positionY.value = y; | ||
private refreshFilters(): void { | ||
if (!this.source || !this.gainNode) { | ||
throw new Error('Cannot update filters on a sound that has been cleaned up'); | ||
} | ||
let connection = this.source; | ||
@@ -356,0 +419,0 @@ this.source.disconnect(); |
35806
577