Reorganised to handle different kind of assets, starting with audio bgm/ambient.

This commit is contained in:
Tony Ribeiro
2023-08-22 01:49:45 +02:00
parent e4e0733565
commit 4cf2b73eb1
5 changed files with 25 additions and 34 deletions

View File

@@ -58,10 +58,9 @@ const DEFAULT_EXPRESSIONS = [
"neutral" "neutral"
]; ];
const SPRITE_DOM_ID = "#expression-image"; const SPRITE_DOM_ID = "#expression-image";
const AMBIENT_FOLDER = "sounds/ambient/";
let fallback_BGMS = []; // Initialized only once with module workers let fallback_BGMS = null; // Initialized only once with module workers
let ambients = []; // Initialized only once with module workers let ambients = null; // Initialized only once with module workers
let characterMusics = {}; // Updated with module workers let characterMusics = {}; // Updated with module workers
let currentCharacterBGM = null; let currentCharacterBGM = null;
@@ -259,7 +258,7 @@ $(document).ready(function () {
//#############################// //#############################//
async function getAmbientList() { async function getAmbientList() {
console.debug(DEBUG_PREFIX, "getting ambient adio files"); console.debug(DEBUG_PREFIX, "getting ambient audio files");
try { try {
const result = await fetch(`/get_default_ambient_list`); const result = await fetch(`/get_default_ambient_list`);
@@ -556,7 +555,7 @@ async function updateAmbient() {
let audio_file_path = null; let audio_file_path = null;
for(const i of ambients) { for(const i of ambients) {
console.debug(i) console.debug(i)
if (i.includes(currentBackground)) { if (i.includes(decodeURIComponent(currentBackground))) {
audio_file_path = i; audio_file_path = i;
break; break;
} }
@@ -571,6 +570,7 @@ async function updateAmbient() {
console.log(DEBUG_PREFIX,"Updating ambient"); console.log(DEBUG_PREFIX,"Updating ambient");
console.log(DEBUG_PREFIX,"Checking file",audio_file_path); console.log(DEBUG_PREFIX,"Checking file",audio_file_path);
const audio = $("#audio_ambient");
audio.animate({volume: 0.0}, 2000, function() { audio.animate({volume: 0.0}, 2000, function() {
audio.attr("src",audio_file_path); audio.attr("src",audio_file_path);
audio[0].play(); audio[0].play();

View File

@@ -1,5 +1,5 @@
{ {
"display_name": "Sounds", "display_name": "Audio",
"loading_order": 14, "loading_order": 14,
"requires": [], "requires": [],
"optional": ["classify"], "optional": ["classify"],

View File

@@ -317,8 +317,9 @@ const directories = {
instruct: 'public/instruct', instruct: 'public/instruct',
context: 'public/context', context: 'public/context',
backups: 'backups/', backups: 'backups/',
quickreplies: 'public/QuickReplies' quickreplies: 'public/QuickReplies',
// TODO: add ambient music here assets_bgm: 'public/assets/audio/bgm',
assets_ambient: 'public/assets/audio/ambient'
}; };
// CSRF Protection // // CSRF Protection //
@@ -4943,32 +4944,27 @@ app.post('/delete_extension', jsonParser, async (request, response) => {
* @returns {void} * @returns {void}
*/ */
app.get('/get_default_bgm_list', jsonParser, function (request, response) { app.get('/get_default_bgm_list', jsonParser, function (request, response) {
const AUDIO_FOLDER = "sounds"; const musicsPath = directories.assets_bgm;
const BGM_FOLDER = "bgm"; let files = [];
const musicsPath = path.join(AUDIO_FOLDER,BGM_FOLDER); let files_paths = [];
let musics = []; console.info("Checking audio file into",musicsPath);
try { try {
if (fs.existsSync(musicsPath) && fs.statSync(musicsPath).isDirectory()) { if (fs.existsSync(musicsPath) && fs.statSync(musicsPath).isDirectory()) {
musics = fs.readdirSync(musicsPath) files = fs.readdirSync(musicsPath)
.filter(file => { .filter(file => {
const mimeType = mime.lookup(file); const mimeType = mime.lookup(file);
return mimeType && mimeType.startsWith('audio/'); return mimeType && mimeType.startsWith('audio/');
})
.map((file) => {
const pathToMusic = path.join(musicsPath, file);
return {
label: path.parse(pathToMusic).name.toLowerCase(),
path: `/${AUDIO_FOLDER}/${BGM_FOLDER}/${file}`,
};
}); });
for(const i of files)
files_paths.push(`/assets/audio/bgm/${i}`);
} }
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
} }
finally { finally {
return response.send(musics); return response.send(files_paths);
} }
}); });
@@ -4981,32 +4977,27 @@ app.get('/get_default_bgm_list', jsonParser, function (request, response) {
* @returns {void} * @returns {void}
*/ */
app.get('/get_default_ambient_list', jsonParser, function (request, response) { app.get('/get_default_ambient_list', jsonParser, function (request, response) {
const AUDIO_FOLDER = "sounds"; const musicsPath = directories.assets_ambient;
const BGM_FOLDER = "ambient"; let files = [];
const musicsPath = path.join(AUDIO_FOLDER,BGM_FOLDER); let files_paths = [];
let musics = []; console.info("Checking audio file into",musicsPath);
try { try {
if (fs.existsSync(musicsPath) && fs.statSync(musicsPath).isDirectory()) { if (fs.existsSync(musicsPath) && fs.statSync(musicsPath).isDirectory()) {
musics = fs.readdirSync(musicsPath) files = fs.readdirSync(musicsPath)
.filter(file => { .filter(file => {
const mimeType = mime.lookup(file); const mimeType = mime.lookup(file);
return mimeType && mimeType.startsWith('audio/'); return mimeType && mimeType.startsWith('audio/');
})
.map((file) => {
const pathToMusic = path.join(musicsPath, file);
return {
label: path.parse(pathToMusic).name.toLowerCase(),
path: `/${AUDIO_FOLDER}/${BGM_FOLDER}/${file}`,
};
}); });
for(const i of files)
files_paths.push(path.join(`/assets/audio/ambient/${i}`));
} }
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
} }
finally { finally {
return response.send(musics); return response.send(files_paths);
} }
}); });