SOUND4 x1.CLOUD Library [1.5.6]
Loading...
Searching...
No Matches
Sample using Generic C++ interface (multi-process compatible)

C++ example (dynamic link, any process)

This wrapper abstracts the processing specifics, working with any SOUND4 .CL process.

It uses sound4cl.hpp header.

See full example cpp/sound4cl-pipe.cpp

Loading the library

std::string libname = "libsound4.x1.cloud.so";
sound4::CProcessor processor;
if (!processor.Load(libname)) {
std::cerr << "Failed to load processing library" << std::endl;
return 1;
}
Dynamic library interface.
Definition sound4cl.hpp:299
bool Load(const std::filesystem::path &filepath)
Loads the library.
Definition sound4cl.hpp:408

Setup and configure

processor.SetLoggerCallback([](sound4::LogSeverity severity,const std::string& msg) {
std::cerr << msg << std::endl;
});
sound4::CInstance instance(processor);
instance.SetParam("ADMIN_USER","admin");
instance.SetParam("ADMIN_SECRET","admin");
Instance handling class.
Definition sound4cl.hpp:873
void SetLogSeverity(LogSeverity severity)
Definition sound4cl.hpp:806
void SetLoggerCallback(log_cb_t cb)
Definition sound4cl.hpp:812
@ verbose5
verbose5
Definition sound4cl.hpp:275

Create the processing instance

if (!instance.Create(LoginKey, RadioName, KeyId, AccessKey, {})) {
std::cerr << "Failed to create the processing instance" << std::endl;
return 1;
}

Processing loop

size_t input_sample_count = instance.GetChunkFrames() * processor.GetChannelCount();
size_t output_sample_count = instance.GetChunkFrames() * processor.GetChannelCount();
while (!stopthread)
{
// Read a chunk from stdin
if (std::fread(instance.GetBufferIn(), sizeof(float), input_sample_count, stdin) != input_sample_count) {
std::cerr << "ProcessLoop: input stopped"<< std::endl;
stopthread=true;
break;
}
if (stopthread) break;
// Process it
instance.ProcessAudio();
// Write it to stdout
if (fwrite(instance.GetBufferOut(), sizeof(float), output_sample_count, stdout) != output_sample_count) {
std::cerr << "Failed to write audio output" << std::endl;
stopthread=true;
}
}
unsigned int GetChannelCount()
Definition sound4cl.hpp:743

Starting a web-server

if (!instance.StartWebServer(listen_port,0)) {
std::cerr << "Failed to create the web server on port " << listen_port << std::endl;
return 1;
}

Communicating with the instance

See also
jsonsyntax
auto client = instance.NewClient();
auto answer = client->ProcessJson(R"JSON({"get":{"processorname":null}})JSON");
std::cerr << "Request for processor name returned "<<answer<<std::endl;

Custom preset manager example

public:
MyPresetManager() = default;
virtual ~MyPresetManager() = default;
bool SetupPath(const std::filesystem::path& a_preset_path)
{
m_preset_path=a_preset_path;
std::error_code ec;
if ( !std::filesystem::exists(m_preset_path,ec ) ) {
return false;
}
if ( !std::filesystem::is_directory(m_preset_path, ec) ) {
return false;
}
return true;
}
virtual bool IsReadOnly() override { return false; };
virtual bool Exists(const std::filesystem::path &name) override
{
std::error_code ec;
return std::filesystem::exists(GetRealPath(name), ec);
}
virtual bool Remove(const std::filesystem::path &name) override
{
std::error_code ec;
return std::filesystem::remove(GetRealPath(name), ec);
}
virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to) override
{
std::error_code ec;
std::filesystem::rename(GetRealPath(from), GetRealPath(to), ec);
return (!ec);
}
virtual std::vector<std::filesystem::path> GetAll() override
{
std::vector<std::filesystem::path> list;
std::error_code ec;
for (auto it = std::filesystem::directory_iterator(m_preset_path.c_str(), std::filesystem::directory_options::skip_permission_denied, ec);
!ec && it != std::filesystem::directory_iterator{};
it.increment(ec) )
{
if (it->is_regular_file(ec)) {
list.push_back(it->path().lexically_relative(m_preset_path));
}
}
return list;
}
virtual std::string Read(const std::filesystem::path &filename) override
{
std::ifstream f(GetRealPath(filename));
if ( f.fail() ) {
return std::string();
}
std::ostringstream ss;
ss << f.rdbuf();
return ss.str();
}
virtual bool Write(const std::filesystem::path &filename, const std::string &content) override
{
std::ofstream f(GetRealPath(filename));
if ( f.fail() ) {
return false;
}
f << content;
if ( f.fail() ) {
return false;
}
return true;
}
private:
std::filesystem::path m_preset_path;
std::filesystem::path GetRealPath(const std::filesystem::path &name)
{
return m_preset_path / name;
}
};
[Custom preset manager]
virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to) override
virtual ~MyPresetManager()=default
virtual bool Exists(const std::filesystem::path &name) override
virtual bool IsReadOnly() override
virtual bool Write(const std::filesystem::path &filename, const std::string &content) override
MyPresetManager()=default
virtual bool Remove(const std::filesystem::path &name) override
virtual std::vector< std::filesystem::path > GetAll() override
virtual std::string Read(const std::filesystem::path &filename) override
bool SetupPath(const std::filesystem::path &a_preset_path)
Custom preset handler helper.
Definition sound4cl.hpp:850

Custom preset manager loading

MyPresetManager preset_manager;
if (!store_path.empty()) {
if (!preset_manager.SetupPath(store_path)) {
std::cerr << "Failed to setup the custom preset manager on path " << store_path.c_str() << std::endl;
return 1;
}
instance.SetPresetManager(&preset_manager);
}