Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sound stuck after using ma_sound_stop_with_fade_in_milliseconds() #714

Open
LeifHenriksen opened this issue Aug 10, 2023 · 6 comments
Open

Comments

@LeifHenriksen
Copy link

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

@mackron
Copy link
Owner

mackron commented Aug 10, 2023

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.

@LeifHenriksen
Copy link
Author

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.

@mackron
Copy link
Owner

mackron commented Aug 11, 2023

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.

@Oldes
Copy link
Contributor

Oldes commented Sep 30, 2023

I've just got a hit with this issue.

@Oldes
Copy link
Contributor

Oldes commented Sep 30, 2023

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.

@LeifHenriksen
Copy link
Author

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants