SOUND4 BIGVOICE.CL Library [1.1.6]
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// --------------------------------------------------------------
41std::string GetEnv(const std::string& name)
42{
43 char *data=getenv(name.c_str());
44 if (!data) return {};
45 return std::string(data);
46}
47
48
49// Reads from stdin, write to stdout
51{
52bool stopthread=false;
53
54 #if defined(_WIN32) || defined(WIN32)
55 // Windows: The stdin, stdout, and stderr streams always open in text mode by default
56 _setmode(_fileno(stdin), _O_BINARY);
57 _setmode(_fileno(stdout), _O_BINARY);
58 #endif
59
61 while (!stopthread)
62 {
63 // Read a chunk from stdin
64 if (std::fread(instance.GetBufferIn().data(), sizeof(float), sound4::bigvoice::InputSampleSize, stdin) != sound4::bigvoice::InputSampleSize) {
65 std::cerr << "ProcessLoop: input stopped"<< std::endl;
66 stopthread=true;
67 break;
68 }
69 if (stopthread) break;
70 // Process it
71 instance.ProcessAudio();
72 // Write it to stdout
73 if (fwrite(instance.GetBufferOut().data(), sizeof(float), sound4::bigvoice::OutputSampleSize, stdout) != sound4::bigvoice::OutputSampleSize) {
74 std::cerr << "Failed to write audio output" << std::endl;
75 stopthread=true;
76 }
77 }
79}
80
81void Usage(const char *progname)
82{
83 std::cerr << "Usage: " << progname << std::endl;;
84 std::cerr << " Reads audio from stdin and return processed audio in stdout" << std::endl;
85 std::cerr << "Environment:"<< std::endl;;
86 std::cerr << " - S4STORE_PATH : path to store config [default: (no storage)]"<< std::endl;;
87 std::cerr << " - S4LOGINKEY : SOUND4 Login key (or S4USERID)"<< std::endl;;
88 std::cerr << " - RADIO_NAME : SOUND4 Radio Name"<< std::endl;;
89 std::cerr << " - ACCESS_KEY_ID : SOUND4 Access Key ID"<< std::endl;;
90 std::cerr << " - ACCESS_KEY_SECRET : SOUND4 Access Key Secret"<< std::endl;;
91 std::cerr << std::endl;
92 std::cerr << "Example:"<< std::endl;
93 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;;
94 #if defined(_WIN32) || defined(WIN32)
95 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;;
96 #else
97 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;;
98 #endif
99}
100
101void MyLogger(sound4::bigvoice::LogSeverity severity,const std::string& msg)
102{
103 std::cerr << msg << std::endl;
104}
105
106int main(int argc, const char **argv)
107{
108#if (BIGVOICE_HAS_WEBSERVER)
109 int listen_port = 8080;
110#endif // (BIGVOICE_HAS_WEBSERVER)
111 auto store_path_str=GetEnv("S4STORE_PATH");
112
113 // Get credentials from environment
114 auto LoginKey=GetEnv("S4LOGINKEY");
115 if (LoginKey.empty()) {
116 LoginKey=GetEnv("S4USERID");
117 }
118 auto RadioName=GetEnv("RADIO_NAME");
119 auto KeyId=GetEnv("ACCESS_KEY_ID");
120 auto AccessKey=GetEnv("ACCESS_KEY_SECRET");
121
122 const char *progname=argv[0];
123
124 if (argc>1) {
125 if ((strcasecmp(argv[1],"-h")==0) || (strcasecmp(argv[1],"--help")==0)) {
126 Usage(progname);
127 return 0;
128 }
129 }
130 if (LoginKey.empty() || RadioName.empty() || KeyId.empty() || AccessKey.empty()) {
131 std::cerr << "Missing credentials in environment" << std::endl;
132 Usage(progname);
133 return 1;
134 }
135 std::filesystem::path store_path;
136 if (!store_path_str.empty()) {
137 std::error_code ec;
138 store_path = std::filesystem::path(store_path_str);
139 if (!std::filesystem::exists(store_path,ec) || !std::filesystem::is_directory(store_path,ec)) {
140 std::cerr << "Storage folder '"<<store_path.c_str()<<"' is not a valid directory" << std::endl;
141 Usage(progname);
142 return 1;
143 }
144 }
145
149
151 instance.SetParam("ADMIN_USER","admin");
152 instance.SetParam("ADMIN_SECRET","admin");
154
155 std::cerr << "Creating" << std::endl;
157 if (!instance.Create(LoginKey, RadioName, KeyId, AccessKey, store_path)) {
158 std::cerr << "Failed to create the processing instance" << std::endl;
159 return 1;
160 }
162
163#if (BIGVOICE_HAS_WEBSERVER)
164 std::cerr << "Starting web server" << std::endl;
166 if (!instance.StartWebServer(listen_port,0)) {
167 std::cerr << "Failed to create the web server on port " << listen_port << std::endl;
168 return 1;
169 }
171#endif // (BIGVOICE_HAS_WEBSERVER)
172
173 {
175 auto client = instance.NewClient();
176 auto answer = client->ProcessJson(R"JSON({"get":{"processorname":null}})JSON");
177 std::cerr << "Request for processor name returned "<<answer<<std::endl;
179 }
180
181 ProcessLoop(instance);
182
183 std::cerr << "Stopping now" << std::endl;
184 instance.Stop();
185
186 std::cerr << "End" << std::endl;
187 return 0;
188}
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 &Access_Key_ID, const std::string &Access_Key_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
@ verbose5
verbose5
static void SetLogSeverity(LogSeverity severity)
const size_t OutputSampleSize
static unsigned int GetSampleRate()
const size_t InputSampleSize
static unsigned int GetChannelCount()
static void SetLoggerCallback(log_cb_t cb)
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.