22 #include <stringapiset.h>
25 #define fs_strdup wcsdup
28 #define fs_strdup strdup
33 static char *plughost_custom_reader(
const fs_char *filename,
void* userdata);
34 static void plughost_custom_reader_free(
char *content,
void* userdata);
35 static int plughost_custom_writer(
const fs_char *filename,
const char *content,
void* userdata);
36 static int plughost_custom_exists(
const fs_char *filename,
void* userdata);
37 static fs_char** plughost_custom_getall(
void* userdata);
38 static void plughost_custom_getall_free(
fs_char** all,
void* userdata);
39 static int plughost_custom_remove(
const fs_char *filename,
void* userdata);
40 static int plughost_custom_rename(
const fs_char *from,
const fs_char *to,
void* userdata);
51 static constexpr
const char *process_name =
"SOUND4 Plugin Host";
52 static constexpr
const char *process_shortname =
"plughost";
89 static inline std::string WStringToUTF8(
const std::wstring& wstr) {
90 if (wstr.empty())
return {};
91 int size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (
int)wstr.size(), NULL, 0, NULL, NULL);
92 std::string str(size, 0);
93 WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (
int)wstr.size(), &str[0], size, NULL, NULL);
101 static inline std::wstring UTF8ToWString(
const std::string& str) {
102 if (str.empty())
return std::wstring();
103 int size = MultiByteToWideChar(CP_UTF8, 0, &str[0], (
int)str.size(), NULL, 0);
104 std::wstring wstr(size, 0);
105 MultiByteToWideChar(CP_UTF8, 0, &str[0], (
int)str.size(), &wstr[0], size);
136 static inline void AudioConvertFrom(
const uint8_t *payload,
float *output,
size_t nSpl,
plughost_SampleFormat fmt) {
139 static inline void AudioConvertTo(
const float *input, uint8_t *payload,
size_t nSpl,
plughost_SampleFormat fmt) {
142 static inline void StereoToMono(
const float *input,
float *output,
size_t nSpl) {
145 static inline void MonoToStereo(
const float *input,
float *output,
size_t nSpl) {
148 static inline void StereoToMono_Planar(
const float *inputL,
const float *inputR,
float *output,
size_t nSpl) {
151 static inline void MonoToStereo_Planar(
const float *input,
float *outputL,
float *outputR,
size_t nSpl) {
154 static inline void AudioMonoFromLiveStereo(
const uint8_t *payload,
float *output) {
157 static inline void AudioMonoToLiveStereo(
const float *input, uint8_t *payload) {
168 static inline std::string GetVersion()
183 static inline unsigned int GetChunkSizeInFrames()
196 static inline std::vector<unsigned int> GetPossibleChunkSizeInFrames()
198 std::vector<unsigned int> list;
200 list.push_back(*src);
212 static inline unsigned int GetChannelCount()
225 static inline unsigned int GetAudioInputCount()
238 static inline unsigned int GetAudioOutputCount()
250 static inline unsigned int GetSampleRate()
260 static inline void SanityCheck(
bool a_bCheckFrames =
true)
264 throw std::runtime_error(
"Bad library sampling rate");
267 throw std::runtime_error(
"Bad library frame size");
270 throw std::runtime_error(
"Bad library channel count");
273 throw std::runtime_error(
"Bad library input count");
276 throw std::runtime_error(
"Bad library output count");
286 static inline std::string GetFormatName(
const SampleFormat fmt)
297 static inline SampleFormat GetFormatFromName(
const std::string& name)
308 static inline unsigned int GetBytesFromFormat(
const SampleFormat fmt)
328 _log_cb(severity, std::string(c_msg));
334 static inline void SetLogSeverity(
LogSeverity severity)
341 static inline void SetLoggerCallback(
log_cb_t cb)
347 #if PLUGHOST_HAS_CLOUDBUS
351 : m_bus(plughost_NewBus()) {}
354 plughost_FreeBus(m_bus);
357 plughost_CBus *Get()
const {
return m_bus; }
359 plughost_CBus *m_bus;
373 virtual bool Exists(
const std::filesystem::path &name) = 0;
374 virtual bool Remove(
const std::filesystem::path &name) = 0;
375 virtual bool Rename(
const std::filesystem::path &from,
const std::filesystem::path &to) = 0;
376 virtual std::vector<std::filesystem::path>
GetAll() = 0;
377 virtual std::string
Read(
const std::filesystem::path &filename) = 0;
378 virtual bool Write(
const std::filesystem::path &filename,
const std::string &content) =0;
393 if (!params)
throw std::bad_alloc();
409 bool IsOk()
const {
return instance!=
nullptr; }
417 void SetParam(
const std::string& name,
const std::string& value)
431 std::string ret(c_value);
447 if (!preset_manager)
return;
450 plughost_custom_reader,
451 plughost_custom_reader_free,
452 plughost_custom_writer,
453 plughost_custom_exists,
454 plughost_custom_getall,
455 plughost_custom_getall_free,
456 plughost_custom_remove,
457 plughost_custom_rename,
463 #if PLUGHOST_HAS_CLOUDBUS
467 void SetBus(
const CBus& bus) {
469 plughost_SetInstanceBus(params,bus.Get());
487 bool Create(
const std::string& LoginKey,
const std::string& RadioName,
const std::string& AWS_Access_ID,
const std::string& AWS_Access_Secret,
const std::filesystem::path& save_path,
int json_port = 0,
unsigned int frames_per_chunk=64)
490 std::string l_save_path;
491 if (!save_path.empty()) {
492 l_save_path = save_path.u8string();
494 instance =
plughost_InitProcess3(LoginKey.c_str(), RadioName.c_str(), AWS_Access_ID.c_str(), AWS_Access_Secret.c_str(), l_save_path.c_str(), params, frames_per_chunk);
495 if (!instance)
return false;
496 if (!init_metadata.empty()) {
497 for (
auto&& [key,value]: init_metadata) {
500 init_metadata.clear();
502 update_thread = std::thread([
this, json_port](){
559 init_metadata.push_back( {key, value} );
570 std::vector< std::tuple<std::string,std::string> > values;
573 for (
const char** c_value=c_values; *c_value; ) {
574 std::string key(*c_value);
577 std::string descr(*c_value);
579 values.push_back( {key,descr} );
592 #if PLUGHOST_HAS_WEBSERVER
637 if (!instance)
return 0;
648 if (!instance)
return;
649 #if PLUGHOST_HAS_WEBSERVER
652 if (update_thread.joinable()) {
654 update_thread.join();
685 return reinterpret_cast< std::array<float,InputSampleSize>&
>(*buf);
702 return reinterpret_cast< std::array<float,OutputSampleSize>&
>(*buf);
749 std::vector<T> output;
750 unsigned int out_offset=0;
751 unsigned int in_offset=0;
752 unsigned int todo = input.size();
754 unsigned int left =
AddAudio(&input[in_offset], todo);
756 output.resize(out_offset + out_avail);
757 GetAudio(&output[out_offset], out_avail);
758 out_offset+=out_avail;
759 in_offset += todo-left;
776 if (!client)
throw std::bad_alloc();
795 std::string
ProcessJson(
const std::string &request,
bool *NeedSave =
nullptr)
800 if (!canswer)
return {};
801 std::string answer(canswer);
804 *NeedSave=(need_save!=0);
821 return std::make_shared<CClient>(instance);
826 unsigned int AddAudio(
const T* payload,
unsigned int nFrame)
832 unsigned int GetAudio(T* payload,
unsigned int max_nFrame)
841 std::thread update_thread;
842 std::vector< std::pair<std::string,std::string> > init_metadata;
843 #if PLUGHOST_HAS_WEBSERVER
853 static char *plughost_custom_reader(
const fs_char *filename,
void* userdata) {
855 std::string content=preset_loader->
Read(filename);
856 return strdup(content.c_str());
858 static void plughost_custom_reader_free(
char *content,
void* userdata) {
859 if (!content)
return;
862 static int plughost_custom_writer(
const fs_char *filename,
const char *content,
void* userdata) {
864 auto res=preset_loader->
Write(filename,content);
867 static int plughost_custom_exists(
const fs_char *filename,
void* userdata) {
869 auto res=preset_loader->
Exists(filename);
872 static fs_char** plughost_custom_getall(
void* userdata) {
874 auto res=preset_loader->
GetAll();
876 for (
size_t n=0;n<res.size();n++) {
879 all[res.size()]=
nullptr;
882 static void plughost_custom_getall_free(
fs_char** all,
void* userdata) {
884 for (
fs_char** one=all;*one;one++) {
889 static int plughost_custom_remove(
const fs_char *filename,
void* userdata) {
891 auto res=preset_loader->
Remove(filename);
894 static int plughost_custom_rename(
const fs_char *from,
const fs_char *to,
void* userdata) {
896 auto res=preset_loader->
Rename(from,to);
CClient(const CInstance &)=delete
CClient(plughost_CInstance *instance)
CClient & operator=(const CInstance &)=delete
std::string ProcessJson(const std::string &request, bool *NeedSave=nullptr)
Process a JSON request and returns the answer.
void SetPresetManager(CPresetLoader *preset_manager)
std::array< float, OutputSampleSize > & GetBufferOut()
unsigned int GetChunkFrames()
bool StartWebServer(int http_port, int https_port=0)
bool Create(const std::string &LoginKey, const std::string &RadioName, const std::string &AWS_Access_ID, const std::string &AWS_Access_Secret, const std::filesystem::path &save_path, int json_port=0, unsigned int frames_per_chunk=64)
void ProcessAudio_Planar(float const *const *input, float *const *output)
std::string GetParam(const std::string &name)
unsigned int GetAudio(T *payload, unsigned int max_nFrame)
void SetParam(const std::string &name, const std::string &value)
CInstance & operator=(const CInstance &)=delete
virtual void OnUpdateThreadStop()
unsigned int GetEstimatedDelay()
unsigned int AddAudio(const T *payload, unsigned int nFrame)
void PresetManager_InformChange(const std::filesystem::path &relative_path, PresetChange_Kind change_kind)
virtual void OnUpdateThreadStart()
std::shared_ptr< CClient > NewClient()
CInstance(const CInstance &)=delete
void SetMetadata(const std::string &key, const char *value)
std::array< float, InputSampleSize > & GetBufferIn()
std::vector< std::tuple< std::string, std::string > > GetMetadataInfos()
std::vector< T > ProcessAnyAudio(const std::vector< T > input)
virtual ~CPresetLoader()=default
virtual std::string Read(const std::filesystem::path &filename)=0
virtual bool Remove(const std::filesystem::path &name)=0
virtual bool Exists(const std::filesystem::path &name)=0
virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to)=0
virtual bool Write(const std::filesystem::path &filename, const std::string &content)=0
virtual bool IsReadOnly()=0
virtual std::vector< std::filesystem::path > GetAll()=0
enum plughost_SampleFormat plughost_GetFormatFromName(const char *name)
unsigned int plughost_GetOutputCount(struct plughost_CInstance *instance)
unsigned int plughost_GetBytesFromFormat(const enum plughost_SampleFormat fmt)
unsigned int plughost_AddAudio(struct plughost_CInstance *instance, const uint8_t *payload, unsigned int nFrame, enum plughost_SampleFormat fmt)
unsigned int plughost_GetAudio(struct plughost_CInstance *instance, uint8_t *payload, unsigned int max_nFrame, enum plughost_SampleFormat fmt)
const char * plughost_GetFormatName(const enum plughost_SampleFormat fmt)
@ F32_NATIVE
32-bit floating-point, native
@ S16_NATIVE
16-bit signed integer, native
@ S32_NATIVE
32-bit signed integer, native
void plughost_DeleteClient(struct plughost_CClientInstance *client)
void plughost_FreeJsonAnswer(const char *json_str)
struct plughost_CClientInstance * plughost_NewClient(struct plughost_CInstance *instance)
const char * plughost_ProcessJson(struct plughost_CClientInstance *client, const char *json_str, int *need_save)
void plughost_AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, enum plughost_SampleFormat fmt)
void plughost_StereoToMono(const float *input, float *output, size_t nFrame)
void plughost_MonoToStereo(const float *input, float *output, size_t nFrame)
void plughost_AudioMonoToLiveStereo(const float *input, uint8_t *payload)
void plughost_StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
void plughost_AudioConvertTo(const float *input, uint8_t *payload, size_t nSpl, enum plughost_SampleFormat fmt)
void plughost_MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
void plughost_AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
float * plughost_GetBufferIn(struct plughost_CInstance *instance)
void plughost_ProcessAudio_Planar(struct plughost_CInstance *instance, float const *const *input, float *const *output)
void plughost_ProcessAudio(struct plughost_CInstance *instance, const float *input, float *output)
float * plughost_GetBufferOut(struct plughost_CInstance *instance)
unsigned int plughost_GetEstimatedDelay(struct plughost_CInstance *instance)
unsigned int plughost_GetAudioOutputCount()
#define PLUGHOST_CHANNEL_COUNT
const char * plughost_GetVersion()
unsigned int plughost_GetSampleRate()
#define PLUGHOST_AUDIO_INPUT_COUNT
unsigned int plughost_GetChannelCount()
unsigned int * plughost_GetPossibleChunkSizeInFrames()
#define PLUGHOST_AUDIOFRAME_COUNT
void plughost_SetLogSeverity(enum plughost_LogSeverity severity)
unsigned int plughost_GetAudioInputCount()
#define PLUGHOST_AUDIO_OUTPUT_COUNT
#define PLUGHOST_SAMPLE_RATE
unsigned int plughost_GetChunkSizeInFrames()
void plughost_SetLoggerCallback(plughost_loggerfn logger)
const char * plughost_GetParameter(struct plughost_CParameters *params, const char *name)
void plughost_FreeParameterValue(const char *value)
int plughost_TerminateProcess(struct plughost_CInstance *instance)
struct plughost_CParameters * plughost_NewParameters()
struct plughost_CInstance * plughost_InitProcess3(const char *LoginKey, const char *RadioName, const char *AWS_Access_ID, const char *AWS_Access_Secret, const char *save_path, const struct plughost_CParameters *parameters, unsigned int frames_per_chunk)
void plughost_ExitProcess(struct plughost_CInstance *instance)
void plughost_FreeParameters(struct plughost_CParameters *params)
void plughost_SetParameter(struct plughost_CParameters *params, const char *name, const char *value)
unsigned int plughost_GetProcessChunkFrames(struct plughost_CInstance *instance)
void plughost_SetPresetManager(struct plughost_CParameters *params, plughost_storage_reader reader, plughost_storage_reader_free, plughost_storage_writer writer, plughost_storage_exists exists, plughost_storage_getall getall, plughost_storage_getall_free getall_free, plughost_storage_remove remove, plughost_storage_rename rename, int IsReadOnly, void *userdata)
plughost_PresetChange_Kind
void plughost_PresetManager_InformChange(struct plughost_CInstance *instance, const fs_char *relative_path, enum plughost_PresetChange_Kind change_kind)
void plughost_StartUpdateThread(struct plughost_CInstance *instance, unsigned int port)
int plughost_WaitUpdateThreadReady(struct plughost_CInstance *instance, int milliseconds)
void plughost_StopUpdateThread(struct plughost_CInstance *instance)
#define SOUND4_WEBSERVER_HTTPS_OK
int plughost_Webserver_Status(uint64_t id)
int plughost_Webserver_Stop(uint64_t id, int timeout_ms)
#define SOUND4_WEBSERVER_HTTP_OK
#define SOUND4_INVALID_WEBSERVER_ID
uint64_t plughost_Webserver(unsigned int listenport, unsigned int listenport_secure, struct plughost_CInstance *instance)
const size_t OutputSampleSize
plughost_LogSeverity LogSeverity
const size_t OutputChannels
const size_t InputChannels
plughost_SampleFormat SampleFormat
const size_t InputSampleSize
std::function< void(LogSeverity, const std::string &)> log_cb_t
#define fs_strdup
helper for OS dependant file name copy