Skip to content

Commit

Permalink
Add ALLEGRO_PLAYMODE_LOOP_ONCE.
Browse files Browse the repository at this point in the history
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Apr 2, 2022
1 parent 15e4353 commit 76baeb7
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions addons/acodec/mp3.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ ALLEGRO_AUDIO_STREAM *_al_load_mp3_audio_stream(const char *filename, size_t buf

return stream;
}
#include <stdio.h>


static bool mp3_stream_seek(ALLEGRO_AUDIO_STREAM * stream, double time)
{
Expand Down Expand Up @@ -201,7 +201,7 @@ static size_t mp3_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
double ctime = mp3_stream_get_position(stream);
double btime = (double)samples_needed / mp3file->freq;

if (stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONEDIR && ctime + btime > mp3file->loop_end) {
if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > mp3file->loop_end) {
samples_needed = (mp3file->loop_end - ctime) * mp3file->freq;
}
if (samples_needed < 0)
Expand Down
16 changes: 7 additions & 9 deletions addons/acodec/ogg.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,13 @@ static size_t ogg_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
double btime = ((double)buf_size / ((double)word_size * (double)extra->vi->channels)) / rate;
unsigned long read;

if (stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONEDIR) {
if (ctime + btime > extra->loop_end) {
const int frame_size = word_size * extra->vi->channels;
read_length = (extra->loop_end - ctime) * rate * (double)word_size * (double)extra->vi->channels;
if (read_length < 0)
return 0;
if (read_length % frame_size > 0) {
read_length += (frame_size - (read_length % frame_size));
}
if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > extra->loop_end) {
const int frame_size = word_size * extra->vi->channels;
read_length = (extra->loop_end - ctime) * rate * (double)word_size * (double)extra->vi->channels;
if (read_length < 0)
return 0;
if (read_length % frame_size > 0) {
read_length += (frame_size - (read_length % frame_size));
}
}
while (pos < (unsigned long)read_length) {
Expand Down
12 changes: 5 additions & 7 deletions addons/acodec/opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,11 @@ static size_t ogg_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
double btime = ((double)buf_size / (word_size * channels)) / rate;
long read;

if (stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONEDIR) {
if (ctime + btime > extra->loop_end) {
read_length = (extra->loop_end - ctime) * rate * word_size * channels;
if (read_length < 0)
return 0;
read_length += read_length % word_size;
}
if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > extra->loop_end) {
read_length = (extra->loop_end - ctime) * rate * word_size * channels;
if (read_length < 0)
return 0;
read_length += read_length % word_size;
}

buf_in_word= read_length/word_size;
Expand Down
2 changes: 1 addition & 1 deletion addons/acodec/wav.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ static size_t wav_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
ctime = wav_stream_get_position(stream);
btime = ((double)buf_size / (double)bytes_per_sample) / (double)(wavfile->freq);

if (stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONEDIR && ctime + btime > wavfile->loop_end) {
if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > wavfile->loop_end) {
samples = ((wavfile->loop_end - ctime) * (double)(wavfile->freq));
}
else {
Expand Down
4 changes: 3 additions & 1 deletion addons/audio/allegro5/allegro_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ enum ALLEGRO_PLAYMODE
ALLEGRO_PLAYMODE_LOOP = 0x101,
ALLEGRO_PLAYMODE_BIDIR = 0x102,
_ALLEGRO_PLAYMODE_STREAM_ONCE = 0x103, /* internal */
_ALLEGRO_PLAYMODE_STREAM_ONEDIR = 0x104 /* internal */
_ALLEGRO_PLAYMODE_STREAM_ONEDIR = 0x104, /* internal */
ALLEGRO_PLAYMODE_LOOP_ONCE = 0x105,
_ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE = 0x106, /* internal */
};


Expand Down
12 changes: 12 additions & 0 deletions addons/audio/kcm_mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ static bool fix_looped_position(ALLEGRO_SAMPLE_INSTANCE *spl)
}
return true;

case ALLEGRO_PLAYMODE_LOOP_ONCE:
if (spl->pos < spl->loop_end && spl->pos >= 0) {
return true;
}
if (spl->step >= 0)
spl->pos = 0;
else
spl->pos = spl->loop_end - 1;
spl->is_playing = false;
return false;

case ALLEGRO_PLAYMODE_ONCE:
if (spl->pos < spl->spl_data.len && spl->pos >= 0) {
return true;
Expand All @@ -226,6 +237,7 @@ static bool fix_looped_position(ALLEGRO_SAMPLE_INSTANCE *spl)
return false;

case _ALLEGRO_PLAYMODE_STREAM_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:
stream = (ALLEGRO_AUDIO_STREAM *)spl;
is_empty = false;
Expand Down
6 changes: 6 additions & 0 deletions addons/audio/kcm_mixer_helpers.inc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static INLINE const void *linear_spl32(SAMP_BUF * samp_buf, const ALLEGRO_SAMPLE
if (p1 >= spl->spl_data.len)
p1 = p0;
break;
case ALLEGRO_PLAYMODE_LOOP_ONCE:
case ALLEGRO_PLAYMODE_LOOP:
if (p1 >= spl->loop_end)
p1 = spl->loop_start;
Expand All @@ -125,6 +126,7 @@ static INLINE const void *linear_spl32(SAMP_BUF * samp_buf, const ALLEGRO_SAMPLE
}
break;
case _ALLEGRO_PLAYMODE_STREAM_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:
p0--;
p1--;
Expand Down Expand Up @@ -240,6 +242,7 @@ static INLINE const void *linear_spl16(SAMP_BUF * samp_buf, const ALLEGRO_SAMPLE
if (p1 >= spl->spl_data.len)
p1 = p0;
break;
case ALLEGRO_PLAYMODE_LOOP_ONCE:
case ALLEGRO_PLAYMODE_LOOP:
if (p1 >= spl->loop_end)
p1 = spl->loop_start;
Expand All @@ -252,6 +255,7 @@ static INLINE const void *linear_spl16(SAMP_BUF * samp_buf, const ALLEGRO_SAMPLE
}
break;
case _ALLEGRO_PLAYMODE_STREAM_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:
p0--;
p1--;
Expand Down Expand Up @@ -373,6 +377,7 @@ static INLINE const void *cubic_spl32(SAMP_BUF * samp_buf, const ALLEGRO_SAMPLE_
if (p3 >= spl->spl_data.len)
p3 = spl->spl_data.len - 1;
break;
case ALLEGRO_PLAYMODE_LOOP_ONCE:
case ALLEGRO_PLAYMODE_LOOP:
case ALLEGRO_PLAYMODE_BIDIR:
/* These positions should really wrap/bounce instead of clamping
Expand All @@ -386,6 +391,7 @@ static INLINE const void *cubic_spl32(SAMP_BUF * samp_buf, const ALLEGRO_SAMPLE_
p3 = spl->loop_start;
break;
case _ALLEGRO_PLAYMODE_STREAM_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:
/* Lag by three samples in total. */
p0 -= 2;
Expand Down
6 changes: 5 additions & 1 deletion addons/audio/kcm_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ bool al_set_audio_stream_playmode(ALLEGRO_AUDIO_STREAM *stream,
stream->spl.loop = _ALLEGRO_PLAYMODE_STREAM_ONCE;
return true;
}
if (val == ALLEGRO_PLAYMODE_LOOP_ONCE) {
stream->spl.loop = _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE;
}
else if (val == ALLEGRO_PLAYMODE_LOOP) {
/* Only streams creating by al_load_audio_stream() support
* looping. */
Expand Down Expand Up @@ -747,7 +750,8 @@ void *_al_kcm_feed_stream(ALLEGRO_THREAD *self, void *vstream)
* Don't quit in case the user decides to seek and then restart the
* stream. */
if (bytes_written != bytes &&
stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONCE) {
(stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONCE ||
stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE)) {
al_drain_audio_stream(stream);

if (!finished_event_sent) {
Expand Down
2 changes: 2 additions & 0 deletions docs/src/refman/audio.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ Sample and stream playback mode.
* ALLEGRO_PLAYMODE_LOOP - the sample/stream is played from start to finish (or
between the two loop points). When it reaches the end, it restarts from the
beginning.
* ALLEGRO_PLAYMODE_LOOP_ONCE - just like ALLEGRO_PLAYMODE_ONCE, but
respects the loop end point.
* ALLEGRO_PLAYMODE_BIDIR - the sample is played from start to finish (or
between the two loop points). When it reaches the end, it reverses the
playback direction and plays until it reaches the beginning when it
Expand Down
6 changes: 5 additions & 1 deletion misc/make_mixer_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
#
# Run:
# misc/make_resamplers.py | indent -kr -i3 -l0
# misc/make_mixer_helpers.py | indent -kr -i3 -l0

import sys, re

Expand Down Expand Up @@ -134,6 +134,7 @@ def make_linear_interpolator(name, fmt):
if (p1 >= spl->spl_data.len)
p1 = p0;
break;
case ALLEGRO_PLAYMODE_LOOP_ONCE:
case ALLEGRO_PLAYMODE_LOOP:
if (p1 >= spl->loop_end)
p1 = spl->loop_start;
Expand All @@ -146,6 +147,7 @@ def make_linear_interpolator(name, fmt):
}
break;
case _ALLEGRO_PLAYMODE_STREAM_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:""" +
# For audio streams, sample i+1 may be in the next buffer fragment,
# which may not even be generated yet. So we lag by one sample and
Expand Down Expand Up @@ -228,6 +230,7 @@ def make_cubic_interpolator(name, fmt):
if (p3 >= spl->spl_data.len)
p3 = spl->spl_data.len - 1;
break;
case ALLEGRO_PLAYMODE_LOOP_ONCE:
case ALLEGRO_PLAYMODE_LOOP:
case ALLEGRO_PLAYMODE_BIDIR:
/* These positions should really wrap/bounce instead of clamping
Expand All @@ -241,6 +244,7 @@ def make_cubic_interpolator(name, fmt):
p3 = spl->loop_start;
break;
case _ALLEGRO_PLAYMODE_STREAM_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE:
case _ALLEGRO_PLAYMODE_STREAM_ONEDIR:
/* Lag by three samples in total. */
p0 -= 2;
Expand Down

0 comments on commit 76baeb7

Please sign in to comment.