Skip to content

Commit

Permalink
"Directly" bind sf::SoundStream::Chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
crumblingstatue committed Oct 17, 2024
1 parent 6657568 commit 0cdd8f0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
16 changes: 2 additions & 14 deletions CSFML/src/Audio/CustomSoundStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
#include <cstddef>
#include <cstdint>

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 {
Expand All @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions src/audio/sound_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct SoundStreamPlayer<'a, S: SoundStream + 'a> {
}

unsafe extern "C" fn get_data_callback<S: SoundStream>(
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();
Expand All @@ -52,10 +52,7 @@ unsafe extern "C" fn get_data_callback<S: SoundStream>(
}
};
(*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
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/ffi/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -47,9 +47,8 @@ type sfSoundRecorderProcessCallback =
Option<unsafe extern "C" fn(samples: *const i16, len: usize, user_data: *mut c_void) -> bool>;
type sfSoundRecorderStopCallback = Option<unsafe extern "C" fn(user_data: *mut c_void)>;

type sfCustomSoundStreamGetDataCallback = Option<
unsafe extern "C" fn(chunk: *mut sfCustomSoundStreamChunk, user_data: *mut c_void) -> bool,
>;
type sfCustomSoundStreamGetDataCallback =
Option<unsafe extern "C" fn(chunk: *mut sfSoundStreamChunk, user_data: *mut c_void) -> bool>;
type sfCustomSoundStreamSeekCallback =
Option<unsafe extern "C" fn(pos: i64, user_data: *mut c_void)>;

Expand Down

0 comments on commit 0cdd8f0

Please sign in to comment.