Doly C++ SDK v1.00
Loading...
Searching...
No Matches
SoundControl/main.cpp

Demonstrates:

  • Initializing SoundControl
  • Playing sound files
  • Handling sound-related events
#include <chrono>
#include <thread>
#include <spdlog/spdlog.h>
#include "SoundControl.h"
#include "Helper.h"
void onSoundBegin(uint16_t id, float volume)
{
spdlog::info("Sound begin id:{}, volume:{:.2f}", id, volume);
}
void onSoundComplete(uint16_t id)
{
spdlog::info("Sound complete id:{}", id);
}
void onSoundAbort(uint16_t id)
{
spdlog::info("Sound aborted id:{}", id);
}
void onSoundError(uint16_t id)
{
spdlog::error("Sound error id:{}", id);
}
int main()
{
// Setup spdlog
spdlog::set_level(spdlog::level::info); // Set as needed
spdlog::flush_on(spdlog::level::trace); // flush everything
// *** IMPORTANT ***
// Stop doly service if running,
// otherwise instance of libraries cause conflict
spdlog::error("Doly service stop failed");
return -1;
}
// Initialize Sound Control
if (SoundControl::init() < 0)
{
spdlog::error("SoundControl init failed");
return -2;
}
// get SoundControl version
spdlog::info("SoundControl Version:{:.3f}", SoundControl::getVersion());
// Add event listeners
// Set volume
SoundControl::setVolume(80); // set volume to 80%
// Play sound
int ret = SoundControl::play("../sound_test.wav", 1);
if (ret < 0)
spdlog::error("Play failed, error code:{}", ret);
// Wait for sound to complete
while (ret == 0 && SoundControl::getState() != SoundState::STOP)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Cleanup
return 0;
}
Shared helper utilities and common types used across the Doly SDK.
Public API for Doly sound playback control.
@ STOP
Definition SoundControl.h:31
int8_t stopDolyService()
Stop the background Doly service (if running).
int8_t setVolume(uint8_t volume)
Set playback volume as a percentage.
SoundState getState()
Get current sound playback state.
float getVersion()
Get current library version.
int8_t dispose()
Dispose/stop the sound subsystem and release resources.
int8_t init()
Initialize sound control.
int8_t play(std::string file_name, uint16_t id)
Start playing a sound file (non-blocking).
void AddListenerOnError(void(*onAbort)(uint16_t id))
Register a static/free function to receive error events.
void AddListenerOnAbort(void(*onAbort)(uint16_t id))
Register a static/free function to receive abort events.
void RemoveListenerOnError(void(*onAbort)(uint16_t id))
Unregister a previously registered error callback.
void AddListenerOnComplete(void(*onComplete)(uint16_t id))
Register a static/free function to receive completion events.
void AddListenerOnBegin(void(*onComplete)(uint16_t id, float volume))
Register a static/free function to receive begin events.
void RemoveListenerOnBegin(void(*onComplete)(uint16_t id, float volume))
Unregister a previously registered begin callback.
void RemoveListenerOnComplete(void(*onComplete)(uint16_t id))
Unregister a previously registered completion callback.
void RemoveListenerOnAbort(void(*onAbort)(uint16_t id))
Unregister a previously registered abort callback.