SOUND4 BIGVOICE.CL Library [1.1.6]
Loading...
Searching...
No Matches
sound4cl-pipe.cpp
Go to the documentation of this file.
1
6#include <unistd.h>
7#include <iostream>
8#include <strings.h>
9#include <sys/types.h>
10#include <filesystem>
11#include <fstream>
12#include <sstream>
13#if defined(_WIN32) || defined(WIN32)
14 #include <io.h>
15 #include <fcntl.h>
16#endif
17
18#include "sound4cl.hpp"
19
20// --------------------------------------------------------------
61std::string GetEnv(const std::string& name)
62{
63 char *data=getenv(name.c_str());
64 if (!data) return {};
65 return std::string(data);
66}
67
68
69// Reads from stdin, write to stdout
71{
72bool stopthread=false;
73
74 #if defined(_WIN32) || defined(WIN32)
75 // Windows: The stdin, stdout, and stderr streams always open in text mode by default
76 _setmode(_fileno(stdin), _O_BINARY);
77 _setmode(_fileno(stdout), _O_BINARY);
78 #endif
79
81 size_t input_sample_count = instance.GetChunkFrames() * processor.GetChannelCount();
82 size_t output_sample_count = instance.GetChunkFrames() * processor.GetChannelCount();
83 while (!stopthread)
84 {
85 // Read a chunk from stdin
86 if (std::fread(instance.GetBufferIn(), sizeof(float), input_sample_count, stdin) != input_sample_count) {
87 std::cerr << "ProcessLoop: input stopped"<< std::endl;
88 stopthread=true;
89 break;
90 }
91 if (stopthread) break;
92 // Process it
93 instance.ProcessAudio();
94 // Write it to stdout
95 if (fwrite(instance.GetBufferOut(), sizeof(float), output_sample_count, stdout) != output_sample_count) {
96 std::cerr << "Failed to write audio output" << std::endl;
97 stopthread=true;
98 }
99 }
101}
102
105public:
106 MyPresetManager() = default;
107 virtual ~MyPresetManager() = default;
108
109 bool SetupPath(const std::filesystem::path& a_preset_path)
110 {
111 m_preset_path=a_preset_path;
112 std::error_code ec;
113 if ( !std::filesystem::exists(m_preset_path,ec ) ) {
114 return false;
115 }
116 if ( !std::filesystem::is_directory(m_preset_path, ec) ) {
117 return false;
118 }
119 return true;
120 }
121 virtual bool IsReadOnly() override { return false; };
122 virtual bool Exists(const std::filesystem::path &name) override
123 {
124 std::error_code ec;
125 return std::filesystem::exists(GetRealPath(name), ec);
126 }
127 virtual bool Remove(const std::filesystem::path &name) override
128 {
129 std::error_code ec;
130 return std::filesystem::remove(GetRealPath(name), ec);
131 }
132 virtual bool Rename(const std::filesystem::path &from, const std::filesystem::path &to) override
133 {
134 std::error_code ec;
135 std::filesystem::rename(GetRealPath(from), GetRealPath(to), ec);
136 return (!ec);
137 }
138 virtual std::vector<std::filesystem::path> GetAll() override
139 {
140 std::vector<std::filesystem::path> list;
141 std::error_code ec;
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{};
144 it.increment(ec) )
145 {
146 if (it->is_regular_file(ec)) {
147 list.push_back(it->path().lexically_relative(m_preset_path));
148 }
149 }
150 return list;
151 }
152 virtual std::string Read(const std::filesystem::path &filename) override
153 {
154 std::ifstream f(GetRealPath(filename));
155 if ( f.fail() ) {
156 return std::string();
157 }
158 std::ostringstream ss;
159 ss << f.rdbuf();
160 return ss.str();
161 }
162 virtual bool Write(const std::filesystem::path &filename, const std::string &content) override
163 {
164 std::ofstream f(GetRealPath(filename));
165 if ( f.fail() ) {
166 return false;
167 }
168 f << content;
169 if ( f.fail() ) {
170 return false;
171 }
172 return true;
173 }
174private:
175 std::filesystem::path m_preset_path;
176 std::filesystem::path GetRealPath(const std::filesystem::path &name)
177 {
178 return m_preset_path / name;
179 }
180};
182
183int main(int argc, const char **argv)
184{
185 int listen_port = 8080;
186 auto store_path_str=GetEnv("S4STORE_PATH");
188 std::string libname = "libsound4.bigvoice.cl.so";
190
191
192 // Get credentials from environment
193 auto LoginKey=GetEnv("S4LOGINKEY");
194 if (LoginKey.empty()) {
195 LoginKey=GetEnv("S4USERID");
196 }
197 auto RadioName=GetEnv("RADIO_NAME");
198 auto KeyId=GetEnv("ACCESS_KEY_ID");
199 auto AccessKey=GetEnv("ACCESS_KEY_SECRET");
200
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") {
205 if (argc<n+1) {
206 std::cerr << "--port needs a number"<< std::endl;
207 return 1;
208 }
209 n++;
210 listen_port=atoi(argv[n]);
211 } else if (arg=="-l" || arg=="--lib") {
212 if (argc<n+1) {
213 std::cerr << "--port needs a number"<< std::endl;
214 return 1;
215 }
216 n++;
217 libname=argv[n];
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;;
235 #else
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;;
237 #endif
238 return 0;
239 } else {
240 std::cerr << "unknown argument '"<<arg<<"'"<< std::endl;
241 return 1;
242 }
243 }
244
245
247 sound4::CProcessor processor;
248 if (!processor.Load(libname)) {
249 std::cerr << "Failed to load processing library" << std::endl;
250 return 1;
251 }
253
254 if (LoginKey.empty() || RadioName.empty() || KeyId.empty() || AccessKey.empty()) {
255 std::cerr << "Missing credentials in environment" << std::endl;
256 return 1;
257 }
258 std::filesystem::path store_path;
259 if (!store_path_str.empty()) {
260 store_path = std::filesystem::path(store_path_str);
261 std::error_code ec;
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;
264 return 1;
265 }
266 }
267
270 processor.SetLoggerCallback([](sound4::LogSeverity severity,const std::string& msg) {
271 std::cerr << msg << std::endl;
272 });
273
274 sound4::CInstance instance(processor);
275 instance.SetParam("ADMIN_USER","admin");
276 instance.SetParam("ADMIN_SECRET","admin");
278
280 MyPresetManager preset_manager;
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;
284 return 1;
285 }
286 instance.SetPresetManager(&preset_manager);
287 }
289
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;
294 return 1;
295 }
297
298 std::cerr << "Starting web server" << std::endl;
300 if (!instance.StartWebServer(listen_port,0)) {
301 std::cerr << "Failed to create the web server on port " << listen_port << std::endl;
302 return 1;
303 }
305
306 {
308 auto client = instance.NewClient();
309 auto answer = client->ProcessJson(R"JSON({"get":{"processorname":null}})JSON");
310 std::cerr << "Request for processor name returned "<<answer<<std::endl;
312 }
313
314 ProcessLoop(instance, processor);
315
316 std::cerr << "Stopping now" << std::endl;
317 instance.Stop();
318
319 std::cerr << "End" << std::endl;
320 return 0;
321}
[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)
Instance handling class.
Definition sound4cl.hpp:873
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)
Definition sound4cl.hpp:970
unsigned int GetChunkFrames()
void SetParam(const std::string &name, const std::string &value)
Definition sound4cl.hpp:925
bool StartWebServer(int http_port, int https_port=0)
std::shared_ptr< CClient > NewClient()
float * GetBufferOut()
float * GetBufferIn()
Custom preset handler helper.
Definition sound4cl.hpp:850
Dynamic library interface.
Definition sound4cl.hpp:299
bool Load(const std::filesystem::path &filepath)
Loads the library.
Definition sound4cl.hpp:408
void SetLogSeverity(LogSeverity severity)
Definition sound4cl.hpp:806
void SetLoggerCallback(log_cb_t cb)
Definition sound4cl.hpp:812
unsigned int GetChannelCount()
Definition sound4cl.hpp:743
@ verbose5
verbose5
Definition sound4cl.hpp:275
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.