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

Add an AudioStreamGeneratorPlayback.get_frames_buffered() method #11180

Open
Brawmario opened this issue Nov 18, 2024 · 0 comments
Open

Add an AudioStreamGeneratorPlayback.get_frames_buffered() method #11180

Brawmario opened this issue Nov 18, 2024 · 0 comments

Comments

@Brawmario
Copy link

Brawmario commented Nov 18, 2024

Describe the project you are working on

I'm working on the libsm64-godot addon, and in this project, the C library I use (libsm64) generates PCM audio data every "tick" (1/30 of a second). In order to play this audio, I use a AudioStreamGenerator resource as a audio buffer. This way, I don't have to worry about the timing between Godot's process delta and the libsm64 delta, since the audio gets "queued" by the AudioStreamGeneratorPlayback.push_buffer() method.

Describe the problem or limitation you are having in your project

There is no (easy and direct way) of getting the amount of currently buffered audio frames in a AudioStreamGeneratorPlayback object. This feature is needed for my project, since my code has to check every "libsm64 tick" if there is too many audio frames already queued, and if so, throw away the current tick instead of queuing it. This is necessary in order to avoid the played audio from de-syncing, since the length of the audio generated doesn't perfectly line up with the tick length every time. There is an AudioStreamGeneratorPlayback.get_frames_available() method already, but no way to get the currently buffered frames (and no method to get the total size of the buffer, so no direct way of calculating it).

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Pretty self explanatory, simply adding an AudioStreamGeneratorPlayback.get_frames_buffered() method would solve this issue. Maybe there could be an AudioStreamGeneratorPlayback.get_frames_buffer_size() method as well, for the sake of thoroughness.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

It would be as simple as:

int AudioStreamGeneratorPlayback::get_frames_buffered() const {
	return buffer.data_left();
}

It would perfectly mirror the already available AudioStreamGeneratorPlayback.get_frames_available() method.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Calling AudioStreamGeneratorPlayback.get_frames_available() with the buffer empty does technically return the size of the buffer, but it is not always possible to know when the buffered will be completely empty (or even if the buffer is actually empty).

If you know how the AudioStreamGenerator object that generated the AudioStreamGeneratorPlayback object allocates the buffer (by mutiplying the buffer seconds length by the sample rate and finding the next power of two), you can replicate the math in order to find out the total buffer length. However this requires internal engine logic knowledge to figure out and would be subject to change without warning.

Is there a reason why this should be core and not an add-on in the asset library?

The internal buffer object in AudioStreamGeneratorPlayback is private (as it should be), and therefore there's no way to implement this feature outside of directly adding such a method in the engine's core code.

@Brawmario Brawmario changed the title Add a AudioStreamGeneratorPlayback.get_frames_buffered() method Add an AudioStreamGeneratorPlayback.get_frames_buffered() method Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants