annotate src/ljmusic.c @ 2:80a2761bd3a4

change DS keys (add alt. rotate)
author paulo@localhost
date Mon, 23 Mar 2009 01:19:12 -0700
parents
children
rev   line source
paulo@0 1 /* FIXME: add license block */
paulo@0 2 #include <allegro.h>
paulo@0 3 #include "ljmusic.h"
paulo@0 4
paulo@0 5 #if LJMUSIC_USING_DUMB
paulo@0 6 #include <aldumb.h>
paulo@0 7 static int dumb_inited;
paulo@0 8 #endif
paulo@0 9 #if LJMUSIC_USING_VORBIS
paulo@0 10 #include "ljvorbis.h"
paulo@0 11 #endif
paulo@0 12
paulo@0 13 #define WARNINGS 0
paulo@0 14
paulo@0 15 struct LJMusic {
paulo@0 16 int paused;
paulo@0 17 #if LJMUSIC_USING_VORBIS
paulo@0 18 LJVorbis *ogg;
paulo@0 19 #endif
paulo@0 20 #if LJMUSIC_USING_DUMB
paulo@0 21 DUH *duh;
paulo@0 22 AL_DUH_PLAYER *duhplayer;
paulo@0 23 #endif
paulo@0 24 };
paulo@0 25
paulo@0 26 struct LJMusic *LJMusic_new(void) {
paulo@0 27 struct LJMusic *m = malloc(sizeof(struct LJMusic));
paulo@0 28 if (!m) {
paulo@0 29 return NULL;
paulo@0 30 }
paulo@0 31 #if LJMUSIC_USING_VORBIS
paulo@0 32 m->ogg = NULL;
paulo@0 33 #endif
paulo@0 34 #if LJMUSIC_USING_DUMB
paulo@0 35 m->duh = NULL;
paulo@0 36 m->duhplayer = NULL;
paulo@0 37 #endif
paulo@0 38 return m;
paulo@0 39 }
paulo@0 40
paulo@0 41 void LJMusic_delete(struct LJMusic *m) {
paulo@0 42 if (!m) {
paulo@0 43 return;
paulo@0 44 }
paulo@0 45 LJMusic_unload(m);
paulo@0 46 free(m);
paulo@0 47 }
paulo@0 48
paulo@0 49 int LJMusic_load(struct LJMusic *m, const char *filename) {
paulo@0 50 if (!m || !filename) {
paulo@0 51 return -1;
paulo@0 52 }
paulo@0 53
paulo@0 54 LJMusic_unload(m);
paulo@0 55 const char *ext = get_extension(filename);
paulo@0 56
paulo@0 57 #if LJMUSIC_USING_VORBIS
paulo@0 58 if (!ustricmp(ext, "ogg")) {
paulo@0 59 m->ogg = LJVorbis_open(filename);
paulo@0 60 return m->ogg ? 1 : 0;
paulo@0 61 }
paulo@0 62 #endif
paulo@0 63
paulo@0 64 #if LJMUSIC_USING_DUMB
paulo@0 65 if (!dumb_inited) {
paulo@0 66 atexit(dumb_exit);
paulo@0 67 dumb_register_stdfiles();
paulo@0 68 }
paulo@0 69 if (!ustricmp(ext, "it")) {
paulo@0 70 m->duh = dumb_load_it_quick(filename);
paulo@0 71 return m->duh ? 1 : 0;
paulo@0 72 }
paulo@0 73 if (!ustricmp(ext, "xm")) {
paulo@0 74 m->duh = dumb_load_xm_quick(filename);
paulo@0 75 return m->duh ? 1 : 0;
paulo@0 76 }
paulo@0 77 if (!ustricmp(ext, "s3m")) {
paulo@0 78 m->duh = dumb_load_s3m_quick(filename);
paulo@0 79 return m->duh ? 1 : 0;
paulo@0 80 }
paulo@0 81 if (!ustricmp(ext, "mod")) {
paulo@0 82 m->duh = dumb_load_mod_quick(filename);
paulo@0 83 return m->duh ? 1 : 0;
paulo@0 84 }
paulo@0 85 #endif
paulo@0 86
paulo@0 87 return 0;
paulo@0 88 }
paulo@0 89
paulo@0 90 void LJMusic_unload(struct LJMusic *m) {
paulo@0 91 if (!m) {
paulo@0 92 return;
paulo@0 93 }
paulo@0 94 LJMusic_stop(m);
paulo@0 95
paulo@0 96 #if LJMUSIC_USING_DUMB
paulo@0 97 if (m->duh) {
paulo@0 98 unload_duh(m->duh);
paulo@0 99 m->duh = NULL;
paulo@0 100 }
paulo@0 101 #endif
paulo@0 102
paulo@0 103 #if LJMUSIC_USING_VORBIS
paulo@0 104 if (m->ogg) {
paulo@0 105 LJVorbis_close(m->ogg);
paulo@0 106 m->ogg = NULL;
paulo@0 107 }
paulo@0 108 #endif
paulo@0 109
paulo@0 110 }
paulo@0 111
paulo@0 112 void LJMusic_setLoop(struct LJMusic *m, unsigned long int start) {
paulo@0 113 #if LJMUSIC_USING_VORBIS
paulo@0 114 if (m->ogg) {
paulo@0 115 LJVorbis_setLoop(m->ogg, start);
paulo@0 116 }
paulo@0 117 #endif
paulo@0 118 }
paulo@0 119
paulo@0 120 void LJMusic_start(struct LJMusic *m, int bufferSize, int vol) {
paulo@0 121 if (!m) {
paulo@0 122 return;
paulo@0 123 }
paulo@0 124
paulo@0 125 LJMusic_stop(m);
paulo@0 126
paulo@0 127 #if LJMUSIC_USING_DUMB
paulo@0 128 if (m->duh) {
paulo@0 129 m->duhplayer = al_start_duh(m->duh, 2, 0,
paulo@0 130 vol / 256.0,
paulo@0 131 bufferSize,
paulo@0 132 48000);
paulo@0 133 }
paulo@0 134 #endif
paulo@0 135 #if LJMUSIC_USING_VORBIS
paulo@0 136 if (m->ogg) {
paulo@0 137 LJVorbis_start(m->ogg, bufferSize, vol, 128);
paulo@0 138 }
paulo@0 139 #endif
paulo@0 140 m->paused = 0;
paulo@0 141 }
paulo@0 142
paulo@0 143 void LJMusic_stop(struct LJMusic *m) {
paulo@0 144 if (!m) {
paulo@0 145 return;
paulo@0 146 }
paulo@0 147
paulo@0 148 #if LJMUSIC_USING_DUMB
paulo@0 149 if (m->duhplayer) {
paulo@0 150 al_stop_duh(m->duhplayer);
paulo@0 151 m->duhplayer = NULL;
paulo@0 152 }
paulo@0 153 #endif
paulo@0 154
paulo@0 155 #if LJMUSIC_USING_VORBIS
paulo@0 156 LJVorbis_stop(m->ogg);
paulo@0 157 #endif
paulo@0 158 }
paulo@0 159
paulo@0 160 void LJMusic_poll(struct LJMusic *m) {
paulo@0 161 if (!m) {
paulo@0 162 return;
paulo@0 163 }
paulo@0 164
paulo@0 165 #if LJMUSIC_USING_DUMB
paulo@0 166 if (m->duhplayer) {
paulo@0 167 al_poll_duh(m->duhplayer);
paulo@0 168 }
paulo@0 169 #endif
paulo@0 170
paulo@0 171 #if LJMUSIC_USING_VORBIS
paulo@0 172 if (m->ogg) {
paulo@0 173 LJVorbis_poll(m->ogg);
paulo@0 174 }
paulo@0 175 #endif
paulo@0 176 }
paulo@0 177
paulo@0 178 void LJMusic_pause(struct LJMusic *m, int value) {
paulo@0 179 value = value ? 1 : 0;
paulo@0 180 if (!m) {
paulo@0 181
paulo@0 182 }
paulo@0 183 if (WARNINGS && (value == m->paused)) {
paulo@0 184 alert("LJMusic_pause", "unchanging.", "", "OK", 0, 13, 0);
paulo@0 185 return;
paulo@0 186 }
paulo@0 187 #if LJMUSIC_USING_DUMB
paulo@0 188 if (m->duhplayer) {
paulo@0 189 if (value) {
paulo@0 190 al_pause_duh(m->duhplayer);
paulo@0 191 } else {
paulo@0 192 al_resume_duh(m->duhplayer);
paulo@0 193 }
paulo@0 194 }
paulo@0 195 #endif
paulo@0 196
paulo@0 197 #if LJMUSIC_USING_VORBIS
paulo@0 198 if (m->ogg) {
paulo@0 199 LJVorbis_pause(m->ogg, value);
paulo@0 200 }
paulo@0 201 #endif
paulo@0 202 m->paused = value;
paulo@0 203 }
paulo@0 204