Add musbgm implementation for emscripten

This commit is contained in:
kichikuou
2021-09-25 22:54:57 +09:00
parent a5aa8d0672
commit 6268cb6a9b
5 changed files with 209 additions and 68 deletions
+2 -2
View File
@@ -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()
+84
View File
@@ -0,0 +1,84 @@
/*
* bgi.c: BGI (BGM information) parser
*
* Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
* 1998- <masaki-c@is.aist-nara.ac.jp>
* 2021 kichikuou <KichikuouChrome@gmail.com>
*
* 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 <stdio.h>
#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;
}
+36
View File
@@ -0,0 +1,36 @@
/*
* bgi.c: BGI (BGM information) parser
*
* Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
* 1998- <masaki-c@is.aist-nara.ac.jp>
* 2021 kichikuou <KichikuouChrome@gmail.com>
*
* 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__ */
+85
View File
@@ -0,0 +1,85 @@
/*
* bgm.emscripten.c BGM (*BA.ALD) emscripten implementation
*
* Copyright (C) 2021 <KichikuouChrome@gmail.com>
*
* 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 <stdio.h>
#include <emscripten.h>
#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;
}
+2 -66
View File
@@ -22,7 +22,6 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
@@ -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() {