comparison src/ljmusic.c @ 0:c84446dfb3f5

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