13#if defined(_WIN32) || defined(WIN32)
60std::string
GetEnv(
const std::string& name)
62 char *data=getenv(name.c_str());
64 return std::string(data);
73 #if defined(_WIN32) || defined(WIN32)
75 _setmode(_fileno(stdin), _O_BINARY);
76 _setmode(_fileno(stdout), _O_BINARY);
85 if (std::fread(instance.
GetBufferIn(),
sizeof(
float), input_sample_count, stdin) != input_sample_count) {
86 std::cerr <<
"ProcessLoop: input stopped"<< std::endl;
90 if (stopthread)
break;
94 if (fwrite(instance.
GetBufferOut(),
sizeof(
float), output_sample_count, stdout) != output_sample_count) {
95 std::cerr <<
"Failed to write audio output" << std::endl;
108 bool SetupPath(
const std::filesystem::path& a_preset_path)
110 m_preset_path=a_preset_path;
112 if ( !std::filesystem::exists(m_preset_path,ec ) ) {
115 if ( !std::filesystem::is_directory(m_preset_path, ec) ) {
121 virtual bool Exists(
const std::filesystem::path &name)
override
124 return std::filesystem::exists(GetRealPath(name), ec);
126 virtual bool Remove(
const std::filesystem::path &name)
override
129 return std::filesystem::remove(GetRealPath(name), ec);
131 virtual bool Rename(
const std::filesystem::path &from,
const std::filesystem::path &to)
override
134 std::filesystem::rename(GetRealPath(from), GetRealPath(to), ec);
137 virtual std::vector<std::filesystem::path>
GetAll()
override
139 std::vector<std::filesystem::path> list;
141 for (
auto it = std::filesystem::directory_iterator(m_preset_path.c_str(), std::filesystem::directory_options::skip_permission_denied, ec);
142 !ec && it != std::filesystem::directory_iterator{};
145 if (it->is_regular_file(ec)) {
146 list.push_back(it->path().lexically_relative(m_preset_path));
151 virtual std::string
Read(
const std::filesystem::path &filename)
override
153 std::ifstream f(GetRealPath(filename));
155 return std::string();
157 std::ostringstream ss;
161 virtual bool Write(
const std::filesystem::path &filename,
const std::string &content)
override
163 std::ofstream f(GetRealPath(filename));
174 std::filesystem::path m_preset_path;
175 std::filesystem::path GetRealPath(
const std::filesystem::path &name)
177 return m_preset_path / name;
182int main(
int argc,
const char **argv)
184 int listen_port = 8080;
185 auto store_path_str=
GetEnv(
"S4STORE_PATH");
187 std::string libname =
"libsound4.bigvoice.cl.so";
192 auto LoginKey=
GetEnv(
"S4LOGINKEY");
193 if (LoginKey.empty()) {
194 LoginKey=
GetEnv(
"S4USERID");
196 auto RadioName=
GetEnv(
"RADIO_NAME");
197 auto KeyId=
GetEnv(
"ACCESS_KEY_ID");
198 auto AccessKey=
GetEnv(
"ACCESS_KEY_SECRET");
200 const char *progname=argv[0];
201 for (
int n=1;n<argc;n++) {
202 std::string arg = argv[n];
203 if (arg==
"-p" || arg==
"--port") {
205 std::cerr <<
"--port needs a number"<< std::endl;
209 listen_port=atoi(argv[n]);
210 }
else if (arg==
"-l" || arg==
"--lib") {
212 std::cerr <<
"--port needs a number"<< std::endl;
217 }
else if (arg==
"-h" || arg==
"--help") {
218 std::cerr <<
"Usage: " << progname <<
" [-p port] [-l libname.so]"<<std::endl;;
219 std::cerr <<
" Reads audio from stdin and return processed audio in stdout" << std::endl;
220 std::cerr <<
" -l, --lib : library to load ["<<libname<<
"]" << std::endl;
221 std::cerr <<
" -p, --port : port for web server ["<<listen_port<<
"]" << std::endl;
222 std::cerr << std::endl;
223 std::cerr <<
"Environment:"<< std::endl;;
224 std::cerr <<
" - S4STORE_PATH : path to store config [default: (no storage)]"<< std::endl;;
225 std::cerr <<
" - S4LOGINKEY : SOUND4 Login key (or S4USERID)"<< std::endl;;
226 std::cerr <<
" - RADIO_NAME : SOUND4 Radio Name"<< std::endl;;
227 std::cerr <<
" - ACCESS_KEY_ID : SOUND4 Access Key ID"<< std::endl;;
228 std::cerr <<
" - ACCESS_KEY_SECRET : SOUND4 Access Key Secret"<< std::endl;
229 std::cerr << std::endl;
230 std::cerr <<
"Example:"<< std::endl;
231 std::cerr <<
" ffmpeg -re -i file.wav -ar 48000 -ac 1 -f f32le - | "<<progname<<
" | ffplay -f f32le -ar 48000 -ac 1 -i -" << std::endl;;
232 #if defined(_WIN32) || defined(WIN32)
233 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;;
235 std::cerr <<
" ffmpeg -f pulse -i default -ar 48000 -ac 1 -f f32le - | "<<progname<<
" | ffplay -f f32le -ar 48000 -ac 1 -i -" << std::endl;;
239 std::cerr <<
"unknown argument '"<<arg<<
"'"<< std::endl;
247 if (!processor.
Load(libname)) {
248 std::cerr <<
"Failed to load processing library" << std::endl;
253 if (LoginKey.empty() || RadioName.empty() || KeyId.empty() || AccessKey.empty()) {
254 std::cerr <<
"Missing credentials in environment" << std::endl;
257 std::filesystem::path store_path;
258 if (!store_path_str.empty()) {
259 store_path = std::filesystem::path(store_path_str);
261 if (!std::filesystem::exists(store_path,ec) || !std::filesystem::is_directory(store_path,ec)) {
262 std::cerr <<
"Storage folder '"<<store_path.c_str()<<
"' is not a valid directory" << std::endl;
270 std::cerr << msg << std::endl;
274 instance.
SetParam(
"ADMIN_USER",
"admin");
275 instance.
SetParam(
"ADMIN_SECRET",
"admin");
280 if (!store_path.empty()) {
281 if (!preset_manager.
SetupPath(store_path)) {
282 std::cerr <<
"Failed to setup the custom preset manager on path " << store_path.c_str() << std::endl;
289 std::cerr <<
"Creating" << std::endl;
291 if (!instance.
Create(LoginKey, RadioName, KeyId, AccessKey, {})) {
292 std::cerr <<
"Failed to create the processing instance" << std::endl;
297 std::cerr <<
"Starting web server" << std::endl;
300 std::cerr <<
"Failed to create the web server on port " << listen_port << std::endl;
308 auto answer = client->ProcessJson(R
"JSON({"get":{"processorname":null}})JSON");
309 std::cerr << "Request for processor name returned "<<answer<<std::endl;
315 std::cerr <<
"Stopping now" << std::endl;
318 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()
const LogSeverity verbose5
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.