diff src/ljmusic.c @ 0:c84446dfb3f5

initial add
author paulo@localhost
date Fri, 13 Mar 2009 00:39:12 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ljmusic.c	Fri Mar 13 00:39:12 2009 -0700
     1.3 @@ -0,0 +1,204 @@
     1.4 +/* FIXME: add license block */
     1.5 +#include <allegro.h>
     1.6 +#include "ljmusic.h"
     1.7 +
     1.8 +#if LJMUSIC_USING_DUMB
     1.9 +#include <aldumb.h>
    1.10 +static int dumb_inited;
    1.11 +#endif
    1.12 +#if LJMUSIC_USING_VORBIS
    1.13 +#include "ljvorbis.h"
    1.14 +#endif
    1.15 +
    1.16 +#define WARNINGS 0
    1.17 +
    1.18 +struct LJMusic {
    1.19 +  int paused;
    1.20 +#if LJMUSIC_USING_VORBIS
    1.21 +  LJVorbis *ogg;
    1.22 +#endif
    1.23 +#if LJMUSIC_USING_DUMB
    1.24 +  DUH *duh;
    1.25 +  AL_DUH_PLAYER *duhplayer;
    1.26 +#endif
    1.27 +};
    1.28 +
    1.29 +struct LJMusic *LJMusic_new(void) {
    1.30 +  struct LJMusic *m = malloc(sizeof(struct LJMusic));
    1.31 +  if (!m) {
    1.32 +    return NULL;
    1.33 +  }
    1.34 +#if LJMUSIC_USING_VORBIS
    1.35 +  m->ogg = NULL;
    1.36 +#endif
    1.37 +#if LJMUSIC_USING_DUMB
    1.38 +  m->duh = NULL;
    1.39 +  m->duhplayer = NULL;
    1.40 +#endif
    1.41 +  return m;
    1.42 +}
    1.43 +
    1.44 +void LJMusic_delete(struct LJMusic *m) {
    1.45 +  if (!m) {
    1.46 +    return;
    1.47 +  }
    1.48 +  LJMusic_unload(m);
    1.49 +  free(m);
    1.50 +}
    1.51 +
    1.52 +int LJMusic_load(struct LJMusic *m, const char *filename) {
    1.53 +  if (!m || !filename) {
    1.54 +    return -1;
    1.55 +  }
    1.56 +
    1.57 +  LJMusic_unload(m);
    1.58 +  const char *ext = get_extension(filename);
    1.59 +
    1.60 +#if LJMUSIC_USING_VORBIS
    1.61 +  if (!ustricmp(ext, "ogg")) {
    1.62 +    m->ogg = LJVorbis_open(filename);
    1.63 +    return m->ogg ? 1 : 0;
    1.64 +  }
    1.65 +#endif
    1.66 +
    1.67 +#if LJMUSIC_USING_DUMB
    1.68 +  if (!dumb_inited) {
    1.69 +    atexit(dumb_exit);
    1.70 +    dumb_register_stdfiles();
    1.71 +  }
    1.72 +  if (!ustricmp(ext, "it")) {
    1.73 +    m->duh = dumb_load_it_quick(filename);
    1.74 +    return m->duh ? 1 : 0;
    1.75 +  }
    1.76 +  if (!ustricmp(ext, "xm")) {
    1.77 +    m->duh = dumb_load_xm_quick(filename);
    1.78 +    return m->duh ? 1 : 0;
    1.79 +  }
    1.80 +  if (!ustricmp(ext, "s3m")) {
    1.81 +    m->duh = dumb_load_s3m_quick(filename);
    1.82 +    return m->duh ? 1 : 0;
    1.83 +  }
    1.84 +  if (!ustricmp(ext, "mod")) {
    1.85 +    m->duh = dumb_load_mod_quick(filename);
    1.86 +    return m->duh ? 1 : 0;
    1.87 +  }
    1.88 +#endif
    1.89 +
    1.90 +  return 0;
    1.91 +}
    1.92 +
    1.93 +void LJMusic_unload(struct LJMusic *m) {
    1.94 +  if (!m) {
    1.95 +    return;
    1.96 +  }
    1.97 +  LJMusic_stop(m);
    1.98 +
    1.99 +#if LJMUSIC_USING_DUMB
   1.100 +  if (m->duh) {
   1.101 +    unload_duh(m->duh);
   1.102 +    m->duh = NULL;
   1.103 +  }
   1.104 +#endif
   1.105 +
   1.106 +#if LJMUSIC_USING_VORBIS
   1.107 +  if (m->ogg) {
   1.108 +    LJVorbis_close(m->ogg);
   1.109 +    m->ogg = NULL;
   1.110 +  }
   1.111 +#endif
   1.112 +
   1.113 +}
   1.114 +
   1.115 +void LJMusic_setLoop(struct LJMusic *m, unsigned long int start) {
   1.116 +#if LJMUSIC_USING_VORBIS
   1.117 +  if (m->ogg) {
   1.118 +    LJVorbis_setLoop(m->ogg, start);
   1.119 +  }
   1.120 +#endif
   1.121 +}
   1.122 +
   1.123 +void LJMusic_start(struct LJMusic *m, int bufferSize, int vol) {
   1.124 +  if (!m) {
   1.125 +    return;
   1.126 +  }
   1.127 +
   1.128 +  LJMusic_stop(m);
   1.129 +
   1.130 +#if LJMUSIC_USING_DUMB
   1.131 +  if (m->duh) {
   1.132 +    m->duhplayer = al_start_duh(m->duh, 2, 0,
   1.133 +                                vol / 256.0,
   1.134 +                                bufferSize,
   1.135 +                                48000);
   1.136 +  }
   1.137 +#endif
   1.138 +#if LJMUSIC_USING_VORBIS
   1.139 +  if (m->ogg) {
   1.140 +    LJVorbis_start(m->ogg, bufferSize, vol, 128);
   1.141 +  }
   1.142 +#endif
   1.143 +  m->paused = 0;
   1.144 +}
   1.145 +
   1.146 +void LJMusic_stop(struct LJMusic *m) {
   1.147 +  if (!m) {
   1.148 +    return;
   1.149 +  }
   1.150 +
   1.151 +#if LJMUSIC_USING_DUMB
   1.152 +  if (m->duhplayer) {
   1.153 +    al_stop_duh(m->duhplayer);
   1.154 +    m->duhplayer = NULL;
   1.155 +  }
   1.156 +#endif
   1.157 +
   1.158 +#if LJMUSIC_USING_VORBIS
   1.159 +  LJVorbis_stop(m->ogg);
   1.160 +#endif
   1.161 +}
   1.162 +
   1.163 +void LJMusic_poll(struct LJMusic *m) {
   1.164 +  if (!m) {
   1.165 +    return;
   1.166 +  }
   1.167 +
   1.168 +#if LJMUSIC_USING_DUMB
   1.169 +  if (m->duhplayer) {
   1.170 +    al_poll_duh(m->duhplayer);
   1.171 +  }
   1.172 +#endif
   1.173 +
   1.174 +#if LJMUSIC_USING_VORBIS
   1.175 +  if (m->ogg) {
   1.176 +    LJVorbis_poll(m->ogg);
   1.177 +  }
   1.178 +#endif
   1.179 +}
   1.180 +
   1.181 +void LJMusic_pause(struct LJMusic *m, int value) {
   1.182 +  value = value ? 1 : 0;
   1.183 +  if (!m) {
   1.184 +
   1.185 +  }
   1.186 +  if (WARNINGS && (value == m->paused)) {
   1.187 +    alert("LJMusic_pause", "unchanging.", "", "OK", 0, 13, 0);
   1.188 +    return;
   1.189 +  }
   1.190 +#if LJMUSIC_USING_DUMB
   1.191 +  if (m->duhplayer) {
   1.192 +    if (value) {
   1.193 +      al_pause_duh(m->duhplayer);
   1.194 +    } else {
   1.195 +      al_resume_duh(m->duhplayer);
   1.196 +    }
   1.197 +  }
   1.198 +#endif
   1.199 +
   1.200 +#if LJMUSIC_USING_VORBIS
   1.201 +  if (m->ogg) {
   1.202 +    LJVorbis_pause(m->ogg, value);
   1.203 +  }
   1.204 +#endif
   1.205 +  m->paused = value;
   1.206 +}
   1.207 +