Add impulse response stuff

master
Talon 2021-11-05 18:15:07 +01:00
parent 7d24304b46
commit db78cae144
13 changed files with 74 additions and 2 deletions

BIN
assets/music1.wav 100644

Binary file not shown.

BIN
assets/music2.wav 100644

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -60,6 +60,21 @@ export default class RoomBuilder {
return this; 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() { create() {
return this.room; return this.room;
} }

View File

@ -23,4 +23,16 @@ export default class Output {
play(file) { play(file) {
this.sound.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);
}
} }

View File

@ -12,9 +12,15 @@ export default class Room {
this.canExitLogic = null; this.canExitLogic = null;
this.tickCallback = null; this.tickCallback = null;
this.context = null; this.context = null;
this.music = null;
this.ambience = null;
this.impulse = null;
} }
async onEnter() { 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); if (this.enterCallback) return this.enterCallback(this.context);
} }

View File

@ -4,7 +4,6 @@ import Resonator from '../framework/resonator';
export default class Sound { export default class Sound {
constructor() { constructor() {
this.res = new Resonator(); this.res = new Resonator();
this.res.setEnvironmentImpulse(`assets/Greek7EchoHall.wav`);
this.ambience = null; this.ambience = null;
this.music = null; this.music = null;
this.previousAmbience = null; this.previousAmbience = null;
@ -15,4 +14,34 @@ export default class Sound {
const sound = this.res.loadImmediate(file); const sound = this.res.loadImmediate(file);
sound.play(); 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);
}
} }

View File

@ -15,5 +15,6 @@ export default class Convolver extends BaseEffect {
this.channelSplitter.connect(this.channelMerger, 1, 1); this.channelSplitter.connect(this.channelMerger, 1, 1);
node.connect(this.channelSplitter); node.connect(this.channelSplitter);
this.channelMerger.connect(this.effectNode); this.channelMerger.connect(this.effectNode);
this.inputNode = node;
} }
} }

View File

@ -14,6 +14,7 @@ export default class AudioSource {
this.graph = graph; this.graph = graph;
this.type = type; this.type = type;
this.playbackRate = 1; this.playbackRate = 1;
this.volume = 1;
this.init(); this.init();
} }
init() { init() {
@ -133,6 +134,7 @@ export default class AudioSource {
if (!this.node) { if (!this.node) {
return; return;
} }
this.gain.gain.setValueAtTime(this.getVolume(), this.context.getContext().currentTime);
this.gain.gain.exponentialRampToValueAtTime(0.0001, this.context.getContext().currentTime + time); this.gain.gain.exponentialRampToValueAtTime(0.0001, this.context.getContext().currentTime + time);
setTimeout(() => this.stop(), time * 1000); setTimeout(() => this.stop(), time * 1000);
} }

View File

@ -91,6 +91,7 @@ export class StreamingSource {
if (!this.node) { if (!this.node) {
return; return;
} }
this.gain.gain.setValueAtTime(this.getVolume(), this.context.getContext().currentTime);
this.gain.gain.exponentialRampToValueAtTime(0.0001, this.context.getContext().currentTime + time); this.gain.gain.exponentialRampToValueAtTime(0.0001, this.context.getContext().currentTime + time);
setTimeout(() => this.stop(), time * 1000); setTimeout(() => this.stop(), time * 1000);
} }

View File

@ -27,4 +27,7 @@ Just... make it stop. Please.`
.withItem("stone") .withItem("stone")
.withItem("cup") .withItem("cup")
.withItem("torch") .withItem("torch")
.withAmbience("assets/cave1.wav")
.withMusic("assets/music1.wav")
.withImpulse("assets/Greek7EchoHall.wav")
.create(); .create();

View File

@ -6,4 +6,7 @@ export default new RoomBuilder()
.withFirstDescription("You first step foot in this dark loomy tunnel.") .withFirstDescription("You first step foot in this dark loomy tunnel.")
.withDescription("The walls are wet. Everything is wet. Ugh. Why do you even.") .withDescription("The walls are wet. Everything is wet. Ugh. Why do you even.")
.withExit("south", "start") .withExit("south", "start")
.withAmbience("assets/windy1.wav")
.withMusic("assets/music2.wav")
.withImpulse("assets/parkingGarage.wav")
.create(); .create();