Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/log latency #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ cmake-build-release
.cmake
*.wav
*.token
.vscode
17 changes: 4 additions & 13 deletions headers/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,17 @@ class Audio {

int64_t getBytesPerSamples() const { return sizeof(data[0]); }

template<std::size_t chunkLength>
std::vector<std::array<int16_t, chunkLength> > getAudioChunks() const;
std::vector<std::vector<int16_t> > getAudioChunks(size_t chunkLength) const;

int64_t getChannels() const { return channels; }

private:
std::unique_ptr<int16_t[]> data;
int64_t length{0};
int64_t samplingRate{0};
int64_t channels{1};
};

template<std::size_t chunkLength>
std::vector<std::array<int16_t, chunkLength> > Audio::getAudioChunks() const {
const auto numberOfChunks = static_cast<std::size_t >(std::ceil(static_cast<double>(length) / chunkLength));
std::vector<std::array<int16_t, chunkLength> > chunks(numberOfChunks, std::array<int16_t, chunkLength>());
for (int i = 0; i < numberOfChunks - 1; ++i)
std::copy_n(&data[i * chunkLength], chunkLength, chunks[i].begin());
chunks[numberOfChunks - 1].fill(0);
std::copy_n(&data[(numberOfChunks - 1) * chunkLength], chunkLength - (numberOfChunks * chunkLength - length),
chunks[numberOfChunks - 1].begin());
return chunks;
}

#endif //CLI_CLIENT_AUDIO_H

