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

[not-for-merge]Allow limited number of overruns in sherpa-ncnn-alsa #235

Open
wants to merge 8 commits into
base: master
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
34 changes: 34 additions & 0 deletions sherpa-ncnn/csrc/alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ const std::vector<float> &Alsa::Read16(int32_t num_samples) {
int32_t count =
snd_pcm_readi(capture_handle_, samples16_.data(), num_samples);
if (count == -EPIPE) {
static int32_t num_over_runs = 0;
++num_over_runs;
fprintf(stderr, "number of overruns: %d/%d\n", num_over_runs,
num_allowed_over_runs_);

if (num_over_runs < num_allowed_over_runs_) {
int32_t err = snd_pcm_prepare(capture_handle_);
if (err) {
fprintf(stderr, "Failed to recover from overrun: %s\n",
snd_strerror(err));
exit(-1);
}

static std::vector<float> tmp;
return tmp;
}
fprintf(
stderr,
"An overrun occurred, which means the RTF of the current "
Expand Down Expand Up @@ -238,6 +254,24 @@ const std::vector<float> &Alsa::Read32(int32_t num_samples) {
int32_t count =
snd_pcm_readi(capture_handle_, samples32_.data(), num_samples);
if (count == -EPIPE) {
static int32_t num_over_runs = 0;
++num_over_runs;

fprintf(stderr, "number of overruns: %d/%d\n", num_over_runs,
num_allowed_over_runs_);

if (num_over_runs < num_allowed_over_runs_) {
int32_t err = snd_pcm_prepare(capture_handle_);
if (err) {
fprintf(stderr, "Failed to recover from overrun: %s\n",
snd_strerror(err));
exit(-1);
}

static std::vector<float> tmp;
return tmp;
}

fprintf(
stderr,
"An overrun occurred, which means the RTF of the current "
Expand Down
2 changes: 2 additions & 0 deletions sherpa-ncnn/csrc/alsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Alsa {

std::vector<float> samples1_; // normalized version of samples_
std::vector<float> samples2_; // possibly resampled from samples1_

int32_t num_allowed_over_runs_ = 20;
};

} // namespace sherpa_ncnn
Expand Down
30 changes: 30 additions & 0 deletions sherpa-ncnn/csrc/sherpa-ncnn-alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <cctype> // std::tolower
#include <cstdint>
#include <fstream>

#include "sherpa-ncnn/csrc/alsa.h"
#include "sherpa-ncnn/csrc/display.h"
Expand Down Expand Up @@ -139,8 +140,37 @@ as the device_name.
std::string last_text;
int32_t segment_index = 0;
sherpa_ncnn::Display display;

fprintf(stderr, "Writing to test.pcm\n");
fprintf(stderr, "Please use\n");
fprintf(stderr,
" sox -t raw -c 1 -e floating-point -b 32 -r %d test.pcm test.wav\n",
alsa.GetExpectedSampleRate());
fprintf(stderr, "to convert test.pcm to test.wav\n");

std::ofstream os("test.pcm", std::ios::out | std::ios::binary);

int32_t num_samples_wrote = 0;

while (!stop) {
const std::vector<float> samples = alsa.Read(chunk);
if (!samples.empty()) {
if (os.is_open()) {
fprintf(stderr, "Writing %d samples\n", int32_t(samples.size()));
os.write(reinterpret_cast<const char *>(samples.data()),
samples.size() * sizeof(float));
num_samples_wrote += samples.size();
fprintf(stderr, "Wrote %d samples so far\n", num_samples_wrote);
if (num_samples_wrote > 10 * alsa.GetExpectedSampleRate()) {
fprintf(stderr,
"Closing test.pcm after writing 10 seconds of data\n");
os.close();
}
}
} else {
fprintf(stderr, "empty samples!");
continue;
}

s->AcceptWaveform(expected_sampling_rate, samples.data(), samples.size());
while (recognizer.IsReady(s.get())) {
Expand Down