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";
if (!processor.
Load(libname)) {
std::cerr << "Failed to load processing library" << std::endl;
return 1;
}
Dynamic library interface.
bool Load(const std::filesystem::path &filepath)
Loads the library.
Setup and configure
std::cerr << msg << std::endl;
});
instance.SetParam("ADMIN_USER","admin");
instance.SetParam("ADMIN_SECRET","admin");
void SetLogSeverity(LogSeverity severity)
void SetLoggerCallback(log_cb_t cb)
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)
{
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;
instance.ProcessAudio();
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()
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:
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;
}
};
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.
Custom preset manager loading
if (!store_path.empty()) {
std::cerr << "Failed to setup the custom preset manager on path " << store_path.c_str() << std::endl;
return 1;
}
instance.SetPresetManager(&preset_manager);
}