Skip to content

Commit

Permalink
We have audio! Thanks @orsi!
Browse files Browse the repository at this point in the history
  • Loading branch information
orsi authored and Continuities committed Sep 10, 2020
1 parent d6d1c1b commit a601473
Show file tree
Hide file tree
Showing 106 changed files with 1,414 additions and 398 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.idea
lang/.DS_Store
.DS_Store
node_modules
Binary file added audio/asteroid-hit-1.flac
Binary file not shown.
Binary file added audio/asteroid-hit-2.flac
Binary file not shown.
Binary file added audio/asteroid-hit-3.flac
Binary file not shown.
Binary file added audio/asteroid-hit-4.flac
Binary file not shown.
Binary file added audio/asteroid-hit-5.flac
Binary file not shown.
Binary file added audio/asteroid-hit-6.flac
Binary file not shown.
Binary file added audio/asteroid-hit-7.flac
Binary file not shown.
Binary file added audio/asteroid-hit-8.flac
Binary file not shown.
Binary file added audio/build.flac
Binary file not shown.
Binary file added audio/buy.flac
Binary file not shown.
Binary file added audio/check-traps.flac
Binary file not shown.
Binary file added audio/craft.flac
Binary file not shown.
Binary file added audio/crash.flac
Binary file not shown.
Binary file added audio/death.flac
Binary file not shown.
Binary file added audio/dusty-path.flac
Binary file not shown.
Binary file added audio/eat-meat.flac
Binary file not shown.
Binary file added audio/embark.flac
Binary file not shown.
Binary file added audio/encounter-tier-1.flac
Binary file not shown.
Binary file added audio/encounter-tier-2.flac
Binary file not shown.
Binary file added audio/encounter-tier-3.flac
Binary file not shown.
Binary file added audio/ending.flac
Binary file not shown.
Binary file added audio/event-beast-attack.flac
Binary file not shown.
Binary file added audio/event-beggar.flac
Binary file not shown.
Binary file added audio/event-hut-fire.flac
Binary file not shown.
Binary file added audio/event-mysterious-wanderer.flac
Binary file not shown.
Binary file added audio/event-noises-inside.flac
Binary file not shown.
Binary file added audio/event-noises-outside.flac
Binary file not shown.
Binary file added audio/event-nomad.flac
Binary file not shown.
Binary file added audio/event-plague.flac
Binary file not shown.
Binary file added audio/event-ruined-trap.flac
Binary file not shown.
Binary file added audio/event-scout.flac
Binary file not shown.
Binary file added audio/event-shady-builder.flac
Binary file not shown.
Binary file added audio/event-sick-man.flac
Binary file not shown.
Binary file added audio/event-sickness.flac
Binary file not shown.
Binary file added audio/event-soldier-attack.flac
Binary file not shown.
Binary file added audio/event-thief.flac
Binary file not shown.
Binary file added audio/event-wandering-master.flac
Binary file not shown.
Binary file added audio/fire-burning.flac
Binary file not shown.
Binary file added audio/fire-dead.flac
Binary file not shown.
Binary file added audio/fire-flickering.flac
Binary file not shown.
Binary file added audio/fire-roaring.flac
Binary file not shown.
Binary file added audio/fire-smoldering.flac
Binary file not shown.
Binary file added audio/footsteps-1.flac
Binary file not shown.
Binary file added audio/footsteps-2.flac
Binary file not shown.
Binary file added audio/footsteps-3.flac
Binary file not shown.
Binary file added audio/footsteps-4.flac
Binary file not shown.
Binary file added audio/footsteps-5.flac
Binary file not shown.
Binary file added audio/footsteps-6.flac
Binary file not shown.
Binary file added audio/gather-wood.flac
Binary file not shown.
Binary file added audio/landmark-battlefield.flac
Binary file not shown.
Binary file added audio/landmark-borehole.flac
Binary file not shown.
Binary file added audio/landmark-cave.flac
Binary file not shown.
Binary file added audio/landmark-city.flac
Binary file not shown.
Binary file added audio/landmark-coalmine.flac
Binary file not shown.
Binary file added audio/landmark-crashed-ship.flac
Binary file not shown.
Binary file added audio/landmark-destroyed-village.flac
Binary file not shown.
Binary file added audio/landmark-friendly-outpost.flac
Binary file not shown.
Binary file added audio/landmark-house.flac
Binary file not shown.
Binary file added audio/landmark-ironmine.flac
Binary file not shown.
Binary file added audio/landmark-sulphurmine.flac
Binary file not shown.
Binary file added audio/landmark-swamp.flac
Binary file not shown.
Binary file added audio/landmark-town.flac
Binary file not shown.
Binary file added audio/large-village.flac
Binary file not shown.
Binary file added audio/lift-off.flac
Binary file not shown.
Binary file added audio/light-fire.flac
Binary file not shown.
Binary file added audio/lonely-hut.flac
Binary file not shown.
Binary file added audio/modest-village.flac
Binary file not shown.
Binary file added audio/raucous-village.flac
Binary file not shown.
Binary file added audio/reinforce-hull.flac
Binary file not shown.
Binary file added audio/ship.flac
Binary file not shown.
Binary file added audio/silent-forest.flac
Binary file not shown.
Binary file added audio/space.flac
Binary file not shown.
Binary file added audio/stoke-fire.flac
Binary file not shown.
Binary file added audio/tiny-village.flac
Binary file not shown.
Binary file added audio/upgrade-engine.flac
Binary file not shown.
Binary file added audio/use-meds.flac
Binary file not shown.
Binary file added audio/weapon-melee-1.flac
Binary file not shown.
Binary file added audio/weapon-melee-2.flac
Binary file not shown.
Binary file added audio/weapon-melee-3.flac
Binary file not shown.
Binary file added audio/weapon-ranged-1.flac
Binary file not shown.
Binary file added audio/weapon-ranged-2.flac
Binary file not shown.
Binary file added audio/weapon-ranged-3.flac
Binary file not shown.
Binary file added audio/weapon-unarmed-1.flac
Binary file not shown.
Binary file added audio/weapon-unarmed-2.flac
Binary file not shown.
Binary file added audio/weapon-unarmed-3.flac
Binary file not shown.
Binary file added audio/world.flac
Binary file not shown.
8 changes: 8 additions & 0 deletions dev-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';

