From f11bdb48928d2057c162842bf98e1806396a747c Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 18 Apr 2020 16:01:13 -0400 Subject: [PATCH] Add volume control to audio clips --- docs/assets/js/sounds/sounds.js | 2 +- docs/assets/js/sounds/soundsnippet.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/assets/js/sounds/sounds.js b/docs/assets/js/sounds/sounds.js index 62e2452..164acff 100644 --- a/docs/assets/js/sounds/sounds.js +++ b/docs/assets/js/sounds/sounds.js @@ -17,7 +17,7 @@ let soundAssetMap = { // All available sounds let soundAssets = { - debug_ding: new SoundSnippet("debug-ding") + debug_ding: new SoundSnippet("debug-ding", audioAssetType.sfx) } /** diff --git a/docs/assets/js/sounds/soundsnippet.js b/docs/assets/js/sounds/soundsnippet.js index e222e44..a5c11fd 100644 --- a/docs/assets/js/sounds/soundsnippet.js +++ b/docs/assets/js/sounds/soundsnippet.js @@ -2,13 +2,20 @@ // Counter for hash generation let _hashCounter = 0; +// Audio types +let audioAssetType = { + bgm: 1, + sfx:2 +} + class SoundSnippet { /** * Load a sound asset to a snipper * @param {string} asset_name Asset name as defined in soundassetmap.js + * @param {*} asset_type Asset type. This changes the volume channel */ - constructor(asset_name) { + constructor(asset_name, asset_type) { // Store asset name this.asset_name = asset_name; @@ -20,6 +27,8 @@ class SoundSnippet { // Read actual asset path this.assetPath = soundAssetMap[asset_name]; + this.assetType = asset_type; + } /** @@ -33,6 +42,18 @@ class SoundSnippet { // Create a callback for loading finished this.audio.once("load", callback); + + // Configure a thread for updating the clip volume + setInterval(() => { + + // Read the asset volume from the settings screen + let audio_volume = (this.assetType == 1)? volume.bgm :volume.sfx; + + // Configure the clip volume + this.audio.volume(audio_volume); + + }, 500); + }