Description
Consider the following minimal implementation of AudioStream
and AudioStreamPlaybackResampled
:
use godot::classes::native::AudioFrame;
use godot::classes::{AudioStreamPlayback, IAudioStream, IAudioStreamPlaybackResampled};
use godot::prelude::*;
#[derive(GodotClass)]
#[class(init, base=AudioStream)]
struct Foo {}
#[godot_api]
impl IAudioStream for Foo {
fn instantiate_playback(&self) -> Option<Gd<AudioStreamPlayback>> {
Some(FooPlayback::new_gd().upcast())
}
}
#[derive(GodotClass)]
#[class(init, base=AudioStreamPlaybackResampled)]
struct FooPlayback {}
#[godot_api]
impl IAudioStreamPlaybackResampled for FooPlayback {
unsafe fn mix_resampled(&mut self, _dst_buffer: *mut AudioFrame, frame_count: i32) -> i32 {
godot_print!("mix_resampled");
frame_count
}
fn get_stream_sampling_rate(&self) -> f32 {
8000.0
}
unsafe fn mix(&mut self, _buffer: *mut AudioFrame, _rate_scale: f32, frames: i32) -> i32 {
godot_print!("mix");
frames
}
}
Include the above code in a godot-rust project, then open the corresponding Godot project.
Create a scene containing an AudioStreamPlayer
.
Attach a new instance of Foo
as the stream
property of the AudioStreamPlayer
in the inspector.
Tick Autoplay
in the inspector.
Run the scene.
Godot produces the following output:
mix_resampled
mix_resampled
mix_resampled
mix_resampled
mix_resampled
mix_resampled
...etc
No sound plays because the minimal implementation doesn't actually generate any sound, but if it did (in mix_resampled
) then that would be working as expected.
However, notice that IAudioStreamPlaybackResampled
requires an implementation of mix
but Godot never calls it. I assume Godot always calls its internal implementation of AudioStreamPlaybackResampled::mix
, which calls mix_resampled
and then resamples the sound for us.