diff --git a/assets/music1.wav b/assets/music1.wav new file mode 100644 index 0000000..703b85f Binary files /dev/null and b/assets/music1.wav differ diff --git a/assets/music2.wav b/assets/music2.wav new file mode 100644 index 0000000..d513c82 Binary files /dev/null and b/assets/music2.wav differ diff --git a/assets/parkingGarage.wav b/assets/parkingGarage.wav new file mode 100644 index 0000000..8a63f87 Binary files /dev/null and b/assets/parkingGarage.wav differ diff --git a/assets/smallPrehistoricCave.wav b/assets/smallPrehistoricCave.wav new file mode 100644 index 0000000..1adf8b9 Binary files /dev/null and b/assets/smallPrehistoricCave.wav differ diff --git a/src/engine/builders/room.js b/src/engine/builders/room.js index e7e4698..bfd7f92 100644 --- a/src/engine/builders/room.js +++ b/src/engine/builders/room.js @@ -9,7 +9,7 @@ export default class RoomBuilder { this.room.id = ID; return this; } - + withTitle(title) { this.room.title = title; return this; @@ -60,6 +60,21 @@ export default class RoomBuilder { return this; } + withMusic(file) { + this.room.music = file; + return this; + } + + withAmbience(file) { + this.room.ambience = file; + return this; + } + + withImpulse(file) { + this.room.impulse = file; + return this; + } + create() { return this.room; } diff --git a/src/engine/output.js b/src/engine/output.js index a68ba85..dc4f153 100644 --- a/src/engine/output.js +++ b/src/engine/output.js @@ -23,4 +23,16 @@ export default class Output { play(file) { this.sound.play(file); } + + setAmbience(file) { + return this.sound.setAmbience(file); + } + + setMusic(file) { + return this.sound.setMusic(file); + } + + setImpulse(file) { + this.sound.setImpulse(file); + } } \ No newline at end of file diff --git a/src/engine/room.js b/src/engine/room.js index 9f56ccc..e274749 100644 --- a/src/engine/room.js +++ b/src/engine/room.js @@ -12,9 +12,15 @@ export default class Room { this.canExitLogic = null; this.tickCallback = null; this.context = null; + this.music = null; + this.ambience = null; + this.impulse = null; } async onEnter() { + this.context.output.setMusic(this.music); + this.context.output.setAmbience(this.ambience); + this.context.output.setImpulse(this.impulse); if (this.enterCallback) return this.enterCallback(this.context); } diff --git a/src/engine/sound.js b/src/engine/sound.js index d5d31e1..408e68a 100644 --- a/src/engine/sound.js +++ b/src/engine/sound.js @@ -4,7 +4,6 @@ import Resonator from '../framework/resonator'; export default class Sound { constructor() { this.res = new Resonator(); - this.res.setEnvironmentImpulse(`assets/Greek7EchoHall.wav`); this.ambience = null; this.music = null; this.previousAmbience = null; @@ -15,4 +14,34 @@ export default class Sound { const sound = this.res.loadImmediate(file); sound.play(); } + + async setAmbience(file) { + if (this.ambience) { + this.previousAmbience = this.ambience; + this.ambience = null; + setTimeout(() => this.previousAmbience.fadeOut(6), 1500); + // setTimeout(() => this.previousAmbience.destroy(), 6000); + } + if (!file) return; + this.ambience = this.res.stream(file, 0); + this.ambience.play(); + this.ambience.loop(true); + this.ambience.fadeIn(3); + } + + setMusic(file) { + if (this.music) { + this.previousMusic = this.music; + setTimeout(() => this.previousMusic.fadeOut(2), 500); + setTimeout(() => this.previousMusic.destroy(), 2000); + } + if (!file) return; + this.music = this.res.stream(file, 1); + this.music.play(); + this.music.fadeIn(2); + } + + setImpulse(file) { + this.res.setEnvironmentImpulse(file); + } } \ No newline at end of file diff --git a/src/framework/resonator/effects/convolver.js b/src/framework/resonator/effects/convolver.js index b36cbec..1b5a732 100644 --- a/src/framework/resonator/effects/convolver.js +++ b/src/framework/resonator/effects/convolver.js @@ -15,5 +15,6 @@ export default class Convolver extends BaseEffect { this.channelSplitter.connect(this.channelMerger, 1, 1); node.connect(this.channelSplitter); this.channelMerger.connect(this.effectNode); + this.inputNode = node; } } diff --git a/src/framework/resonator/sources/audio-source.js b/src/framework/resonator/sources/audio-source.js index 2828713..fb7a507 100644 --- a/src/framework/resonator/sources/audio-source.js +++ b/src/framework/resonator/sources/audio-source.js @@ -14,6 +14,7 @@ export default class AudioSource { this.graph = graph; this.type = type; this.playbackRate = 1; + this.volume = 1; this.init(); } init() { @@ -133,6 +134,7 @@ export default class AudioSource { if (!this.node) { return; } + this.gain.gain.setValueAtTime(this.getVolume(), this.context.getContext().currentTime); this.gain.gain.exponentialRampToValueAtTime(0.0001, this.context.getContext().currentTime + time); setTimeout(() => this.stop(), time * 1000); } diff --git a/src/framework/resonator/sources/streaming-source.js b/src/framework/resonator/sources/streaming-source.js index 39e02c1..d549fe9 100644 --- a/src/framework/resonator/sources/streaming-source.js +++ b/src/framework/resonator/sources/streaming-source.js @@ -91,6 +91,7 @@ export class StreamingSource { if (!this.node) { return; } + this.gain.gain.setValueAtTime(this.getVolume(), this.context.getContext().currentTime); this.gain.gain.exponentialRampToValueAtTime(0.0001, this.context.getContext().currentTime + time); setTimeout(() => this.stop(), time * 1000); } diff --git a/src/game/rooms/start.js b/src/game/rooms/start.js index 33b5528..fcc4614 100644 --- a/src/game/rooms/start.js +++ b/src/game/rooms/start.js @@ -27,4 +27,7 @@ Just... make it stop. Please.` .withItem("stone") .withItem("cup") .withItem("torch") +.withAmbience("assets/cave1.wav") +.withMusic("assets/music1.wav") +.withImpulse("assets/Greek7EchoHall.wav") .create(); \ No newline at end of file diff --git a/src/game/rooms/tunnel1.js b/src/game/rooms/tunnel1.js index 98f46dd..775ef20 100644 --- a/src/game/rooms/tunnel1.js +++ b/src/game/rooms/tunnel1.js @@ -6,4 +6,7 @@ export default new RoomBuilder() .withFirstDescription("You first step foot in this dark loomy tunnel.") .withDescription("The walls are wet. Everything is wet. Ugh. Why do you even.") .withExit("south", "start") +.withAmbience("assets/windy1.wav") +.withMusic("assets/music2.wav") +.withImpulse("assets/parkingGarage.wav") .create(); \ No newline at end of file