From 6268cb6a9bbdb4801ef150af55beb4e2621c2850 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Mon, 20 Sep 2021 20:07:00 +0900 Subject: [PATCH] Add musbgm implementation for emscripten --- CMakeLists.txt | 4 +-- src/bgi.c | 84 +++++++++++++++++++++++++++++++++++++++++++ src/bgi.h | 36 +++++++++++++++++++ src/bgm.emscripten.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ src/bgm.sdlmixer.c | 68 ++--------------------------------- 5 files changed, 209 insertions(+), 68 deletions(-) create mode 100644 src/bgi.c create mode 100644 src/bgi.h create mode 100644 src/bgm.emscripten.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 36868ad..107cc55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,10 +92,10 @@ endif() if (EMSCRIPTEN) list(APPEND SRC_AUDIO) - list(APPEND SRC_AUDIO pcm.emscripten.c bgm.dummy.c) + list(APPEND SRC_AUDIO pcm.emscripten.c bgm.emscripten.c bgi.c) list(APPEND SUMMARY_AUDIO "Emscripten") elseif (SDL2MIXER_FOUND) - list(APPEND SRC_AUDIO pcm.sdlmixer.c bgm.sdlmixer.c) + list(APPEND SRC_AUDIO pcm.sdlmixer.c bgm.sdlmixer.c bgi.c) list(APPEND SUMMARY_AUDIO "SDL_mixer") set(ENABLE_SDLMIXER 1) else() diff --git a/src/bgi.c b/src/bgi.c new file mode 100644 index 0000000..9f2162b --- /dev/null +++ b/src/bgi.c @@ -0,0 +1,84 @@ +/* + * bgi.c: BGI (BGM information) parser + * + * Copyright (C) 1997-1998 Masaki Chikama (Wren) + * 1998- + * 2021 kichikuou + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * +*/ +#include "config.h" + +#include + +#include "portab.h" +#include "system.h" +#include "bgi.h" + +#define BGI_MAX 100 + +static int bgi_nfile; +static bgi_t bgi_data[BGI_MAX]; + +static char *bgi_gets(char *buf, int n, FILE *fp) { + char *s = buf; + int c; + while (--n > 0 && (c = fgetc(fp)) != EOF) { + c = c >> 4 | (c & 0xf) << 4; // decrypt + *s++ = c; + if (c == '\n') + break; + } + if (s == buf && c == EOF) + return NULL; + *s = '\0'; + return buf; +} + +int bgi_read(const char *path) { + if (!path) + return NG; + + FILE *fp = fopen(path, "rb"); + if (!fp) { + WARNING("Could not open %s\n", path); + return NG; + } + + char buf[100]; + while (bgi_nfile < BGI_MAX && bgi_gets(buf, sizeof(buf), fp)) { + int terminator; + if (sscanf(buf, " %d, %d, %d, %d, %d", + &bgi_data[bgi_nfile].no, + &bgi_data[bgi_nfile].loopno, + &bgi_data[bgi_nfile].looptop, + &bgi_data[bgi_nfile].len, + &terminator) != 5 + || terminator != -1) { + continue; + } + bgi_nfile++; + } + return OK; +} + +bgi_t *bgi_find(int no) { + for (int i = 0; i < bgi_nfile; i++) { + if (bgi_data[i].no == no) + return &bgi_data[i]; + } + return NULL; +} diff --git a/src/bgi.h b/src/bgi.h new file mode 100644 index 0000000..7589c0d --- /dev/null +++ b/src/bgi.h @@ -0,0 +1,36 @@ +/* + * bgi.c: BGI (BGM information) parser + * + * Copyright (C) 1997-1998 Masaki Chikama (Wren) + * 1998- + * 2021 kichikuou + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * +*/ +#ifndef _BGI_H__ +#define _BGI_H__ + +typedef struct { + int no; // シナリオ上での曲番号 + int loopno; // ループ回数 (0の場合は無限) + int looptop; // ループ時先頭戻り位置 (サンプル数) + int len; // 曲長さ (サンプル数) +} bgi_t; + +extern int bgi_read(const char *path); +extern bgi_t *bgi_find(int no); + +#endif /* _BGI_H__ */ diff --git a/src/bgm.emscripten.c b/src/bgm.emscripten.c new file mode 100644 index 0000000..a7b5258 --- /dev/null +++ b/src/bgm.emscripten.c @@ -0,0 +1,85 @@ +/* + * bgm.emscripten.c BGM (*BA.ALD) emscripten implementation + * + * Copyright (C) 2021 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#include +#include +#include "portab.h" +#include "nact.h" +#include "bgm.h" +#include "bgi.h" +#include "sdl_core.h" + +int musbgm_init() { + return bgi_read(nact->files.bgi); +} + +int musbgm_exit() { + musbgm_stopall(0); + return OK; +} + +EM_JS(int, musbgm_play, (int no, int time, int vol), { + xsystem35.cdPlayer.fade(time * 10, vol / 100); + xsystem35.cdPlayer.play(no, 1); + return xsystem35.Status.OK; +}); + +EM_JS(int, musbgm_stop, (int no, int time), { + xsystem35.cdPlayer.stop(time * 10); + return xsystem35.Status.OK; +}); + +EM_JS(int, musbgm_fade, (int no, int time, int vol), { + xsystem35.cdPlayer.fade(time * 10, vol / 100); + return xsystem35.Status.OK; +}); + +int musbgm_getpos(int no) { + int t = EM_ASM_INT_V( return xsystem35.cdPlayer.getPosition(); ); + if (!t || (t & 0xff) != no) + return NG; + return (t >> 8) * 100 / 75; // frame -> 10ms +} + +int musbgm_getlen(int no) { + bgi_t *bgi = bgi_find(no); + if (!bgi) return 0; + + // FIXME: This assumes 44.1kHz + return bgi->len / 441; +} + +int musbgm_isplaying(int no) { + int t = EM_ASM_INT_V( return xsystem35.cdPlayer.getPosition(); ); + return ((t & 0xff) == no); +} + +int musbgm_stopall(int time) { + return musbgm_stop(0, time); +} + +int musbgm_wait(int no, int timeout) { + for (int i = 0; i * 16 < timeout * 10; i++) { + if (!musbgm_isplaying(no)) + break; + sdl_wait_vsync(); + } + return OK; +} diff --git a/src/bgm.sdlmixer.c b/src/bgm.sdlmixer.c index e8f0f46..0c5fb49 100644 --- a/src/bgm.sdlmixer.c +++ b/src/bgm.sdlmixer.c @@ -22,7 +22,6 @@ #include "config.h" -#include #include #include #include @@ -33,76 +32,15 @@ #include "system.h" #include "nact.h" #include "bgm.h" +#include "bgi.h" #include "music_private.h" #include "ald_manager.h" -#define BGI_MAX 100 - -typedef struct { - int no; // シナリオ上での曲番号 - int loopno; // ループ回数 (0の場合は無限) - int looptop; // ループ時先頭戻り位置 (サンプル数) - int len; // 曲長さ (サンプル数) -} bgi_t; - -static int bgi_nfile; -static bgi_t bgi_data[BGI_MAX]; - static int current_no; static Mix_Music *mix_music; static dridata* dfile; static Uint32 start_time; -static char *bgi_gets(char *buf, int n, FILE *fp) { - char *s = buf; - int c; - while (--n > 0 && (c = fgetc(fp)) != EOF) { - c = c >> 4 | (c & 0xf) << 4; // decrypt - *s++ = c; - if (c == '\n') - break; - } - if (s == buf && c == EOF) - return NULL; - *s = '\0'; - return buf; -} - -static void bgi_read() { - if (nact->files.bgi == NULL) - return; - - FILE *fp = fopen(nact->files.bgi, "rb"); - if (!fp) { - WARNING("Could not open %s\n", nact->files.bgi); - return; - } - - char buf[100]; - while (bgi_nfile < BGI_MAX && bgi_gets(buf, sizeof(buf), fp)) { - int terminator; - if (sscanf(buf, " %d, %d, %d, %d, %d", - &bgi_data[bgi_nfile].no, - &bgi_data[bgi_nfile].loopno, - &bgi_data[bgi_nfile].looptop, - &bgi_data[bgi_nfile].len, - &terminator) != 5 - || terminator != -1) { - continue; - } - bgi_nfile++; - } - return; -} - -static bgi_t *bgi_find(int no) { - for (int i = 0; i < bgi_nfile; i++) { - if (bgi_data[i].no == no) - return &bgi_data[i]; - } - return NULL; -} - static void free_music() { current_no = 0; if (mix_music) { @@ -138,9 +76,7 @@ static Mix_Music *bgm_load(int no) { } int musbgm_init() { - bgi_read(); - if (!bgi_nfile) return NG; - return OK; + return bgi_read(nact->files.bgi); } int musbgm_exit() {