Skip to content

Commit 1c7486a

Browse files
SiegeLordExSiegeLord
authored andcommitted
Fix internal looping for mod files.
Fixes liballeg#1439
1 parent aa1005d commit 1c7486a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

addons/acodec/modaudio.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ static dumb_off_t dfs_get_size(void *f)
159159

160160
/* Stream Functions */
161161

162+
static int loop_callback(void *data)
163+
{
164+
bool *internal_loop = (bool *)data;
165+
*internal_loop = true;
166+
return 0;
167+
}
168+
162169
static size_t modaudio_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
163170
size_t buf_size)
164171
{
@@ -168,28 +175,34 @@ static size_t modaudio_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
168175
const int sample_size = 4;
169176
size_t written = 0;
170177
size_t i;
178+
bool internal_loop = false;
171179

172180
DUMB_IT_SIGRENDERER *it_sig = lib.duh_get_it_sigrenderer(df->sig);
173181
if (it_sig) {
174182
lib.dumb_it_set_loop_callback(it_sig,
175183
stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONCE
176-
? lib.dumb_it_callback_terminate : NULL, NULL);
184+
? lib.dumb_it_callback_terminate : loop_callback, &internal_loop);
177185
}
178186

179187
while (written < buf_size) {
180188
long size_to_read = (buf_size - written) / sample_size;
181189
long position = lib.duh_sigrenderer_get_position(df->sig);
182-
bool loop = false;
190+
bool manual_loop = false;
191+
internal_loop = false;
192+
/* If manual looping is not enabled, then we need to implement
193+
* short-stopping manually. */
183194
if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && df->loop_end != -1 &&
184195
position + 65536 * size_to_read / 44100 >= df->loop_end) {
185196
size_to_read = (df->loop_end - position) * 44100 / 65536;
186197
if (size_to_read < 0)
187198
size_to_read = 0;
188-
loop = true;
199+
manual_loop = true;
189200
}
190201
written += lib.duh_render(df->sig, 16, 0, 1.0, 65536.0 / 44100.0,
191202
size_to_read, &(((char *)data)[written])) * sample_size;
192-
if (loop || (long)written < size_to_read * sample_size) {
203+
/* For internal loops, we don't rewind. */
204+
if (!internal_loop &&
205+
((long)written < size_to_read * sample_size || manual_loop)) {
193206
break;
194207
}
195208
}
@@ -323,13 +336,13 @@ static ALLEGRO_AUDIO_STREAM *modaudio_stream_init(ALLEGRO_FILE* f,
323336
mf->length = lib.duh_get_length(duh) / 65536.0;
324337
if (mf->length < 0) {
325338
mf->length = 0;
326-
mf->loop_start = -1;
327-
mf->loop_end = -1;
328-
}
329-
else {
330-
mf->loop_start = 0;
331-
mf->loop_end = (int)(mf->length * 65536.0);
332339
}
340+
/*
341+
* Set these to -1, so that we can default to the internal loop
342+
* points.
343+
*/
344+
mf->loop_start = -1;
345+
mf->loop_end = -1;
333346

334347
stream->extra = mf;
335348
stream->feeder = modaudio_stream_update;

0 commit comments

Comments
 (0)