Added remaining API functions and preset change callback.

This commit is contained in:
Kai Blaschke
2022-11-17 16:34:41 +01:00
parent 5fe7e8c3aa
commit 94ee2a360b
4 changed files with 67 additions and 6 deletions

View File

@ -150,6 +150,15 @@ PROJECTM_EXPORT projectm_settings* projectm_alloc_settings();
*/
PROJECTM_EXPORT void projectm_free_settings(const projectm_settings* settings);
/**
* @brief Callback function that is executed whenever projectM wants to switch to a new preset.
*
* @param is_hard_cut If true, the transition was triggered by a beat-driven event.
* @param user_data A user-defined data pointer that was provided when registering the callback,
* e.g. context information.
*/
typedef void (*projectm_preset_switch_requested_event)(bool is_hard_cut, void* user_data);
/**
* @brief Callback function that is executed if a preset change failed.
*
@ -228,6 +237,20 @@ PROJECTM_EXPORT void projectm_load_preset_file(projectm_handle instance, const c
PROJECTM_EXPORT void projectm_load_preset_data(projectm_handle instance, const char* data,
bool smooth_transition);
/**
* @brief Sets a callback function that will be called when a preset change is requested.
*
* Only one callback can be registered per projectM instance. To remove the callback, use NULL.
*
* @param instance The projectM instance handle.
* @param callback A pointer to the callback function.
* @param user_data A pointer to any data that will be sent back in the callback, e.g. context
* information.
*/
PROJECTM_EXPORT void projectm_set_preset_switch_requested_event_callback(projectm_handle instance,
projectm_preset_switch_requested_event callback,
void* user_data);
/**
* @brief Sets a callback function that will be called when a preset change failed.
*
@ -235,7 +258,8 @@ PROJECTM_EXPORT void projectm_load_preset_data(projectm_handle instance, const c
*
* @param instance The projectM instance handle.
* @param callback A pointer to the callback function.
* @param user_data A pointer to any data that will be sent back in the callback, e.g. context information.
* @param user_data A pointer to any data that will be sent back in the callback, e.g. context
* information.
*/
PROJECTM_EXPORT void projectm_set_preset_switch_failed_event_callback(projectm_handle instance,
projectm_preset_switch_failed_event callback,

View File

@ -266,13 +266,13 @@ auto ProjectM::RenderFrameOnlyPass1(Pipeline* pipeline) -> Pipeline*
//if preset is done and we're not already switching
if (m_timeKeeper->PresetProgressA() >= 1.0 && !m_timeKeeper->IsSmoothing())
{
// Call preset change callback
PresetSwitchRequestedEvent(false);
}
else if (Settings().hardCutEnabled &&
(m_beatDetect->vol - m_beatDetect->volOld > Settings().hardCutSensitivity) &&
m_timeKeeper->CanHardCut())
{
// Call preset change callback
PresetSwitchRequestedEvent(true);
}
m_presetChangeNotified = true;
}

View File

@ -4,6 +4,7 @@
#include <cstring>
#include <utility>
#include <sstream>
projectMWrapper::projectMWrapper(std::string configFile)
: ProjectM(std::move(configFile))
@ -15,6 +16,14 @@ projectMWrapper::projectMWrapper(class ProjectM::Settings settings)
{
}
void projectMWrapper::PresetSwitchRequestedEvent(bool isHardCut) const
{
if (_presetSwitchRequestedEventCallback)
{
_presetSwitchRequestedEventCallback(isHardCut, _presetSwitchRequestedEventUserData);
}
}
void projectMWrapper::PresetSwitchFailedEvent(const std::string& presetFilename,
const std::string& failureMessage) const
{
@ -130,6 +139,29 @@ void projectm_destroy(projectm_handle instance)
delete projectMInstance;
}
void projectm_load_preset_file(projectm_handle instance, const char* filename,
bool smooth_transition)
{
auto projectMInstance = handle_to_instance(instance);
projectMInstance->LoadPresetFile(filename, smooth_transition);
}
void projectm_load_preset_data(projectm_handle instance, const char* data,
bool smooth_transition)
{
std::stringstream presetDataStream(data);
auto projectMInstance = handle_to_instance(instance);
projectMInstance->LoadPresetData(presetDataStream, smooth_transition);
}
void projectm_set_preset_switch_requested_event_callback(projectm_handle instance,
projectm_preset_switch_requested_event callback, void* user_data)
{
auto projectMInstance = handle_to_instance(instance);
projectMInstance->_presetSwitchRequestedEventCallback = callback;
projectMInstance->_presetSwitchRequestedEventUserData = user_data;
}
void projectm_set_preset_switch_failed_event_callback(projectm_handle instance,
projectm_preset_switch_failed_event callback, void* user_data)
{
@ -427,10 +459,12 @@ static auto PcmAdd(projectm_handle instance, const BufferType* samples, unsigned
{
auto* projectMInstance = handle_to_instance(instance);
if(channels == PROJECTM_MONO) {
if (channels == PROJECTM_MONO)
{
projectMInstance->Pcm().AddMono(samples, count);
}
else {
else
{
projectMInstance->Pcm().AddStereo(samples, count);
}
}

View File

@ -34,8 +34,11 @@ public:
void PresetSwitchFailedEvent(const std::string& presetFilename,
const std::string& failureMessage) const override;
void PresetSwitchRequestedEvent(bool isHardCut) const override;
projectm_preset_switch_failed_event _presetSwitchFailedEventCallback{ nullptr };
void* _presetSwitchFailedEventUserData{ nullptr };
projectm_preset_switch_requested_event _presetSwitchRequestedEventCallback{ nullptr };
void* _presetSwitchRequestedEventUserData{ nullptr };
};