SOUND4 IMPACT.CL Library [1.0.2]
sound4.impact.cl-pipe2.cpp
Go to the documentation of this file.
1 #include <unistd.h>
2 #include <iostream>
3 #include <strings.h>
4 #include <sys/types.h>
5 #include <filesystem>
6 
7 #include "sound4.impact.cl.hpp"
8 
9 using namespace sound4::impact;
10 
11 
12 std::string GetEnv(const std::string& name)
13 {
14  char *data=getenv(name.c_str());
15  if (!data) return {};
16  return std::string(data);
17 }
18 
19 
20 // Reads from stdin, write to stdout
21 void ProcessLoop(CInstance &instance)
22 {
23 bool stopthread=false;
24 
25  #if defined(_WIN32) || defined(WIN32)
26  // Windows: The stdin, stdout, and stderr streams always open in text mode by default
27  _setmode(_fileno(stdin), _O_BINARY);
28  _setmode(_fileno(stdout), _O_BINARY);
29  #endif
30 
31  while (!stopthread)
32  {
33  // Read a chunk from stdin
34  if (std::fread(instance.GetBufferIn(), sizeof(float), InputSampleSize, stdin) != InputSampleSize) {
35  std::cerr << "ProcessLoop: input stopped"<< std::endl;
36  stopthread=true;
37  break;
38  }
39  if (stopthread) break;
40  // Process it
41  instance.ProcessAudio();
42  // Write it to stdout
43  if (fwrite(instance.GetBufferOut(), sizeof(float), OutputSampleSize, stdout) != OutputSampleSize) {
44  std::cerr << "Failed to write audio output" << std::endl;
45  stopthread=true;
46  }
47  }
48 }
49 
50 void DoLog(LogSeverity severity,const std::string& msg)
51 {
52  std::cerr << msg << std::endl;
53 }
54 
55 int main(int argc, const char **argv)
56 {
59 
60  CInstance instance;
61  int listen_port = 8080;
62 
63  // Get credentials from environment
64  auto LoginKey=GetEnv("S4LOGINKEY");
65  if (LoginKey.empty()) {
66  LoginKey=GetEnv("S4USERID");
67  }
68  auto RadioName=GetEnv("RADIO_NAME");
69  auto KeyId=GetEnv("S4_AWS_ACCESS_KEY_ID");
70  auto AccessKey=GetEnv("S4_AWS_SECRET_ACCESS_KEY");
71 
72  instance.SetParam("ADMIN_USER","admin");
73  instance.SetParam("ADMIN_SECRET","admin");
74 
75  std::cout << "Creating" << std::endl;
76  if (!instance.Create(LoginKey, RadioName, KeyId, AccessKey, std::filesystem::path())) {
77  std::cerr << "Failed to create the processing instance" << std::endl;
78  return 1;
79  }
80 
81  std::cout << "Starting web server" << std::endl;
82  if (!instance.StartWebServer(listen_port,0)) {
83  std::cerr << "Failed to create the web server on port " << listen_port << std::endl;
84  return 1;
85  }
86 
87  ProcessLoop(instance);
88 
89  std::cout << "Stopping now" << std::endl;
90  instance.Stop();
91 
92  std::cout << "End" << std::endl;
93  return 0;
94 }
void SetParam(const std::string &name, const std::string &value)
bool StartWebServer(int http_port, int https_port=0)
bool Create(const std::string &LoginKey, const std::string &RadioName, const std::string &AWS_Access_ID, const std::string &AWS_Access_Secret, const std::filesystem::path &save_path)
impact_LogSeverity
@ verbose5
const size_t OutputSampleSize
void SetLogSeverity(LogSeverity severity)
const size_t InputSampleSize
void SetLoggerCallback(log_cb_t cb)
void ProcessLoop(CInstance &instance)
int main(int argc, const char **argv)
std::string GetEnv(const std::string &name)
void DoLog(LogSeverity severity, const std::string &msg)