Fangjun Kuang
Committed by GitHub

Print informative error messages for sherpa-onnx-alsa on errors. (#486)

@@ -144,6 +144,24 @@ const std::vector<float> &Alsa::Read(int32_t num_samples) { @@ -144,6 +144,24 @@ const std::vector<float> &Alsa::Read(int32_t num_samples) {
144 144
145 // count is in frames. Each frame contains actual_channel_count_ samples 145 // count is in frames. Each frame contains actual_channel_count_ samples
146 int32_t count = snd_pcm_readi(capture_handle_, samples_.data(), num_samples); 146 int32_t count = snd_pcm_readi(capture_handle_, samples_.data(), num_samples);
  147 + if (count == -EPIPE) {
  148 + static int32_t n = 0;
  149 + if (++n > 5) {
  150 + fprintf(
  151 + stderr,
  152 + "Too many overruns. It is very likely that the RTF on your board is "
  153 + "larger than 1. Please use ./bin/sherpa-onnx to compute the RTF.\n");
  154 + exit(-1);
  155 + }
  156 + fprintf(stderr, "XRUN.\n");
  157 + snd_pcm_prepare(capture_handle_);
  158 +
  159 + static std::vector<float> tmp;
  160 + return tmp;
  161 + } else if (count < 0) {
  162 + fprintf(stderr, "Can't read PCM device: %s\n", snd_strerror(count));
  163 + exit(-1);
  164 + }
147 165
148 samples_.resize(count * actual_channel_count_); 166 samples_.resize(count * actual_channel_count_);
149 167
@@ -104,7 +104,7 @@ as the device_name. @@ -104,7 +104,7 @@ as the device_name.
104 104
105 int32_t segment_index = 0; 105 int32_t segment_index = 0;
106 while (!stop) { 106 while (!stop) {
107 - const std::vector<float> samples = alsa.Read(chunk); 107 + const std::vector<float> &samples = alsa.Read(chunk);
108 108
109 stream->AcceptWaveform(expected_sample_rate, samples.data(), 109 stream->AcceptWaveform(expected_sample_rate, samples.data(),
110 samples.size()); 110 samples.size());