paulo@0: /* FIXME: add license block */ paulo@0: #include paulo@0: #include "ljmusic.h" paulo@0: paulo@0: #if LJMUSIC_USING_DUMB paulo@0: #include paulo@0: static int dumb_inited; paulo@0: #endif paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: #include "ljvorbis.h" paulo@0: #endif paulo@0: paulo@0: #define WARNINGS 0 paulo@0: paulo@0: struct LJMusic { paulo@0: int paused; paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: LJVorbis *ogg; paulo@0: #endif paulo@0: #if LJMUSIC_USING_DUMB paulo@0: DUH *duh; paulo@0: AL_DUH_PLAYER *duhplayer; paulo@0: #endif paulo@0: }; paulo@0: paulo@0: struct LJMusic *LJMusic_new(void) { paulo@0: struct LJMusic *m = malloc(sizeof(struct LJMusic)); paulo@0: if (!m) { paulo@0: return NULL; paulo@0: } paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: m->ogg = NULL; paulo@0: #endif paulo@0: #if LJMUSIC_USING_DUMB paulo@0: m->duh = NULL; paulo@0: m->duhplayer = NULL; paulo@0: #endif paulo@0: return m; paulo@0: } paulo@0: paulo@0: void LJMusic_delete(struct LJMusic *m) { paulo@0: if (!m) { paulo@0: return; paulo@0: } paulo@0: LJMusic_unload(m); paulo@0: free(m); paulo@0: } paulo@0: paulo@0: int LJMusic_load(struct LJMusic *m, const char *filename) { paulo@0: if (!m || !filename) { paulo@0: return -1; paulo@0: } paulo@0: paulo@0: LJMusic_unload(m); paulo@0: const char *ext = get_extension(filename); paulo@0: paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: if (!ustricmp(ext, "ogg")) { paulo@0: m->ogg = LJVorbis_open(filename); paulo@0: return m->ogg ? 1 : 0; paulo@0: } paulo@0: #endif paulo@0: paulo@0: #if LJMUSIC_USING_DUMB paulo@0: if (!dumb_inited) { paulo@0: atexit(dumb_exit); paulo@0: dumb_register_stdfiles(); paulo@0: } paulo@0: if (!ustricmp(ext, "it")) { paulo@0: m->duh = dumb_load_it_quick(filename); paulo@0: return m->duh ? 1 : 0; paulo@0: } paulo@0: if (!ustricmp(ext, "xm")) { paulo@0: m->duh = dumb_load_xm_quick(filename); paulo@0: return m->duh ? 1 : 0; paulo@0: } paulo@0: if (!ustricmp(ext, "s3m")) { paulo@0: m->duh = dumb_load_s3m_quick(filename); paulo@0: return m->duh ? 1 : 0; paulo@0: } paulo@0: if (!ustricmp(ext, "mod")) { paulo@0: m->duh = dumb_load_mod_quick(filename); paulo@0: return m->duh ? 1 : 0; paulo@0: } paulo@0: #endif paulo@0: paulo@0: return 0; paulo@0: } paulo@0: paulo@0: void LJMusic_unload(struct LJMusic *m) { paulo@0: if (!m) { paulo@0: return; paulo@0: } paulo@0: LJMusic_stop(m); paulo@0: paulo@0: #if LJMUSIC_USING_DUMB paulo@0: if (m->duh) { paulo@0: unload_duh(m->duh); paulo@0: m->duh = NULL; paulo@0: } paulo@0: #endif paulo@0: paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: if (m->ogg) { paulo@0: LJVorbis_close(m->ogg); paulo@0: m->ogg = NULL; paulo@0: } paulo@0: #endif paulo@0: paulo@0: } paulo@0: paulo@0: void LJMusic_setLoop(struct LJMusic *m, unsigned long int start) { paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: if (m->ogg) { paulo@0: LJVorbis_setLoop(m->ogg, start); paulo@0: } paulo@0: #endif paulo@0: } paulo@0: paulo@0: void LJMusic_start(struct LJMusic *m, int bufferSize, int vol) { paulo@0: if (!m) { paulo@0: return; paulo@0: } paulo@0: paulo@0: LJMusic_stop(m); paulo@0: paulo@0: #if LJMUSIC_USING_DUMB paulo@0: if (m->duh) { paulo@0: m->duhplayer = al_start_duh(m->duh, 2, 0, paulo@0: vol / 256.0, paulo@0: bufferSize, paulo@0: 48000); paulo@0: } paulo@0: #endif paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: if (m->ogg) { paulo@0: LJVorbis_start(m->ogg, bufferSize, vol, 128); paulo@0: } paulo@0: #endif paulo@0: m->paused = 0; paulo@0: } paulo@0: paulo@0: void LJMusic_stop(struct LJMusic *m) { paulo@0: if (!m) { paulo@0: return; paulo@0: } paulo@0: paulo@0: #if LJMUSIC_USING_DUMB paulo@0: if (m->duhplayer) { paulo@0: al_stop_duh(m->duhplayer); paulo@0: m->duhplayer = NULL; paulo@0: } paulo@0: #endif paulo@0: paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: LJVorbis_stop(m->ogg); paulo@0: #endif paulo@0: } paulo@0: paulo@0: void LJMusic_poll(struct LJMusic *m) { paulo@0: if (!m) { paulo@0: return; paulo@0: } paulo@0: paulo@0: #if LJMUSIC_USING_DUMB paulo@0: if (m->duhplayer) { paulo@0: al_poll_duh(m->duhplayer); paulo@0: } paulo@0: #endif paulo@0: paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: if (m->ogg) { paulo@0: LJVorbis_poll(m->ogg); paulo@0: } paulo@0: #endif paulo@0: } paulo@0: paulo@0: void LJMusic_pause(struct LJMusic *m, int value) { paulo@0: value = value ? 1 : 0; paulo@0: if (!m) { paulo@0: paulo@0: } paulo@0: if (WARNINGS && (value == m->paused)) { paulo@0: alert("LJMusic_pause", "unchanging.", "", "OK", 0, 13, 0); paulo@0: return; paulo@0: } paulo@0: #if LJMUSIC_USING_DUMB paulo@0: if (m->duhplayer) { paulo@0: if (value) { paulo@0: al_pause_duh(m->duhplayer); paulo@0: } else { paulo@0: al_resume_duh(m->duhplayer); paulo@0: } paulo@0: } paulo@0: #endif paulo@0: paulo@0: #if LJMUSIC_USING_VORBIS paulo@0: if (m->ogg) { paulo@0: LJVorbis_pause(m->ogg, value); paulo@0: } paulo@0: #endif paulo@0: m->paused = value; paulo@0: } paulo@0: