Skip to content

Allow overriding protected methods on sfMusic and sfSoundStream #397

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- name: Get CMake and Ninja
uses: lukka/get-cmake@latest
with:
cmakeVersion: ${{ runner.os == 'Windows' && '3.25' || '3.22' }}
ninjaVersion: latest

- name: Install Linux Dependencies
Expand Down
51 changes: 51 additions & 0 deletions include/CSFML/Audio/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <CSFML/Audio/SoundChannel.h>
#include <CSFML/Audio/SoundSourceCone.h>
#include <CSFML/Audio/SoundStatus.h>
#include <CSFML/Audio/SoundStream.h>
#include <CSFML/Audio/Types.h>
#include <CSFML/System/InputStream.h>
#include <CSFML/System/Time.h>
Expand All @@ -41,6 +42,16 @@
#include <stddef.h>


typedef bool (*sfMusicOnGetDataOriginal)(sfMusic* music, sfSoundStreamChunk* data); ///< sf::Music::onGetData callback
typedef bool (*sfMusicOnGetDataMixin)(sfMusicOnGetDataOriginal originalImpl, sfMusic* music, sfSoundStreamChunk* data); ///< sf::Music::onGetData mixin

typedef void (*sfMusicOnSeekOriginal)(sfMusic* music, sfTime data); ///< sf::Music::onSeek callback
typedef void (*sfMusicOnSeekMixin)(sfMusicOnSeekOriginal originalImpl, sfMusic* music, sfTime timeOffset); ///< sf::Music::onSeek mixin

typedef bool (*sfMusicOnLoopOriginal)(sfMusic* music, uint64_t* position); ///< sf::Music::onLoop callback
typedef bool (*sfMusicOnLoopMixin)(sfMusicOnLoopOriginal originalImpl, sfMusic* music, uint64_t* position); ///< sf::Music::onLoop mixin


////////////////////////////////////////////////////////////
/// \brief Structure defining a time range
///
Expand All @@ -52,6 +63,46 @@ typedef struct
} sfTimeSpan;


////////////////////////////////////////////////////////////
/// \brief Override the behaviour of requesting a new chunk
/// of audio samples from the stream source
///
/// This function fills the chunk from the next samples
/// to read from the audio file.
///
/// \param music Music object
/// \param mixin Method override to set, a null override restores the original behaviour
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API void sfMusic_setOnGetData(sfMusic* music, sfMusicOnGetDataMixin mixin);


////////////////////////////////////////////////////////////
/// \brief Override the behaviour of changing the current
/// playing position in the stream source
///
/// \param music Music object
/// \param mixin Method override to set, a null override restores the original behaviour
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API void sfMusic_setOnSeek(sfMusic* music, sfMusicOnSeekMixin mixin);


////////////////////////////////////////////////////////////
/// \brief Override the behaviour of changing the current
/// playing position in the stream source to the loop offset
///
/// This is called by the underlying `SoundStream` whenever it needs us to reset
/// the seek position for a loop. We then determine whether we are looping on a
/// loop point or the end-of-file, perform the seek, and return the new position.
///
/// \param music Music object
/// \param mixin Method override to set, a null override restores the original behaviour
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API void sfMusic_setOnLoop(sfMusic* music, sfMusicOnLoopMixin mixin);


////////////////////////////////////////////////////////////
/// \brief Create a new music and load it from a file
///
Expand Down
22 changes: 20 additions & 2 deletions include/CSFML/Audio/SoundStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,38 @@
#include <stddef.h>


typedef bool (*sfSoundStreamOnLoopOriginal)(sfSoundStream* soundStream, uint64_t* position); ///< sf::SoundStream::onLoop callback
typedef bool (*sfSoundStreamOnLoopMixin)(sfSoundStreamOnLoopOriginal, sfSoundStream* soundStream, uint64_t* position); ///< sf::SoundStream::onLoop mixin


