From 0cdd8f0c71c7183f00829431b68a3c72890cce0d Mon Sep 17 00:00:00 2001 From: crumblingstatue Date: Fri, 18 Oct 2024 01:09:26 +0200 Subject: [PATCH] "Directly" bind sf::SoundStream::Chunk --- CSFML/src/Audio/CustomSoundStream.cpp | 16 ++-------------- src/audio/sound_stream.rs | 7 ++----- src/ffi/audio.rs | 9 ++++----- 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/CSFML/src/Audio/CustomSoundStream.cpp b/CSFML/src/Audio/CustomSoundStream.cpp index f621b85f..393340f0 100644 --- a/CSFML/src/Audio/CustomSoundStream.cpp +++ b/CSFML/src/Audio/CustomSoundStream.cpp @@ -3,13 +3,7 @@ #include #include -typedef struct -{ - int16_t const *samples; ///< Pointer to the audio samples - unsigned int sampleCount; ///< Number of samples pointed by Samples -} sfCustomSoundStreamChunk; - -typedef bool (*sfCustomSoundStreamGetDataCallback)(sfCustomSoundStreamChunk *, void *); +typedef bool (*sfCustomSoundStreamGetDataCallback)(sf::SoundStream::Chunk *, void *); typedef void (*sfCustomSoundStreamSeekCallback)(int64_t, void *); class sfCustomSoundStream : public sf::SoundStream { @@ -26,13 +20,7 @@ class sfCustomSoundStream : public sf::SoundStream { private: virtual bool onGetData(Chunk &data) { - sfCustomSoundStreamChunk chunk = {NULL, 0}; - bool ok = (myGetDataCallback(&chunk, myUserData)); - - data.samples = chunk.samples; - data.sampleCount = chunk.sampleCount; - - return ok; + return (myGetDataCallback(&data, myUserData)); } virtual void onSeek(sf::Time timeOffset) { diff --git a/src/audio/sound_stream.rs b/src/audio/sound_stream.rs index 1c25f290..2d148f7e 100644 --- a/src/audio/sound_stream.rs +++ b/src/audio/sound_stream.rs @@ -38,7 +38,7 @@ pub struct SoundStreamPlayer<'a, S: SoundStream + 'a> { } unsafe extern "C" fn get_data_callback( - chunk: *mut crate::ffi::audio::sfCustomSoundStreamChunk, + chunk: *mut crate::ffi::audio::sfSoundStreamChunk, user_data: *mut c_void, ) -> bool { let stream: *mut S = user_data.cast(); @@ -52,10 +52,7 @@ unsafe extern "C" fn get_data_callback( } }; (*chunk).samples = data.as_ptr(); - (*chunk).sample_count = data - .len() - .try_into() - .expect("Overflow casting data length to sample count"); + (*chunk).sample_count = data.len(); keep_playing } } diff --git a/src/ffi/audio.rs b/src/ffi/audio.rs index 97e39fa6..ed1c3392 100644 --- a/src/ffi/audio.rs +++ b/src/ffi/audio.rs @@ -15,9 +15,9 @@ decl_opaque! { pub type sfSoundBuffer = crate::audio::SoundBuffer; #[repr(C)] -pub struct sfCustomSoundStreamChunk { +pub struct sfSoundStreamChunk { pub samples: *const i16, - pub sample_count: c_uint, + pub sample_count: usize, } #[repr(C)] @@ -47,9 +47,8 @@ type sfSoundRecorderProcessCallback = Option bool>; type sfSoundRecorderStopCallback = Option; -type sfCustomSoundStreamGetDataCallback = Option< - unsafe extern "C" fn(chunk: *mut sfCustomSoundStreamChunk, user_data: *mut c_void) -> bool, ->; +type sfCustomSoundStreamGetDataCallback = + Option bool>; type sfCustomSoundStreamSeekCallback = Option;