Refactor musbgm_* interface

This commit is contained in:
kichikuou
2025-02-02 13:08:04 +09:00
parent 386c3c0fcd
commit 5e0844e9ea
7 changed files with 46 additions and 53 deletions
+4 -4
View File
@@ -48,14 +48,14 @@ static char *bgi_gets(char *buf, int n, FILE *fp) {
return buf;
}
int bgi_read(const char *path) {
bool bgi_read(const char *path) {
if (!path)
return NG;
return false;
FILE *fp = fopen(path, "rb");
if (!fp) {
WARNING("Could not open %s", path);
return NG;
return false;
}
char buf[100];
@@ -72,7 +72,7 @@ int bgi_read(const char *path) {
}
bgi_nfile++;
}
return OK;
return true;
}
bgi_t *bgi_find(int no) {
+3 -1
View File
@@ -23,6 +23,8 @@
#ifndef _BGI_H__
#define _BGI_H__
#include <stdbool.h>
typedef struct {
int no; // シナリオ上での曲番号
int loopno; // ループ回数 (0の場合は無限)
@@ -30,7 +32,7 @@ typedef struct {
int len; // 曲長さ (サンプル数)
} bgi_t;
extern int bgi_read(const char *path);
extern bool bgi_read(const char *path);
extern bgi_t *bgi_find(int no);
#endif /* _BGI_H__ */
+12 -17
View File
@@ -26,38 +26,34 @@
#include "bgi.h"
#include "sdl_core.h"
int musbgm_init(DRIFILETYPE type, int base_no) {
bool musbgm_init(DRIFILETYPE type, int base_no) {
if (type == DRIFILE_BGM)
return bgi_read(nact->files.bgi);
else
EM_ASM({ xsystem35.cdPlayer.setBGMLoader($0, $1); }, type, base_no);
return OK;
return true;
}
int musbgm_exit(void) {
void musbgm_exit(void) {
musbgm_stopall(0);
return OK;
}
int musbgm_reset(void) {
void musbgm_reset(void) {
musbgm_stopall(0);
return OK;
}
EM_JS(int, musbgm_play, (int no, int time, int vol, int loop_count), {
EM_JS(bool, musbgm_play, (int no, int time, int vol, int loop_count), {
xsystem35.cdPlayer.fade(time * 10, vol / 100);
xsystem35.cdPlayer.play(no, loop_count == 0);
return xsystem35.Status.OK;
return 1;
});
EM_JS(int, musbgm_stop, (int no, int time), {
EM_JS(void, 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), {
EM_JS(void, musbgm_fade, (int no, int time, int vol), {
xsystem35.cdPlayer.fade(time * 10, vol / 100);
return xsystem35.Status.OK;
});
int musbgm_getpos(int no) {
@@ -75,20 +71,19 @@ int musbgm_getlen(int no) {
return bgi->len / 441;
}
int musbgm_isplaying(int no) {
bool 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);
void musbgm_stopall(int time) {
musbgm_stop(0, time);
}
int musbgm_wait(int no, int timeout) {
void 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;
}
+10 -9
View File
@@ -19,18 +19,19 @@
#ifndef __BGM_H__
#define __BGM_H__
#include <stdbool.h>
#include "ald_manager.h"
int musbgm_init(DRIFILETYPE type, int base_no);
int musbgm_exit(void);
int musbgm_reset(void);
int musbgm_play(int no, int time, int vol, int loop_count);
int musbgm_stop(int no, int time);
int musbgm_fade(int no, int time, int vol);
bool musbgm_init(DRIFILETYPE type, int base_no);
void musbgm_exit(void);
void musbgm_reset(void);
bool musbgm_play(int no, int time, int vol, int loop_count);
void musbgm_stop(int no, int time);
void musbgm_fade(int no, int time, int vol);
int musbgm_getpos(int no);
int musbgm_getlen(int no);
int musbgm_isplaying(int no);
int musbgm_stopall(int time);
int musbgm_wait(int no, int timeout);
bool musbgm_isplaying(int no);
void musbgm_stopall(int time);
void musbgm_wait(int no, int timeout);
#endif /* __BGM_H__ */
+15 -20
View File
@@ -78,30 +78,28 @@ static Mix_Music *bgm_load(int no) {
return mix_music;
}
int musbgm_init(DRIFILETYPE type, int base) {
bool musbgm_init(DRIFILETYPE type, int base) {
dri_type = type;
base_no = base;
if (type == DRIFILE_BGM)
return bgi_read(nact->files.bgi);
return OK;
return true;
}
int musbgm_exit(void) {
void musbgm_exit(void) {
free_music();
return OK;
}
int musbgm_reset(void) {
void musbgm_reset(void) {
free_music();
return OK;
}
int musbgm_play(int no, int time, int vol, int loop_count) {
bool musbgm_play(int no, int time, int vol, int loop_count) {
if (current_no)
musbgm_stop(current_no, 0);
if (!bgm_load(no))
return NG;
return false;
// We don't use the loop information in the BGI file, but SDL_mixer
// understands loop info in the WAVE's "smpl" chunk.
@@ -109,26 +107,24 @@ int musbgm_play(int no, int time, int vol, int loop_count) {
Mix_VolumeMusic(vol * MIX_MAX_VOLUME / 100);
if (Mix_FadeInMusic(mix_music, loop_count == 0 ? -1 : loop_count, time * 10) != 0) {
free_music();
return NG;
return false;
}
start_time = SDL_GetTicks();
return OK;
return true;
}
int musbgm_stop(int no, int time) {
void musbgm_stop(int no, int time) {
if (no == current_no)
Mix_FadeOutMusic(time * 10);
return OK;
}
int musbgm_fade(int no, int time, int vol) {
void musbgm_fade(int no, int time, int vol) {
if (no != current_no)
return NG;
return;
// SDL_mixer doesn't provide arbitrary fading, so just set the volume immediately.
Mix_VolumeMusic(vol * MIX_MAX_VOLUME / 100);
return OK;
}
int musbgm_getpos(int no) {
@@ -147,16 +143,15 @@ int musbgm_getlen(int no) {
return bgi->len / 441;
}
int musbgm_isplaying(int no) {
bool musbgm_isplaying(int no) {
return no == current_no && Mix_PlayingMusic();
}
int musbgm_stopall(int time) {
return musbgm_stop(current_no, time);
void musbgm_stopall(int time) {
musbgm_stop(current_no, time);
}
int musbgm_wait(int no, int timeout) {
void musbgm_wait(int no, int timeout) {
while (timeout-- > 0 && musbgm_isplaying(no))
SDL_Delay(10);
return OK;
}
+1 -1
View File
@@ -42,7 +42,7 @@ static void cdrom_bgm_reset(void) {
}
static bool cdrom_bgm_start(int trk, int loop) {
if (musbgm_play(trk, 0, 100, loop) != OK)
if (!musbgm_play(trk, 0, 100, loop))
return false;
current_track = trk;
return true;
+1 -1
View File
@@ -58,7 +58,7 @@ int muscd_init(void) {
int muscd_init_bgm(DRIFILETYPE type, int base_no) {
muscd_exit();
prv.cddev = &cdrom_bgm;
return musbgm_init(type, base_no);
return musbgm_init(type, base_no) ? OK : NG;
}
int muscd_exit(void) {