alpinestudios / oogabooga

ooga booga
Other
223 stars 247 forks source link

added function that checks if the audio player has reached the end of its source sample (e.g. has finished playing). #6

Closed bonmas14 closed 4 months ago

bonmas14 commented 4 months ago

Hello I was doing some work with audio playing and found out that there's no function that tells if its at the end of Audio_Source. It could be used in situations when you set release_when_done to false and need to restart sample after its done playing (Because it still in AUDIO_PLAYER_STATE_PLAYING so it starts playing again).

asbott commented 4 months ago

Hey there, thank you for taking the time to contribute!

I'm not sure I'm understanding the problem it's solving, could you clarfiy?

If by "restart sample after its done playing" you mean for the player to loop over then there is audio_player_set_looping.

Also, release_when_done was mostly meant to be used for the play_one_audio_clip procedures, and it should default to false when you do audio_player_get_one().

And audio_player_get_current_progression_factor() should return exactly 1.0 if the player is not looping and it reached the last frame.

Other than that, I haven't had much feedback on the audio system yet, so I'm curious: how do you find it? Is it working well for you?

bonmas14 commented 4 months ago

Hi, i was thinking how I can play samples but without phasing caused by multiple samples played at small delay. So what i've did is played sample and if it's on the end I can play it again. It basically skips playing of sample if it doesn't come to its end.

I've added this function because relying on if (audio_player_get_current_progression_factor() == 1.0) seems to me not great because its just float, I didn't know that it returns explicitly 1.0 at the end, for me it could return something like 0.9999999 or 1.0000007.

Also about feedback, I think audio player needs to have something like playback_speed so we can have simple pitch controll of sound (Resampling). I wanted to contibute it, but I've never worked with audio on os level so I cant do this.

asbott commented 4 months ago

We're doing operations on float64's, so as long as the frame counts are within a range of 2^52 (which they will always be), then I'm pretty sure that audio_player_get_current_progression_factor will return exactly 1.0 without precision issues.

For playback speed, it wouldn't be done in the os layer but in audio.c. There is already a resample_frames() but I wrote that for small conversions in sample rates (usually between 44100hz & 48000hz), and I'm not sure it will hold up very well for a lot of down/upsampling so it would need some improvements.

I'll merge this though, I think more people will find it useful.