2828
2929namespace 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_;
0 commit comments