13#if defined(_WIN32) || defined(WIN32)
61std::string
GetEnv(
const std::string& name)
63 char *data=getenv(name.c_str());
65 return std::string(data);
74 #if defined(_WIN32) || defined(WIN32)
76 _setmode(_fileno(stdin), _O_BINARY);
77 _setmode(_fileno(stdout), _O_BINARY);
86 if (std::fread(instance.
GetBufferIn(),
sizeof(
float), input_sample_count, stdin) != input_sample_count) {
87 std::cerr <<
"ProcessLoop: input stopped"<< std::endl;
91 if (stopthread)
break;
95 if (fwrite(instance.
GetBufferOut(),
sizeof(
float), output_sample_count, stdout) != output_sample_count) {
96 std::cerr <<
"Failed to write audio output" << std::endl;
109 bool SetupPath(
const std::filesystem::path& a_preset_path)
111 m_preset_path=a_preset_path;
113 if ( !std::filesystem::exists(m_preset_path,ec ) ) {
116 if ( !std::filesystem::is_directory(m_preset_path, ec) ) {
122 virtual bool Exists(
const std::filesystem::path &name)
override
125 return std::filesystem::exists(GetRealPath(name), ec);
127 virtual bool Remove(
const std::filesystem::path &name)
override
130 return std::filesystem::remove(GetRealPath(name), ec);
132 virtual bool Rename(
const std::filesystem::path &from,
const std::filesystem::path &to)
override
135 std::filesystem::rename(GetRealPath(from), GetRealPath(to), ec);
138 virtual std::vector<std::filesystem::path>
GetAll()
override
140 std::vector<std::filesystem::path> list;
142 for (
auto it = std::filesystem::directory_iterator(m_preset_path.c_str(), std::filesystem::directory_options::skip_permission_denied, ec);
143 !ec && it != std::filesystem::directory_iterator{};
146 if (it->is_regular_file(ec)) {
147 list.push_back(it->path().lexically_relative(m_preset_path));
152 virtual std::string
Read(
const std::filesystem::path &filename)
override
154 std::ifstream f(GetRealPath(filename));
156 return std::string();
158 std::ostringstream ss;
162 virtual bool Write(
const std::filesystem::path &filename,
const std::string &content)
override
164 std::ofstream f(GetRealPath(filename));
175 std::filesystem::path m_preset_path;
176 std::filesystem::path GetRealPath(
const std::filesystem::path &name)
178 return m_preset_path / name;
183int main(
int argc,
const char **argv)
185 int listen_port = 8080;
186 auto store_path_str=
GetEnv(
"S4STORE_PATH");
188 std::string libname =
"libsound4.bigvoice.cl.so";
193 auto LoginKey=
GetEnv(
"S4LOGINKEY");
194 if (LoginKey.empty()) {
195 LoginKey=
GetEnv(
"S4USERID");
197 auto RadioName=
GetEnv(
"RADIO_NAME");
198 auto KeyId=
GetEnv(
"ACCESS_KEY_ID");
199 auto AccessKey=
GetEnv(
"ACCESS_KEY_SECRET");
201 const char *progname=argv[0];
202 for (
int n=1;n<argc;n++) {
203 std::string arg = argv[n];
204 if (arg==
"-p" || arg==
"--port") {
206 std::cerr <<
"--port needs a number"<< std::endl;
210 listen_port=atoi(argv[n]);
211 }
else if (arg==
"-l" || arg==
"--lib") {
213 std::cerr <<
"--port needs a number"<< std::endl;
218 }
else if (arg==
"-h" || arg==
"--help") {
219 std::cerr <<
"Usage: " << progname <<
" [-p port] [-l libname.so]"<<std::endl;;
220 std::cerr <<
" Reads audio from stdin and return processed audio in stdout" << std::endl;
221 std::cerr <<
" -l, --lib : library to load ["<<libname<<
"]" << std::endl;
222 std::cerr <<
" -p, --port : port for web server ["<<listen_port<<
"]" << std::endl;
223 std::cerr << std::endl;
224 std::cerr <<
"Environment:"<< std::endl;;
225 std::cerr <<
" - S4STORE_PATH : path to store config [default: (no storage)]"<< std::endl;;
226 std::cerr <<
" - S4LOGINKEY : SOUND4 Login key (or S4USERID)"<< std::endl;;
227 std::cerr <<
" - RADIO_NAME : SOUND4 Radio Name"<< std::endl;;
228 std::cerr <<
" - ACCESS_KEY_ID : SOUND4 Access Key ID"<< std::endl;;
229 std::cerr <<
" - ACCESS_KEY_SECRET : SOUND4 Access Key Secret"<< std::endl;
230 std::cerr << std::endl;
231 std::cerr <<
"Example:"<< std::endl;
232 std::cerr <<
" ffmpeg -re -i file.wav -ar 48000 -ac 1 -f f32le - | "<<progname<<
" | ffplay -f f32le -ar 48000 -ac 1 -i -" << std::endl;;
233 #if defined(_WIN32) || defined(WIN32)
234 std::cerr <<
" ffmpeg -f dshow -i audio=\"Microphone\" -ar 48000 -ac 1 -f f32le - | "<<progname<<
" | ffplay -f f32le -ar 48000 -ac 1 -i -" << std::endl;;
236 std::cerr <<
" ffmpeg -f pulse -i default -ar 48000 -ac 1 -f f32le - | "<<progname<<
" | ffplay -f f32le -ar 48000 -ac 1 -i -" << std::endl;;
240 std::cerr <<
"unknown argument '"<<arg<<
"'"<< std::endl;
248 if (!processor.
Load(libname)) {
249 std::cerr <<
"Failed to load processing library" << std::endl;
254 if (LoginKey.empty() || RadioName.empty() || KeyId.empty() || AccessKey.empty()) {
255 std::cerr <<
"Missing credentials in environment" << std::endl;
258 std::filesystem::path store_path;
259 if (!store_path_str.empty()) {
260 store_path = std::filesystem::path(store_path_str);
262 if (!std::filesystem::exists(store_path,ec) || !std::filesystem::is_directory(store_path,ec)) {
263 std::cerr <<
"Storage folder '"<<store_path.c_str()<<
"' is not a valid directory" << std::endl;
271 std::cerr << msg << std::endl;
275 instance.
SetParam(
"ADMIN_USER",
"admin");
276 instance.
SetParam(
"ADMIN_SECRET",
"admin");
281 if (!store_path.empty()) {
282 if (!preset_manager.
SetupPath(store_path)) {
283 std::cerr <<
"Failed to setup the custom preset manager on path " << store_path.c_str() << std::endl;
290 std::cerr <<
"Creating" << std::endl;
292 if (!instance.
Create(LoginKey, RadioName, KeyId, AccessKey, {})) {
293 std::cerr <<
"Failed to create the processing instance" << std::endl;
298 std::cerr <<
"Starting web server" << std::endl;
301 std::cerr <<
"Failed to create the web server on port " << listen_port << std::endl;
309 auto answer = client->ProcessJson(R
"JSON({"get":{"processorname":null}})JSON");
310 std::cerr << "Request for processor name returned "<<answer<<std::endl;
316 std::cerr <<
"Stopping now" << std::endl;
319 std::cerr <<
"End" << std::endl;
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)
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 SetPresetManager(CPresetLoader *preset_manager)
unsigned int GetChunkFrames()
void SetParam(const std::string &name, const std::string &value)
bool StartWebServer(int http_port, int https_port=0)
std::shared_ptr< CClient > NewClient()
Custom preset handler helper.
Dynamic library interface.
bool Load(const std::filesystem::path &filepath)
Loads the library.
void SetLogSeverity(LogSeverity severity)
void SetLoggerCallback(log_cb_t cb)
unsigned int GetChannelCount()
int main(int argc, const char **argv)
[Custom preset manager]
std::string GetEnv(const std::string &name)
void ProcessLoop(sound4::CInstance &instance, sound4::CProcessor &processor)
C++ wrapper for dynamic loading library.