Created micTask for noise detection Installed GoogleTest Refactored audio module
83 lines
2.0 KiB
C++
83 lines
2.0 KiB
C++
#include "domain/Recorder.h"
|
|
|
|
#include "config/config.h"
|
|
#include "utils/Time.h"
|
|
|
|
Time& timeModule = Time::getInstance();
|
|
|
|
Recorder::Recorder(MicType micType, Http* http) {
|
|
_audio = new Audio(micType);
|
|
_http = http;
|
|
}
|
|
|
|
Recorder::~Recorder() {
|
|
delete _audio;
|
|
}
|
|
|
|
void Recorder::recordAudio() {
|
|
_ready();
|
|
|
|
if (_http->connect(RAT_IP, RAT_PORT)) {
|
|
Serial.println("========================================");
|
|
Serial.println("| RECORDING...");
|
|
_audio->record();
|
|
Serial.println("| Recording finished");
|
|
|
|
_transcribe();
|
|
|
|
} else {
|
|
Serial.println("| Connection failed");
|
|
}
|
|
}
|
|
|
|
void Recorder::_ready() {
|
|
_http->send("POST", "/ready", RAT_IP, RAT_PORT, "", "");
|
|
}
|
|
|
|
void Recorder::_transcribe() {
|
|
String payloadStart = _formPayloadStart();
|
|
String payloadEnd = _formPayloadEnd();
|
|
|
|
int audioLength = _audio->getSize();
|
|
int jsonLength = payloadStart.length() + audioLength + payloadEnd.length();
|
|
|
|
_http->printHeader("POST", "/save_to_file", RAT_IP, "application/json");
|
|
_http->printContentLength(jsonLength);
|
|
|
|
this->_printContent(payloadStart, payloadEnd, _http->getClient());
|
|
}
|
|
|
|
void Recorder::_printContent(String payloadStart, String payloadEnd, Stream* stream) {
|
|
stream->print(payloadStart);
|
|
_audio->print(stream);
|
|
stream->print(payloadEnd);
|
|
|
|
_logContent(payloadStart, payloadEnd);
|
|
}
|
|
|
|
void Recorder::_logContent(String payloadStart, String payloadEnd) {
|
|
Serial.print("| ");
|
|
Serial.print(payloadStart);
|
|
Serial.print("...");
|
|
Serial.print(payloadEnd);
|
|
}
|
|
|
|
String Recorder::_formPayloadStart() {
|
|
String payloadStart = "";
|
|
|
|
payloadStart += "{\"audio\":{\"micros\":";
|
|
payloadStart += String(_audio->getStartMicros() - timeModule.getInitialMicros());
|
|
payloadStart += ",\"initialTime\":";
|
|
payloadStart += String(timeModule.getInitialTime());
|
|
payloadStart += ",\"content\":\"";
|
|
|
|
return payloadStart;
|
|
}
|
|
|
|
String Recorder::_formPayloadEnd() {
|
|
return "\"}}\r\n\r\n";
|
|
}
|
|
|
|
bool Recorder::isNoiseDetected(int threshold) {
|
|
return _audio->isNoiseDetected(threshold);
|
|
} |