9 changes: 9 additions & 0 deletions headers/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class Configuration {

bool getFormatting() const;

bool getVerbosity() const;

uint32_t getChunkSize() const;

uint32_t getNumberOfChannels() const;

private:
std::string language;
std::string topic;
Expand All @@ -51,6 +57,9 @@ class Configuration {
bool notSecure;
bool diarization;
bool formatting;
bool verbose;
uint32_t chunkSize;
uint32_t numberOfChannels;
};


Expand Down
17 changes: 16 additions & 1 deletion src/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@ Audio::Audio(const std::string &audioPath) {
auto samplingRate = sndfileHandle.samplerate();
if (!sndfileHandle.formatCheck(SF_FORMAT_PCM_16 | SF_FORMAT_WAV, sndfileHandle.channels(), samplingRate))
throw GrpcException("Unsupported file audio format");
INFO("Read {} samples with {} bytes per sample", length, sizeof(data));
INFO("Read {} samples with {} bytes per sample", length, sizeof(int16_t));

this->length = length;
this->samplingRate = samplingRate;
this->channels = sndfileHandle.channels();
this->data = std::make_unique<int16_t[]>(length);
std::copy_n(data, length, this->data.get());
}

std::vector<std::vector<int16_t> > Audio::getAudioChunks(size_t chunkLength) const {

const auto numberOfChunks = static_cast<std::size_t >(std::ceil(static_cast<double>(length) / chunkLength));
if (numberOfChunks <= 0)
return {};
std::vector<std::vector<int16_t> > chunks(numberOfChunks, std::vector<int16_t>(chunkLength, 0));
for (int i = 0; i < numberOfChunks - 1; ++i)
std::copy_n(&data[i * chunkLength], chunkLength, chunks[i].begin());
std::copy_n(&data[(numberOfChunks - 1) * chunkLength], chunkLength - (numberOfChunks * chunkLength - length),
chunks[numberOfChunks - 1].begin());

return chunks;
}
39 changes: 32 additions & 7 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

#include <cxxopts.hpp>

#include <Audio.h>

#include <spdlog/spdlog.h>


Configuration::Configuration() : host("csr.api.speechcenter.verbio.com"), topic("generic"), language("en-US"),
sampleRate(8000) {}
sampleRate(8000), chunkSize(20000), numberOfChannels(1) {}

Configuration::Configuration(int argc, char **argv) : Configuration() {
parse(argc, argv);
Expand All @@ -28,8 +32,6 @@ void Configuration::parse(int argc, char **argv) {
("l,language",
"Language to use for the recognition: en-US, en-GB, pt-BR, es, es-419, tr, ja, fr, fr-CA, de, it",
cxxopts::value(language)->default_value(language))
("s,sample-rate", "Sampling rate for the audio recognition: 8000, 16000.",
cxxopts::value<uint32_t>(sampleRate)->default_value(std::to_string(sampleRate)))
("t,token", "Path to the authentication token file", cxxopts::value(tokenPath))
("H,host", "URL of the Host or server trying to reach",
cxxopts::value(host)->default_value("eu.speechcenter.verbio.com"))
Expand All @@ -38,6 +40,8 @@ void Configuration::parse(int argc, char **argv) {
("d,diarization", "Toggle for diarization", cxxopts::value<bool>(diarization)->default_value("false"))
("f,formatting", "Toggle for formatting", cxxopts::value<bool>(formatting)->default_value("false"))
("A,asr-version", "Selectable asr version. Must be V1 | V2", cxxopts::value(asrVersion))
("c,chunk-size", "Size of audio chunk", cxxopts::value<uint32_t>(chunkSize)->default_value(std::to_string(chunkSize)))
("v,verbose", "Toogle for verbose output", cxxopts::value<bool>(verbose)->default_value("false"))
("h,help", "this help message");
auto parsedOptions = options.parse(argc, argv);

Expand All @@ -47,6 +51,14 @@ void Configuration::parse(int argc, char **argv) {
}
if ((parsedOptions.count("t") == 0) == (parsedOptions.count("g") == 0))
throw GrpcException("Topic and grammar options are mutually exclusive and at least one is needed.");

auto const &audio = Audio(audioPath);
sampleRate = audio.getSamplingRate();
numberOfChannels = audio.getChannels();

if (verbose){
spdlog::set_level(spdlog::level::trace);
}
}

std::string Configuration::getAudioPath() const {
Expand All @@ -73,10 +85,6 @@ std::string Configuration::getTopic() const {
return topic;
}

uint32_t Configuration::getSampleRate() const {
return sampleRate;
}

bool Configuration::getNotSecure() const {
return notSecure;
}
Expand All @@ -92,3 +100,20 @@ bool Configuration::getDiarization() const {
bool Configuration::getFormatting() const {
return formatting;
}

bool Configuration::getVerbosity() const {
return verbose;
}

uint32_t Configuration::getChunkSize() const {
return chunkSize;
}


uint32_t Configuration::getSampleRate() const {
return sampleRate;
}

uint32_t Configuration::getNumberOfChannels() const {
return numberOfChannels;
}
16 changes: 9 additions & 7 deletions src/RecognitionClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ void RecognitionClient::write(

INFO("Sending audio...");
int requestCount = 0;
double totalAudioSentInSeconds{0};
for (const auto &request: buildAudioRequests()) {
constexpr int bytesPerSamples = 2;// PCM16
double loopAudioSentInMilliseconds = request.audio().length() * 1000.00 / (bytesPerSamples * configuration.getSampleRate());
auto deadline = std::chrono::system_clock::now() +
std::chrono::milliseconds(
request.audio().length() * 1000 / (bytesPerSamples * configuration.getSampleRate()));
std::chrono::milliseconds(static_cast<int64_t>(loopAudioSentInMilliseconds));
if (!stream->Write(request)) {
auto status = stream->Finish();
ERROR("{} ({}: {})", status.error_message(), status.error_code(), status.error_details());
throw StreamException(status.error_message());
}
++requestCount;
if (requestCount % 10 == 0)
INFO("Sent {} bytes of audio", requestCount * request.audio().length());
totalAudioSentInSeconds += loopAudioSentInMilliseconds / 1000.00;

DEBUG ("Sent {} bytes of audio", requestCount * request.audio().length());
DEBUG("Total time of audio sent {}", totalAudioSentInSeconds);

std::this_thread::sleep_until(deadline);
}
Expand Down Expand Up @@ -174,7 +177,7 @@ std::vector<Request> RecognitionClient::buildAudioRequests() {
INFO("Audio bytes: " + std::to_string(lengthInBytes));

std::vector<Request> requests;
auto chunks = audio.getAudioChunks<20000>();
auto chunks = audio.getAudioChunks(configuration.getChunkSize());
for (const auto &chunk: chunks) {
Request request;
request.set_audio(static_cast<const void *>(chunk.data()), chunk.size() * audio.getBytesPerSamples());
Expand All @@ -194,6 +197,7 @@ RecognitionClient::buildRecognitionParameters() {
parameters->set_enable_formatting(configuration.getFormatting());
INFO("Enabled diarization: {}", configuration.getDiarization());
parameters->set_enable_diarization(configuration.getDiarization());
parameters->set_audio_channels_number(configuration.getNumberOfChannels());

return parameters;
}
Expand All @@ -202,8 +206,6 @@ std::unique_ptr<PCM>
RecognitionClient::buildPCM(const uint32_t &sampleRate) {
std::unique_ptr<PCM> pcm(
new PCM());
if (sampleRate != 16000 && sampleRate != 8000)
throw UnsupportedSampleRate(std::to_string(sampleRate));
pcm->set_sample_rate_hz(sampleRate);
return pcm;
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST(Audio, chunks) {
constexpr int chunkLengthInSamples = 2203;
std::array<int16_t, numberOfSamples> rawAudio;
for (int i = 0; i < numberOfSamples; ++i) rawAudio[i] = i;
auto chunks = Audio(rawAudio.data(), 8000, numberOfSamples).getAudioChunks<chunkLengthInSamples>();
auto chunks = Audio(rawAudio.data(), 8000, numberOfSamples).getAudioChunks(2203);
EXPECT_EQ(chunks.size(), std::ceil(static_cast<float>(numberOfSamples) / chunkLengthInSamples));
for (int chunk = 0; chunk < chunks.size(); ++chunk)
for (int i = 0; i < chunkLengthInSamples; ++i)
Expand Down