const PORT = 8080;

const app = express();
app.use(express.static('.'));

app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
</script>

<script src="script/Button.js"></script>
<script src="script/audioLibrary.js"></script>
<script src="script/audio.js"></script>
<script src="script/engine.js"></script>
<script src="script/state_manager.js"></script>
<script src="script/header.js"></script>
Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "adarkroom",
"type": "module",
"version": "1.0.0",
"private": true,
"repository": "[email protected]:doublespeakgames/adarkroom.git",
"author": "Michael Townsend <[email protected]>",
"license": "MPL-2.0",
"scripts": {
"start": "node dev-server.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
236 changes: 236 additions & 0 deletions script/audio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/**
* Module that takes care of audio playback
*/
var AudioEngine = {
FADE_TIME: 1,
AUDIO_BUFFER_CACHE: {},
_audioContext: null,
_master: null,
_currentBackgroundMusic: null,
_currentEventAudio: null,
_currentSoundEffectAudio: null,
init: function () {
AudioEngine._initAudioContext();
},
_initAudioContext: function () {
AudioEngine._audioContext = new (window.AudioContext || window.webkitAudioContext);
AudioEngine._createMasterChannel();
},
_createMasterChannel: function () {
// create master
AudioEngine._master = AudioEngine._audioContext.createGain();
AudioEngine._master.gain.setValueAtTime(1.0, AudioEngine._audioContext.currentTime);
AudioEngine._master.connect(AudioEngine._audioContext.destination);
},
_getMissingAudioBuffer: function () {
// plays beeping sound to indicate missing audio
var buffer = AudioEngine._audioContext.createBuffer(
1,
AudioEngine._audioContext.sampleRate,
AudioEngine._audioContext.sampleRate
);
// Fill the buffer
var bufferData = buffer.getChannelData(0);
for (var i = 0; i < buffer.length / 2; i++) {
bufferData[i] = Math.sin(i * 0.05) / 4; // max .25 gain value
}
return buffer;
},
_playSound: function (buffer) {
if (AudioEngine._currentSoundEffectAudio &&
AudioEngine._currentSoundEffectAudio.source.buffer == buffer) {
return;
}

var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.onended = function(event) {
// dereference current sound effect when finished
if (AudioEngine._currentSoundEffectAudio &&
AudioEngine._currentSoundEffectAudio.source.buffer == buffer) {
AudioEngine._currentSoundEffectAudio = null;
}
};

source.connect(AudioEngine._master);
source.start();

AudioEngine._currentSoundEffectAudio = {
source: source
};
},
_playBackgroundMusic: function (buffer) {
var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.loop = true;

var envelope = AudioEngine._audioContext.createGain();
envelope.gain.setValueAtTime(0.0, AudioEngine._audioContext.currentTime);

var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME;

// fade out current background music
if (AudioEngine._currentBackgroundMusic &&
AudioEngine._currentBackgroundMusic.source &&
AudioEngine._currentBackgroundMusic.source.playbackState !== 0) {
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(0.0, fadeTime);
AudioEngine._currentBackgroundMusic.source.stop(fadeTime + 0.3); // make sure fade has completed
}

// fade in new backgorund music
source.connect(envelope);
envelope.connect(AudioEngine._master);
source.start();
envelope.gain.linearRampToValueAtTime(1.0, fadeTime);

// update current background music
AudioEngine._currentBackgroundMusic = {
source: source,
envelope: envelope
};
},
_playEventMusic: function (buffer) {
var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.loop = true;

var envelope = AudioEngine._audioContext.createGain();
envelope.gain.setValueAtTime(0.0, AudioEngine._audioContext.currentTime);

var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;

// turn down current background music
if (AudioEngine._currentBackgroundMusic != null) {
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(0.2, fadeTime);
}

// fade in event music
source.connect(envelope);
envelope.connect(AudioEngine._master);
source.start();
envelope.gain.linearRampToValueAtTime(1.0, fadeTime);

// update reference
AudioEngine._currentEventAudio = {
source: source,
envelope: envelope
};
},
_stopEventMusic: function () {
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;

// fade out event music and stop
if (AudioEngine._currentEventAudio) {
var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value;
AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.linearRampToValueAtTime(0.0, fadeTime);
AudioEngine._currentEventAudio.source.stop(fadeTime + 1); // make sure fade has completed
AudioEngine._currentEventAudio = null;
}

// turn up background music
if (AudioEngine._currentBackgroundMusic) {
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(1.0, fadeTime);
}
},
isAudioContextRunning: function () {
return AudioEngine._audioContext.state !== 'suspended';
},
tryResumingAudioContext: function() {
if (AudioEngine._audioContext.state === 'suspended') {
AudioEngine._audioContext.resume();
}
},
playBackgroundMusic: function (src) {
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
AudioEngine._playBackgroundMusic(buffer);
});
},
playEventMusic: function (src) {
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
AudioEngine._playEventMusic(buffer);
});
},
stopEventMusic: function () {
AudioEngine._stopEventMusic();
},
playSound: function (src) {
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
AudioEngine._playSound(buffer);
});
},
loadAudioFile: function (src) {
if (src.indexOf('http') === -1) {
src = window.location + src;
}
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
return new Promise(function (resolve, reject) {
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
});
} else {
var request = new Request(src);
return fetch(request).then(function (response) {
return response.arrayBuffer();
}).then(function (buffer) {
if (buffer.byteLength === 0) {
console.error('cannot load audio from ' + src);
return AudioEngine._getMissingAudioBuffer();
}

return AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) {
AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData;
return AudioEngine.AUDIO_BUFFER_CACHE[src];
});
});
}
},
setBackgroundMusicVolume: function (volume, s) {
if (AudioEngine._master == null) return; // master may not be ready yet
if (volume === undefined) {
volume = 1.0;
}
if (s === undefined) {
s = 1.0;
}

// cancel any current schedules and then ramp
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(
volume,
AudioEngine._audioContext.currentTime + s
);
},
setMasterVolume: function (volume, s) {
if (AudioEngine._master == null) return; // master may not be ready yet
if (volume === undefined) {
volume = 1.0;
}
if (s === undefined) {
s = 1.0;
}

// cancel any current schedules and then ramp
var currentGainValue = AudioEngine._master.gain.value;
AudioEngine._master.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._master.gain.setValueAtTime(currentGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._master.gain.linearRampToValueAtTime(
volume,
AudioEngine._audioContext.currentTime + s
);
}
};
91 changes: 91 additions & 0 deletions script/audioLibrary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Module that defines all audio files
*/
var AudioLibrary = {
MUSIC_DUSTY_PATH: 'audio/dusty-path.flac',
MUSIC_SILENT_FOREST: 'audio/silent-forest.flac',
MUSIC_LONELY_HUT: 'audio/lonely-hut.flac',
MUSIC_TINY_VILLAGE: 'audio/tiny-village.flac',
MUSIC_MODEST_VILLAGE: 'audio/modest-village.flac',
MUSIC_LARGE_VILLAGE: 'audio/large-village.flac',
MUSIC_RAUCOUS_VILLAGE: 'audio/raucous-village.flac',
MUSIC_FIRE_DEAD: 'audio/fire-dead.flac',
MUSIC_FIRE_SMOLDERING: 'audio/fire-smoldering.flac',
MUSIC_FIRE_FLICKERING: 'audio/fire-flickering.flac',
MUSIC_FIRE_BURNING: 'audio/fire-burning.flac',
MUSIC_FIRE_ROARING: 'audio/fire-roaring.flac',
MUSIC_WORLD: 'audio/world.flac',
MUSIC_SPACE: 'audio/space.flac',
MUSIC_ENDING: 'audio/ending.flac',
MUSIC_SHIP: 'audio/ship.flac',
EVENT_NOMAD: 'audio/event-nomad.flac',
EVENT_NOISES_OUTSIDE: 'audio/event-noises-outside.flac',
EVENT_NOISES_INSIDE: 'audio/event-noises-inside.flac',
EVENT_BEGGAR: 'audio/event-beggar.flac',
EVENT_SHADY_BUILDER: 'audio/event-shady-builder.flac',
EVENT_MYSTERIOUS_WANDERER: 'audio/event-mysterious-wanderer.flac',
EVENT_SCOUT: 'audio/event-scout.flac',
EVENT_WANDERING_MASTER: 'audio/event-wandering-master.flac',
EVENT_SICK_MAN: 'audio/event-sick-man.flac',
EVENT_RUINED_TRAP: 'audio/event-ruined-trap.flac',
EVENT_HUT_FIRE: 'audio/event-hut-fire.flac',
EVENT_SICKNESS: 'audio/event-sickness.flac',
EVENT_PLAGUE: 'audio/event-plague.flac',
EVENT_BEAST_ATTACK: 'audio/event-beast-attack.flac',
EVENT_SOLDIER_ATTACK: 'audio/event-soldier-attack.flac',
EVENT_THIEF: 'audio/event-thief.flac',
LANDMARK_FRIENDLY_OUTPOST: 'audio/landmark-friendly-outpost.flac',
LANDMARK_SWAMP: 'audio/landmark-swamp.flac',
LANDMARK_CAVE: 'audio/landmark-cave.flac',
LANDMARK_TOWN: 'audio/landmark-town.flac',
LANDMARK_CITY: 'audio/landmark-city.flac',
LANDMARK_HOUSE: 'audio/landmark-house.flac',
LANDMARK_BATTLEFIELD: 'audio/landmark-battlefield.flac',
LANDMARK_BOREHOLE: 'audio/landmark-borehole.flac',
LANDMARK_CRASHED_SHIP: 'audio/landmark-crashed-ship.flac',
LANDMARK_SULPHUR_MINE: 'audio/landmark-sulphurmine.flac',
LANDMARK_COAL_MINE: 'audio/landmark-coalmine.flac',
LANDMARK_IRON_MINE: 'audio/landmark-ironmine.flac',
LANDMARK_DESTROYED_VILLAGE: 'audio/landmark-destroyed-village.flac',
ENCOUNTER_TIER_1: 'audio/encounter-tier-1.flac',
ENCOUNTER_TIER_2: 'audio/encounter-tier-2.flac',
ENCOUNTER_TIER_3: 'audio/encounter-tier-3.flac',
LIGHT_FIRE: 'audio/light-fire.flac',
STOKE_FIRE: 'audio/stoke-fire.flac',
BUILD: 'audio/build.flac',
CRAFT: 'audio/craft.flac',
BUY: 'audio/buy.flac',
GATHER_WOOD: 'audio/gather-wood.flac',
CHECK_TRAPS: 'audio/check-traps.flac',
EMBARK: 'audio/embark.flac',
FOOTSTEPS_1: 'audio/footsteps-1.flac',
FOOTSTEPS_2: 'audio/footsteps-2.flac',
FOOTSTEPS_3: 'audio/footsteps-3.flac',
FOOTSTEPS_4: 'audio/footsteps-4.flac',
FOOTSTEPS_5: 'audio/footsteps-5.flac',
FOOTSTEPS_6: 'audio/footsteps-6.flac',
EAT_MEAT: 'audio/eat-meat.flac',
USE_MEDS: 'audio/use-meds.flac',
WEAPON_UNARMED_1: 'audio/weapon-unarmed-1.flac',
WEAPON_UNARMED_2: 'audio/weapon-unarmed-2.flac',
WEAPON_UNARMED_3: 'audio/weapon-unarmed-3.flac',
WEAPON_MELEE_1: 'audio/weapon-melee-1.flac',
WEAPON_MELEE_2: 'audio/weapon-melee-2.flac',
WEAPON_MELEE_3: 'audio/weapon-melee-3.flac',
WEAPON_RANGED_1: 'audio/weapon-ranged-1.flac',
WEAPON_RANGED_2: 'audio/weapon-ranged-2.flac',
WEAPON_RANGED_3: 'audio/weapon-ranged-3.flac',
DEATH: 'audio/death.flac',
REINFORCE_HULL: 'audio/reinforce-hull.flac',
UPGRADE_ENGINE: 'audio/upgrade-engine.flac',
LIFT_OFF: 'audio/lift-off.flac',
ASTEROID_HIT_1: 'audio/asteroid-hit-1.flac',
ASTEROID_HIT_2: 'audio/asteroid-hit-2.flac',
ASTEROID_HIT_3: 'audio/asteroid-hit-3.flac',
ASTEROID_HIT_4: 'audio/asteroid-hit-4.flac',
ASTEROID_HIT_5: 'audio/asteroid-hit-5.flac',
ASTEROID_HIT_6: 'audio/asteroid-hit-6.flac',
ASTEROID_HIT_7: 'audio/asteroid-hit-7.flac',
ASTEROID_HIT_8: 'audio/asteroid-hit-8.flac',
CRASH: 'audio/crash.flac',
};
Loading

0 comments on commit a601473

Please sign in to comment.