Skip to content

Commit 32a4f4f

Browse files
committed
'timeout' parameter removed from the interfaces of the feature input classes, used in the online decoders
git-svn-id: https://svn.code.sf.net/p/kaldi/code/trunk@2685 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
1 parent 8268bf7 commit 32a4f4f

17 files changed

Lines changed: 132 additions & 98 deletions

‎src/gst-plugin/gst-audio-source.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void GstBufferSource::SetEnded(bool ended) {
6161
}
6262

6363

64-
bool GstBufferSource::Read(Vector<BaseFloat> *data, uint32 timeout) {
64+
bool GstBufferSource::Read(Vector<BaseFloat> *data) {
6565
uint32 nsamples_req = data->Dim(); // (16bit) samples requested
6666
int16 buf[data->Dim()];
6767
uint32 nbytes_transferred = 0;

‎src/gst-plugin/gst-audio-source.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
#ifndef KALDI_SRC_GST_PLUGIN_GST_AUDIO_SOURCE_H_
1919
#define KALDI_SRC_GST_PLUGIN_GST_AUDIO_SOURCE_H_
2020

21+
#include <online/online-audio-source.h>
2122
#include <matrix/kaldi-vector.h>
2223
#include <gst/gst.h>
2324

2425
namespace kaldi {
2526

2627

27-
// OnlineAudioSource implementation using a queue of Gst Buffers
28-
class GstBufferSource {
28+
// OnlineAudioSourceItf implementation using a queue of Gst Buffers
29+
class GstBufferSource : public OnlineAudioSourceItf {
2930
public:
3031
typedef int16 SampleType; // hardcoded 16-bit audio
3132

3233
GstBufferSource();
3334

34-
// Implementation of the OnlineAudioSource "interface"
35-
bool Read(Vector<BaseFloat> *data, uint32 timeout = 0);
35+
// Implementation of the OnlineAudioSourceItf
36+
bool Read(Vector<BaseFloat> *data);
3637

3738
void PushBuffer(GstBuffer *buf);
3839

‎src/gst-plugin/gst-online-gmm-decode-faster.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
namespace kaldi {
3434

3535

36-
typedef OnlineFeInput<GstBufferSource, Mfcc> FeInput;
36+
typedef OnlineFeInput<Mfcc> FeInput;
3737

3838
G_BEGIN_DECLS
3939

‎src/online/online-audio-source.cc‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ int PaCallback(const void *input, void *output,
3636
}
3737

3838

39-
OnlinePaSource::OnlinePaSource(const uint32 sample_rate,
39+
OnlinePaSource::OnlinePaSource(const uint32 timeout,
40+
const uint32 sample_rate,
4041
const uint32 rb_size,
4142
const uint32 report_interval)
42-
: sample_rate_(sample_rate), pa_started_(false),
43+
: timeout_(timeout), timed_out_(false),
44+
sample_rate_(sample_rate), pa_started_(false),
4345
report_interval_(report_interval), nread_calls_(0),
4446
noverflows_(0), samples_lost_(0) {
4547
using namespace std;
@@ -79,7 +81,7 @@ OnlinePaSource::~OnlinePaSource() {
7981
}
8082

8183

82-
bool OnlinePaSource::Read(Vector<BaseFloat> *data, int32 timeout) {
84+
bool OnlinePaSource::Read(Vector<BaseFloat> *data) {
8385
if (!pa_started_) { // start stream the first time Read() is called
8486
PaError paerr = Pa_StartStream(pa_stream_);
8587
if (paerr != paNoError)
@@ -95,14 +97,16 @@ bool OnlinePaSource::Read(Vector<BaseFloat> *data, int32 timeout) {
9597
samples_lost_ = noverflows_ = 0;
9698
}
9799
uint32 nsamples_req = data->Dim(); // samples to request
100+
timed_out_ = false;
98101
while (true) {
99102
ring_buffer_size_t nsamples = PaUtil_GetRingBufferReadAvailable(&pa_ringbuf_);
100103
if (nsamples >= nsamples_req)
101104
break;
102-
if (timeout != 0) {
105+
if (timeout_ > 0) {
103106
int32 elapsed = static_cast<int32>(timer.Elapsed() * 1000);
104-
if (elapsed > timeout) {
107+
if (elapsed > timeout_) {
105108
nsamples_req = nsamples;
109+
timed_out_ = true;
106110
KALDI_VLOG(2) << "OnlinePaSource::Read() timeout";
107111
break;
108112
}
@@ -142,7 +146,7 @@ int OnlinePaSource::Callback(const void *input, void *output,
142146
}
143147

144148

145-
bool OnlineVectorSource::Read(Vector<BaseFloat> *data, int32 timeout) {
149+
bool OnlineVectorSource::Read(Vector<BaseFloat> *data) {
146150
KALDI_ASSERT(data->Dim() > 0);
147151
int32 n_elem = std::min(src_.Dim() - pos_,
148152
static_cast<uint32>(data->Dim()));

‎src/online/online-audio-source.h‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,57 @@
2828

2929
namespace kaldi {
3030

31-
// There is in fact no real hierarchy of classes w/ virtual methods etc.,
32-
// as C++ templates are used instead. The class below is given just
33-
// to document the interface.
34-
class OnlineAudioSource {
31+
class OnlineAudioSourceItf {
3532
public:
3633
// Reads from the audio source, and writes the samples converted to BaseFloat
37-
// into the vector pointed by "data". The user sets data->Dim() as a way of
38-
// requesting that many samples. If timeout > 0, this function is to wait no
39-
// longer than "timeout" milliseconds before returning data-- by that time, it
40-
// will return as much data as it has.
34+
// into the vector pointed by "data".
35+
// The user sets data->Dim() as a way of requesting that many samples.
4136
// The function returns true if there may be more data, and false if it
4237
// knows we are at the end of the stream.
4338
// In case an unexpected and unrecoverable error occurs the function throws
4439
// an exception of type std::runtime_error (e.g. by using KALDI_ERR macro).
45-
bool Read(Vector<BaseFloat> *data, int32 timeout = 0) { return 0; }
40+
//
41+
// NOTE: The older version of this interface had a second paramater - "timeout".
42+
// We decided to remove it, because we don't envision usage scenarios,
43+
// where "timeout" will need to be changed dynamically from call to call.
44+
// If the particular audio source can experience timeouts for some reason
45+
// (e.g. the samples are received over a network connection)
46+
// we encourage the implementors to configure timeout using a
47+
// constructor parameter.
48+
// The suggested semantics are: if timeout is used and is greater than 0,
49+
// this method has to wait no longer than "timeout" milliseconds before
50+
// returning data-- by that time, it will return as much data as it has.
51+
virtual bool Read(Vector<BaseFloat> *data) = 0;
52+
53+
virtual ~OnlineAudioSourceItf() { }
4654
};
4755

4856

49-
// OnlineAudioSource implementation using PortAudio to read samples in real-time
57+
// OnlineAudioSourceItf implementation using PortAudio to read samples in real-time
5058
// from a sound card/microphone.
51-
class OnlinePaSource {
59+
class OnlinePaSource : public OnlineAudioSourceItf {
5260
public:
5361
typedef int16 SampleType; // hardcoded 16-bit audio
5462
typedef ring_buffer_size_t rbs_t;
5563

5664
// PortAudio is initialized here, so it may throw an exception on error
65+
// "timeout": if > 0, and the acquisition takes more than this number of
66+
// milliseconds, Compute() will return the data it has so far
67+
// If no data was received until timeout expired, Compute() returns
68+
// false (assumes sensible timeout).
5769
// "sample_rate": the input rate to request from PortAudio
5870
// "rb_size": requested size of PA's ring buffer - will be round up to
5971
// power of 2
6072
// "report_interval": if not 0, PA ring buffer overflow will be reported
6173
// at every ovfw_msg_interval-th call to Read().
6274
// Putting 0 into this argument disables the reporting.
63-
OnlinePaSource(const uint32 sample_rate,
75+
OnlinePaSource(const uint32 timeout,
76+
const uint32 sample_rate,
6477
const uint32 rb_size,
6578
const uint32 report_interval);
6679

67-
// Implementation of the OnlineAudioSource "interface" - see above
68-
bool Read(Vector<BaseFloat> *data, int32 timeout = 0);
80+
// Implementation of the OnlineAudioSourceItf
81+
bool Read(Vector<BaseFloat> *data);
6982

7083
// Making friends with the callback so it will be able to access a private
7184
// member function to delegate the processing
@@ -75,6 +88,10 @@ class OnlinePaSource {
7588
PaStreamCallbackFlags status_flags,
7689
void *user_data);
7790

91+
// Returns True if the last call to Read() failed to read the requested
92+
// number of samples due to timeout.
93+
bool TimedOut() { return timed_out_; }
94+
7895
~OnlinePaSource();
7996

8097
private:
@@ -84,6 +101,10 @@ class OnlinePaSource {
84101
const PaStreamCallbackTimeInfo *time_info,
85102
PaStreamCallbackFlags status_flags);
86103

104+
uint32 timeout_; // timeout in milliseconds. if > 0, after this many ms. we
105+
// give up trying to read data from PortAudio
106+
bool timed_out_; // True if the last call to Read() failed to obtain the requested
107+
// number of samples, because of timeout
87108
uint32 sample_rate_; // the sampling rate of the input audio
88109
int32 rb_size_;
89110
char *ring_buffer_; // points to the actual buffer used by PA to store samples
@@ -108,13 +129,13 @@ int PaCallback(const void *input, void *output,
108129
// Simulates audio input, by returning data from a Vector.
109130
// This class is mostly meant to be used for online decoder testing using
110131
// pre-recorded audio
111-
class OnlineVectorSource {
132+
class OnlineVectorSource: public OnlineAudioSourceItf {
112133
public:
113134
OnlineVectorSource(const VectorBase<BaseFloat> &input)
114135
: src_(input), pos_(0) {}
115136

116-
// Implementation of the OnlineAudioSource "interface" - see above
117-
bool Read(Vector<BaseFloat> *data, int32 timeout);
137+
// Implementation of the OnlineAudioSourceItf
138+
bool Read(Vector<BaseFloat> *data);
118139

119140
private:
120141
Vector<BaseFloat> src_;

‎src/online/online-feat-input.cc‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// See the Apache 2 License for the specific language governing permissions and
2020
// limitations under the License.
2121

22-
#include "online/online-feat-input.h"
22+
#include "online-feat-input.h"
2323

2424
namespace kaldi {
2525

@@ -30,12 +30,12 @@ namespace kaldi {
3030
// This function prevents those initial non-productive calls, which
3131
// may otherwise confuse decoder code into thinking there is
3232
// a problem with the stream (too many timeouts), and cause it to fail.
33-
bool OnlineCmnInput::Compute(Matrix<BaseFloat> *output, int32 timeout) {
33+
bool OnlineCmnInput::Compute(Matrix<BaseFloat> *output) {
3434

3535
int32 orig_nr = output->NumRows(), orig_nc = output->NumCols();
3636
int32 initial_t_in = t_in_;
3737
bool ans;
38-
while ((ans = ComputeInternal(output, timeout))) {
38+
while ((ans = ComputeInternal(output))) {
3939
if (output->NumRows() == 0 &&
4040
t_in_ != initial_t_in) {
4141
// we produced no output but added to our internal buffer.
@@ -71,14 +71,13 @@ int32 OnlineCmnInput::NumOutputFrames(int32 num_new_frames,
7171
// What happens at the start of the utterance is not really ideal, it would be
7272
// better to have some "fake stats" extracted from typical data from this domain,
7373
// to start with. We'll have to do this later.
74-
bool OnlineCmnInput::ComputeInternal(Matrix<BaseFloat> *output,
75-
int32 timeout) {
74+
bool OnlineCmnInput::ComputeInternal(Matrix<BaseFloat> *output) {
7675
KALDI_ASSERT(output->NumRows() > 0 && output->NumCols() == Dim());
7776

7877
Matrix<BaseFloat> input;
7978
input.Swap(output);
8079

81-
bool more_data = input_->Compute(&input, timeout);
80+
bool more_data = input_->Compute(&input);
8281

8382
int32 num_input_frames = input.NumRows();
8483

@@ -166,9 +165,7 @@ OnlineUdpInput::OnlineUdpInput(int32 port, int32 feature_dim):
166165
}
167166

168167

169-
bool OnlineUdpInput::Compute(Matrix<BaseFloat> *output, int32 timeout) {
170-
KALDI_ASSERT(timeout == 0 &&
171-
"Timeout parameter currently not supported by OnlineUdpInput!");
168+
bool OnlineUdpInput::Compute(Matrix<BaseFloat> *output) {
172169
char buf[65535];
173170
socklen_t caddr_len = sizeof(client_addr_);
174171
ssize_t nrecv = recvfrom(sock_desc_, buf, sizeof(buf), 0,
@@ -254,15 +251,15 @@ void OnlineLdaInput::TransformToOutput(const MatrixBase<BaseFloat> &spliced_feat
254251
}
255252
}
256253

257-
bool OnlineLdaInput::Compute(Matrix<BaseFloat> *output, int32 timeout) {
254+
bool OnlineLdaInput::Compute(Matrix<BaseFloat> *output) {
258255
KALDI_ASSERT(output->NumRows() > 0 &&
259256
output->NumCols() == linear_transform_.NumRows());
260257
// If output->NumRows() == 0, it corresponds to a request for zero frames,
261258
// which makes no sense.
262259

263260
// We request the same number of frames of data that we were requested.
264261
Matrix<BaseFloat> input(output->NumRows(), input_dim_);
265-
bool ans = input_->Compute(&input, timeout);
262+
bool ans = input_->Compute(&input);
266263
// If we got no input (timed out) and we're not at the end, we return
267264
// empty output.
268265

@@ -331,8 +328,8 @@ void OnlineLdaInput::ComputeNextRemainder(const MatrixBase<BaseFloat> &input) {
331328
}
332329

333330

334-
bool OnlineCacheInput::Compute(Matrix<BaseFloat> *output, int32 timeout) {
335-
bool ans = input_->Compute(output, timeout);
331+
bool OnlineCacheInput::Compute(Matrix<BaseFloat> *output) {
332+
bool ans = input_->Compute(output);
336333
if (output->NumRows() != 0)
337334
data_.push_back(new Matrix<BaseFloat>(*output));
338335
return ans;
@@ -417,15 +414,15 @@ void OnlineDeltaInput::DeltaComputation(const MatrixBase<BaseFloat> &input,
417414
}
418415
}
419416

420-
bool OnlineDeltaInput::Compute(Matrix<BaseFloat> *output, int32 timeout) {
417+
bool OnlineDeltaInput::Compute(Matrix<BaseFloat> *output) {
421418
KALDI_ASSERT(output->NumRows() > 0 &&
422419
output->NumCols() == Dim());
423420
// If output->NumRows() == 0, it corresponds to a request for zero frames,
424421
// which makes no sense.
425422

426423
// We request the same number of frames of data that we were requested.
427424
Matrix<BaseFloat> input(output->NumRows(), input_dim_);
428-
bool ans = input_->Compute(&input, timeout);
425+
bool ans = input_->Compute(&input);
429426

430427
// If we got no input (timed out) and we're not at the end, we return
431428
// empty output.
@@ -484,7 +481,7 @@ void OnlineFeatureMatrix::GetNextFeatures() {
484481
int32 iter;
485482
for (iter = 0; iter < opts_.num_tries; iter++) {
486483
Matrix<BaseFloat> next_features(opts_.batch_size, feat_dim_);
487-
finished_ = ! input_->Compute(&next_features, opts_.timeout);
484+
finished_ = ! input_->Compute(&next_features);
488485
if (next_features.NumRows() == 0 && ! finished_) {
489486
// It timed out. Try again.
490487
continue;

0 commit comments

Comments
 (0)