csabahruska / proteaaudio

Simple audio library for Windows, Linux, OSX. Supports Mp3, Ogg, Wav playback and multichannel mixing.
28 stars 8 forks source link

Get current position of audio engine #17

Open huantianad opened 10 months ago

huantianad commented 10 months ago

Hello, I'm looking to use this library for a simple terminal based rhythm game. Not looking for too much functionality, so this library seems great. However, one thing that would be very helpful is a way to query the current position of the audio engine as it plays a specific sound, potentially based off of the current number of samples played, something similar to Unity's AudioSettings.dspTime. This would allow the game to be synced to a more accurate audio position.

csabahruska commented 10 months ago

Hi, Do you need to query the global mixer sample count/time or you also would like to get the sample count/time for each sound?

mixerTime :: IO Double
soundTime :: Sound -> IO Double

or the same with sample position:

mixerSamplePosition :: IO Int
soundSamplePosition :: Sound -> IO Int

IMO a more generic approach would be to add info query functions like:

mixerInfo :: IO MixerInfo
sampleInfo :: Sample -> IO SampleInfo
soundInfo :: Sound -> IO SoundInfo

and include the necessary data in these Info types, like: time, sample rate, volume, sample count, channel count, etc.

What do you think?

huantianad commented 10 months ago

I think the last option is probably the best and most flexible, would also help with #15 as well.

For my use case, I think the most useful stat is the current position of a specific Sound (which would be the currently playing music; the position would be used to sync to the music), but having the other info exposed is probably useful for others.

I don't need the raw sample count itself, but I mention it as it seems to be what other tools use to calculate audio position in seconds.

huantianad commented 10 months ago

Hey just wanted to ask how much work this would be for you and how long it would take. I would like to use a feature like this, but might work with a separate timer for now.

I know this project isn't your main priority or anything, so I don't want you to feel like I'm pressuring you or anything. Perhaps I could take a try at doing something myself, though I'm very unfamiliar with this library's internals haha

Thanks again for working on this great library!

csabahruska commented 10 months ago

Hi, I'm quite busy at work currently. Probably I can work on this during the Christmas holidays. But IMO if you have some C experience then you could start developing this feature also. It's not that complicated and I can give support by answering your questions or giving feedback.

The proteaaudio and proteaaudio-sdl libraries share as much code as possible and the code structure is the following:

IMO the files that needs to be changed are: