Skip to content

Commit

Permalink
FIX: using engine's global time offset when setting start/stop sound …
Browse files Browse the repository at this point in the history
…times
  • Loading branch information
Oldes committed Sep 7, 2023
1 parent 5f169a9 commit 0da2b3f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
46 changes: 26 additions & 20 deletions src/miniaudio-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ u32 word_playback;
u32 word_capture;


static ma_uint64 time_to_frames(RXIARG *arg, ma_sound *sound) {
ma_engine *engine = ma_sound_get_engine(sound);
return (arg->uint64 * ma_engine_get_sample_rate(engine)) / 1000000000;
}
static ma_uint64 abs_sound_frames(RXIARG *arg, ma_sound *sound) {
ma_engine *engine = ma_sound_get_engine(sound);
return arg->uint64 + ma_engine_get_time_in_pcm_frames(engine);
}
static ma_uint64 abs_sound_time_to_frames(RXIARG *arg, ma_sound *sound) {
ma_engine *engine = ma_sound_get_engine(sound);
ma_uint64 frames = (arg->uint64 * ma_engine_get_sample_rate(engine)) / 1000000000;
return frames + ma_engine_get_time_in_pcm_frames(engine);
}

static void onSoundEnd(void* hob, ma_sound* pSound) {
trace("sound end ");
}
Expand Down Expand Up @@ -256,6 +270,8 @@ int MASound_get_path(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg) {
int MASound_set_path(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg) {
ma_sound* sound = (ma_sound*)hob->data;
word = RL_FIND_WORD(sound_words, word);
ma_uint64 frames;

switch (word) {
case W_SOUND_VOLUME:
if (!(*type == RXT_DECIMAL || *type == RXT_PERCENT)) return PE_BAD_SET_TYPE;
Expand All @@ -277,28 +293,18 @@ int MASound_set_path(REBHOB *hob, REBCNT word, REBCNT *type, RXIARG *arg) {
if (*type != RXT_LOGIC) return PE_BAD_SET_TYPE;
ma_sound_set_looping(sound, arg->int32a);
break;

case W_SOUND_START:
if (*type == RXT_INTEGER) {
ma_sound_set_start_time_in_pcm_frames(sound, arg->uint64);
break;
}
else if (*type == RXT_TIME) {
if (arg->uint64 < 0) return PE_BAD_SET;
ma_sound_set_start_time_in_milliseconds(engine, arg->uint64 / 1000000);
break;
}
return PE_BAD_SET_TYPE;
case W_SOUND_STOP:
if (*type == RXT_INTEGER) {
ma_sound_set_stop_time_in_pcm_frames(sound, arg->uint64);
break;
}
else if (*type == RXT_TIME) {
if (arg->uint64 < 0) return PE_BAD_SET;
ma_sound_set_stop_time_in_milliseconds(engine, arg->uint64 / 1000000);
break;
}
return PE_BAD_SET_TYPE;
if (*type == RXT_INTEGER) frames = abs_sound_frames(arg, sound);
else if (*type == RXT_TIME) frames = abs_sound_time_to_frames(arg, sound);
else return PE_BAD_SET_TYPE;
if (frames < 0) frames = 0;
if (word == W_SOUND_START)
ma_sound_set_start_time_in_pcm_frames(sound, frames);
else
ma_sound_set_stop_time_in_pcm_frames(sound, frames);
break;

default:
return PE_BAD_SET;
Expand Down
14 changes: 10 additions & 4 deletions test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ probe device: audio/init-playback 1

;; load a sound for later use...
probe sound: audio/load %assets/zblunk_02.wav

;; reset the playback start time
device/frames: 0

sound/start: 44100
print ["Sound will start in" sound/start "frames (1s)..."]
audio/start sound
audio/start sound ;; this sound will start after 44100 frames!
wait 1
print "Now there should be sound!"
print "Now there should be the sound!"
wait 1

print "Now start a loop..."
;; play a looping sound...
probe drums: audio/play/loop %assets/drumloop.wav
print "Now start a loop with fast fade-in (0.5 seconds)..."
probe drums: audio/play/loop/fade %assets/drumloop.wav 0:0:0.5

;; list resources linked with the playback device...
print ["Available sounds:" mold device/resources]
Expand All @@ -45,12 +49,14 @@ device/time: 0:0:2
print ["Device global time in PCM frames:" device/frames "as time:" device/time]

;; stop the music with a fade 5 seconds...
print "Now stop the loop in 5 seconds fade-out..."
audio/stop/fade :drums 0:0:5

;; wait for the sound to fade out...
wait 5

;; play already loaded sound...
sound/pan: 1.0
audio/play :sound
wait 1

Expand Down

0 comments on commit 0da2b3f

Please sign in to comment.