SOUND4 IMPACT.CL Library [1.3.17]
Loading...
Searching...
No Matches
sound4cl.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <string>
11#include <exception>
12#include <thread>
13#include <functional>
14#include <memory>
15#include <array>
16#include <assert.h>
17#include <stdexcept>
18#include <filesystem>
19#include <string.h>
20
21#if defined(__unix__) || defined(__APPLE__)
22 #define UNIXLIKE 1
23#else
24 #define UNIXLIKE 0
25#endif
26
27#if UNIXLIKE
28 #include <dlfcn.h>
29#elif defined(_WIN32)
30 #include <libloaderapi.h>
31 #ifdef _MSC_VER
32 #pragma warning(disable : 4996)
33 #endif
34#else
35 #error "Unsupported OS"
36#endif
37
38#include "sound4cl_cdef.h"
39
40 // bridge C callbacks to CPresetLoader
41 extern "C" {
42 static char *sound4cl_custom_reader(const fs_char *filename, void* userdata);
43 static void sound4cl_custom_reader_free(char *content, void* userdata);
44 static int sound4cl_custom_writer(const fs_char *filename, const char *content, void* userdata);
45 static int sound4cl_custom_exists(const fs_char *filename, void* userdata);
46 static fs_char** sound4cl_custom_getall(void* userdata);
47 static void sound4cl_custom_getall_free(fs_char** all, void* userdata);
48 static int sound4cl_custom_remove(const fs_char *filename, void* userdata);
49 static int sound4cl_custom_rename(const fs_char *from, const fs_char *to, void* userdata);
50 };
51
55namespace sound4 {
56
75
79 namespace helper {
80 #ifdef _WIN32
86 static inline std::string WStringToUTF8(const std::wstring& wstr) {
87 if (wstr.empty()) return {};
88 int size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
89 std::string str(size, 0);
90 WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &str[0], size, NULL, NULL);
91 return str;
92 }
98 static inline std::wstring UTF8ToWString(const std::string& str) {
99 if (str.empty()) return std::wstring();
100 int size = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
101 std::wstring wstr(size, 0);
102 MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstr[0], size);
103 return wstr;
104 }
105 #endif // _WIN32
106
112 template<typename T>
114 };
115 template<>
116 struct SampleFormat<int16_t> {
117 const sound4::SampleFormat format = S16_NATIVE;
118 };
119 template<>
120 struct SampleFormat<int32_t> {
121 const sound4::SampleFormat format = S32_NATIVE;
122 };
123 template<>
124 struct SampleFormat<float> {
125 const sound4::SampleFormat format = F32_NATIVE;
126 };
127
133 template <typename T>
135 // Storage definition, depending on OS
136 #if UNIXLIKE
137 using DynFunc_t = void *;
138 #else
139 using DynFunc_t = FARPROC;
140 #endif
141 private:
142 DynFunc_t m_ptr = NULL;
143 public:
144 DynFuncHolder() = default;
146 operator bool() const { return (m_ptr!=NULL); }
147 bool IsOk() const { return m_ptr!=NULL; }
148 operator T* () const { return reinterpret_cast<T*>(m_ptr); }
149 };
150
156 // Storage definition, depending on OS
157 #if UNIXLIKE
158 using DynLib_t = void *;
159 #else
160 using DynLib_t = HMODULE;
161 #endif
162 private:
163 DynLib_t m_lib {};
164 public:
165 CDynLoader() = default;
166 CDynLoader(CDynLoader&& other) noexcept : m_lib(other.m_lib) { other.m_lib = NULL; }
168 if (this != &other) { Close(); m_lib = other.m_lib; other.m_lib = NULL; }
169 return *this;
170 }
171 // No copy allowed
172 CDynLoader(const CDynLoader&) = delete;
173 CDynLoader& operator=(CDynLoader const&) = delete;
174 operator bool() const {
175 return (m_lib!=NULL);
176 }
177 bool IsOk() const {
178 return (m_lib!=NULL);
179 }
180 #if UNIXLIKE
181 void Close() {
182 if (IsOk()) {
183 #ifndef __SANITIZE_ADDRESS__ // avoid unloading to keep call stack
184 dlclose(m_lib);
185 #endif // __SANITIZE_ADDRESS__
186 m_lib=NULL;
187 }
188 }
189 bool Load(const std::filesystem::path& dynlib)
190 {
191 Close();
192 m_lib=dlopen(dynlib.c_str(), RTLD_NOW|RTLD_LOCAL);
193 return IsOk();
194 }
195 template <typename T>
196 DynFuncHolder<T> GetSymbol(const std::string& name) {
197 auto ptr=dlsym(m_lib, name.c_str());
198 if (!ptr) {
199 throw std::runtime_error("Missing function in library");
200 }
201 return DynFuncHolder<T>(ptr);
202 }
203 template <typename T>
204 DynFuncHolder<T> GetSymbol_safe(const std::string& name) {
205 auto ptr=dlsym(m_lib, name.c_str());
206 return DynFuncHolder<T>(ptr);
207 }
208 static std::filesystem::path GetThisLibraryPath(void) {
209 static Dl_info info;
210 if (! dladdr((void *) GetThisLibraryPath, & info)) return {};
211 auto rp=realpath(info.dli_fname, NULL);
212 if (!rp) {
213 #if defined(__unix__)
214 rp=realpath("/proc/self/exe", NULL);
215 #elif defined(__APPLE__)
216 // How to solve this for Apple ?
217 #endif
218 }
219 std::filesystem::path p(rp);
220 free(rp);
221 return p;
222 }
223 #else
230 void Close() {
231 if (IsOk()) {
232 // NOTE: On Windows, FreeLibrary may create a lot of troubles, should use FreeLibraryAndExitThread but...
233 // So define SOUND4_CALL_FREELIBRARY before including this file if you really want to unload the library dynamically
234 #if defined(SOUND4_CALL_FREELIBRARYANDEXITTHREAD)
235 FreeLibraryAndExitThread(m_lib, 0);
236 #elif defined(SOUND4_CALL_FREELIBRARY)
237 FreeLibrary(m_lib);
238 #endif // SOUND4_CALL_FREELIBRARY
239 m_lib=NULL;
240 }
241 }
242 template <typename T>
243 DynFuncHolder<T> GetSymbol(const std::string& name) {
244 auto ptr=GetProcAddress(m_lib, name.c_str());
245 if (!ptr) {
246 throw std::runtime_error("Missing function in library");
247 }
248 return DynFuncHolder<T>(ptr);
249 }
250 template <typename T>
251 DynFuncHolder<T> GetSymbol_safe(const std::string& name) {
252 auto ptr=GetProcAddress(m_lib, name.c_str());
253 return DynFuncHolder<T>(ptr);
254 }
255 static std::filesystem::path GetThisLibraryPath(void) {
256 static wchar_t path[MAX_PATH]={};
257 HMODULE hm = NULL;
261 {
262 if (GetModuleFileNameW(hm, path, MAX_PATH) > 0) {
263 return std::filesystem::path(path);
264 }
265 }
266 return {};
267 }
268 #endif
269 virtual ~CDynLoader() {
270 Close();
271 }
272 }; // class CDynLoader
273 }; // namespace helper
274
275
280 none = sound4cl_none,
281 fatal = sound4cl_fatal,
282 error = sound4cl_error,
283 warning = sound4cl_warning,
284 info = sound4cl_info,
285 verbose = sound4cl_verbose,
286 verbose2 = sound4cl_verbose2,
287 verbose3 = sound4cl_verbose3,
288 verbose4 = sound4cl_verbose4,
289 verbose5 = sound4cl_verbose5,
290 };
291
295 using log_cb_t = std::function<void(LogSeverity,const std::string&)>;
296
301 extern "C" {
302 static inline void _log_cb_c(sound4cl_LogSeverity severity, const char *c_msg) {
303 _log_cb((LogSeverity)severity, std::string(c_msg));
304 }
305 }
306
314 private:
315 helper::CDynLoader m_lib;
316 bool m_bOK = false;
317 std::string m_prefix; // The prefix of functions, depends on the library (cloud_, impact_...)
318 protected:
319 friend class CBus;
320 friend class CInstance;
321 friend class CAudioConverter;
322
323 // All those wrapped functions have the same signature as there C sound4cl_XXX
324 helper::DynFuncHolder< decltype(sound4cl_GetVersion ) > fnGetVersion ;
325 helper::DynFuncHolder< decltype(sound4cl_GetChunkSizeInFrames ) > fnGetChunkSizeInFrames ;
326 helper::DynFuncHolder< decltype(sound4cl_GetChannelCount ) > fnGetChannelCount ;
327 helper::DynFuncHolder< decltype(sound4cl_GetAudioInputCount ) > fnGetAudioInputCount ;
328 helper::DynFuncHolder< decltype(sound4cl_GetAudioOutputCount ) > fnGetAudioOutputCount ;
329 helper::DynFuncHolder< decltype(sound4cl_GetSampleRate ) > fnGetSampleRate ;
330 helper::DynFuncHolder< decltype(sound4cl_SetLoggerCallback ) > fnSetLoggerCallback ;
331 helper::DynFuncHolder< decltype(sound4cl_SetLogSeverity ) > fnSetLogSeverity ;
332 helper::DynFuncHolder< decltype(sound4cl_NewParameters ) > fnNewParameters ;
333 helper::DynFuncHolder< decltype(sound4cl_FreeParameters ) > fnFreeParameters ;
334 helper::DynFuncHolder< decltype(sound4cl_SetParameter ) > fnSetParameter ;
335 helper::DynFuncHolder< decltype(sound4cl_GetParameter ) > fnGetParameter ;
336 helper::DynFuncHolder< decltype(sound4cl_FreeParameterValue ) > fnFreeParameterValue ;
337 helper::DynFuncHolder< decltype(sound4cl_InitProcess ) > fnInitProcess ;
338 helper::DynFuncHolder< decltype(sound4cl_InitProcess2 ) > fnInitProcess2 ;
339 helper::DynFuncHolder< decltype(sound4cl_TerminateProcess ) > fnTerminateProcess ;
340 helper::DynFuncHolder< decltype(sound4cl_ExitProcess ) > fnExitProcess ;
341 helper::DynFuncHolder< decltype(sound4cl_StartUpdateThread ) > fnStartUpdateThread ;
342 helper::DynFuncHolder< decltype(sound4cl_StopUpdateThread ) > fnStopUpdateThread ;
343 helper::DynFuncHolder< decltype(sound4cl_WaitUpdateThreadReady ) > fnWaitUpdateThreadReady ;
344 helper::DynFuncHolder< decltype(sound4cl_ProcessAudio ) > fnProcessAudio ;
345 helper::DynFuncHolder< decltype(sound4cl_ProcessAudio_Planar ) > fnProcessAudio_Planar ;
346 helper::DynFuncHolder< decltype(sound4cl_GetBufferIn ) > fnGetBufferIn ;
347 helper::DynFuncHolder< decltype(sound4cl_GetBufferOut ) > fnGetBufferOut ;
348 helper::DynFuncHolder< decltype(sound4cl_GetEstimatedDelay ) > fnGetEstimatedDelay ;
349 helper::DynFuncHolder< decltype(sound4cl_GetFormatName ) > fnGetFormatName ;
350 helper::DynFuncHolder< decltype(sound4cl_GetFormatFromName ) > fnGetFormatFromName ;
351 helper::DynFuncHolder< decltype(sound4cl_GetBytesFromFormat ) > fnGetBytesFromFormat ;
352 helper::DynFuncHolder< decltype(sound4cl_GetMaxPacketFrame ) > fnGetMaxPacketFrame ;
353 helper::DynFuncHolder< decltype(sound4cl_AddAudio ) > fnAddAudio ;
354 helper::DynFuncHolder< decltype(sound4cl_AddPadAudio ) > fnAddPadAudio ;
355 helper::DynFuncHolder< decltype(sound4cl_GetOutputCount ) > fnGetOutputCount ;
356 helper::DynFuncHolder< decltype(sound4cl_GetAudio ) > fnGetAudio ;
357 helper::DynFuncHolder< decltype(sound4cl_AudioConvertFrom ) > fnAudioConvertFrom ;
358 helper::DynFuncHolder< decltype(sound4cl_AudioConvertTo ) > fnAudioConvertTo ;
359 helper::DynFuncHolder< decltype(sound4cl_StereoToMono ) > fnStereoToMono ;
360 helper::DynFuncHolder< decltype(sound4cl_MonoToStereo ) > fnMonoToStereo ;
361 helper::DynFuncHolder< decltype(sound4cl_AudioMonoFromLiveStereo) > fnAudioMonoFromLiveStereo;
362 helper::DynFuncHolder< decltype(sound4cl_AudioMonoToLiveStereo ) > fnAudioMonoToLiveStereo ;
363 helper::DynFuncHolder< decltype(sound4cl_NewClient ) > fnNewClient ;
364 helper::DynFuncHolder< decltype(sound4cl_DeleteClient ) > fnDeleteClient ;
365 helper::DynFuncHolder< decltype(sound4cl_ProcessJson ) > fnProcessJson ;
366 helper::DynFuncHolder< decltype(sound4cl_FreeJsonAnswer ) > fnFreeJsonAnswer ;
367 helper::DynFuncHolder< decltype(sound4cl_SaveState ) > fnSaveState ;
368
369 // only for processes supporting webserver
370 helper::DynFuncHolder< decltype(sound4cl_Webserver_tcp ) > fnWebserver_tcp ;
371 helper::DynFuncHolder< decltype(sound4cl_Webserver_tcp2 ) > fnWebserver_tcp2 ;
372 helper::DynFuncHolder< decltype(sound4cl_Webserver ) > fnWebserver ;
373 helper::DynFuncHolder< decltype(sound4cl_Webserver_Stop ) > fnWebserver_Stop ;
374 helper::DynFuncHolder< decltype(sound4cl_Webserver_Status ) > fnWebserver_Status ;
375
376 // added 2023-03-22
377 helper::DynFuncHolder< decltype(sound4cl_StereoToMono_Planar ) > fnStereoToMono_Planar ;
378 helper::DynFuncHolder< decltype(sound4cl_MonoToStereo_Planar ) > fnMonoToStereo_Planar ;
379
380 // added 2023-05-22
381 helper::DynFuncHolder< decltype(sound4cl_SetMetadata ) > fnSetMetadata ;
382 helper::DynFuncHolder< decltype(sound4cl_GetMetadataInfos ) > fnGetMetadataInfos ;
383 helper::DynFuncHolder< decltype(sound4cl_FreeMetadataInfos ) > fnFreeMetadataInfos ;
384
385 // added 2023-06-20
386 helper::DynFuncHolder< decltype(sound4cl_SetPresetManager ) > fnSetPresetManager ;
387 helper::DynFuncHolder< decltype(sound4cl_PresetManager_InformChange ) > fnPresetManager_InformChange ;
388
389 // added 2023-09-21
390 helper::DynFuncHolder< decltype(sound4cl_GetPossibleChunkSizeInFrames ) > fnGetPossibleChunkSizeInFrames ;
391 helper::DynFuncHolder< decltype(sound4cl_GetProcessChunkFrames ) > fnGetProcessChunkFrames ;
392 helper::DynFuncHolder< decltype(sound4cl_InitProcess3 ) > fnInitProcess3 ;
393
394 // added 2023-06-26, only for processes supporting bus
395 helper::DynFuncHolder< decltype(sound4cl_NewBus ) > fnNewBus ;
396 helper::DynFuncHolder< decltype(sound4cl_FreeBus ) > fnFreeBus ;
397 helper::DynFuncHolder< decltype(sound4cl_SetInstanceBus ) > fnSetInstanceBus ;
398
399 // added 2024-02-19
400 helper::DynFuncHolder< decltype(sound4cl_SetMetadataMulti ) > fnSetMetadataMulti ;
401
402 // added 2024-05-23
403 helper::DynFuncHolder< decltype(sound4cl_Webserver_SetAppHealth ) > fnWebserver_SetAppHealth ;
404 helper::DynFuncHolder< decltype(sound4cl_Webserver_GetAppHealth ) > fnWebserver_GetAppHealth ;
405 helper::DynFuncHolder< decltype(sound4cl_Webserver_FreeString ) > fnWebserver_FreeString ;
406
407#ifdef _WIN32
408 // added 2025-05-28
409 helper::DynFuncHolder< decltype(sound4cl_SetInstanceTracing ) > fnSetInstanceTracing ;
410 helper::DynFuncHolder< decltype(sound4cl_SetInstanceTracingProcessActivity ) > fnSetInstanceTracingProcessActivity ;
411#endif // _WIN32
412
413 // added 2026-02-17
414 helper::DynFuncHolder< decltype(sound4cl_GetAudioConverter ) > fnGetAudioConverter ;
415 helper::DynFuncHolder< decltype(sound4cl_FreeAudioConverter ) > fnFreeAudioConverter ;
416 helper::DynFuncHolder< decltype(sound4cl_AudioConverter_From ) > fnAudioConverter_From ;
417 helper::DynFuncHolder< decltype(sound4cl_AudioConverter_To ) > fnAudioConverter_To ;
418
419 template <typename T>
420 helper::DynFuncHolder<T> GetPrefixSymbol(const std::string& name) {
421 std::string prefix_name = m_prefix+name;
422 return m_lib.GetSymbol<T>(prefix_name);
423 }
424
425 public:
426 CProcessor() = default;
427
435 bool Load(const std::filesystem::path& filepath) {
436 m_lib.Close();
437 // If a path is given, use it directly and do not try other path
438 if (filepath.has_parent_path() && !m_lib.Load(filepath)) {
439 return false;
440 } else if (!m_lib.IsOk()) {
441 auto thisdir=helper::CDynLoader::GetThisLibraryPath().parent_path();
442 auto filename = filepath.filename();
443 #ifndef _WIN32
444 // Linux: if in a bin directory, try ../lib first
445 if (!m_lib.IsOk() && thisdir.filename()=="bin") {
446 auto libdir = thisdir.parent_path() / "lib";
447 m_lib.Load(libdir / filename);
448 }
449 #endif // !_WIN32
450 if (!m_lib.IsOk()) {
451 // Search in the same directory this code is in
452 m_lib.Load(thisdir / filename);
453 }
454 if (!m_lib.IsOk()) {
455 // Try current path
456 std::error_code ec;
457 auto p = std::filesystem::current_path(ec);
458 if (!ec) {
459 m_lib.Load(p / filename);
460 }
461 }
462 if (!m_lib.IsOk()) {
463 return false;
464 }
465 }
466 // Get information from the library itself
467 try {
468 auto GetInfo = m_lib.GetSymbol< decltype(sound4cl_SOUND4_GetProcessInfo ) >("SOUND4_GetProcessInfo" );
469 auto info = GetInfo();
470 m_prefix = info->prefix;
471 } catch (std::runtime_error& ) {
472 // Compatibility: search in known libraries without this function
473 #ifdef _WIN32
474 std::string filename = helper::WStringToUTF8(filepath.stem());
475 #else // !_WIN32
476 std::string filename = filepath.stem();
477 // remove lib prefix
478 filename=filename.substr(3);
479 #endif // ! _WIN32
480 if (filename=="sound4.x1.cloud") {
481 m_prefix="cloudx1_";
482 } else if (filename=="sound4.impact.cl") {
483 m_prefix="impact_";
484 } else if (filename=="sound4.bigvoice.cl") {
485 m_prefix="bigvoice_";
486 } else {
487 return false;
488 }
489 }
490
491 // Load all C functions
492 try {
493 fnGetVersion = GetPrefixSymbol< decltype(sound4cl_GetVersion ) >("GetVersion" );
494 fnGetChunkSizeInFrames = GetPrefixSymbol< decltype(sound4cl_GetChunkSizeInFrames ) >("GetChunkSizeInFrames" );
495 fnGetChannelCount = GetPrefixSymbol< decltype(sound4cl_GetChannelCount ) >("GetChannelCount" );
496 fnGetAudioInputCount = GetPrefixSymbol< decltype(sound4cl_GetAudioInputCount ) >("GetAudioInputCount" );
497 fnGetAudioOutputCount = GetPrefixSymbol< decltype(sound4cl_GetAudioOutputCount ) >("GetAudioOutputCount" );
498 fnGetSampleRate = GetPrefixSymbol< decltype(sound4cl_GetSampleRate ) >("GetSampleRate" );
499 fnSetLoggerCallback = GetPrefixSymbol< decltype(sound4cl_SetLoggerCallback ) >("SetLoggerCallback" );
500 fnSetLogSeverity = GetPrefixSymbol< decltype(sound4cl_SetLogSeverity ) >("SetLogSeverity" );
501 fnNewParameters = GetPrefixSymbol< decltype(sound4cl_NewParameters ) >("NewParameters" );
502 fnFreeParameters = GetPrefixSymbol< decltype(sound4cl_FreeParameters ) >("FreeParameters" );
503 fnSetParameter = GetPrefixSymbol< decltype(sound4cl_SetParameter ) >("SetParameter" );
504 fnGetParameter = GetPrefixSymbol< decltype(sound4cl_GetParameter ) >("GetParameter" );
505 fnFreeParameterValue = GetPrefixSymbol< decltype(sound4cl_FreeParameterValue ) >("FreeParameterValue" );
506 fnInitProcess = GetPrefixSymbol< decltype(sound4cl_InitProcess ) >("InitProcess" );
507 fnInitProcess2 = GetPrefixSymbol< decltype(sound4cl_InitProcess2 ) >("InitProcess2" );
508 fnTerminateProcess = GetPrefixSymbol< decltype(sound4cl_TerminateProcess ) >("TerminateProcess" );
509 fnExitProcess = GetPrefixSymbol< decltype(sound4cl_ExitProcess ) >("ExitProcess" );
510 fnStartUpdateThread = GetPrefixSymbol< decltype(sound4cl_StartUpdateThread ) >("StartUpdateThread" );
511 fnStopUpdateThread = GetPrefixSymbol< decltype(sound4cl_StopUpdateThread ) >("StopUpdateThread" );
512 fnWaitUpdateThreadReady = GetPrefixSymbol< decltype(sound4cl_WaitUpdateThreadReady ) >("WaitUpdateThreadReady" );
513 fnProcessAudio = GetPrefixSymbol< decltype(sound4cl_ProcessAudio ) >("ProcessAudio" );
514 fnProcessAudio_Planar = GetPrefixSymbol< decltype(sound4cl_ProcessAudio_Planar ) >("ProcessAudio_Planar" );
515 fnGetBufferIn = GetPrefixSymbol< decltype(sound4cl_GetBufferIn ) >("GetBufferIn" );
516 fnGetBufferOut = GetPrefixSymbol< decltype(sound4cl_GetBufferOut ) >("GetBufferOut" );
517 fnGetEstimatedDelay = GetPrefixSymbol< decltype(sound4cl_GetEstimatedDelay ) >("GetEstimatedDelay" );
518 fnGetFormatName = GetPrefixSymbol< decltype(sound4cl_GetFormatName ) >("GetFormatName" );
519 fnGetFormatFromName = GetPrefixSymbol< decltype(sound4cl_GetFormatFromName ) >("GetFormatFromName" );
520 fnGetBytesFromFormat = GetPrefixSymbol< decltype(sound4cl_GetBytesFromFormat ) >("GetBytesFromFormat" );
521 fnGetMaxPacketFrame = GetPrefixSymbol< decltype(sound4cl_GetMaxPacketFrame ) >("GetMaxPacketFrame" );
522 fnAddAudio = GetPrefixSymbol< decltype(sound4cl_AddAudio ) >("AddAudio" );
523 fnAddPadAudio = GetPrefixSymbol< decltype(sound4cl_AddPadAudio ) >("AddPadAudio" );
524 fnGetOutputCount = GetPrefixSymbol< decltype(sound4cl_GetOutputCount ) >("GetOutputCount" );
525 fnGetAudio = GetPrefixSymbol< decltype(sound4cl_GetAudio ) >("GetAudio" );
526 fnAudioConvertFrom = GetPrefixSymbol< decltype(sound4cl_AudioConvertFrom ) >("AudioConvertFrom" );
527 fnAudioConvertTo = GetPrefixSymbol< decltype(sound4cl_AudioConvertTo ) >("AudioConvertTo" );
528 fnStereoToMono = GetPrefixSymbol< decltype(sound4cl_StereoToMono ) >("StereoToMono" );
529 fnMonoToStereo = GetPrefixSymbol< decltype(sound4cl_MonoToStereo ) >("MonoToStereo" );
530 fnAudioMonoFromLiveStereo = GetPrefixSymbol< decltype(sound4cl_AudioMonoFromLiveStereo) >("AudioMonoFromLiveStereo");
531 fnAudioMonoToLiveStereo = GetPrefixSymbol< decltype(sound4cl_AudioMonoToLiveStereo ) >("AudioMonoToLiveStereo" );
532 fnNewClient = GetPrefixSymbol< decltype(sound4cl_NewClient ) >("NewClient" );
533 fnDeleteClient = GetPrefixSymbol< decltype(sound4cl_DeleteClient ) >("DeleteClient" );
534 fnProcessJson = GetPrefixSymbol< decltype(sound4cl_ProcessJson ) >("ProcessJson" );
535 fnFreeJsonAnswer = GetPrefixSymbol< decltype(sound4cl_FreeJsonAnswer ) >("FreeJsonAnswer" );
536 fnSaveState = GetPrefixSymbol< decltype(sound4cl_SaveState ) >("SaveState" );
537 } catch (std::runtime_error& ) {
538 return false;
539 }
540 // Webserver : not for all projects
541 try {
542 fnWebserver_tcp = GetPrefixSymbol< decltype(sound4cl_Webserver_tcp ) >("Webserver_tcp" );
543 fnWebserver_tcp2 = GetPrefixSymbol< decltype(sound4cl_Webserver_tcp2 ) >("Webserver_tcp2" );
544 fnWebserver = GetPrefixSymbol< decltype(sound4cl_Webserver ) >("Webserver" );
545 fnWebserver_Stop = GetPrefixSymbol< decltype(sound4cl_Webserver_Stop ) >("Webserver_Stop" );
546 fnWebserver_Status = GetPrefixSymbol< decltype(sound4cl_Webserver_Status ) >("Webserver_Status" );
547 } catch (std::runtime_error& ) {
548 // Ignored, handler will take care
549 }
550 // Stereo/Mono conversion : must be present
551 try {
552 // added 2023-03-22
553 fnStereoToMono_Planar = GetPrefixSymbol< decltype(sound4cl_StereoToMono_Planar ) >("StereoToMono_Planar" );
554 fnMonoToStereo_Planar = GetPrefixSymbol< decltype(sound4cl_MonoToStereo_Planar ) >("MonoToStereo_Planar" );
555
556 } catch (std::runtime_error& ) {
557 return false;
558 }
559 // C functions allowed to be missing
560 try {
561 // added 2023-05-22
562 fnSetMetadata = GetPrefixSymbol< decltype(sound4cl_SetMetadata ) >("SetMetadata" );
563 fnGetMetadataInfos = GetPrefixSymbol< decltype(sound4cl_GetMetadataInfos ) >("GetMetadataInfos" );
564 fnFreeMetadataInfos = GetPrefixSymbol< decltype(sound4cl_FreeMetadataInfos ) >("FreeMetadataInfos" );
565 // added 2023-06-20
566 fnSetPresetManager = GetPrefixSymbol< decltype(sound4cl_SetPresetManager ) >("SetPresetManager" );
567 fnPresetManager_InformChange = GetPrefixSymbol< decltype(sound4cl_PresetManager_InformChange ) >("PresetManager_InformChange" );
568
569 // added 2023-09-21
570 fnGetPossibleChunkSizeInFrames = GetPrefixSymbol< decltype(sound4cl_GetPossibleChunkSizeInFrames ) >("GetPossibleChunkSizeInFrames" );
571 fnGetProcessChunkFrames = GetPrefixSymbol< decltype(sound4cl_GetProcessChunkFrames ) >("GetProcessChunkFrames" );
572 fnInitProcess3 = GetPrefixSymbol< decltype(sound4cl_InitProcess3 ) >("InitProcess3" );
573 } catch (std::runtime_error& ) {
574 // Ignored, handler will take care
575 }
576
577 // bus: not for all projects
578 try {
579 // added 2023-06-26
580 fnNewBus = GetPrefixSymbol< decltype(sound4cl_NewBus ) >("NewBus" );
581 fnFreeBus = GetPrefixSymbol< decltype(sound4cl_FreeBus ) >("FreeBus" );
582 fnSetInstanceBus = GetPrefixSymbol< decltype(sound4cl_SetInstanceBus ) >("SetInstanceBus" );
583 } catch (std::runtime_error& ) {
584 // Ignored, handler will take care
585 }
586
587 // Other optional
588 try {
589 // added 2024-02-19
590 fnSetMetadataMulti = GetPrefixSymbol< decltype(sound4cl_SetMetadataMulti ) >("SetMetadataMulti" );
591
592 } catch (std::runtime_error& ) {
593 // Ignored, handler will take care
594 }
595
596 // Other optional, web server only
597 try {
598 // added 2024-05-23
599 fnWebserver_SetAppHealth = GetPrefixSymbol< decltype(sound4cl_Webserver_SetAppHealth ) >("Webserver_SetAppHealth" );
600 fnWebserver_GetAppHealth = GetPrefixSymbol< decltype(sound4cl_Webserver_GetAppHealth ) >("Webserver_GetAppHealth" );
601 fnWebserver_FreeString = GetPrefixSymbol< decltype(sound4cl_Webserver_FreeString ) >("Webserver_FreeString" );
602 } catch (std::runtime_error& ) {
603 // Ignored, handler will take care
604 }
605
606#ifdef _WIN32
607 // Other optional, windows tracing
608 try {
609 // added 2025-05-28
610 fnSetInstanceTracing = GetPrefixSymbol< decltype(sound4cl_SetInstanceTracing ) >("SetInstanceTracing" );
611 fnSetInstanceTracingProcessActivity= GetPrefixSymbol< decltype(sound4cl_SetInstanceTracingProcessActivity ) >("SetInstanceTracingProcessActivity");
612 } catch (std::runtime_error& ) {
613 // Ignored, handler will take care
614 }
615#endif // _WIN32
616
617 // Other optional
618 try {
619 // added 2026-02-17
620 fnGetAudioConverter = GetPrefixSymbol< decltype(sound4cl_GetAudioConverter ) >("GetAudioConverter" );
621 fnFreeAudioConverter = GetPrefixSymbol< decltype(sound4cl_FreeAudioConverter ) >("FreeAudioConverter" );
622 fnAudioConverter_From = GetPrefixSymbol< decltype(sound4cl_AudioConverter_From ) >("AudioConverter_From" );
623 fnAudioConverter_To = GetPrefixSymbol< decltype(sound4cl_AudioConverter_To ) >("AudioConverter_To" );
624 } catch (std::runtime_error& ) {
625 // Ignored, handler will take care
626 }
627
628 m_bOK=true;
629 return true;
630 }
637 bool IsOk() const {
638 return m_bOK;
639 }
640
641 // --------------------------------------------------------------
648 // --------------------------------------------------------------
659 void AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, SampleFormat fmt)
660 { fnAudioConvertFrom(payload, output, nSpl, (sound4cl_SampleFormat)fmt); }
661
672 void AudioConvertTo(const float *input, uint8_t *payload, size_t nSpl, SampleFormat fmt)
673 { fnAudioConvertTo(input, payload, nSpl, (sound4cl_SampleFormat)fmt); }
674
684 void StereoToMono(const float *input, float *output, size_t nFrame)
685 { fnStereoToMono(input, output, nFrame); }
686
696 void MonoToStereo(const float *input, float *output, size_t nFrame)
697 { fnMonoToStereo(input, output, nFrame); }
698
709 void StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
710 { fnStereoToMono_Planar(inputL, inputR, output, nFrame); }
711
722 void MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
723 { fnMonoToStereo_Planar(input, outputL, outputR, nFrame); }
724
731 void AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
732 { fnAudioMonoFromLiveStereo(payload, output); }
733
740 void AudioMonoToLiveStereo(const float *input, uint8_t *payload)
741 { fnAudioMonoToLiveStereo(input, payload); }
742
750 std::string GetVersion()
751 { return std::string(fnGetVersion()); }
752
763 unsigned int GetChunkSizeInFrames()
764 { return fnGetChunkSizeInFrames(); }
765
774 std::vector<unsigned int> GetPossibleChunkSizeInFrames()
775 {
776 std::vector<unsigned int> list;
777 if (fnGetPossibleChunkSizeInFrames) {
778 for (unsigned int* src=fnGetPossibleChunkSizeInFrames();*src;src++) {
779 list.push_back(*src);
780 }
781 } else {
782 list.push_back(fnGetChunkSizeInFrames());
783 }
784 return list;
785 }
786
792 unsigned int GetChannelCount()
793 { return fnGetChannelCount(); }
794
802 unsigned int GetAudioInputCount()
803 { return fnGetAudioInputCount(); }
804
812 unsigned int GetAudioOutputCount()
813 { return fnGetAudioOutputCount(); }
814
822 unsigned int GetSampleRate()
823 { return fnGetSampleRate(); }
824
831 std::string GetFormatName( const SampleFormat fmt)
832 { return std::string(fnGetFormatName((sound4cl_SampleFormat)fmt)); }
833
840 SampleFormat GetFormatFromName( const std::string& name)
841 { return SampleFormat(fnGetFormatFromName(name.c_str())); }
842
849 unsigned int GetBytesFromFormat( const SampleFormat fmt)
850 { return fnGetBytesFromFormat((sound4cl_SampleFormat)fmt); }
851
856 { fnSetLogSeverity((sound4cl_LogSeverity)severity); }
857
862 { _log_cb=cb; fnSetLoggerCallback(&_log_cb_c); }
863 };
864
870 class CBus {
871 public:
873 : m_dynlib(dynlib)
874 {
875 if (m_dynlib.fnNewBus) {
876 m_bus=m_dynlib.fnNewBus();
877 }
878 }
880 {
881 if (m_bus && m_dynlib.fnFreeBus) {
882 m_dynlib.fnFreeBus(m_bus);
883 }
884 m_bus = nullptr;
885 }
886 protected:
887 friend class CInstance;
888 sound4cl_CBus *Get() const { return m_bus; }
889 private:
890 CProcessor& m_dynlib;
891 sound4cl_CBus *m_bus;
892 };
893
900 public:
901 CPresetLoader() = default;
902 virtual ~CPresetLoader() = default;
903
904 virtual bool IsReadOnly() = 0;
905 virtual bool Exists(const std::filesystem::path &name) = 0;
906 virtual bool Remove(const std::filesystem::path &name) = 0;
907 virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to) = 0;
908 virtual std::vector<std::filesystem::path> GetAll() = 0;
909 virtual std::string Read(const std::filesystem::path &filename) = 0;
910 virtual bool Write(const std::filesystem::path &filename, const std::string &content) =0;
911 };
922 class CInstance {
923 public:
930 : m_dynlib(dynlib)
931 {
932 params = m_dynlib.fnNewParameters();
933 if (!params) throw std::bad_alloc();
934 }
935
944 CInstance(CProcessor& dynlib, void* _instance)
945 : m_dynlib(dynlib)
946 , instance(reinterpret_cast<sound4cl_CInstance*>(_instance))
947 , m_bOwned(false)
948 {
949 }
950 // non-copyable
951 CInstance( const CInstance& ) = delete;
952 CInstance& operator=( const CInstance& ) = delete;
956 virtual ~CInstance()
957 {
958 Stop();
959 if (params) m_dynlib.fnFreeParameters(params);
960 }
964 bool IsOk() const { return instance!=nullptr; }
974 void SetParam(const std::string& name, const std::string& value)
975 {
976 assert(params);
977 m_dynlib.fnSetParameter(params, name.c_str(), value.c_str());
978 }
985 std::string GetParam(const std::string& name)
986 {
987 assert(params);
988 auto c_value=m_dynlib.fnGetParameter(params, name.c_str());
989 std::string ret(c_value);
990 m_dynlib.fnFreeParameterValue(c_value);
991 return ret;
992 }
993
1002 void SetBus(const CBus& bus) {
1003 assert(params);
1004 if (m_dynlib.fnSetInstanceBus) {
1005 m_dynlib.fnSetInstanceBus(params,bus.Get());
1006 }
1007 }
1008
1019 void SetPresetManager(CPresetLoader *preset_manager)
1020 {
1021 if (!preset_manager) return;
1022 if (!m_dynlib.fnSetPresetManager) return;
1023 assert(params);
1024 m_dynlib.fnSetPresetManager(params,
1025 sound4cl_custom_reader,
1026 sound4cl_custom_reader_free,
1027 sound4cl_custom_writer,
1028 sound4cl_custom_exists,
1029 sound4cl_custom_getall,
1030 sound4cl_custom_getall_free,
1031 sound4cl_custom_remove,
1032 sound4cl_custom_rename,
1033 preset_manager->IsReadOnly(),
1034 preset_manager
1035 );
1036 }
1037
1060 bool Create(const std::string& LoginKey, const std::string& RadioName, const std::string& Access_Key_ID, const std::string& Access_Key_Secret, const std::filesystem::path& save_path, int json_port = 0, unsigned int frames_per_chunk=0)
1061 {
1062 assert(!instance);
1063 std::string l_save_path;
1064 if (!save_path.empty()) {
1065 #if (__cplusplus<202002L)
1066 l_save_path = save_path.u8string();
1067 #else // C++20
1068 l_save_path = (const char*)save_path.u8string().c_str();
1069 #endif // C++20
1070 }
1071 if (frames_per_chunk==0) {
1072 frames_per_chunk=m_dynlib.GetChunkSizeInFrames();
1073 }
1074 if (m_dynlib.fnInitProcess3) {
1075 instance = m_dynlib.fnInitProcess3(LoginKey.c_str(), RadioName.c_str(), Access_Key_ID.c_str(), Access_Key_Secret.c_str(), l_save_path.c_str(), params, frames_per_chunk);
1076 } else if (frames_per_chunk!=m_dynlib.GetChunkSizeInFrames()) {
1077 return false;
1078 } else {
1079 instance = m_dynlib.fnInitProcess2(LoginKey.c_str(), RadioName.c_str(), Access_Key_ID.c_str(), Access_Key_Secret.c_str(), l_save_path.c_str(), params);
1080 }
1081 if (!instance) return false;
1082 if (!init_metadata.empty()) {
1083 for (auto&& [key,value]: init_metadata) {
1084 m_dynlib.fnSetMetadata(instance, key.c_str(), value.c_str());
1085 }
1086 init_metadata.clear();
1087 }
1088 update_thread = std::thread([this, json_port](){
1090 m_dynlib.fnStartUpdateThread(instance, json_port);
1092 });
1093 int timeout=5000;
1094 #ifdef DEBUG
1095 timeout=60*1000; // For debugger pauses
1096 #endif
1097 if (m_dynlib.fnWaitUpdateThreadReady(instance, timeout)<0) {
1098 return false;
1099 }
1100 return true;
1101 }
1102
1110 unsigned int GetChunkFrames() {
1111 if (instance && m_dynlib.fnGetProcessChunkFrames) {
1112 return m_dynlib.fnGetProcessChunkFrames(instance);
1113 }
1114 return m_dynlib.fnGetChunkSizeInFrames();
1115 }
1116
1124 void PresetManager_InformChange(const std::filesystem::path& relative_path, PresetChange_Kind change_kind)
1125 {
1126 if (m_dynlib.fnPresetManager_InformChange) {
1127 m_dynlib.fnPresetManager_InformChange(instance, relative_path.c_str(), (sound4cl_PresetChange_Kind) change_kind);
1128 }
1129 }
1130
1142 void SetMetadata(const std::string& key, const char* value)
1143 {
1144 if (m_dynlib.fnSetMetadata) {
1145 if (instance) {
1146 m_dynlib.fnSetMetadata(instance, key.c_str(), value);
1147 } else {
1148 init_metadata.push_back( {key, value} );
1149 }
1150 }
1151 }
1152
1162 void SetMetadataMulti(const std::unordered_map<std::string, const char*>& list)
1163 {
1164 if (instance) {
1165 if (m_dynlib.fnSetMetadataMulti) {
1166 typedef const char* pchar;
1167 pchar* keyvalue=new pchar[2*list.size()+1];
1168 size_t n=0;
1169 for (auto&& [key,value]: list) {
1170 keyvalue[2*n+0]=key.c_str();
1171 keyvalue[2*n+1]=value;
1172 n++;
1173 }
1174 keyvalue[2*n+0]=nullptr;
1175
1176 sound4cl_SetMetadataMulti(instance, keyvalue);
1177
1178 delete[] keyvalue;
1179 } else {
1180 for (auto&& [key,value]: list) {
1181 SetMetadata(key, value);
1182 }
1183 }
1184 } else {
1185 for (auto&& [key,value]: list) {
1186 if (value) {
1187 init_metadata.push_back( {key, value} );
1188 }
1189 }
1190 }
1191 }
1192
1198 std::vector< std::tuple<std::string,std::string> > GetMetadataInfos()
1199 {
1200 std::vector< std::tuple<std::string,std::string> > values;
1201 if (m_dynlib.fnGetMetadataInfos) {
1202 const char** c_values = m_dynlib.fnGetMetadataInfos(instance);
1203 if (c_values) {
1204 for (const char** c_value=c_values; *c_value; ) {
1205 std::string key(*c_value);
1206 c_value++;
1207 if (*c_value) {
1208 std::string descr(*c_value);
1209 c_value++;
1210 values.push_back( {key,descr} );
1211 }
1212 }
1213 m_dynlib.fnFreeMetadataInfos(instance, c_values);
1214 }
1215 }
1216 return values;
1217 }
1218
1222 virtual void OnUpdateThreadStart() {}
1226 virtual void OnUpdateThreadStop() {}
1227
1228
1242 bool StartWebServer(int http_port, int https_port=0)
1243 {
1244 assert(instance);
1245 assert(webserver==SOUND4_INVALID_WEBSERVER_ID);
1246 if (!m_dynlib.fnWebserver) {
1247 return false;
1248 }
1249 webserver = m_dynlib.fnWebserver(http_port,https_port,instance);
1250 if (webserver==SOUND4_INVALID_WEBSERVER_ID) {
1251 return false;
1252 }
1253 int web_status=m_dynlib.fnWebserver_Status(webserver);
1254 if (http_port && (web_status & SOUND4_WEBSERVER_HTTP_OK) == 0) {
1255 return false;
1256 }
1257 if (https_port && (web_status & SOUND4_WEBSERVER_HTTPS_OK) == 0) {
1258 return false;
1259 }
1260 return true;
1261 }
1262
1270 void StopWebServer(int timeout_ms = 1000)
1271 {
1272 if (!m_dynlib.fnWebserver_Stop) return;
1273 if (webserver != SOUND4_INVALID_WEBSERVER_ID) {
1274 m_dynlib.fnWebserver_Stop(webserver, timeout_ms);
1275 webserver = SOUND4_INVALID_WEBSERVER_ID;
1276 }
1277 }
1278
1282 void SetWebServerAppHealth(int httpcode, const std::string& contenttype, const std::string& content)
1283 {
1284 assert(instance);
1285 if (!m_dynlib.fnWebserver_SetAppHealth) return;
1286 m_dynlib.fnWebserver_SetAppHealth(instance, httpcode, contenttype.c_str(), content.c_str());
1287 }
1291 void GetWebServerAppHealth(int &httpcode, std::string& contenttype, std::string& content)
1292 {
1293 assert(instance);
1294 if (!m_dynlib.fnWebserver_GetAppHealth) return;
1295 char* c_contenttype=nullptr;
1296 char* c_content=nullptr;
1297 m_dynlib.fnWebserver_GetAppHealth(instance, &httpcode, &c_contenttype, &c_content);
1298 contenttype=c_contenttype;
1299 content=c_content;
1300 m_dynlib.fnWebserver_FreeString(c_contenttype);
1301 m_dynlib.fnWebserver_FreeString(c_content);
1302 }
1312 {
1313 if (!instance) return 0;
1314 return m_dynlib.fnTerminateProcess(instance);
1315 }
1316
1322 void Stop()
1323 {
1324 if (!instance) return;
1325 if (m_bOwned) {
1326 StopWebServer();
1327 if (update_thread.joinable()) {
1328 m_dynlib.fnStopUpdateThread(instance); // this will exit the update thread
1329 update_thread.join();
1330 }
1331 m_dynlib.fnExitProcess(instance);
1332 }
1333 instance=nullptr;
1334 }
1335
1345 unsigned int GetEstimatedDelay()
1346 {
1347 assert(instance);
1348 return m_dynlib.fnGetEstimatedDelay(instance);
1349 }
1350
1364 {
1365 assert(instance);
1366 return m_dynlib.fnGetBufferIn(instance);
1367
1368 }
1381 {
1382 assert(instance);
1383 return m_dynlib.fnGetBufferOut(instance);
1384 }
1389 {
1390 assert(instance);
1391 m_dynlib.fnProcessAudio(instance, GetBufferIn(), GetBufferOut());
1392 }
1404 void ProcessAudio_Planar(float const * const *input, float * const *output)
1405 {
1406 assert(instance);
1407 m_dynlib.fnProcessAudio_Planar(instance, input, output);
1408 }
1409
1421 template<typename T>
1422 std::vector<T> ProcessAnyAudio(const std::vector<T> input)
1423 {
1424 assert(instance);
1425 std::vector<T> output;
1426 unsigned int out_offset=0;
1427 unsigned int in_offset=0;
1428 unsigned int todo = input.size();
1429 while (todo>0) {
1430 unsigned int left = AddAudio(&input[in_offset], todo);
1431 unsigned int out_avail = m_dynlib.fnGetOutputCount(instance);
1432 output.resize(out_offset + out_avail);
1433 GetAudio(&output[out_offset], out_avail);
1434 out_offset+=out_avail;
1435 in_offset += todo-left;
1436 todo=left;
1437 }
1438 return output;
1439 }
1440
1446 class CClient {
1447 public:
1448 CClient(sound4cl_CInstance* instance, CProcessor& dynlib)
1449 : m_dynlib(dynlib)
1450 {
1451 assert(instance);
1452 client=m_dynlib.fnNewClient(instance);
1453 if (!client) throw std::bad_alloc();
1454 }
1455 // non-copyable
1456 CClient( const CInstance& ) = delete;
1457 CClient& operator=( const CInstance& ) = delete;
1459 {
1460 if (client) {
1461 m_dynlib.fnDeleteClient(client);
1462 client=nullptr;
1463 }
1464 }
1472 std::string ProcessJson(const std::string &request, bool *NeedSave = nullptr)
1473 {
1474 assert(client);
1475 int need_save=0;
1476 const char *canswer = m_dynlib.fnProcessJson(client, request.c_str(), &need_save);
1477 if (!canswer) return {};
1478 std::string answer(canswer);
1479 m_dynlib.fnFreeJsonAnswer (canswer);
1480 if (NeedSave) {
1481 *NeedSave=(need_save!=0);
1482 }
1483 return answer;
1484 }
1485 private:
1486 CProcessor& m_dynlib;
1487 sound4cl_CClientInstance *client = nullptr;
1488 };
1496 std::shared_ptr<CClient> NewClient()
1497 {
1498 assert(instance);
1499 return std::make_shared<CClient>(instance, m_dynlib);
1500 }
1501
1512 bool SaveState() {
1513 assert(instance);
1514 if (!m_dynlib.fnSaveState || m_dynlib.fnSaveState(instance)!=0) {
1515 return false;
1516 }
1517 return true;
1518 }
1519#ifdef _WIN32
1526 void SetInstanceTracing(TraceLoggingHProvider tracing_provider, GUID activity_guid = {}) {
1527 if (m_dynlib.fnSetInstanceTracing) {
1528 m_dynlib.fnSetInstanceTracing(params, tracing_provider, activity_guid);
1529 }
1530 }
1537 void SetInstanceTracingProcessActivity(GUID activity_guid) {
1538 assert(instance);
1539 if (m_dynlib.fnSetInstanceTracingProcessActivity) {
1540 m_dynlib.fnSetInstanceTracingProcessActivity(instance, activity_guid);
1541 }
1542 }
1543#endif // _WIN32
1544 protected:
1545 template<typename T>
1546 unsigned int AddAudio(const T* payload, unsigned int nFrame)
1547 {
1548 assert(instance);
1549 return m_dynlib.fnAddAudio(instance, reinterpret_cast<const T*>(payload), nFrame, helper::SampleFormat<T>::format);
1550 }
1551 template<typename T>
1552 unsigned int GetAudio(T* payload, unsigned int max_nFrame)
1553 {
1554 assert(instance);
1555 return m_dynlib.fnGetAudio(instance, reinterpret_cast<T*>(payload), max_nFrame, helper::SampleFormat<T>::format);
1556 }
1557
1558 private:
1559 CProcessor& m_dynlib;
1560 sound4cl_CParameters* params = nullptr;
1561 sound4cl_CInstance* instance = nullptr;
1562 std::thread update_thread;
1563 std::vector< std::pair<std::string,std::string> > init_metadata;
1564 uint64_t webserver = SOUND4_INVALID_WEBSERVER_ID;
1565 bool m_bOwned = true;
1566 };
1567
1568
1569 // --------------------------------------------------------------
1576 ConverterTargetMode_AsInput = sound4cl_ConverterTargetMode_AsInput,
1577 ConverterTargetMode_Mono = sound4cl_ConverterTargetMode_Mono,
1578 ConverterTargetMode_Stereo = sound4cl_ConverterTargetMode_Stereo,
1579 ConverterTargetMode_StereoToMono = sound4cl_ConverterTargetMode_StereoToMono,
1580 ConverterTargetMode_MonoToStereo = sound4cl_ConverterTargetMode_MonoToStereo,
1581 };
1582
1591 public:
1598 : m_dynlib(dynlib)
1599 { }
1600
1605 {
1606 Delete();
1607 }
1608
1624 bool Configure(SampleFormat fmt, size_t nFrame, size_t payl_ch, size_t payl_ch_offset, ConverterTargetMode target_mode)
1625 {
1626 Delete();
1627 if (m_dynlib.fnGetAudioConverter)
1628 m_cvt = m_dynlib.fnGetAudioConverter((sound4cl_SampleFormat)fmt, nFrame, payl_ch, payl_ch_offset, (sound4cl_ConverterTargetMode)target_mode);
1629 return IsOK();
1630 }
1631 void Delete()
1632 {
1633 if (m_cvt && m_dynlib.fnFreeAudioConverter) {
1634 m_dynlib.fnFreeAudioConverter(m_cvt);
1635 }
1636 m_cvt = nullptr;
1637 }
1638 bool IsOK() const { return (m_cvt!=nullptr); }
1639
1648 void ConvertFrom(const uint8_t *payload, float *output)
1649 { m_dynlib.fnAudioConverter_From(m_cvt, payload, output); }
1650
1659 void ConvertTo(const float *input, uint8_t *payload)
1660 { m_dynlib.fnAudioConverter_To(m_cvt, input, payload); }
1661
1662 private:
1663 CProcessor& m_dynlib;
1664 sound4cl_CAudioConverter *m_cvt = nullptr;
1665 };
1666
1667}; // namespace sound4
1668
1669
1670// bridge C callbacks to CPresetLoader
1671extern "C" {
1672 static char *sound4cl_custom_reader(const fs_char *filename, void* userdata) {
1673 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1674 std::string content=preset_loader->Read(filename);
1675 return strdup(content.c_str());
1676 }
1677 static void sound4cl_custom_reader_free(char *content, void* userdata) {
1678 if (!content) return;
1679 free(content);
1680 }
1681 static int sound4cl_custom_writer(const fs_char *filename, const char *content, void* userdata) {
1682 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1683 auto res=preset_loader->Write(filename,content);
1684 return res?0:-1;
1685 }
1686 static int sound4cl_custom_exists(const fs_char *filename, void* userdata) {
1687 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1688 auto res=preset_loader->Exists(filename);
1689 return res?0:-1;
1690 }
1691 static fs_char** sound4cl_custom_getall(void* userdata) {
1692 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1693 auto res=preset_loader->GetAll();
1694 fs_char**all=(fs_char**)malloc((res.size()+1)*sizeof(const fs_char*));
1695 for (size_t n=0;n<res.size();n++) {
1696 all[n]=fs_strdup(res[n].c_str());
1697 }
1698 all[res.size()]=nullptr;
1699 return all;
1700 }
1701 static void sound4cl_custom_getall_free(fs_char** all, void* userdata) {
1702 if (!all) return;
1703 for (fs_char** one=all;*one;one++) {
1704 free(*one);
1705 }
1706 free(all);
1707 }
1708 static int sound4cl_custom_remove(const fs_char *filename, void* userdata) {
1709 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1710 auto res=preset_loader->Remove(filename);
1711 return res?0:-1;
1712 }
1713 static int sound4cl_custom_rename(const fs_char *from, const fs_char *to, void* userdata) {
1714 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1715 auto res=preset_loader->Rename(from,to);
1716 return res?0:-1;
1717 }
1718
1719};
Audio converter helper.
void ConvertFrom(const uint8_t *payload, float *output)
virtual ~CAudioConverter()
Destroy the CAudioConverter object.
void ConvertTo(const float *input, uint8_t *payload)
bool Configure(SampleFormat fmt, size_t nFrame, size_t payl_ch, size_t payl_ch_offset, ConverterTargetMode target_mode)
CAudioConverter(CProcessor &dynlib)
Construct a new empty CAudioConverter object.
Shared Bus.
Definition sound4cl.hpp:870
sound4cl_CBus * Get() const
Definition sound4cl.hpp:888
CBus(CProcessor &dynlib)
Definition sound4cl.hpp:872
CClient(const CInstance &)=delete
CClient(sound4cl_CInstance *instance, CProcessor &dynlib)
std::string ProcessJson(const std::string &request, bool *NeedSave=nullptr)
Process a JSON request and returns the answer.
CClient & operator=(const CInstance &)=delete
Instance handling class.
Definition sound4cl.hpp:922
bool Create(const std::string &LoginKey, const std::string &RadioName, const std::string &Access_Key_ID, const std::string &Access_Key_Secret, const std::filesystem::path &save_path, int json_port=0, unsigned int frames_per_chunk=0)
void ProcessAudio_Planar(float const *const *input, float *const *output)
std::vector< T > ProcessAnyAudio(const std::vector< T > input)
virtual ~CInstance()
Definition sound4cl.hpp:956
CInstance & operator=(const CInstance &)=delete
CInstance(const CInstance &)=delete
void GetWebServerAppHealth(int &httpcode, std::string &contenttype, std::string &content)
void SetPresetManager(CPresetLoader *preset_manager)
void SetBus(const CBus &bus)
virtual void OnUpdateThreadStop()
unsigned int GetChunkFrames()
CInstance(CProcessor &dynlib, void *_instance)
Definition sound4cl.hpp:944
void SetWebServerAppHealth(int httpcode, const std::string &contenttype, const std::string &content)
CInstance(CProcessor &dynlib)
Definition sound4cl.hpp:929
void SetParam(const std::string &name, const std::string &value)
Definition sound4cl.hpp:974
virtual void OnUpdateThreadStart()
bool IsOk() const
Definition sound4cl.hpp:964
bool StartWebServer(int http_port, int https_port=0)
void StopWebServer(int timeout_ms=1000)
void PresetManager_InformChange(const std::filesystem::path &relative_path, PresetChange_Kind change_kind)
void SetMetadataMulti(const std::unordered_map< std::string, const char * > &list)
unsigned int AddAudio(const T *payload, unsigned int nFrame)
unsigned int GetEstimatedDelay()
std::vector< std::tuple< std::string, std::string > > GetMetadataInfos()
std::shared_ptr< CClient > NewClient()
float * GetBufferOut()
void SetMetadata(const std::string &key, const char *value)
void SetInstanceTracing(TraceLoggingHProvider tracing_provider, GUID activity_guid={})
float * GetBufferIn()
unsigned int GetAudio(T *payload, unsigned int max_nFrame)
std::string GetParam(const std::string &name)
Definition sound4cl.hpp:985
void SetInstanceTracingProcessActivity(GUID activity_guid)
Custom preset handler helper.
Definition sound4cl.hpp:899
virtual std::string Read(const std::filesystem::path &filename)=0
virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to)=0
virtual bool Exists(const std::filesystem::path &name)=0
virtual bool Write(const std::filesystem::path &filename, const std::string &content)=0
virtual ~CPresetLoader()=default
virtual bool Remove(const std::filesystem::path &name)=0
virtual std::vector< std::filesystem::path > GetAll()=0
virtual bool IsReadOnly()=0
Dynamic library interface.
Definition sound4cl.hpp:313
bool Load(const std::filesystem::path &filepath)
Loads the library.
Definition sound4cl.hpp:435
void SetLogSeverity(LogSeverity severity)
Definition sound4cl.hpp:855
unsigned int GetAudioInputCount()
Definition sound4cl.hpp:802
void SetLoggerCallback(log_cb_t cb)
Definition sound4cl.hpp:861
unsigned int GetChunkSizeInFrames()
Definition sound4cl.hpp:763
SampleFormat GetFormatFromName(const std::string &name)
Definition sound4cl.hpp:840
std::vector< unsigned int > GetPossibleChunkSizeInFrames()
Definition sound4cl.hpp:774
std::string GetFormatName(const SampleFormat fmt)
Definition sound4cl.hpp:831
unsigned int GetBytesFromFormat(const SampleFormat fmt)
Definition sound4cl.hpp:849
unsigned int GetAudioOutputCount()
Definition sound4cl.hpp:812
helper::DynFuncHolder< T > GetPrefixSymbol(const std::string &name)
Definition sound4cl.hpp:420
unsigned int GetSampleRate()
Definition sound4cl.hpp:822
std::string GetVersion()
Definition sound4cl.hpp:750
bool IsOk() const
Check if the library was loaded correctly.
Definition sound4cl.hpp:637
unsigned int GetChannelCount()
Definition sound4cl.hpp:792
CProcessor()=default
Helper to load dynamic library.
Definition sound4cl.hpp:155
DynFuncHolder< T > GetSymbol_safe(const std::string &name)
Definition sound4cl.hpp:251
CDynLoader(CDynLoader &&other) noexcept
Definition sound4cl.hpp:166
CDynLoader & operator=(CDynLoader const &)=delete
CDynLoader & operator=(CDynLoader &&other) noexcept
Definition sound4cl.hpp:167
bool Load(const std::filesystem::path &dynlib, uint32_t loadflags=LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR|LOAD_LIBRARY_SEARCH_APPLICATION_DIR|LOAD_LIBRARY_SEARCH_USER_DIRS|LOAD_LIBRARY_SEARCH_SYSTEM32)
Definition sound4cl.hpp:224
CDynLoader(const CDynLoader &)=delete
static std::filesystem::path GetThisLibraryPath(void)
Definition sound4cl.hpp:255
DynFuncHolder< T > GetSymbol(const std::string &name)
Definition sound4cl.hpp:243
Helper to wrap a function pointer.
Definition sound4cl.hpp:134
DynFuncHolder(DynFunc_t a_ptr)
Definition sound4cl.hpp:145
void AudioConvertTo(const float *input, uint8_t *payload, size_t nSpl, SampleFormat fmt)
Definition sound4cl.hpp:672
void AudioMonoToLiveStereo(const float *input, uint8_t *payload)
Definition sound4cl.hpp:740
void StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
Definition sound4cl.hpp:709
void MonoToStereo(const float *input, float *output, size_t nFrame)
Definition sound4cl.hpp:696
void AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, SampleFormat fmt)
Definition sound4cl.hpp:659
void StereoToMono(const float *input, float *output, size_t nFrame)
Definition sound4cl.hpp:684
void AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
Definition sound4cl.hpp:731
void MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
Definition sound4cl.hpp:722
ConverterTargetMode
@ ConverterTargetMode_MonoToStereo
Stereo process, using mono extract/payload (converts mono to stereo)
@ ConverterTargetMode_Mono
Mono process (using mono extract/payload)
@ ConverterTargetMode_Stereo
Stereo process (using stereo extract/payload)
@ ConverterTargetMode_AsInput
Same channels as input (no extract/insert)
@ ConverterTargetMode_StereoToMono
Mono process, using stereo extract/payload (converts stereo to mono)
wchar_t fs_char
#define SOUND4_WEBSERVER_HTTPS_OK
#define SOUND4_WEBSERVER_HTTP_OK
#define SOUND4_INVALID_WEBSERVER_ID
static std::string WStringToUTF8(const std::wstring &wstr)
Definition sound4cl.hpp:86
static std::wstring UTF8ToWString(const std::string &str)
Definition sound4cl.hpp:98
@ S32_NATIVE
32-bit signed integer, native
Definition sound4cl.hpp:72
@ S16_LE
16-bit signed integer, little-endian
Definition sound4cl.hpp:62
@ S24_BE
24-bit signed integer, big-endian
Definition sound4cl.hpp:65
@ F32_LE
32-bit floating-point, little-endian
Definition sound4cl.hpp:68
@ S24_LE
24-bit signed integer, little-endian
Definition sound4cl.hpp:64
@ S32_LE
32-bit signed integer, little-endian
Definition sound4cl.hpp:66
@ F32_BE
32-bit floating-point, big-endian
Definition sound4cl.hpp:69
@ S16_BE
16-bit signed integer, big-endian
Definition sound4cl.hpp:63
@ S32_BE
32-bit signed integer, big-endian
Definition sound4cl.hpp:67
@ INVALID_FORMAT
Invalid.
Definition sound4cl.hpp:61
@ F32_NATIVE
32-bit floating-point, native
Definition sound4cl.hpp:73
@ S24_NATIVE
24-bit signed integer, native
Definition sound4cl.hpp:71
@ S16_NATIVE
16-bit signed integer, native
Definition sound4cl.hpp:70
@ fatal
fatal error
Definition sound4cl.hpp:281
@ verbose
verbose
Definition sound4cl.hpp:285
@ none
Not used.
Definition sound4cl.hpp:280
@ verbose3
verbose3
Definition sound4cl.hpp:287
@ verbose5
verbose5
Definition sound4cl.hpp:289
@ verbose2
verbose2
Definition sound4cl.hpp:286
@ warning
warning
Definition sound4cl.hpp:283
@ error
error
Definition sound4cl.hpp:282
@ info
info
Definition sound4cl.hpp:284
@ verbose4
verbose4
Definition sound4cl.hpp:288
static void _log_cb_c(sound4cl_LogSeverity severity, const char *c_msg)
Definition sound4cl.hpp:302
std::function< void(LogSeverity, const std::string &)> log_cb_t
Definition sound4cl.hpp:295
static log_cb_t _log_cb
Definition sound4cl.hpp:300
PresetChange_Kind
Definition sound4cl.hpp:912
@ change_kind_created
has been created
Definition sound4cl.hpp:913
@ change_kind_deleted
has been deleted
Definition sound4cl.hpp:915
@ change_kind_modified
has been modified
Definition sound4cl.hpp:914
struct _tlgProvider_t const * TraceLoggingHProvider
@ F32_NATIVE
32-bit floating-point, native
@ change_kind_modified
Path has been modified.
@ INVALID_FORMAT
Invalid.
@ S16_NATIVE
16-bit signed integer, native
@ change_kind_created
Path has been created.
@ F32_LE
32-bit floating-point, little-endian
@ S16_BE
16-bit signed integer, big-endian
@ S32_BE
32-bit signed integer, big-endian
@ S24_BE
24-bit signed integer, big-endian
@ S16_LE
16-bit signed integer, little-endian
@ F32_BE
32-bit floating-point, big-endian
@ S32_NATIVE
32-bit signed integer, native
@ change_kind_deleted
Path has been deleted.
@ S24_LE
24-bit signed integer, little-endian
@ S32_LE
32-bit signed integer, little-endian
@ S24_NATIVE
24-bit signed integer, native
Helper for Sample format types.
Definition sound4cl.hpp:113