22 #include <stringapiset.h>
25 #define fs_strdup wcsdup
28 #define fs_strdup strdup
96 if (wstr.empty())
return {};
97 int size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (
int)wstr.size(), NULL, 0, NULL, NULL);
98 std::string str(size, 0);
99 WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (
int)wstr.size(), &str[0], size, NULL, NULL);
108 if (str.empty())
return std::wstring();
109 int size = MultiByteToWideChar(CP_UTF8, 0, &str[0], (
int)str.size(), NULL, 0);
110 std::wstring wstr(size, 0);
111 MultiByteToWideChar(CP_UTF8, 0, &str[0], (
int)str.size(), &wstr[0], size);
175 static inline void StereoToMono(
const float *input,
float *output,
size_t nFrame) {
188 static inline void MonoToStereo(
const float *input,
float *output,
size_t nFrame) {
202 static inline void StereoToMono_Planar(
const float *inputL,
const float *inputR,
float *output,
size_t nFrame) {
216 static inline void MonoToStereo_Planar(
const float *input,
float *outputL,
float *outputR,
size_t nFrame) {
277 std::vector<unsigned int> list;
279 list.push_back(*src);
339 throw std::runtime_error(
"Bad library sampling rate");
342 throw std::runtime_error(
"Bad library frame size");
345 throw std::runtime_error(
"Bad library channel count");
348 throw std::runtime_error(
"Bad library input count");
351 throw std::runtime_error(
"Bad library output count");
403 _log_cb(severity, std::string(c_msg));
422#if BIGVOICE_HAS_CLOUDBUS
454 virtual bool Exists(
const std::filesystem::path &name) = 0;
455 virtual bool Remove(
const std::filesystem::path &name) = 0;
456 virtual bool Rename(
const std::filesystem::path &from,
const std::filesystem::path &to) = 0;
457 virtual std::vector<std::filesystem::path>
GetAll() = 0;
458 virtual std::string
Read(
const std::filesystem::path &filename) = 0;
459 virtual bool Write(
const std::filesystem::path &filename,
const std::string &content) =0;
476 if (!params)
throw std::bad_alloc();
492 bool IsOk()
const {
return instance!=
nullptr; }
502 void SetParam(
const std::string& name,
const std::string& value)
517 std::string ret(c_value);
522#if BIGVOICE_HAS_CLOUDBUS
549 if (!preset_manager)
return;
587 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=12)
590 std::string l_save_path;
591 if (!save_path.empty()) {
592 l_save_path = save_path.u8string();
594 instance =
bigvoice_InitProcess3(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);
595 if (!instance)
return false;
596 if (!init_metadata.empty()) {
597 for (
auto&& [key,value]: init_metadata) {
600 init_metadata.clear();
602 update_thread = std::thread([
this, json_port](){
659 init_metadata.push_back( {key, value} );
675 typedef const char* pchar;
676 pchar* keyvalue=
new pchar[2*list.size()+1];
678 for (
auto&& [key,value]: list) {
679 keyvalue[2*n+0]=key.c_str();
680 keyvalue[2*n+1]=value;
683 keyvalue[2*n+0]=
nullptr;
689 for (
auto&& [key,value]: list) {
691 init_metadata.push_back( {key, value} );
704 std::vector< std::tuple<std::string,std::string> > values;
707 for (
const char** c_value=c_values; *c_value; ) {
708 std::string key(*c_value);
711 std::string descr(*c_value);
713 values.push_back( {key,descr} );
730#if BIGVOICE_HAS_WEBSERVER
791 char* c_contenttype=
nullptr;
792 char* c_content=
nullptr;
794 contenttype=c_contenttype;
811 if (!instance)
return 0;
820 if (!instance)
return;
821#if BIGVOICE_HAS_WEBSERVER
824 if (update_thread.joinable()) {
826 update_thread.join();
863 return reinterpret_cast< std::array<float,InputSampleSize>&
>(*buf);
881 return reinterpret_cast< std::array<float,OutputSampleSize>&
>(*buf);
923 std::vector<T> output;
924 unsigned int out_offset=0;
925 unsigned int in_offset=0;
926 unsigned int todo = input.size();
928 unsigned int left =
AddAudio(&input[in_offset], todo);
930 output.resize(out_offset + out_avail);
931 GetAudio(&output[out_offset], out_avail);
932 out_offset+=out_avail;
933 in_offset += todo-left;
950 if (!client)
throw std::bad_alloc();
969 std::string
ProcessJson(
const std::string &request,
bool *NeedSave =
nullptr)
974 if (!canswer)
return {};
975 std::string answer(canswer);
978 *NeedSave=(need_save!=0);
995 return std::make_shared<CClient>(instance);
1017 template<
typename T>
1018 unsigned int AddAudio(
const T* payload,
unsigned int nFrame)
1023 template<
typename T>
1024 unsigned int GetAudio(T* payload,
unsigned int max_nFrame)
1033 std::thread update_thread;
1034 std::vector< std::pair<std::string,std::string> > init_metadata;
1035#if BIGVOICE_HAS_WEBSERVER
1047 std::string content=preset_loader->
Read(filename);
1048 return strdup(content.c_str());
1051 if (!content)
return;
1056 auto res=preset_loader->
Write(filename,content);
1061 auto res=preset_loader->
Exists(filename);
1066 auto res=preset_loader->
GetAll();
1068 for (
size_t n=0;n<res.size();n++) {
1069 all[n]=fs_strdup(res[n].c_str());
1071 all[res.size()]=
nullptr;
1076 for (
fs_char** one=all;*one;one++) {
1083 auto res=preset_loader->
Remove(filename);
1088 auto res=preset_loader->
Rename(from,to);
bigvoice_CBus * Get() const
CClient(bigvoice_CInstance *instance)
std::string ProcessJson(const std::string &request, bool *NeedSave=nullptr)
Process a JSON request and returns the answer.
CClient & operator=(const CInstance &)=delete
CClient(const CInstance &)=delete
void SetBus(const CBus &bus)
void SetMetadataMulti(const std::unordered_map< std::string, const char * > &list)
virtual void OnUpdateThreadStart()
void GetWebServerAppHealth(int &httpcode, std::string &contenttype, std::string &content)
std::string GetParam(const std::string &name)
std::array< float, OutputSampleSize > & GetBufferOut()
void PresetManager_InformChange(const std::filesystem::path &relative_path, PresetChange_Kind change_kind)
unsigned int GetChunkFrames()
unsigned int GetEstimatedDelay()
std::vector< T > ProcessAnyAudio(const std::vector< T > input)
void ProcessAudio_Planar(float const *const *input, float *const *output)
unsigned int AddAudio(const T *payload, unsigned int nFrame)
virtual void OnUpdateThreadStop()
void StopWebServer(int timeout_ms=1000)
CInstance & operator=(const CInstance &)=delete
void SetPresetManager(CPresetLoader *preset_manager)
unsigned int GetAudio(T *payload, unsigned int max_nFrame)
bool StartWebServer(int http_port, int https_port=0)
void SetMetadata(const std::string &key, const char *value)
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=12)
void SetWebServerAppHealth(int httpcode, const std::string &contenttype, const std::string &content)
std::vector< std::tuple< std::string, std::string > > GetMetadataInfos()
CInstance(const CInstance &)=delete
std::array< float, InputSampleSize > & GetBufferIn()
std::shared_ptr< CClient > NewClient()
void SetParam(const std::string &name, const std::string &value)
Custom preset handler helper.
virtual ~CPresetLoader()=default
virtual bool Write(const std::filesystem::path &filename, const std::string &content)=0
virtual bool IsReadOnly()=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 Remove(const std::filesystem::path &name)=0
virtual std::vector< std::filesystem::path > GetAll()=0
virtual std::string Read(const std::filesystem::path &filename)=0
unsigned int bigvoice_GetOutputCount(struct bigvoice_CInstance *instance)
unsigned int bigvoice_GetAudio(struct bigvoice_CInstance *instance, uint8_t *payload, unsigned int max_nFrame, enum bigvoice_SampleFormat fmt)
const char * bigvoice_GetFormatName(const enum bigvoice_SampleFormat fmt)
unsigned int bigvoice_GetBytesFromFormat(const enum bigvoice_SampleFormat fmt)
enum bigvoice_SampleFormat bigvoice_GetFormatFromName(const char *name)
unsigned int bigvoice_AddAudio(struct bigvoice_CInstance *instance, const uint8_t *payload, unsigned int nFrame, enum bigvoice_SampleFormat fmt)
int bigvoice_SaveState(struct bigvoice_CInstance *instance)
void bigvoice_DeleteClient(struct bigvoice_CClientInstance *client)
const char * bigvoice_ProcessJson(struct bigvoice_CClientInstance *client, const char *json_str, int *need_save)
void bigvoice_FreeJsonAnswer(const char *json_str)
struct bigvoice_CClientInstance * bigvoice_NewClient(struct bigvoice_CInstance *instance)
struct bigvoice_CBus * bigvoice_NewBus()
void bigvoice_FreeBus(struct bigvoice_CBus *bus)
void bigvoice_SetInstanceBus(struct bigvoice_CParameters *params, struct bigvoice_CBus *bus)
void bigvoice_AudioConvertTo(const float *input, uint8_t *payload, size_t nSpl, enum bigvoice_SampleFormat fmt)
void bigvoice_AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, enum bigvoice_SampleFormat fmt)
void bigvoice_AudioMonoToLiveStereo(const float *input, uint8_t *payload)
void bigvoice_StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
void bigvoice_MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
void bigvoice_AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
void bigvoice_MonoToStereo(const float *input, float *output, size_t nFrame)
void bigvoice_StereoToMono(const float *input, float *output, size_t nFrame)
void bigvoice_ProcessAudio(struct bigvoice_CInstance *instance, const float *input, float *output)
unsigned int bigvoice_GetEstimatedDelay(struct bigvoice_CInstance *instance)
float * bigvoice_GetBufferOut(struct bigvoice_CInstance *instance)
float * bigvoice_GetBufferIn(struct bigvoice_CInstance *instance)
void bigvoice_ProcessAudio_Planar(struct bigvoice_CInstance *instance, float const *const *input, float *const *output)
#define BIGVOICE_SAMPLE_RATE
#define BIGVOICE_CHANNEL_COUNT
unsigned int bigvoice_GetAudioInputCount()
unsigned int bigvoice_GetChannelCount()
void bigvoice_SetLoggerCallback(bigvoice_loggerfn logger)
void bigvoice_SetLogSeverity(enum bigvoice_LogSeverity severity)
#define BIGVOICE_AUDIO_OUTPUT_COUNT
unsigned int bigvoice_GetChunkSizeInFrames()
unsigned int bigvoice_GetAudioOutputCount()
#define BIGVOICE_AUDIO_INPUT_COUNT
const char * bigvoice_GetVersion()
#define BIGVOICE_AUDIOFRAME_COUNT
unsigned int bigvoice_GetSampleRate()
unsigned int * bigvoice_GetPossibleChunkSizeInFrames()
void bigvoice_FreeParameterValue(const char *value)
int bigvoice_TerminateProcess(struct bigvoice_CInstance *instance)
struct bigvoice_CInstance * bigvoice_InitProcess3(const char *LoginKey, const char *RadioName, const char *Access_Key_ID, const char *Access_Key_Secret, const char *save_path, const struct bigvoice_CParameters *parameters, unsigned int frames_per_chunk)
void bigvoice_FreeParameters(struct bigvoice_CParameters *params)
void bigvoice_ExitProcess(struct bigvoice_CInstance *instance)
struct bigvoice_CParameters * bigvoice_NewParameters()
unsigned int bigvoice_GetProcessChunkFrames(struct bigvoice_CInstance *instance)
void bigvoice_SetParameter(struct bigvoice_CParameters *params, const char *name, const char *value)
const char * bigvoice_GetParameter(struct bigvoice_CParameters *params, const char *name)
bigvoice_PresetChange_Kind
void bigvoice_SetPresetManager(struct bigvoice_CParameters *params, bigvoice_storage_reader reader, bigvoice_storage_reader_free, bigvoice_storage_writer writer, bigvoice_storage_exists exists, bigvoice_storage_getall getall, bigvoice_storage_getall_free getall_free, bigvoice_storage_remove remove, bigvoice_storage_rename rename, int IsReadOnly, void *userdata)
void bigvoice_PresetManager_InformChange(struct bigvoice_CInstance *instance, const fs_char *relative_path, enum bigvoice_PresetChange_Kind change_kind)
void bigvoice_StopUpdateThread(struct bigvoice_CInstance *instance)
int bigvoice_WaitUpdateThreadReady(struct bigvoice_CInstance *instance, int milliseconds)
void bigvoice_StartUpdateThread(struct bigvoice_CInstance *instance, unsigned int port)
#define SOUND4_WEBSERVER_HTTPS_OK
uint64_t bigvoice_Webserver(unsigned int listenport, unsigned int listenport_secure, struct bigvoice_CInstance *instance)
int bigvoice_Webserver_Stop(uint64_t id, int timeout_ms)
int bigvoice_Webserver_Status(uint64_t id)
void bigvoice_Webserver_SetAppHealth(struct bigvoice_CInstance *instance, int httpcode, const char *contenttype, const char *content)
#define SOUND4_WEBSERVER_HTTP_OK
void bigvoice_Webserver_FreeString(char *str)
#define SOUND4_INVALID_WEBSERVER_ID
void bigvoice_Webserver_GetAppHealth(struct bigvoice_CInstance *instance, int *httpcode, char **contenttype, char **content)
static std::wstring UTF8ToWString(const std::string &str)
static void MonoToStereo_Planar(const float *input, float *outputL, float *outputR, size_t nFrame)
static void AudioConvertFrom(const uint8_t *payload, float *output, size_t nSpl, bigvoice_SampleFormat fmt)
static void MonoToStereo(const float *input, float *output, size_t nFrame)
static void StereoToMono(const float *input, float *output, size_t nFrame)
static void StereoToMono_Planar(const float *inputL, const float *inputR, float *output, size_t nFrame)
static std::string WStringToUTF8(const std::wstring &wstr)
static void AudioMonoToLiveStereo(const float *input, uint8_t *payload)
static void AudioMonoFromLiveStereo(const uint8_t *payload, float *output)
static void AudioConvertTo(const float *input, uint8_t *payload, size_t nSpl, bigvoice_SampleFormat fmt)
static unsigned int GetAudioInputCount()
static std::vector< unsigned int > GetPossibleChunkSizeInFrames()
const size_t OutputChannels
static std::string GetFormatName(const SampleFormat fmt)
static SampleFormat GetFormatFromName(const std::string &name)
std::function< void(LogSeverity, const std::string &)> log_cb_t
static void SetLogSeverity(LogSeverity severity)
const size_t OutputSampleSize
static void SanityCheck(bool a_bCheckFrames=true)
static unsigned int GetSampleRate()
static constexpr const char * process_name
const size_t InputChannels
const size_t InputSampleSize
static unsigned int GetChannelCount()
static unsigned int GetAudioOutputCount()
static void SetLoggerCallback(log_cb_t cb)
static unsigned int GetChunkSizeInFrames()
static unsigned int GetBytesFromFormat(const SampleFormat fmt)
static constexpr const char * process_shortname
static std::string GetVersion()
static void _log_cb_c(bigvoice_LogSeverity severity, const char *c_msg)
@ S32_NATIVE
32-bit signed integer, native
@ F32_NATIVE
32-bit floating-point, native
@ S16_NATIVE
16-bit signed integer, native
static int bigvoice_custom_exists(const fs_char *filename, void *userdata)
static int bigvoice_custom_rename(const fs_char *from, const fs_char *to, void *userdata)
static int bigvoice_custom_writer(const fs_char *filename, const char *content, void *userdata)
static fs_char ** bigvoice_custom_getall(void *userdata)
static int bigvoice_custom_remove(const fs_char *filename, void *userdata)
static void bigvoice_custom_reader_free(char *content, void *userdata)
static char * bigvoice_custom_reader(const fs_char *filename, void *userdata)
static void bigvoice_custom_getall_free(fs_char **all, void *userdata)