miniaudio icon indicating copy to clipboard operation
miniaudio copied to clipboard

sound stuck after using `ma_sound_stop_with_fade_in_milliseconds()`

Open LeifHenriksen opened this issue 1 year ago • 6 comments

Hi, when using ma_sound_stop_with_fade_in_milliseconds() I cannot restart/reuse the sound afterwards.

Also after ma_sound_stop_with_fade_in_milliseconds() ma_sound_is_playing(sound) and ma_sound_at_end(sound) always return false.

Is this normal behavior? Do I need to restart in another way when using ma_sound_stop_with_fade_in_milliseconds()?

Here is an example:

    ma_sound *sound = ...;
    ma_sound_start(sound);
    
    sleep(1);

#if 1
    ma_sound_stop(sound);
    ma_sound_seek_to_pcm_frame(sound, 0);
    ma_sound_start(sound); // Sound restarts.
#else
    ma_sound_stop_with_fade_in_milliseconds(sound, 0);
    // After this point the sound cannot be used.
    ma_sound_seek_to_pcm_frame(sound, 0);
    ma_sound_start(sound); // Sound does not restart.
#endif

Thanks

LeifHenriksen avatar Aug 10 '23 20:08 LeifHenriksen

This is a good question, and something I should look at now that you've pointed this out. First of all, you'll need to fade your sound back in. You've faded it out, but not faded it back in. That, however, is not the bigger problem. When using ma_sound_stop_with_fade(), it's actually scheduling a stop. That scheduled stop time is in continuously in the past so the engine thinks it's still stopped, even after an explicit start. As a workaround while I consider solutions to this, reset the stop time with this:

ma_sound_set_stop_time(sound, ~(ma_uint64)0);

That, plus fading back in, I think should get you going. I should probably add a ma_sound_start_with_fade() to make that fading back in thing a bit easier.

mackron avatar Aug 10 '23 21:08 mackron

So i tried ma_sound_set_stop_time after ma_sound_stop_with_fade_in_milliseconds and it worked, I was able to reset/reuse the sound. For the fade in I don't really understand what you are talking about, if you are talking about the ma_sound_set_fade_in_milliseconds function I tried it and it worked, but the fix also worked without it.

Example:

    ma_sound_stop_with_fade_in_milliseconds(sound, 0);
    ma_sound_set_stop_time_in_pcm_frames(sound, ~(ma_uint64)0);
    // ma_sound_set_fade_in_milliseconds(sound, 0, 1, 0); // This is not really needed 
    ma_sound_seek_to_pcm_frame(sound, 0);
    ma_sound_start(sound); // Restart OK

The problem that I have with this solution is that there is no way of knowing with just a ma_sound ptr if I should call ma_sound_set_stop_time(sound, ~(ma_uint64)0) or not, so I guess I will have to call it every where just to be safe.

LeifHenriksen avatar Aug 10 '23 23:08 LeifHenriksen

Yes, ma_sound_set_fade_in_milliseconds() is what I was talking about. I'm actually surprised that doesn't make any difference.

If you used ma_sound_stop_with_fade() or ma_sound_set_stop_time() you will want to reset the stop time (with ~(ma_uint32)0) when you restart it. I might add a function called ma_sound_clear_stop/start_time() or something. Not sure what I'll do just yet.

mackron avatar Aug 11 '23 06:08 mackron

I've just got a hit with this issue.

Oldes avatar Sep 30 '23 19:09 Oldes

I think that in my case I will reset the stop time using ma_sound_set_stop_time(sound, ~(ma_uint64)0) in cases where sound's stop time is less than current engine's time.. so user may still set the stop time before starting it (if it is set to some time in future). Maybe it could be done by the library to avoid such a confusion.

Oldes avatar Sep 30 '23 19:09 Oldes

If it helps, I am using the sound_reset function for each sound stopped using ma_sound_stop_with_fade_in_milliseconds

inline static void sound_reset(ma_sound *sound, f32 volume)
{
    ma_sound_set_volume(sound, volume);
    ma_sound_set_stop_time_in_milliseconds(sound, ~(ma_uint64)0);
    ma_sound_set_fade_in_milliseconds(sound, 0.0f, 1.0f, FADE_IN_MS);
    ma_sound_seek_to_pcm_frame(sound, 0);
    ma_sound_start(sound);
}

inline static void sound_stop_with_fade(ma_sound *sound)
{
    ma_sound_stop_with_fade_in_milliseconds(sound, FADE_IN_MS);
}

LeifHenriksen avatar Oct 03 '23 23:10 LeifHenriksen