Debian Codegraph @

addons/acodec/openmpt.c

raw
1 /*
2 * Allegro OpenMPT wrapper
3 * author: Pavel Sountsov
4 */
5
6 #define ALLEGRO_INTERNAL_UNSTABLE
7 #define _FILE_OFFSET_BITS 64
8 #include <ctype.h>
9
10 #include "allegro5/allegro.h"
11 #include "allegro5/allegro_acodec.h"
12 #include "allegro5/allegro_audio.h"
13 #include "allegro5/internal/aintern_audio.h"
14 #include "allegro5/internal/aintern_exitfunc.h"
15 #include "allegro5/internal/aintern_system.h"
16 #include "acodec.h"
17 #include "helper.h"
18
19 #ifndef ALLEGRO_CFG_ACODEC_OPENMPT
20 #error configuration problem, ALLEGRO_CFG_ACODEC_OPENMPT not set
21 #endif
22
23 #include <libopenmpt/libopenmpt.h>
24 #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
25
26 ALLEGRO_DEBUG_CHANNEL("acodec")
27
28
29 typedef struct MOD_FILE
30 {
31 openmpt_module *mod;
32 ALLEGRO_FILE *fh;
33 double length;
34 double loop_start, loop_end;
35 } MOD_FILE;
36
37
38 static size_t stream_read_func(void *stream, void *dst, size_t bytes)
39 {
40 return al_fread((ALLEGRO_FILE*)stream, dst, bytes);
41 }
42
43
44 static int stream_seek_func(void *stream, int64_t offset, int whence)
45 {
46 int allegro_whence;
47 switch (whence)
48 {
49 case OPENMPT_STREAM_SEEK_SET:
50 allegro_whence = ALLEGRO_SEEK_SET;
51 break;
52 case OPENMPT_STREAM_SEEK_CUR:
53 allegro_whence = ALLEGRO_SEEK_CUR;
54 break;
55 case OPENMPT_STREAM_SEEK_END:
56 allegro_whence = ALLEGRO_SEEK_END;
57 break;
58 default:
59 return -1;
60 }
61
62 return al_fseek((ALLEGRO_FILE*)stream, offset, allegro_whence) ? 0 : -1;
63 }
64
65
66 static int64_t stream_tell_func(void *stream)
67 {
68 return al_ftell((ALLEGRO_FILE*)stream);
69 }
70
71
72 static void log_func(const char* message, void *user)
73 {
74 (void)user;
75 ALLEGRO_DEBUG("OpenMPT: %s\n", message);
76 }
77
78
79 static int error_func(int error, void *user)
80 {
81 (void)user;
82 const char* error_str = openmpt_error_string(error);
83 if (error_str) {
84 ALLEGRO_ERROR("OpenMPT error: %s\n", error_str);
85 openmpt_free_string(error_str);
86 }
87 else
88 ALLEGRO_ERROR("OpenMPT error: %d\n", error);
89 return OPENMPT_ERROR_FUNC_RESULT_NONE;
90 }
91
92 // /* Stream Functions */
93 static size_t openmpt_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
94 size_t buf_size)
95 {
96 MOD_FILE *const modf = stream->extra;
97
98 /* the mod files are stereo and 16-bit */
99 const int frame_size = 4;
100 size_t written = 0;
101 size_t i;
102
103 if (stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONCE) {
104 openmpt_module_ctl_set_text(modf->mod, "play.at_end", "stop");
105 openmpt_module_set_repeat_count(modf->mod, 0);
106 } else {
107 openmpt_module_ctl_set_text(modf->mod, "play.at_end", "continue");
108 openmpt_module_set_repeat_count(modf->mod, -1);
109 }
110
111 int count = 0;
112 while (written < buf_size) {
113 long frames_to_read = (buf_size - written * frame_size) / frame_size;
114 double position = openmpt_module_get_position_seconds(modf->mod);
115 bool manual_loop = false;
116 /* If manual looping is not enabled, then we need to implement
117 * short-stopping manually. */
118 if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && modf->loop_end != -1 &&
119 position + frames_to_read / 44100 >= modf->loop_end) {
120 frames_to_read = (long)((modf->loop_end - position) * 44100);
121 if (frames_to_read < 0)
122 frames_to_read = 0;
123 manual_loop = true;
124 }
125 written += openmpt_module_read_interleaved_stereo(modf->mod, 44100, frames_to_read,
126 (int16_t*)&(((char *)data)[written])) * frame_size;
127 if (((long)written < frames_to_read * frame_size) || manual_loop) {
128 break;
129 }
130 count += 1;
131 }
132
133 /* Fill the remainder with silence */
134 for (i = written; i < buf_size; ++i)
135 ((char *)data)[i] = 0;
136
137 return written;
138 }
139
140 static void openmpt_stream_close(ALLEGRO_AUDIO_STREAM *stream)
141 {
142 MOD_FILE *const modf = stream->extra;
143 _al_acodec_stop_feed_thread(stream);
144
145 openmpt_module_destroy(modf->mod);
146
147 if (modf->fh)
148 al_fclose(modf->fh);
149 }
150
151 static bool openmpt_stream_rewind(ALLEGRO_AUDIO_STREAM *stream)
152 {
153 MOD_FILE *const modf = stream->extra;
154 openmpt_module_set_position_seconds(modf->mod, modf->loop_start);
155 return true;
156 }
157
158
159 static bool openmpt_stream_seek(ALLEGRO_AUDIO_STREAM *stream, double time)
160 {
161 MOD_FILE *const modf = stream->extra;
162
163 if (modf->loop_end != -1 && time > modf->loop_end) {
164 return false;
165 }
166
167 openmpt_module_set_position_seconds(modf->mod, time);
168
169 return false;
170 }
171
172
173 static double openmpt_stream_get_position(ALLEGRO_AUDIO_STREAM *stream)
174 {
175 MOD_FILE *const modf = stream->extra;
176 return openmpt_module_get_position_seconds(modf->mod);
177 }
178
179
180 static double openmpt_stream_get_length(ALLEGRO_AUDIO_STREAM *stream)
181 {
182 MOD_FILE *const modf = stream->extra;
183 return modf->length;
184 }
185
186
187 static bool openmpt_stream_set_loop(ALLEGRO_AUDIO_STREAM *stream,
188 double start, double end)
189 {
190 MOD_FILE *const modf = stream->extra;
191
192 modf->loop_start = start;
193 modf->loop_end = end;
194
195 return true;
196 }
197
198
199 static ALLEGRO_AUDIO_STREAM *openmpt_stream_init(ALLEGRO_FILE* f,
200 size_t buffer_count, unsigned int samples
201 )
202 {
203 int64_t start_pos = al_ftell(f);
204 ALLEGRO_AUDIO_STREAM *stream = NULL;
205
206 openmpt_stream_callbacks callbacks = {
207 stream_read_func,
208 stream_seek_func,
209 stream_tell_func,
210 };
211
212 openmpt_module *mod = openmpt_module_create2(
213 callbacks, f,
214 &log_func, NULL,
215 &error_func, NULL,
216 NULL, NULL,
217 NULL);
218
219 // HACK: For whatever reason, OpenMPT is slightly quieter than DUMB.
220 // For many modules, this is *very* close to sqrt(2) which is suggestive of
221 // some stereo gain correction, but I could not find the code responsible.
222 //
223 // Also, for some modules it's actually louder by 2x rather than 1.44x,
224 // which is confusing as well.
225 openmpt_module_set_render_param(mod, OPENMPT_MODULE_RENDER_MASTERGAIN_MILLIBEL, 300);
226
227 if (!mod)
228 return NULL;
229
230 // TODO: OpenMPT recommends 48000 and float
231 // TODO: OpenMPT supports quad channels too
232 stream = al_create_audio_stream(buffer_count, samples, 44100,
233 ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
234
235 if (stream) {
236 MOD_FILE *mf = al_malloc(sizeof(MOD_FILE));
237 mf->mod = mod;
238 mf->fh = NULL;
239 mf->length = openmpt_module_get_duration_seconds(mod);
240 if (mf->length < 0) {
241 mf->length = 0;
242 }
243 /*
244 * Set these to -1, so that we can default to the internal loop
245 * points.
246 */
247 mf->loop_start = -1;
248 mf->loop_end = -1;
249
250 stream->extra = mf;
251 stream->feeder = openmpt_stream_update;
252 stream->unload_feeder = openmpt_stream_close;
253 stream->rewind_feeder = openmpt_stream_rewind;
254 stream->seek_feeder = openmpt_stream_seek;
255 stream->get_feeder_position = openmpt_stream_get_position;
256 stream->get_feeder_length = openmpt_stream_get_length;
257 stream->set_feeder_loop = openmpt_stream_set_loop;
258 _al_acodec_start_feed_thread(stream);
259 }
260 else {
261 ALLEGRO_ERROR("Failed to create stream.\n");
262 goto Error;
263 }
264
265 return stream;
266
267 Error:
268
269 openmpt_module_destroy(mod);
270
271 /* try to return back to where we started to load */
272 if (start_pos != -1)
273 al_fseek(f, start_pos, ALLEGRO_SEEK_SET);
274
275 return NULL;
276 }
277
278
279 static ALLEGRO_AUDIO_STREAM *load_audio_stream_f(ALLEGRO_FILE *f,
280 size_t buffer_count, unsigned int samples)
281 {
282 return openmpt_stream_init(f, buffer_count, samples);
283 }
284
285 static ALLEGRO_AUDIO_STREAM *load_audio_stream(const char *filename,
286 size_t buffer_count, unsigned int samples)
287 {
288 ALLEGRO_FILE *f;
289 ALLEGRO_AUDIO_STREAM *stream;
290 ASSERT(filename);
291
292 f = al_fopen(filename, "rb");
293 if (!f) {
294 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
295 return NULL;
296 }
297
298 stream = load_audio_stream_f(f, buffer_count, samples);
299
300 if (!stream) {
301 al_fclose(f);
302 return NULL;
303 }
304
305 ((MOD_FILE *)stream->extra)->fh = f;
306
307 return stream;
308 }
309
310 bool _al_register_openmpt_loaders(void)
311 {
312 bool ret = true;
313 ret &= al_register_audio_stream_loader(".669", load_audio_stream);
314 ret &= al_register_audio_stream_loader_f(".669", load_audio_stream_f);
315 ret &= al_register_sample_identifier(".669", _al_identify_669);
316 ret &= al_register_audio_stream_loader(".amf", load_audio_stream);
317 ret &= al_register_audio_stream_loader_f(".amf", load_audio_stream_f);
318 ret &= al_register_sample_identifier(".amf", _al_identify_amf);
319 ret &= al_register_audio_stream_loader(".it", load_audio_stream);
320 ret &= al_register_audio_stream_loader_f(".it", load_audio_stream_f);
321 ret &= al_register_sample_identifier(".it", _al_identify_it);
322 ret &= al_register_audio_stream_loader(".mod", load_audio_stream);
323 ret &= al_register_audio_stream_loader_f(".mod", load_audio_stream_f);
324 ret &= al_register_sample_identifier(".mod", _al_identify_mod);
325 ret &= al_register_audio_stream_loader(".mtm", load_audio_stream);
326 ret &= al_register_audio_stream_loader_f(".mtm", load_audio_stream_f);
327 ret &= al_register_sample_identifier(".mtm", _al_identify_mtm);
328 ret &= al_register_audio_stream_loader(".okt", load_audio_stream);
329 ret &= al_register_audio_stream_loader_f(".okt", load_audio_stream_f);
330 ret &= al_register_sample_identifier(".okt", _al_identify_okt);
331 ret &= al_register_audio_stream_loader(".psm", load_audio_stream);
332 ret &= al_register_audio_stream_loader_f(".psm", load_audio_stream_f);
333 ret &= al_register_sample_identifier(".psm", _al_identify_psm);
334 ret &= al_register_audio_stream_loader(".ptm", load_audio_stream);
335 ret &= al_register_audio_stream_loader_f(".ptm", load_audio_stream_f);
336 ret &= al_register_sample_identifier(".ptm", _al_identify_ptm);
337 ret &= al_register_audio_stream_loader(".s3m", load_audio_stream);
338 ret &= al_register_audio_stream_loader_f(".s3m", load_audio_stream_f);
339 ret &= al_register_sample_identifier(".s3m", _al_identify_s3m);
340 ret &= al_register_audio_stream_loader(".stm", load_audio_stream);
341 ret &= al_register_audio_stream_loader_f(".stm", load_audio_stream_f);
342 ret &= al_register_sample_identifier(".stm", _al_identify_stm);
343 ret &= al_register_audio_stream_loader(".xm", load_audio_stream);
344 ret &= al_register_audio_stream_loader_f(".xm", load_audio_stream_f);
345 ret &= al_register_sample_identifier(".xm", _al_identify_xm);
346 return ret;
347 }
348
349 /* vim: set sts=3 sw=3 et: */