////////////////////////////////////////////////////////////
/// \brief defines the data to fill by the OnGetData callback
///
////////////////////////////////////////////////////////////
typedef struct
{
int16_t* samples; ///< Pointer to the audio samples
unsigned int sampleCount; ///< Number of samples pointed by Samples
const int16_t* samples; ///< Pointer to the audio samples
size_t sampleCount; ///< Number of samples pointed by Samples
} sfSoundStreamChunk;

typedef bool (*sfSoundStreamGetDataCallback)(sfSoundStreamChunk*, void*); ///< Type of the callback used to get a sound stream data
typedef void (*sfSoundStreamSeekCallback)(sfTime, void*); ///< Type of the callback used to seek in a sound stream


////////////////////////////////////////////////////////////
/// \brief Override the behaviour of changing the current
/// playing position in the stream source to the loop offset
///
/// This function can be overridden to allow implementation of
/// custom loop points. Otherwise, it just calls `onSeek(Time::Zero)` and returns 0.
///
/// \param soundStream SoundStream object
/// \param mixin Method override to set, a null override restores the original behaviour
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API void sfSoundStream_setOnLoop(sfSoundStream* soundStream, sfSoundStreamOnLoopMixin mixin);


////////////////////////////////////////////////////////////
/// \brief Create a new sound stream
///
Expand Down
2 changes: 2 additions & 0 deletions src/CSFML/Audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(SRC
${INCROOT}/Listener.h
${SRCROOT}/Music.cpp
${SRCROOT}/MusicStruct.hpp
${SRCROOT}/MusicStruct.cpp
${INCROOT}/Music.h
${SRCROOT}/Sound.cpp
${SRCROOT}/SoundStruct.hpp
Expand All @@ -27,6 +28,7 @@ set(SRC
${INCROOT}/SoundSourceCone.h
${INCROOT}/SoundStatus.h
${SRCROOT}/SoundStream.cpp
${SRCROOT}/SoundStreamStruct.cpp
${SRCROOT}/SoundStreamStruct.hpp
${INCROOT}/SoundStream.h
${INCROOT}/Types.h
Expand Down
22 changes: 22 additions & 0 deletions src/CSFML/Audio/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@

#include <cstring>


////////////////////////////////////////////////////////////
void sfMusic_setOnGetData(sfMusic* music, sfMusicOnGetDataMixin mixin)
{
music->OnGetDataMixin = mixin;
}


////////////////////////////////////////////////////////////
void sfMusic_setOnSeek(sfMusic* music, sfMusicOnSeekMixin mixin)
{
music->OnSeekMixin = mixin;
}


////////////////////////////////////////////////////////////
void sfMusic_setOnLoop(sfMusic* music, sfMusicOnLoopMixin mixin)
{
music->OnLoopMixin = mixin;
}


////////////////////////////////////////////////////////////
sfMusic* sfMusic_createFromFile(const char* filename)
{
Expand Down
111 changes: 111 additions & 0 deletions src/CSFML/Audio/MusicStruct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila ([email protected])
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <CSFML/Audio/MusicStruct.hpp>


////////////////////////////////////////////////////////////
bool sfMusic::onGetDataOriginal(sfMusic* music, sfSoundStreamChunk* data)
{
sf::SoundStream::Chunk cppData;

cppData.samples = data->samples;
cppData.sampleCount = data->sampleCount;

bool result = music->sf::Music::onGetData(cppData);

data->samples = cppData.samples;
data->sampleCount = cppData.sampleCount;

return result;
}


////////////////////////////////////////////////////////////
bool sfMusic::onGetData(sf::SoundStream::Chunk& data)
{
if (!OnGetDataMixin)
return sf::Music::onGetData(data);

sfSoundStreamChunk cData;

cData.samples = data.samples;
cData.sampleCount = data.sampleCount;

bool result = OnGetDataMixin(onGetDataOriginal, this, &cData);

data.samples = cData.samples;
data.sampleCount = cData.sampleCount;

return result;
}


////////////////////////////////////////////////////////////
void sfMusic::onSeekOriginal(sfMusic* music, sfTime timeOffset)
{
return music->sf::Music::onSeek(sf::microseconds(timeOffset.microseconds));
}


////////////////////////////////////////////////////////////
void sfMusic::onSeek(sf::Time timeOffset)
{
if (!OnSeekMixin)
return sf::Music::onSeek(timeOffset);

return OnSeekMixin(onSeekOriginal, this, {timeOffset.asMicroseconds()});
}


////////////////////////////////////////////////////////////
bool sfMusic::onLoopOriginal(sfMusic* music, uint64_t* position)
{
auto result = music->sf::Music::onLoop();

if (!result)
{
return false;
}

*position = *result;
return true;
}


////////////////////////////////////////////////////////////
std::optional<std::uint64_t> sfMusic::onLoop()
{
if (!OnLoopMixin)
return sf::Music::onLoop();

std::uint64_t data = 0;

bool noLoop = OnLoopMixin(onLoopOriginal, this, &data);

return noLoop ? std::nullopt : std::optional(data);
}
13 changes: 13 additions & 0 deletions src/CSFML/Audio/MusicStruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <CSFML/Audio/Music.h>
#include <CSFML/Audio/SoundChannel.h>
#include <CSFML/CallbackStream.hpp>

Expand All @@ -42,4 +43,16 @@ struct sfMusic : sf::Music
{
mutable std::vector<sfSoundChannel> Channels;
CallbackStream Stream;

sfMusicOnGetDataMixin OnGetDataMixin = nullptr;
static bool onGetDataOriginal(sfMusic* music, sfSoundStreamChunk* data);
bool onGetData(Chunk& data) override;

sfMusicOnSeekMixin OnSeekMixin = nullptr;
static void onSeekOriginal(sfMusic* music, sfTime timeOffset);
void onSeek(sf::Time timeOffset) override;

sfMusicOnLoopMixin OnLoopMixin = nullptr;
static bool onLoopOriginal(sfMusic* music, uint64_t* position);
std::optional<std::uint64_t> onLoop() override;
};
7 changes: 7 additions & 0 deletions src/CSFML/Audio/SoundStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
#include <cstring>


////////////////////////////////////////////////////////////
void sfSoundStream_setOnLoop(sfSoundStream* soundStream, sfSoundStreamOnLoopMixin mixin)
{
soundStream->OnLoopMixin = mixin;
}


////////////////////////////////////////////////////////////
sfSoundStream* sfSoundStream_create(
sfSoundStreamGetDataCallback onGetData,
Expand Down
53 changes: 53 additions & 0 deletions src/CSFML/Audio/SoundStreamStruct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila ([email protected])
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <CSFML/Audio/SoundStreamStruct.hpp>

bool sfSoundStream::onLoopOriginal(sfSoundStream* music, uint64_t* position)
{
auto result = music->sf::SoundStream::onLoop();

if (!result)
{
return false;
}

*position = *result;
return true;
}

std::optional<std::uint64_t> sfSoundStream::onLoop()
{
if (!OnLoopMixin)
return sf::SoundStream::onLoop();

std::uint64_t data = 0;

bool noLoop = OnLoopMixin(onLoopOriginal, this, &data);

return noLoop ? std::nullopt : std::optional(data);
}
6 changes: 5 additions & 1 deletion src/CSFML/Audio/SoundStreamStruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <CSFML/Audio/SoundChannel.h>
#include <CSFML/Audio/SoundStream.h>

#include <SFML/Audio/SoundStream.hpp>

Expand Down Expand Up @@ -57,7 +58,6 @@ struct sfSoundStream : sf::SoundStream

mutable std::vector<sfSoundChannel> Channels;

private:
bool onGetData(Chunk& data) override
{
sfSoundStreamChunk chunk = {nullptr, 0};
Expand All @@ -78,6 +78,10 @@ struct sfSoundStream : sf::SoundStream
}
}

sfSoundStreamOnLoopMixin OnLoopMixin = nullptr;
static bool onLoopOriginal(sfSoundStream* music, uint64_t* position);
std::optional<std::uint64_t> onLoop() override;

sfSoundStreamGetDataCallback myGetDataCallback;
sfSoundStreamSeekCallback mySeekCallback;
void* myUserData;
Expand Down
Loading