SOUND4 x1.CLOUD Library [1.5.7]
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&&) = default;
167 // No copy allowed
168 CDynLoader(const CDynLoader&) = delete;
169 CDynLoader& operator=(CDynLoader const&) = delete;
170 operator bool() const {
171 return (m_lib!=NULL);
172 }
173 bool IsOk() const {
174 return (m_lib!=NULL);
175 }
176 #if UNIXLIKE
177 void Close() {
178 if (IsOk()) {
179 #ifndef __SANITIZE_ADDRESS__ // avoid unloading to keep call stack
180 dlclose(m_lib);
181 #endif // __SANITIZE_ADDRESS__
182 m_lib=NULL;
183 }
184 }
185 bool Load(const std::filesystem::path& dynlib)
186 {
187 Close();
188 m_lib=dlopen(dynlib.c_str(), RTLD_NOW|RTLD_LOCAL);
189 return IsOk();
190 }
191 template <typename T>
192 DynFuncHolder<T> GetSymbol(const std::string& name) {
193 auto ptr=dlsym(m_lib, name.c_str());
194 if (!ptr) {
195 throw std::runtime_error("Missing function in library");
196 }
197 return DynFuncHolder<T>(ptr);
198 }
199 template <typename T>
200 DynFuncHolder<T> GetSymbol_safe(const std::string& name) {
201 auto ptr=dlsym(m_lib, name.c_str());
202 return DynFuncHolder<T>(ptr);
203 }
204 static std::filesystem::path GetThisLibraryPath(void) {
205 static Dl_info info;
206 if (! dladdr((void *) GetThisLibraryPath, & info)) return {};
207 auto rp=realpath(info.dli_fname, NULL);
208 std::filesystem::path p(rp);
209 free(rp);
210 return p;
211 }
212 #else
219 void Close() {
220 if (IsOk()) {
221 // NOTE: On Windows, FreeLibrary may create a lot of troubles, should use FreeLibraryAndExitThread but...
222 // So define SOUND4_CALL_FREELIBRARY before including this file if you really want to unload the library dynamically
223 #if defined(SOUND4_CALL_FREELIBRARYANDEXITTHREAD)
224 FreeLibraryAndExitThread(m_lib, 0);
225 #elif defined(SOUND4_CALL_FREELIBRARY)
226 FreeLibrary(m_lib);
227 #endif // SOUND4_CALL_FREELIBRARY
228 m_lib=NULL;
229 }
230 }
231 template <typename T>
232 DynFuncHolder<T> GetSymbol(const std::string& name) {
233 auto ptr=GetProcAddress(m_lib, name.c_str());
234 if (!ptr) {
235 throw std::runtime_error("Missing function in library");
236 }
237 return DynFuncHolder<T>(ptr);
238 }
239 template <typename T>
240 DynFuncHolder<T> GetSymbol_safe(const std::string& name) {
241 auto ptr=GetProcAddress(m_lib, name.c_str());
242 return DynFuncHolder<T>(ptr);
243 }
244 static std::filesystem::path GetThisLibraryPath(void) {
245 static wchar_t path[MAX_PATH]={};
246 HMODULE hm = NULL;
250 {
251 if (GetModuleFileNameW(hm, path, MAX_PATH) > 0) {
252 return std::filesystem::path(path);
253 }
254 }
255 return {};
256 }
257 #endif
258 virtual ~CDynLoader() {
259 Close();
260 }
261 }; // class CDynLoader
262 }; // namespace helper
263
264
269 none = sound4cl_none,
270 fatal = sound4cl_fatal,
271 error = sound4cl_error,
272 warning = sound4cl_warning,
273 info = sound4cl_info,
274 verbose = sound4cl_verbose,
275 verbose2 = sound4cl_verbose2,
276 verbose3 = sound4cl_verbose3,
277 verbose4 = sound4cl_verbose4,
278 verbose5 = sound4cl_verbose5,
279 };
280
284 using log_cb_t = std::function<void(LogSeverity,const std::string&)>;
285
290 extern "C" {
291 static inline void _log_cb_c(sound4cl_LogSeverity severity, const char *c_msg) {
292 _log_cb((LogSeverity)severity, std::string(c_msg));
293 }
294 }
295
303 private:
304 helper::CDynLoader m_lib;
305 bool m_bOK = false;
306 std::string m_prefix; // The prefix of functions, depends on the library (cloud_, impact_...)
307 protected:
308 friend class CBus;
309 friend class CInstance;
310
311 // All those wrapped functions have the same signature as there C sound4cl_XXX
312 helper::DynFuncHolder< decltype(sound4cl_GetVersion ) > fnGetVersion ;
313 helper::DynFuncHolder< decltype(sound4cl_GetChunkSizeInFrames ) > fnGetChunkSizeInFrames ;
314 helper::DynFuncHolder< decltype(sound4cl_GetChannelCount ) > fnGetChannelCount ;
315 helper::DynFuncHolder< decltype(sound4cl_GetAudioInputCount ) > fnGetAudioInputCount ;
316 helper::DynFuncHolder< decltype(sound4cl_GetAudioOutputCount ) > fnGetAudioOutputCount ;
317 helper::DynFuncHolder< decltype(sound4cl_GetSampleRate ) > fnGetSampleRate ;
318 helper::DynFuncHolder< decltype(sound4cl_SetLoggerCallback ) > fnSetLoggerCallback ;
319 helper::DynFuncHolder< decltype(sound4cl_SetLogSeverity ) > fnSetLogSeverity ;
320 helper::DynFuncHolder< decltype(sound4cl_NewParameters ) > fnNewParameters ;
321 helper::DynFuncHolder< decltype(sound4cl_FreeParameters ) > fnFreeParameters ;
322 helper::DynFuncHolder< decltype(sound4cl_SetParameter ) > fnSetParameter ;
323 helper::DynFuncHolder< decltype(sound4cl_GetParameter ) > fnGetParameter ;
324 helper::DynFuncHolder< decltype(sound4cl_FreeParameterValue ) > fnFreeParameterValue ;
325 helper::DynFuncHolder< decltype(sound4cl_InitProcess ) > fnInitProcess ;
326 helper::DynFuncHolder< decltype(sound4cl_InitProcess2 ) > fnInitProcess2 ;
327 helper::DynFuncHolder< decltype(sound4cl_TerminateProcess ) > fnTerminateProcess ;
328 helper::DynFuncHolder< decltype(sound4cl_ExitProcess ) > fnExitProcess ;
329 helper::DynFuncHolder< decltype(sound4cl_StartUpdateThread ) > fnStartUpdateThread ;
330 helper::DynFuncHolder< decltype(sound4cl_StopUpdateThread ) > fnStopUpdateThread ;
331 helper::DynFuncHolder< decltype(sound4cl_WaitUpdateThreadReady ) > fnWaitUpdateThreadReady ;
332 helper::DynFuncHolder< decltype(sound4cl_ProcessAudio ) > fnProcessAudio ;
333 helper::DynFuncHolder< decltype(sound4cl_ProcessAudio_Planar ) > fnProcessAudio_Planar ;
334 helper::DynFuncHolder< decltype(sound4cl_GetBufferIn ) > fnGetBufferIn ;
335 helper::DynFuncHolder< decltype(sound4cl_GetBufferOut ) > fnGetBufferOut ;
336 helper::DynFuncHolder< decltype(sound4cl_GetEstimatedDelay ) > fnGetEstimatedDelay ;
337 helper::DynFuncHolder< decltype(sound4cl_GetFormatName ) > fnGetFormatName ;
338 helper::DynFuncHolder< decltype(sound4cl_GetFormatFromName ) > fnGetFormatFromName ;
339 helper::DynFuncHolder< decltype(sound4cl_GetBytesFromFormat ) > fnGetBytesFromFormat ;
340 helper::DynFuncHolder< decltype(sound4cl_GetMaxPacketFrame ) > fnGetMaxPacketFrame ;
341 helper::DynFuncHolder< decltype(sound4cl_AddAudio ) > fnAddAudio ;
342 helper::DynFuncHolder< decltype(sound4cl_AddPadAudio ) > fnAddPadAudio ;
343 helper::DynFuncHolder< decltype(sound4cl_GetOutputCount ) > fnGetOutputCount ;
344 helper::DynFuncHolder< decltype(sound4cl_GetAudio ) > fnGetAudio ;
345 helper::DynFuncHolder< decltype(sound4cl_AudioConvertFrom ) > fnAudioConvertFrom ;
346 helper::DynFuncHolder< decltype(sound4cl_AudioConvertTo ) > fnAudioConvertTo ;
347 helper::DynFuncHolder< decltype(sound4cl_StereoToMono ) > fnStereoToMono ;
348 helper::DynFuncHolder< decltype(sound4cl_MonoToStereo ) > fnMonoToStereo ;
349 helper::DynFuncHolder< decltype(sound4cl_AudioMonoFromLiveStereo) > fnAudioMonoFromLiveStereo;
350 helper::DynFuncHolder< decltype(sound4cl_AudioMonoToLiveStereo ) > fnAudioMonoToLiveStereo ;
351 helper::DynFuncHolder< decltype(sound4cl_NewClient ) > fnNewClient ;
352 helper::DynFuncHolder< decltype(sound4cl_DeleteClient ) > fnDeleteClient ;
353 helper::DynFuncHolder< decltype(sound4cl_ProcessJson ) > fnProcessJson ;
354 helper::DynFuncHolder< decltype(sound4cl_FreeJsonAnswer ) > fnFreeJsonAnswer ;
355 helper::DynFuncHolder< decltype(sound4cl_SaveState ) > fnSaveState ;
356
357 // only for processes supporting webserver
358 helper::DynFuncHolder< decltype(sound4cl_Webserver_tcp ) > fnWebserver_tcp ;
359 helper::DynFuncHolder< decltype(sound4cl_Webserver_tcp2 ) > fnWebserver_tcp2 ;
360 helper::DynFuncHolder< decltype(sound4cl_Webserver ) > fnWebserver ;
361 helper::DynFuncHolder< decltype(sound4cl_Webserver_Stop ) > fnWebserver_Stop ;
362 helper::DynFuncHolder< decltype(sound4cl_Webserver_Status ) > fnWebserver_Status ;
363
364 // added 2023-03-22
365 helper::DynFuncHolder< decltype(sound4cl_StereoToMono_Planar ) > fnStereoToMono_Planar ;
366 helper::DynFuncHolder< decltype(sound4cl_MonoToStereo_Planar ) > fnMonoToStereo_Planar ;
367
368 // added 2023-05-22
369 helper::DynFuncHolder< decltype(sound4cl_SetMetadata ) > fnSetMetadata ;
370 helper::DynFuncHolder< decltype(sound4cl_GetMetadataInfos ) > fnGetMetadataInfos ;
371 helper::DynFuncHolder< decltype(sound4cl_FreeMetadataInfos ) > fnFreeMetadataInfos ;
372
373 // added 2023-06-20
374 helper::DynFuncHolder< decltype(sound4cl_SetPresetManager ) > fnSetPresetManager ;
375 helper::DynFuncHolder< decltype(sound4cl_PresetManager_InformChange ) > fnPresetManager_InformChange ;
376
377 // added 2023-09-21
378 helper::DynFuncHolder< decltype(sound4cl_GetPossibleChunkSizeInFrames ) > fnGetPossibleChunkSizeInFrames ;
379 helper::DynFuncHolder< decltype(sound4cl_GetProcessChunkFrames ) > fnGetProcessChunkFrames ;
380 helper::DynFuncHolder< decltype(sound4cl_InitProcess3 ) > fnInitProcess3 ;
381
382 // added 2023-06-26, only for processes supporting bus
383 helper::DynFuncHolder< decltype(sound4cl_NewBus ) > fnNewBus ;
384 helper::DynFuncHolder< decltype(sound4cl_FreeBus ) > fnFreeBus ;
385 helper::DynFuncHolder< decltype(sound4cl_SetInstanceBus ) > fnSetInstanceBus ;
386
387 // added 2024-02-19
388 helper::DynFuncHolder< decltype(sound4cl_SetMetadataMulti ) > fnSetMetadataMulti ;
389
390 // added 2024-05-23
391 helper::DynFuncHolder< decltype(sound4cl_Webserver_SetAppHealth ) > fnWebserver_SetAppHealth ;
392 helper::DynFuncHolder< decltype(sound4cl_Webserver_GetAppHealth ) > fnWebserver_GetAppHealth ;
393 helper::DynFuncHolder< decltype(sound4cl_Webserver_FreeString ) > fnWebserver_FreeString ;
394
395#ifdef _WIN32
396 // added 2025-05-28
397 helper::DynFuncHolder< decltype(sound4cl_SetInstanceTracing ) > fnSetInstanceTracing ;
398 helper::DynFuncHolder< decltype(sound4cl_SetInstanceTracingProcessActivity ) > fnSetInstanceTracingProcessActivity ;
399#endif // _WIN32
400
401 template <typename T>
402 helper::DynFuncHolder<T> GetPrefixSymbol(const std::string& name) {
403 std::string prefix_name = m_prefix+name;
404 return m_lib.GetSymbol<T>(prefix_name);
405 }
406
407 public:
408 CProcessor() = default;
409
417 bool Load(const std::filesystem::path& filepath) {
418 m_lib.Close();
419 // If a path is given, use it directly and do not try other path
420 if (filepath.has_parent_path() && !m_lib.Load(filepath)) {
421 return false;
422 } else if (!m_lib.IsOk()) {
423 auto thisdir=helper::CDynLoader::GetThisLibraryPath().parent_path();
424 auto filename = filepath.filename();
425 #ifndef _WIN32
426 // Linux: if in a bin directory, try ../lib first
427 if (!m_lib.IsOk() && thisdir.filename()=="bin") {
428 auto libdir = thisdir.parent_path() / "lib";
429 m_lib.Load(libdir / filename);
430 }
431 #endif // !_WIN32
432 if (!m_lib.IsOk()) {
433 // Search in the same directory this code is in
434 m_lib.Load(thisdir / filename);
435 }
436 if (!m_lib.IsOk()) {
437 // Try current path
438 std::error_code ec;
439 auto p = std::filesystem::current_path(ec);
440 if (!ec) {
441 m_lib.Load(p / filename);
442 }
443 }
444 if (!m_lib.IsOk()) {
445 return false;
446 }
447 }
448 // Get information from the library itself
449 try {
450 auto GetInfo = m_lib.GetSymbol< decltype(sound4cl_SOUND4_GetProcessInfo ) >("SOUND4_GetProcessInfo" );
451 auto info = GetInfo();
452 m_prefix = info->prefix;
453 } catch (std::runtime_error& ) {
454 // Compatibility: search in known libraries without this function
455 #ifdef _WIN32
456 std::string filename = helper::WStringToUTF8(filepath.stem());
457 #else // !_WIN32
458 std::string filename = filepath.stem();
459 // remove lib prefix
460 filename=filename.substr(3);
461 #endif // ! _WIN32
462 if (filename=="sound4.x1.cloud") {
463 m_prefix="cloudx1_";
464 } else if (filename=="sound4.impact.cl") {
465 m_prefix="impact_";
466 } else if (filename=="sound4.bigvoice.cl") {
467 m_prefix="bigvoice_";
468 } else {
469 return false;
470 }
471 }
472
473 // Load all C functions
474 try {
475 fnGetVersion = GetPrefixSymbol< decltype(sound4cl_GetVersion ) >("GetVersion" );
476 fnGetChunkSizeInFrames = GetPrefixSymbol< decltype(sound4cl_GetChunkSizeInFrames ) >("GetChunkSizeInFrames" );
477 fnGetChannelCount = GetPrefixSymbol< decltype(sound4cl_GetChannelCount ) >("GetChannelCount" );
478 fnGetAudioInputCount = GetPrefixSymbol< decltype(sound4cl_GetAudioInputCount ) >("GetAudioInputCount" );
479 fnGetAudioOutputCount = GetPrefixSymbol< decltype(sound4cl_GetAudioOutputCount ) >("GetAudioOutputCount" );
480 fnGetSampleRate = GetPrefixSymbol< decltype(sound4cl_GetSampleRate ) >("GetSampleRate" );
481 fnSetLoggerCallback = GetPrefixSymbol< decltype(sound4cl_SetLoggerCallback ) >("SetLoggerCallback" );
482 fnSetLogSeverity = GetPrefixSymbol< decltype(sound4cl_SetLogSeverity ) >("SetLogSeverity" );
483 fnNewParameters = GetPrefixSymbol< decltype(sound4cl_NewParameters ) >("NewParameters" );
484 fnFreeParameters = GetPrefixSymbol< decltype(sound4cl_FreeParameters ) >("FreeParameters" );
485 fnSetParameter = GetPrefixSymbol< decltype(sound4cl_SetParameter ) >("SetParameter" );
486 fnGetParameter = GetPrefixSymbol< decltype(sound4cl_GetParameter ) >("GetParameter" );
487 fnFreeParameterValue = GetPrefixSymbol< decltype(sound4cl_FreeParameterValue ) >("FreeParameterValue" );
488 fnInitProcess = GetPrefixSymbol< decltype(sound4cl_InitProcess ) >("InitProcess" );
489 fnInitProcess2 = GetPrefixSymbol< decltype(sound4cl_InitProcess2 ) >("InitProcess2" );
490 fnTerminateProcess = GetPrefixSymbol< decltype(sound4cl_TerminateProcess ) >("TerminateProcess" );
491 fnExitProcess = GetPrefixSymbol< decltype(sound4cl_ExitProcess ) >("ExitProcess" );
492 fnStartUpdateThread = GetPrefixSymbol< decltype(sound4cl_StartUpdateThread ) >("StartUpdateThread" );
493 fnStopUpdateThread = GetPrefixSymbol< decltype(sound4cl_StopUpdateThread ) >("StopUpdateThread" );
494 fnWaitUpdateThreadReady = GetPrefixSymbol< decltype(sound4cl_WaitUpdateThreadReady ) >("WaitUpdateThreadReady" );
495 fnProcessAudio = GetPrefixSymbol< decltype(sound4cl_ProcessAudio ) >("ProcessAudio" );
496 fnProcessAudio_Planar = GetPrefixSymbol< decltype(sound4cl_ProcessAudio_Planar ) >("ProcessAudio_Planar" );
497 fnGetBufferIn = GetPrefixSymbol< decltype(sound4cl_GetBufferIn ) >("GetBufferIn" );
498 fnGetBufferOut = GetPrefixSymbol< decltype(sound4cl_GetBufferOut ) >("GetBufferOut" );
499 fnGetEstimatedDelay = GetPrefixSymbol< decltype(sound4cl_GetEstimatedDelay ) >("GetEstimatedDelay" );
500 fnGetFormatName = GetPrefixSymbol< decltype(sound4cl_GetFormatName ) >("GetFormatName" );
501 fnGetFormatFromName = GetPrefixSymbol< decltype(sound4cl_GetFormatFromName ) >("GetFormatFromName" );
502 fnGetBytesFromFormat = GetPrefixSymbol< decltype(sound4cl_GetBytesFromFormat ) >("GetBytesFromFormat" );
503 fnGetMaxPacketFrame = GetPrefixSymbol< decltype(sound4cl_GetMaxPacketFrame ) >("GetMaxPacketFrame" );
504 fnAddAudio = GetPrefixSymbol< decltype(sound4cl_AddAudio ) >("AddAudio" );
505 fnAddPadAudio = GetPrefixSymbol< decltype(sound4cl_AddPadAudio ) >("AddPadAudio" );
506 fnGetOutputCount = GetPrefixSymbol< decltype(sound4cl_GetOutputCount ) >("GetOutputCount" );
507 fnGetAudio = GetPrefixSymbol< decltype(sound4cl_GetAudio ) >("GetAudio" );
508 fnAudioConvertFrom = GetPrefixSymbol< decltype(sound4cl_AudioConvertFrom ) >("AudioConvertFrom" );
509 fnAudioConvertTo = GetPrefixSymbol< decltype(sound4cl_AudioConvertTo ) >("AudioConvertTo" );
510 fnStereoToMono = GetPrefixSymbol< decltype(sound4cl_StereoToMono ) >("StereoToMono" );
511 fnMonoToStereo = GetPrefixSymbol< decltype(sound4cl_MonoToStereo ) >("MonoToStereo" );
512 fnAudioMonoFromLiveStereo = GetPrefixSymbol< decltype(sound4cl_AudioMonoFromLiveStereo) >("AudioMonoFromLiveStereo");
513 fnAudioMonoToLiveStereo = GetPrefixSymbol< decltype(sound4cl_AudioMonoToLiveStereo ) >("AudioMonoToLiveStereo" );
514 fnNewClient = GetPrefixSymbol< decltype(sound4cl_NewClient ) >("NewClient" );
515 fnDeleteClient = GetPrefixSymbol< decltype(sound4cl_DeleteClient ) >("DeleteClient" );
516 fnProcessJson = GetPrefixSymbol< decltype(sound4cl_ProcessJson ) >("ProcessJson" );
517 fnFreeJsonAnswer = GetPrefixSymbol< decltype(sound4cl_FreeJsonAnswer ) >("FreeJsonAnswer" );
518 fnSaveState = GetPrefixSymbol< decltype(sound4cl_SaveState ) >("SaveState" );
519 } catch (std::runtime_error& ) {
520 return false;
521 }
522 // Webserver : not for all projects
523 try {
524 fnWebserver_tcp = GetPrefixSymbol< decltype(sound4cl_Webserver_tcp ) >("Webserver_tcp" );
525 fnWebserver_tcp2 = GetPrefixSymbol< decltype(sound4cl_Webserver_tcp2 ) >("Webserver_tcp2" );
526 fnWebserver = GetPrefixSymbol< decltype(sound4cl_Webserver ) >("Webserver" );
527 fnWebserver_Stop = GetPrefixSymbol< decltype(sound4cl_Webserver_Stop ) >("Webserver_Stop" );
528 fnWebserver_Status = GetPrefixSymbol< decltype(sound4cl_Webserver_Status ) >("Webserver_Status" );
529 } catch (std::runtime_error& ) {
530 // Ignored, handler will take care
531 }
532 // Stereo/Mono conversion : must be present
533 try {
534 // added 2023-03-22
535 fnStereoToMono_Planar = GetPrefixSymbol< decltype(sound4cl_StereoToMono_Planar ) >("StereoToMono_Planar" );
536 fnMonoToStereo_Planar = GetPrefixSymbol< decltype(sound4cl_MonoToStereo_Planar ) >("MonoToStereo_Planar" );
537
538 } catch (std::runtime_error& ) {
539 return false;
540 }
541 // C functions allowed to be missing
542 try {
543 // added 2023-05-22
544 fnSetMetadata = GetPrefixSymbol< decltype(sound4cl_SetMetadata ) >("SetMetadata" );
545 fnGetMetadataInfos = GetPrefixSymbol< decltype(sound4cl_GetMetadataInfos ) >("GetMetadataInfos" );
546 fnFreeMetadataInfos = GetPrefixSymbol< decltype(sound4cl_FreeMetadataInfos ) >("FreeMetadataInfos" );
547 // added 2023-06-20
548 fnSetPresetManager = GetPrefixSymbol< decltype(sound4cl_SetPresetManager ) >("SetPresetManager" );
549 fnPresetManager_InformChange = GetPrefixSymbol< decltype(sound4cl_PresetManager_InformChange ) >("PresetManager_InformChange" );
550
551 // added 2023-09-21
552 fnGetPossibleChunkSizeInFrames = GetPrefixSymbol< decltype(sound4cl_GetPossibleChunkSizeInFrames ) >("GetPossibleChunkSizeInFrames" );
553 fnGetProcessChunkFrames = GetPrefixSymbol< decltype(sound4cl_GetProcessChunkFrames ) >("GetProcessChunkFrames" );
554 fnInitProcess3 = GetPrefixSymbol< decltype(sound4cl_InitProcess3 ) >("InitProcess3" );
555 } catch (std::runtime_error& ) {
556 // Ignored, handler will take care
557 }
558
559 // bus: not for all projects
560 try {
561 // added 2023-06-26
562 fnNewBus = GetPrefixSymbol< decltype(sound4cl_NewBus ) >("NewBus" );
563 fnFreeBus = GetPrefixSymbol< decltype(sound4cl_FreeBus ) >("FreeBus" );
564 fnSetInstanceBus = GetPrefixSymbol< decltype(sound4cl_SetInstanceBus ) >("SetInstanceBus" );
565 } catch (std::runtime_error& ) {
566 // Ignored, handler will take care
567 }
568
569 // Other optional
570 try {
571 // added 2024-02-19
572 fnSetMetadataMulti = GetPrefixSymbol< decltype(sound4cl_SetMetadataMulti ) >("SetMetadataMulti" );
573
574 } catch (std::runtime_error& ) {
575 // Ignored, handler will take care
576 }
577
578 // Other optional, web server only
579 try {
580 // added 2024-05-23
581 fnWebserver_SetAppHealth = GetPrefixSymbol< decltype(sound4cl_Webserver_SetAppHealth ) >("Webserver_SetAppHealth" );
582 fnWebserver_GetAppHealth = GetPrefixSymbol< decltype(sound4cl_Webserver_GetAppHealth ) >("Webserver_GetAppHealth" );
583 fnWebserver_FreeString = GetPrefixSymbol< decltype(sound4cl_Webserver_FreeString ) >("Webserver_FreeString" );
584 } catch (std::runtime_error& ) {
585 // Ignored, handler will take care
586 }
587
588#ifdef _WIN32
589 // Other optional, windows tracing
590 try {
591 // added 2025-05-28
592 fnSetInstanceTracing = GetPrefixSymbol< decltype(sound4cl_SetInstanceTracing ) >("SetInstanceTracing" );
593 fnSetInstanceTracingProcessActivity= GetPrefixSymbol< decltype(sound4cl_SetInstanceTracingProcessActivity ) >("SetInstanceTracingProcessActivity");
594 } catch (std::runtime_error& ) {
595 // Ignored, handler will take care
596 }
597#endif // _WIN32
598
599 m_bOK=true;
600 return true;
601 }
608 bool IsOk() const {
609 return m_bOK;
610 }
611
612 // --------------------------------------------------------------
619 // --------------------------------------------------------------
630 void AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, SampleFormat fmt)
631 { fnAudioConvertFrom(payload, output, nSpl, (sound4cl_SampleFormat)fmt); }
632
643 void AudioConvertTo(const float *input, uint8_t *payload, size_t nSpl, SampleFormat fmt)
644 { fnAudioConvertTo(input, payload, nSpl, (sound4cl_SampleFormat)fmt); }
645
655 void StereoToMono(const float *input, float *output, size_t nFrame)
656 { fnStereoToMono(input, output, nFrame); }
657
667 void MonoToStereo(const float *input, float *output, size_t nFrame)
668 { fnMonoToStereo(input, output, nFrame); }
669
680 void StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
681 { fnStereoToMono_Planar(inputL, inputR, output, nFrame); }
682
693 void MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
694 { fnMonoToStereo_Planar(input, outputL, outputR, nFrame); }
695
702 void AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
703 { fnAudioMonoFromLiveStereo(payload, output); }
704
711 void AudioMonoToLiveStereo(const float *input, uint8_t *payload)
712 { fnAudioMonoToLiveStereo(input, payload); }
713
721 std::string GetVersion()
722 { return std::string(fnGetVersion()); }
723
734 unsigned int GetChunkSizeInFrames()
735 { return fnGetChunkSizeInFrames(); }
736
745 std::vector<unsigned int> GetPossibleChunkSizeInFrames()
746 {
747 std::vector<unsigned int> list;
748 if (fnGetPossibleChunkSizeInFrames) {
749 for (unsigned int* src=fnGetPossibleChunkSizeInFrames();*src;src++) {
750 list.push_back(*src);
751 }
752 } else {
753 list.push_back(fnGetChunkSizeInFrames());
754 }
755 return list;
756 }
757
763 unsigned int GetChannelCount()
764 { return fnGetChannelCount(); }
765
773 unsigned int GetAudioInputCount()
774 { return fnGetAudioInputCount(); }
775
783 unsigned int GetAudioOutputCount()
784 { return fnGetAudioOutputCount(); }
785
793 unsigned int GetSampleRate()
794 { return fnGetSampleRate(); }
795
802 std::string GetFormatName( const SampleFormat fmt)
803 { return std::string(fnGetFormatName((sound4cl_SampleFormat)fmt)); }
804
811 SampleFormat GetFormatFromName( const std::string& name)
812 { return SampleFormat(fnGetFormatFromName(name.c_str())); }
813
820 unsigned int GetBytesFromFormat( const SampleFormat fmt)
821 { return fnGetBytesFromFormat((sound4cl_SampleFormat)fmt); }
822
827 { fnSetLogSeverity((sound4cl_LogSeverity)severity); }
828
833 { _log_cb=cb; fnSetLoggerCallback(&_log_cb_c); }
834 };
835
841 class CBus {
842 public:
844 : m_dynlib(dynlib)
845 {
846 if (m_dynlib.fnNewBus) {
847 m_bus=m_dynlib.fnNewBus();
848 }
849 }
851 {
852 if (m_bus && m_dynlib.fnFreeBus) {
853 m_dynlib.fnFreeBus(m_bus);
854 }
855 m_bus = nullptr;
856 }
857 protected:
858 friend class CInstance;
859 sound4cl_CBus *Get() const { return m_bus; }
860 private:
861 CProcessor& m_dynlib;
862 sound4cl_CBus *m_bus;
863 };
864
871 public:
872 CPresetLoader() = default;
873 virtual ~CPresetLoader() = default;
874
875 virtual bool IsReadOnly() = 0;
876 virtual bool Exists(const std::filesystem::path &name) = 0;
877 virtual bool Remove(const std::filesystem::path &name) = 0;
878 virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to) = 0;
879 virtual std::vector<std::filesystem::path> GetAll() = 0;
880 virtual std::string Read(const std::filesystem::path &filename) = 0;
881 virtual bool Write(const std::filesystem::path &filename, const std::string &content) =0;
882 };
893 class CInstance {
894 public:
901 : m_dynlib(dynlib)
902 {
903 params = m_dynlib.fnNewParameters();
904 if (!params) throw std::bad_alloc();
905 }
906
915 CInstance(CProcessor& dynlib, void* _instance)
916 : m_dynlib(dynlib)
917 , instance(reinterpret_cast<sound4cl_CInstance*>(_instance))
918 , m_bOwned(false)
919 {
920 }
921 // non-copyable
922 CInstance( const CInstance& ) = delete;
923 CInstance& operator=( const CInstance& ) = delete;
927 virtual ~CInstance()
928 {
929 Stop();
930 if (params) m_dynlib.fnFreeParameters(params);
931 }
935 bool IsOk() const { return instance!=nullptr; }
945 void SetParam(const std::string& name, const std::string& value)
946 {
947 assert(params);
948 m_dynlib.fnSetParameter(params, name.c_str(), value.c_str());
949 }
956 std::string GetParam(const std::string& name)
957 {
958 assert(params);
959 auto c_value=m_dynlib.fnGetParameter(params, name.c_str());
960 std::string ret(c_value);
961 m_dynlib.fnFreeParameterValue(c_value);
962 return ret;
963 }
964
973 void SetBus(const CBus& bus) {
974 assert(params);
975 if (m_dynlib.fnSetInstanceBus) {
976 m_dynlib.fnSetInstanceBus(params,bus.Get());
977 }
978 }
979
990 void SetPresetManager(CPresetLoader *preset_manager)
991 {
992 if (!preset_manager) return;
993 if (!m_dynlib.fnSetPresetManager) return;
994 assert(params);
995 m_dynlib.fnSetPresetManager(params,
996 sound4cl_custom_reader,
997 sound4cl_custom_reader_free,
998 sound4cl_custom_writer,
999 sound4cl_custom_exists,
1000 sound4cl_custom_getall,
1001 sound4cl_custom_getall_free,
1002 sound4cl_custom_remove,
1003 sound4cl_custom_rename,
1004 preset_manager->IsReadOnly(),
1005 preset_manager
1006 );
1007 }
1008
1031 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)
1032 {
1033 assert(!instance);
1034 std::string l_save_path;
1035 if (!save_path.empty()) {
1036 l_save_path = save_path.u8string();
1037 }
1038 if (frames_per_chunk==0) {
1039 frames_per_chunk=m_dynlib.GetChunkSizeInFrames();
1040 }
1041 if (m_dynlib.fnInitProcess3) {
1042 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);
1043 } else if (frames_per_chunk!=m_dynlib.GetChunkSizeInFrames()) {
1044 return false;
1045 } else {
1046 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);
1047 }
1048 if (!instance) return false;
1049 if (!init_metadata.empty()) {
1050 for (auto&& [key,value]: init_metadata) {
1051 m_dynlib.fnSetMetadata(instance, key.c_str(), value.c_str());
1052 }
1053 init_metadata.clear();
1054 }
1055 update_thread = std::thread([this, json_port](){
1057 m_dynlib.fnStartUpdateThread(instance, json_port);
1059 });
1060 int timeout=5000;
1061 #ifdef DEBUG
1062 timeout=60*1000; // For debugger pauses
1063 #endif
1064 if (m_dynlib.fnWaitUpdateThreadReady(instance, timeout)<0) {
1065 return false;
1066 }
1067 return true;
1068 }
1069
1077 unsigned int GetChunkFrames() {
1078 if (instance && m_dynlib.fnGetProcessChunkFrames) {
1079 return m_dynlib.fnGetProcessChunkFrames(instance);
1080 }
1081 return m_dynlib.fnGetChunkSizeInFrames();
1082 }
1083
1091 void PresetManager_InformChange(const std::filesystem::path& relative_path, PresetChange_Kind change_kind)
1092 {
1093 if (m_dynlib.fnPresetManager_InformChange) {
1094 m_dynlib.fnPresetManager_InformChange(instance, relative_path.c_str(), (sound4cl_PresetChange_Kind) change_kind);
1095 }
1096 }
1097
1109 void SetMetadata(const std::string& key, const char* value)
1110 {
1111 if (m_dynlib.fnSetMetadata) {
1112 if (instance) {
1113 m_dynlib.fnSetMetadata(instance, key.c_str(), value);
1114 } else {
1115 init_metadata.push_back( {key, value} );
1116 }
1117 }
1118 }
1119
1129 void SetMetadataMulti(const std::unordered_map<std::string, const char*>& list)
1130 {
1131 if (instance) {
1132 if (m_dynlib.fnSetMetadataMulti) {
1133 typedef const char* pchar;
1134 pchar* keyvalue=new pchar[2*list.size()+1];
1135 size_t n=0;
1136 for (auto&& [key,value]: list) {
1137 keyvalue[2*n+0]=key.c_str();
1138 keyvalue[2*n+1]=value;
1139 n++;
1140 }
1141 keyvalue[2*n+0]=nullptr;
1142
1143 sound4cl_SetMetadataMulti(instance, keyvalue);
1144
1145 delete[] keyvalue;
1146 } else {
1147 for (auto&& [key,value]: list) {
1148 SetMetadata(key, value);
1149 }
1150 }
1151 } else {
1152 for (auto&& [key,value]: list) {
1153 if (value) {
1154 init_metadata.push_back( {key, value} );
1155 }
1156 }
1157 }
1158 }
1159
1165 std::vector< std::tuple<std::string,std::string> > GetMetadataInfos()
1166 {
1167 std::vector< std::tuple<std::string,std::string> > values;
1168 if (m_dynlib.fnGetMetadataInfos) {
1169 const char** c_values = m_dynlib.fnGetMetadataInfos(instance);
1170 if (c_values) {
1171 for (const char** c_value=c_values; *c_value; ) {
1172 std::string key(*c_value);
1173 c_value++;
1174 if (*c_value) {
1175 std::string descr(*c_value);
1176 c_value++;
1177 values.push_back( {key,descr} );
1178 }
1179 }
1180 m_dynlib.fnFreeMetadataInfos(instance, c_values);
1181 }
1182 }
1183 return values;
1184 }
1185
1189 virtual void OnUpdateThreadStart() {}
1193 virtual void OnUpdateThreadStop() {}
1194
1195
1209 bool StartWebServer(int http_port, int https_port=0)
1210 {
1211 assert(instance);
1212 assert(webserver==SOUND4_INVALID_WEBSERVER_ID);
1213 if (!m_dynlib.fnWebserver) {
1214 return false;
1215 }
1216 webserver = m_dynlib.fnWebserver(http_port,https_port,instance);
1217 if (webserver==SOUND4_INVALID_WEBSERVER_ID) {
1218 return false;
1219 }
1220 int web_status=m_dynlib.fnWebserver_Status(webserver);
1221 if (http_port && (web_status & SOUND4_WEBSERVER_HTTP_OK) == 0) {
1222 return false;
1223 }
1224 if (https_port && (web_status & SOUND4_WEBSERVER_HTTPS_OK) == 0) {
1225 return false;
1226 }
1227 return true;
1228 }
1229
1237 void StopWebServer(int timeout_ms = 1000)
1238 {
1239 if (!m_dynlib.fnWebserver_Stop) return;
1240 if (webserver != SOUND4_INVALID_WEBSERVER_ID) {
1241 m_dynlib.fnWebserver_Stop(webserver, timeout_ms);
1242 webserver = SOUND4_INVALID_WEBSERVER_ID;
1243 }
1244 }
1245
1249 void SetWebServerAppHealth(int httpcode, const std::string& contenttype, const std::string& content)
1250 {
1251 assert(instance);
1252 if (!m_dynlib.fnWebserver_SetAppHealth) return;
1253 m_dynlib.fnWebserver_SetAppHealth(instance, httpcode, contenttype.c_str(), content.c_str());
1254 }
1258 void GetWebServerAppHealth(int &httpcode, std::string& contenttype, std::string& content)
1259 {
1260 assert(instance);
1261 if (!m_dynlib.fnWebserver_GetAppHealth) return;
1262 char* c_contenttype=nullptr;
1263 char* c_content=nullptr;
1264 m_dynlib.fnWebserver_GetAppHealth(instance, &httpcode, &c_contenttype, &c_content);
1265 contenttype=c_contenttype;
1266 content=c_content;
1267 m_dynlib.fnWebserver_FreeString(c_contenttype);
1268 m_dynlib.fnWebserver_FreeString(c_content);
1269 }
1279 {
1280 if (!instance) return 0;
1281 return m_dynlib.fnTerminateProcess(instance);
1282 }
1283
1289 void Stop()
1290 {
1291 if (!instance) return;
1292 if (m_bOwned) {
1293 StopWebServer();
1294 if (update_thread.joinable()) {
1295 m_dynlib.fnStopUpdateThread(instance); // this will exit the update thread
1296 update_thread.join();
1297 }
1298 m_dynlib.fnExitProcess(instance);
1299 }
1300 instance=nullptr;
1301 }
1302
1312 unsigned int GetEstimatedDelay()
1313 {
1314 assert(instance);
1315 return m_dynlib.fnGetEstimatedDelay(instance);
1316 }
1317
1331 {
1332 assert(instance);
1333 return m_dynlib.fnGetBufferIn(instance);
1334
1335 }
1348 {
1349 assert(instance);
1350 return m_dynlib.fnGetBufferOut(instance);
1351 }
1356 {
1357 assert(instance);
1358 m_dynlib.fnProcessAudio(instance, GetBufferIn(), GetBufferOut());
1359 }
1371 void ProcessAudio_Planar(float const * const *input, float * const *output)
1372 {
1373 assert(instance);
1374 m_dynlib.fnProcessAudio_Planar(instance, input, output);
1375 }
1376
1388 template<typename T>
1389 std::vector<T> ProcessAnyAudio(const std::vector<T> input)
1390 {
1391 assert(instance);
1392 std::vector<T> output;
1393 unsigned int out_offset=0;
1394 unsigned int in_offset=0;
1395 unsigned int todo = input.size();
1396 while (todo>0) {
1397 unsigned int left = AddAudio(&input[in_offset], todo);
1398 unsigned int out_avail = m_dynlib.fnGetOutputCount(instance);
1399 output.resize(out_offset + out_avail);
1400 GetAudio(&output[out_offset], out_avail);
1401 out_offset+=out_avail;
1402 in_offset += todo-left;
1403 todo=left;
1404 }
1405 return output;
1406 }
1407
1413 class CClient {
1414 public:
1415 CClient(sound4cl_CInstance* instance, CProcessor& dynlib)
1416 : m_dynlib(dynlib)
1417 {
1418 assert(instance);
1419 client=m_dynlib.fnNewClient(instance);
1420 if (!client) throw std::bad_alloc();
1421 }
1422 // non-copyable
1423 CClient( const CInstance& ) = delete;
1424 CClient& operator=( const CInstance& ) = delete;
1426 {
1427 if (client) {
1428 m_dynlib.fnDeleteClient(client);
1429 client=nullptr;
1430 }
1431 }
1439 std::string ProcessJson(const std::string &request, bool *NeedSave = nullptr)
1440 {
1441 assert(client);
1442 int need_save=0;
1443 const char *canswer = m_dynlib.fnProcessJson(client, request.c_str(), &need_save);
1444 if (!canswer) return {};
1445 std::string answer(canswer);
1446 m_dynlib.fnFreeJsonAnswer (canswer);
1447 if (NeedSave) {
1448 *NeedSave=(need_save!=0);
1449 }
1450 return answer;
1451 }
1452 private:
1453 CProcessor& m_dynlib;
1454 sound4cl_CClientInstance *client = nullptr;
1455 };
1463 std::shared_ptr<CClient> NewClient()
1464 {
1465 assert(instance);
1466 return std::make_shared<CClient>(instance, m_dynlib);
1467 }
1468
1479 bool SaveState() {
1480 assert(instance);
1481 if (!m_dynlib.fnSaveState || m_dynlib.fnSaveState(instance)!=0) {
1482 return false;
1483 }
1484 return true;
1485 }
1486#ifdef _WIN32
1493 void SetInstanceTracing(TraceLoggingHProvider tracing_provider, GUID activity_guid = {}) {
1494 if (m_dynlib.fnSetInstanceTracing) {
1495 m_dynlib.fnSetInstanceTracing(params, tracing_provider, activity_guid);
1496 }
1497 }
1504 void SetInstanceTracingProcessActivity(GUID activity_guid) {
1505 assert(instance);
1506 if (m_dynlib.fnSetInstanceTracingProcessActivity) {
1507 m_dynlib.fnSetInstanceTracingProcessActivity(instance, activity_guid);
1508 }
1509 }
1510#endif // _WIN32
1511 protected:
1512 template<typename T>
1513 unsigned int AddAudio(const T* payload, unsigned int nFrame)
1514 {
1515 assert(instance);
1516 return m_dynlib.fnAddAudio(instance, reinterpret_cast<const T*>(payload), nFrame, helper::SampleFormat<T>::format);
1517 }
1518 template<typename T>
1519 unsigned int GetAudio(T* payload, unsigned int max_nFrame)
1520 {
1521 assert(instance);
1522 return m_dynlib.fnGetAudio(instance, reinterpret_cast<T*>(payload), max_nFrame, helper::SampleFormat<T>::format);
1523 }
1524
1525 private:
1526 CProcessor& m_dynlib;
1527 sound4cl_CParameters* params = nullptr;
1528 sound4cl_CInstance* instance = nullptr;
1529 std::thread update_thread;
1530 std::vector< std::pair<std::string,std::string> > init_metadata;
1531 uint64_t webserver = SOUND4_INVALID_WEBSERVER_ID;
1532 bool m_bOwned = true;
1533 };
1534
1535}; // namespace sound4
1536
1537
1538// bridge C callbacks to CPresetLoader
1539extern "C" {
1540 static char *sound4cl_custom_reader(const fs_char *filename, void* userdata) {
1541 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1542 std::string content=preset_loader->Read(filename);
1543 return strdup(content.c_str());
1544 }
1545 static void sound4cl_custom_reader_free(char *content, void* userdata) {
1546 if (!content) return;
1547 free(content);
1548 }
1549 static int sound4cl_custom_writer(const fs_char *filename, const char *content, void* userdata) {
1550 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1551 auto res=preset_loader->Write(filename,content);
1552 return res?0:-1;
1553 }
1554 static int sound4cl_custom_exists(const fs_char *filename, void* userdata) {
1555 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1556 auto res=preset_loader->Exists(filename);
1557 return res?0:-1;
1558 }
1559 static fs_char** sound4cl_custom_getall(void* userdata) {
1560 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1561 auto res=preset_loader->GetAll();
1562 fs_char**all=(fs_char**)malloc((res.size()+1)*sizeof(const fs_char*));
1563 for (size_t n=0;n<res.size();n++) {
1564 all[n]=fs_strdup(res[n].c_str());
1565 }
1566 all[res.size()]=nullptr;
1567 return all;
1568 }
1569 static void sound4cl_custom_getall_free(fs_char** all, void* userdata) {
1570 if (!all) return;
1571 for (fs_char** one=all;*one;one++) {
1572 free(*one);
1573 }
1574 free(all);
1575 }
1576 static int sound4cl_custom_remove(const fs_char *filename, void* userdata) {
1577 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1578 auto res=preset_loader->Remove(filename);
1579 return res?0:-1;
1580 }
1581 static int sound4cl_custom_rename(const fs_char *from, const fs_char *to, void* userdata) {
1582 sound4::CPresetLoader *preset_loader=reinterpret_cast<sound4::CPresetLoader*>(userdata);
1583 auto res=preset_loader->Rename(from,to);
1584 return res?0:-1;
1585 }
1586
1587};
Shared Bus.
Definition sound4cl.hpp:841
sound4cl_CBus * Get() const
Definition sound4cl.hpp:859
CBus(CProcessor &dynlib)
Definition sound4cl.hpp:843
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:893
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:927
CInstance & operator=(const CInstance &)=delete
CInstance(const CInstance &)=delete
void GetWebServerAppHealth(int &httpcode, std::string &contenttype, std::string &content)
void SetPresetManager(CPresetLoader *preset_manager)
Definition sound4cl.hpp:990
void SetBus(const CBus &bus)
Definition sound4cl.hpp:973
virtual void OnUpdateThreadStop()
unsigned int GetChunkFrames()
CInstance(CProcessor &dynlib, void *_instance)
Definition sound4cl.hpp:915
void SetWebServerAppHealth(int httpcode, const std::string &contenttype, const std::string &content)
CInstance(CProcessor &dynlib)
Definition sound4cl.hpp:900
void SetParam(const std::string &name, const std::string &value)
Definition sound4cl.hpp:945
virtual void OnUpdateThreadStart()
bool IsOk() const
Definition sound4cl.hpp:935
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:956
void SetInstanceTracingProcessActivity(GUID activity_guid)
Custom preset handler helper.
Definition sound4cl.hpp:870
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:302
bool Load(const std::filesystem::path &filepath)
Loads the library.
Definition sound4cl.hpp:417
void SetLogSeverity(LogSeverity severity)
Definition sound4cl.hpp:826
unsigned int GetAudioInputCount()
Definition sound4cl.hpp:773
void SetLoggerCallback(log_cb_t cb)
Definition sound4cl.hpp:832
unsigned int GetChunkSizeInFrames()
Definition sound4cl.hpp:734
SampleFormat GetFormatFromName(const std::string &name)
Definition sound4cl.hpp:811
std::vector< unsigned int > GetPossibleChunkSizeInFrames()
Definition sound4cl.hpp:745
std::string GetFormatName(const SampleFormat fmt)
Definition sound4cl.hpp:802
unsigned int GetBytesFromFormat(const SampleFormat fmt)
Definition sound4cl.hpp:820
unsigned int GetAudioOutputCount()
Definition sound4cl.hpp:783
helper::DynFuncHolder< T > GetPrefixSymbol(const std::string &name)
Definition sound4cl.hpp:402
unsigned int GetSampleRate()
Definition sound4cl.hpp:793
std::string GetVersion()
Definition sound4cl.hpp:721
bool IsOk() const
Check if the library was loaded correctly.
Definition sound4cl.hpp:608
unsigned int GetChannelCount()
Definition sound4cl.hpp:763
CProcessor()=default
Helper to load dynamic library.
Definition sound4cl.hpp:155
DynFuncHolder< T > GetSymbol_safe(const std::string &name)
Definition sound4cl.hpp:240
CDynLoader & operator=(CDynLoader const &)=delete
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:213
CDynLoader(const CDynLoader &)=delete
static std::filesystem::path GetThisLibraryPath(void)
Definition sound4cl.hpp:244
CDynLoader(CDynLoader &&)=default
DynFuncHolder< T > GetSymbol(const std::string &name)
Definition sound4cl.hpp:232
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:643
void AudioMonoToLiveStereo(const float *input, uint8_t *payload)
Definition sound4cl.hpp:711
void StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
Definition sound4cl.hpp:680
void MonoToStereo(const float *input, float *output, size_t nFrame)
Definition sound4cl.hpp:667
void AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, SampleFormat fmt)
Definition sound4cl.hpp:630
void StereoToMono(const float *input, float *output, size_t nFrame)
Definition sound4cl.hpp:655
void AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
Definition sound4cl.hpp:702
void MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
Definition sound4cl.hpp:693
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:270
@ verbose
verbose
Definition sound4cl.hpp:274
@ none
Not used.
Definition sound4cl.hpp:269
@ verbose3
verbose3
Definition sound4cl.hpp:276
@ verbose5
verbose5
Definition sound4cl.hpp:278
@ verbose2
verbose2
Definition sound4cl.hpp:275
@ warning
warning
Definition sound4cl.hpp:272
@ error
error
Definition sound4cl.hpp:271
@ info
info
Definition sound4cl.hpp:273
@ verbose4
verbose4
Definition sound4cl.hpp:277
static void _log_cb_c(sound4cl_LogSeverity severity, const char *c_msg)
Definition sound4cl.hpp:291
std::function< void(LogSeverity, const std::string &)> log_cb_t
Definition sound4cl.hpp:284
static log_cb_t _log_cb
Definition sound4cl.hpp:289
PresetChange_Kind
Definition sound4cl.hpp:883
@ change_kind_created
has been created
Definition sound4cl.hpp:884
@ change_kind_deleted
has been deleted
Definition sound4cl.hpp:886
@ change_kind_modified
has been modified
Definition sound4cl.hpp:885
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