SOUND4 BIGVOICE.CL Library [1.0.4]
sound4.bigvoice.cl-pipe-cxx.cpp
Go to the documentation of this file.
1 
4 #include <unistd.h>
5 #include <iostream>
6 #include <strings.h>
7 #include <sys/types.h>
8 #include <filesystem>
9 #if defined(_WIN32) || defined(WIN32)
10  #include <io.h>
11  #include <fcntl.h>
12 #endif
13 
14 #include "sound4.bigvoice.cl.hpp"
15 
16 // --------------------------------------------------------------
39 std::string GetEnv(const std::string& name)
40 {
41  char *data=getenv(name.c_str());
42  if (!data) return {};
43  return std::string(data);
44 }
45 
46 
47 // Reads from stdin, write to stdout
49 {
50 bool stopthread=false;
51 
52  #if defined(_WIN32) || defined(WIN32)
53  // Windows: The stdin, stdout, and stderr streams always open in text mode by default
54  _setmode(_fileno(stdin), _O_BINARY);
55  _setmode(_fileno(stdout), _O_BINARY);
56  #endif
57 
59  while (!stopthread)
60  {
61  // Read a chunk from stdin
62  if (std::fread(instance.GetBufferIn().data(), sizeof(float), sound4::bigvoice::InputSampleSize, stdin) != sound4::bigvoice::InputSampleSize) {
63  std::cerr << "ProcessLoop: input stopped"<< std::endl;
64  stopthread=true;
65  break;
66  }
67  if (stopthread) break;
68  // Process it
69  instance.ProcessAudio();
70  // Write it to stdout
71  if (fwrite(instance.GetBufferOut().data(), sizeof(float), sound4::bigvoice::OutputSampleSize, stdout) != sound4::bigvoice::OutputSampleSize) {
72  std::cerr << "Failed to write audio output" << std::endl;
73  stopthread=true;
74  }
75  }
77 }
78 
79 void Usage(const char *progname)
80 {
81  std::cerr << "Usage: " << progname << std::endl;;
82  std::cerr << " Reads audio from stdin and return processed audio in stdout" << std::endl;
83  std::cerr << "Environment:"<< std::endl;;
84  std::cerr << " - S4STORE_PATH : path to store config [default: (no storage)]"<< std::endl;;
85  std::cerr << " - S4LOGINKEY : SOUND4 Login key (or S4USERID)"<< std::endl;;
86  std::cerr << " - RADIO_NAME : SOUND4 Radio Name"<< std::endl;;
87  std::cerr << " - ACCESS_KEY_ID : SOUND4 Access Key ID"<< std::endl;;
88  std::cerr << " - ACCESS_KEY_SECRET : SOUND4 Access Key Secret"<< std::endl;;
89  std::cerr << std::endl;
90  std::cerr << "Example:"<< std::endl;
91  std::cerr << "ffmpeg -re -i file.wav -ar "<<sound4::bigvoice::GetSampleRate()<<" -ac "<<sound4::bigvoice::GetChannelCount()<<" -f f32le - | "<<progname<<" | ffplay -f f32le -ar "<<sound4::bigvoice::GetSampleRate()<<" -ac "<<sound4::bigvoice::GetChannelCount()<<" -i -" << std::endl;;
92  #if defined(_WIN32) || defined(WIN32)
93  std::cerr << "ffmpeg -f dshow -i audio=\"Microphone\" -ar "<<sound4::bigvoice::GetSampleRate()<<" -ac "<<sound4::bigvoice::GetChannelCount()<<" -f f32le - | "<<progname<<" | ffplay -f f32le -ar "<<sound4::bigvoice::GetSampleRate()<<" -ac "<<sound4::bigvoice::GetChannelCount()<<" -i -" << std::endl;;
94  #else
95  std::cerr << "ffmpeg -f pulse -i default -ar "<<sound4::bigvoice::GetSampleRate()<<" -ac "<<sound4::bigvoice::GetChannelCount()<<" -f f32le - | "<<progname<<" | ffplay -f f32le -ar "<<sound4::bigvoice::GetSampleRate()<<" -ac "<<sound4::bigvoice::GetChannelCount()<<" -i -" << std::endl;;
96  #endif
97 }
98 
99 void MyLogger(sound4::bigvoice::LogSeverity severity,const std::string& msg)
100 {
101  std::cerr << msg << std::endl;
102 }
103 
104 int main(int argc, const char **argv)
105 {
106 #if (BIGVOICE_HAS_WEBSERVER)
107  int listen_port = 8080;
108 #endif // (BIGVOICE_HAS_WEBSERVER)
109  auto store_path_str=GetEnv("S4STORE_PATH");
110 
111  // Get credentials from environment
112  auto LoginKey=GetEnv("S4LOGINKEY");
113  if (LoginKey.empty()) {
114  LoginKey=GetEnv("S4USERID");
115  }
116  auto RadioName=GetEnv("RADIO_NAME");
117  auto KeyId=GetEnv("ACCESS_KEY_ID");
118  auto AccessKey=GetEnv("ACCESS_KEY_SECRET");
119 
120  const char *progname=argv[0];
121 
122  if (argc>1) {
123  if ((strcasecmp(argv[1],"-h")==0) || (strcasecmp(argv[1],"--help")==0)) {
124  Usage(progname);
125  return 0;
126  }
127  }
128  if (LoginKey.empty() || RadioName.empty() || KeyId.empty() || AccessKey.empty()) {
129  std::cerr << "Missing credentials in environment" << std::endl;
130  Usage(progname);
131  return 1;
132  }
133  std::filesystem::path store_path;
134  if (!store_path_str.empty()) {
135  store_path = std::filesystem::path(store_path_str);
136  if (!std::filesystem::exists(store_path) || !std::filesystem::is_directory(store_path)) {
137  std::cerr << "Storage folder '"<<store_path.c_str()<<"' is not a valid directory" << std::endl;
138  Usage(progname);
139  return 1;
140  }
141  }
142 
144  sound4::bigvoice::SetLogSeverity(verbose5);
145  sound4::bigvoice::SetLoggerCallback(MyLogger);
146 
148  instance.SetParam("ADMIN_USER","admin");
149  instance.SetParam("ADMIN_SECRET","admin");
151 
152  std::cerr << "Creating" << std::endl;
154  if (!instance.Create(LoginKey, RadioName, KeyId, AccessKey, store_path)) {
155  std::cerr << "Failed to create the processing instance" << std::endl;
156  return 1;
157  }
159 
160 #if (BIGVOICE_HAS_WEBSERVER)
161  std::cerr << "Starting web server" << std::endl;
163  if (!instance.StartWebServer(listen_port,0)) {
164  std::cerr << "Failed to create the web server on port " << listen_port << std::endl;
165  return 1;
166  }
168 #endif // (BIGVOICE_HAS_WEBSERVER)
169 
170  {
172  auto client = instance.NewClient();
173  auto answer = client->ProcessJson(R"JSON({"get":{"processorname":null}})JSON");
174  std::cerr << "Request for processor name returned "<<answer<<std::endl;
176  }
177 
178  ProcessLoop(instance);
179 
180  std::cerr << "Stopping now" << std::endl;
181  instance.Stop();
182 
183  std::cerr << "End" << std::endl;
184  return 0;
185 }
std::array< float, OutputSampleSize > & GetBufferOut()
std::array< float, InputSampleSize > & GetBufferIn()
std::shared_ptr< CClient > NewClient()
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, int json_port=0, unsigned int frames_per_chunk=12)
void SetParam(const std::string &name, const std::string &value)
bigvoice_LogSeverity
@ verbose5
const size_t OutputSampleSize
const size_t InputSampleSize
int main(int argc, const char **argv)
void MyLogger(sound4::bigvoice::LogSeverity severity, const std::string &msg)
std::string GetEnv(const std::string &name)
void ProcessLoop(sound4::bigvoice::CInstance &instance)
void Usage(const char *progname)
C++ wrapper for library.