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 support for more output types in AudioDriver #8797

Open
kus04e4ek opened this issue Jan 4, 2024 · 3 comments · May be fixed by godotengine/godot#90013
Open

Add support for more output types in AudioDriver #8797

kus04e4ek opened this issue Jan 4, 2024 · 3 comments · May be fixed by godotengine/godot#90013

Comments

@kus04e4ek
Copy link

kus04e4ek commented Jan 4, 2024

Describe the project you are working on

Oboe AudioDriver

Describe the problem or limitation you are having in your project

To get the data that needs to be outputted in any other format than int32_t, the buffer should be created. Which is memory allocation that can be easily prevented.

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

Change void AudioDriver::audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time) to the template function and create private functions that convert from int32_t to the needed type.

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

class AudioDriver {
private:
     static void set_sample8(void *p_buffer, uint32_t p_index, int32_t p_sample);
     static void set_sample16(void *p_buffer, uint32_t p_index, int32_t p_sample);
     static void set_sample24(void *p_buffer, uint32_t p_index, int32_t p_sample);
     static void set_sample32(void *p_buffer, uint32_t p_index, int32_t p_sample);
     static float set_sample_float(void *p_buffer, uint32_t p_index, int32_t p_sample);

     template<class T>
     void audio_server_process(int p_frames, void *p_buffer, bool p_update_mix_time, T p_set_sample) {
          ...
          _driver_process(p_frames, p_buffer, p_set_sample);
          ...
     }
}

class AudioServer {
private:
     template<class T>
     void _driver_process(int p_frames, void *p_buffer, T p_set_sample) {
          ...
          p_set_sample(p_buffer, i, value);
          ...
     }
}

void AudioServer::set_sample8(void *p_buffer, uint32_t p_index, int32_t p_sample) {
     int8_t* buffer = static_cast<int8_t*>(p_buffer);
     buffer[p_index] = p_sample >> 24;
}

void AudioServer::set_sample16(void *p_buffer, uint32_t p_index, int32_t p_sample) {
     int16_t* buffer = static_cast<int16_t*>(p_buffer);
     buffer[p_index] = p_sample >> 16;
}

void AudioServer::set_sample24(void *p_buffer, uint32_t p_index, int32_t p_sample) {
     /* https://github.com/godotengine/godot/blob/fbaab3cf537a892295aabdfd02c8052e370e6669/drivers/wasapi/audio_driver_wasapi.cpp#L691C9-L691C9 */
}

void AudioServer::set_sample32(void *p_buffer, uint32_t p_index, int32_t p_sample) {
     int32_t* buffer = static_cast<int32_t*>(p_buffer);
     buffer[p_index] = p_sample;
}

void AudioServer::set_sample_float(void *p_buffer, uint32_t p_index, int32_t p_sample) {
     float* buffer = static_cast<float*>(p_buffer);
     buffer[p_index] = p_sample * (2 ^ -32);
}

Through, this might be optimized by the compiler ¯_(ツ)_/¯. The solution might seem overcomplicated, but it's all because of the 24-bit type, that's the reason why the index and buffer is passed to the function, cause c++ doesn't have built-in 24-bit type and the way the value is set to the buffer is weird. Maybe it would be better to create a class that would represent the array and support 24-bit type.

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

You can always create buffer with int32_t type, but for this the memory should be allocated.

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

Can't be addon.

@Calinou
Copy link
Member

Calinou commented Jan 4, 2024

@kus04e4ek
Copy link
Author

Should probably mention that AudioDriverWASAPI already uses a very similar approach (which I didn't understand when I was writing the proposal ._.)

@kus04e4ek
Copy link
Author

Will close it if #8815 will be received well.

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

Successfully merging a pull request may close this issue.

2 participants