SOUND4 BIGVOICE.CL Library [1.1.5]
Loading...
Searching...
No Matches
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
15
16// --------------------------------------------------------------
39std::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{
50bool 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
79void 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
99void MyLogger(sound4::bigvoice::LogSeverity severity,const std::string& msg)
100{
101 std::cerr << msg << std::endl;
102}
103
104int 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 std::error_code ec;
136 store_path = std::filesystem::path(store_path_str);
137 if (!std::filesystem::exists(store_path,ec) || !std::filesystem::is_directory(store_path,ec)) {
138 std::cerr << "Storage folder '"<<store_path.c_str()<<"' is not a valid directory" << std::endl;
139 Usage(progname);
140 return 1;
141 }
142 }
143
145 sound4::bigvoice::SetLogSeverity(verbose5);
146 sound4::bigvoice::SetLoggerCallback(MyLogger);
147
149 instance.SetParam("ADMIN_USER","admin");
150 instance.SetParam("ADMIN_SECRET","admin");
152
153 std::cerr << "Creating" << std::endl;
155 if (!instance.Create(LoginKey, RadioName, KeyId, AccessKey, store_path)) {
156 std::cerr << "Failed to create the processing instance" << std::endl;
157 return 1;
158 }
160
161#if (BIGVOICE_HAS_WEBSERVER)
162 std::cerr << "Starting web server" << std::endl;
164 if (!instance.StartWebServer(listen_port,0)) {
165 std::cerr << "Failed to create the web server on port " << listen_port << std::endl;
166 return 1;
167 }
169#endif // (BIGVOICE_HAS_WEBSERVER)
170
171 {
173 auto client = instance.NewClient();
174 auto answer = client->ProcessJson(R"JSON({"get":{"processorname":null}})JSON");
175 std::cerr << "Request for processor name returned "<<answer<<std::endl;
177 }
178
179 ProcessLoop(instance);
180
181 std::cerr << "Stopping now" << std::endl;
182 instance.Stop();
183
184 std::cerr << "End" << std::endl;
185 return 0;
186}
std::array< float, OutputSampleSize > & GetBufferOut()
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)
std::array< float, InputSampleSize > & GetBufferIn()
std::shared_ptr< CClient > NewClient()
void SetParam(const std::string &name, const std::string &value)
bigvoice_LogSeverity
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.