OnlinePaSource Class Reference

#include <online-audio-source.h>

Inheritance diagram for OnlinePaSource:
Collaboration diagram for OnlinePaSource:

Public Types

typedef int16 SampleType
 
typedef ring_buffer_size_t rbs_t
 

Public Member Functions

 OnlinePaSource (const uint32 timeout, const uint32 sample_rate, const uint32 rb_size, const uint32 report_interval)
 
bool Read (Vector< BaseFloat > *data)
 
bool TimedOut ()
 
 ~OnlinePaSource ()
 
- Public Member Functions inherited from OnlineAudioSourceItf
virtual ~OnlineAudioSourceItf ()
 

Private Member Functions

int Callback (const void *input, void *output, ring_buffer_size_t frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags)
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (OnlinePaSource)
 

Private Attributes

uint32 timeout_
 
bool timed_out_
 
uint32 sample_rate_
 
int32 rb_size_
 
char * ring_buffer_
 
PaUtilRingBuffer pa_ringbuf_
 
PaStream * pa_stream_
 
bool pa_started_
 
uint32 report_interval_
 
uint32 nread_calls_
 
uint32 noverflows_
 
uint32 samples_lost_
 

Friends

int PaCallback (const void *input, void *output, long unsigned frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags, void *user_data)
 

Detailed Description

Definition at line 66 of file online-audio-source.h.

Member Typedef Documentation

◆ rbs_t

typedef ring_buffer_size_t rbs_t

Definition at line 69 of file online-audio-source.h.

◆ SampleType

typedef int16 SampleType

Definition at line 68 of file online-audio-source.h.

Constructor & Destructor Documentation

◆ OnlinePaSource()

OnlinePaSource ( const uint32  timeout,
const uint32  sample_rate,
const uint32  rb_size,
const uint32  report_interval 
)

Definition at line 49 of file online-audio-source.cc.

References KALDI_ASSERT, KALDI_ERR, kaldi::Log(), OnlinePaSource::pa_ringbuf_, OnlinePaSource::pa_stream_, OnlinePaSource::PaCallback, OnlinePaSource::rb_size_, OnlinePaSource::ring_buffer_, and OnlinePaSource::sample_rate_.

53  : timeout_(timeout), timed_out_(false),
54  sample_rate_(sample_rate), pa_started_(false),
55  report_interval_(report_interval), nread_calls_(0),
56  noverflows_(0), samples_lost_(0) {
57  using namespace std;
58 
59  // Note this will work for 32bit integers but not for 64bit.
60  // For 64bit integers even double wouldn't work
61  // You would have to use something like
62  // int64 rb_bits = 0; while (rb_size != 0) {++rb_bits; rb_size >>= 1;}
63  // it would be much faster than two logs of FP numbers (even floats), too,
64  // but I don't have the time to test it.
65  float f = Log(static_cast<float>(rb_size)) / Log(static_cast<float>(2));
66  int32 rb_bits = static_cast<int32>(ceil(f));
67  if (rb_bits > 30) // ok, this limit is somewhat arbitrary
68  throw invalid_argument("PortAudio ring buffer too large!");
69  rb_size_ = 1 << rb_bits;
70  ring_buffer_ = new char[rb_size_];
71  ring_buffer_size_t rbs = PaUtil_InitializeRingBuffer(
72  &pa_ringbuf_, sizeof(SampleType),
73  rb_size_ / sizeof(SampleType), ring_buffer_);
74  if (rbs != 0)
75  KALDI_ERR << "PortAudio ring buffer init error";
76 
77  PaError paerr = Pa_Initialize();
78  if (paerr != paNoError)
79  KALDI_ERR << "PortAudio initialization error";
80  // Monophone, 16-bit input hardcoded
81  KALDI_ASSERT(sizeof(SampleType) == 2 &&
82  "The current OnlinePaSource code assumes 16-bit input");
83  paerr = Pa_OpenDefaultStream(&pa_stream_, 1, 0, paInt16, sample_rate_, 0,
84  PaCallback, this);
85  if (paerr != paNoError)
86  KALDI_ERR << "PortAudio failed to open the default stream";
87 }
friend int PaCallback(const void *input, void *output, long unsigned frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags, void *user_data)
PaUtilRingBuffer pa_ringbuf_
kaldi::int32 int32
double Log(double x)
Definition: kaldi-math.h:100
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ~OnlinePaSource()

Definition at line 90 of file online-audio-source.cc.

References OnlinePaSource::pa_started_, OnlinePaSource::pa_stream_, and OnlinePaSource::ring_buffer_.

90  {
91  if (pa_started_)
92  Pa_StopStream(pa_stream_);
93  if (pa_stream_ != 0) {
94  Pa_CloseStream(pa_stream_);
95  Pa_Terminate();
96  }
97  if (ring_buffer_ != 0)
98  delete [] ring_buffer_;
99 }

Member Function Documentation

◆ Callback()

int Callback ( const void *  input,
void *  output,
ring_buffer_size_t  frame_count,
const PaStreamCallbackTimeInfo *  time_info,
PaStreamCallbackFlags  status_flags 
)
private

Definition at line 156 of file online-audio-source.cc.

References OnlinePaSource::noverflows_, OnlinePaSource::pa_ringbuf_, OnlinePaSource::report_interval_, and OnlinePaSource::samples_lost_.

Referenced by kaldi::PaCallback().

159  {
160  if (report_interval_ != 0) {
161  if (frame_count > PaUtil_GetRingBufferWriteAvailable(&pa_ringbuf_))
162  ++noverflows_;
163  }
164  rbs_t written = PaUtil_WriteRingBuffer(&pa_ringbuf_, input, frame_count);
165  samples_lost_ += frame_count - written;
166  return paContinue;
167 }
PaUtilRingBuffer pa_ringbuf_
ring_buffer_size_t rbs_t

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( OnlinePaSource  )
private

◆ Read()

bool Read ( Vector< BaseFloat > *  data)
virtual

Implements OnlineAudioSourceItf.

Definition at line 102 of file online-audio-source.cc.

References VectorBase< Real >::Dim(), Timer::Elapsed(), rnnlm::i, KALDI_ERR, KALDI_VLOG, KALDI_WARN, OnlinePaSource::noverflows_, OnlinePaSource::nread_calls_, OnlinePaSource::pa_ringbuf_, OnlinePaSource::pa_started_, OnlinePaSource::pa_stream_, OnlinePaSource::report_interval_, Vector< Real >::Resize(), OnlinePaSource::samples_lost_, OnlinePaSource::timed_out_, and OnlinePaSource::timeout_.

102  {
103  if (!pa_started_) { // start stream the first time Read() is called
104  PaError paerr = Pa_StartStream(pa_stream_);
105  if (paerr != paNoError)
106  KALDI_ERR << "Error while trying to open PortAudio stream";
107  pa_started_ = true;
108  }
109  Timer timer;
110  if (report_interval_ != 0
111  && (++nread_calls_ % report_interval_) == 0
112  && noverflows_ > 0) {
113  KALDI_VLOG(1) << noverflows_
114  << " PortAudio ring buffer overflows detected "
115  << "and " << samples_lost_ << " sample(s) were lost";
116  samples_lost_ = noverflows_ = 0;
117  }
118  uint32 nsamples_req = data->Dim(); // samples to request
119  timed_out_ = false;
120  while (true) {
121  ring_buffer_size_t nsamples;
122  nsamples = PaUtil_GetRingBufferReadAvailable(&pa_ringbuf_);
123  if (nsamples >= nsamples_req)
124  break;
125  if (timeout_ > 0) {
126  int32 elapsed = static_cast<int32>(timer.Elapsed() * 1000);
127  if (elapsed > timeout_) {
128  nsamples_req = nsamples;
129  timed_out_ = true;
130  KALDI_VLOG(2) << "OnlinePaSource::Read() timeout";
131  break;
132  }
133  }
134  Pa_Sleep(2);
135  }
136  std::vector<int16> buf(nsamples_req);
137  rbs_t nsamples_rcv;
138  nsamples_rcv = PaUtil_ReadRingBuffer(&pa_ringbuf_, buf.data(), nsamples_req);
139  if (nsamples_rcv != nsamples_req) {
140  KALDI_WARN << "Requested: " << nsamples_req
141  << "; Received: " << nsamples_rcv << " samples";
142  // This would be a PortAudio error.
143  }
144  data->Resize(nsamples_rcv);
145  for (int i = 0; i < nsamples_rcv; ++i)
146  (*data)(i) = static_cast<BaseFloat>(buf[i]);
147 
148  return (nsamples_rcv != 0);
149  // NOTE (Dan): I'm pretty sure this return value is not right, it could be
150  // this way because we're waiting. Vassil or someone will have to figure this
151  // out.
152 }
PaUtilRingBuffer pa_ringbuf_
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
ring_buffer_size_t rbs_t

◆ TimedOut()

bool TimedOut ( )
inline

Definition at line 100 of file online-audio-source.h.

Referenced by main().

100 { return timed_out_; }

Friends And Related Function Documentation

◆ PaCallback

int PaCallback ( const void *  input,
void *  output,
long unsigned  frame_count,
const PaStreamCallbackTimeInfo *  time_info,
PaStreamCallbackFlags  status_flags,
void *  user_data 
)
friend

Definition at line 39 of file online-audio-source.cc.

Referenced by OnlinePaSource::OnlinePaSource().

43  {
44  OnlinePaSource *pa_src = reinterpret_cast<OnlinePaSource*>(user_data);
45  return pa_src->Callback(input, output, frame_count, time_info, status_flags);
46 }
OnlinePaSource(const uint32 timeout, const uint32 sample_rate, const uint32 rb_size, const uint32 report_interval)

Member Data Documentation

◆ noverflows_

uint32 noverflows_
private

Definition at line 123 of file online-audio-source.h.

Referenced by OnlinePaSource::Callback(), and OnlinePaSource::Read().

◆ nread_calls_

uint32 nread_calls_
private

Definition at line 122 of file online-audio-source.h.

Referenced by OnlinePaSource::Read().

◆ pa_ringbuf_

PaUtilRingBuffer pa_ringbuf_
private

◆ pa_started_

bool pa_started_
private

Definition at line 120 of file online-audio-source.h.

Referenced by OnlinePaSource::Read(), and OnlinePaSource::~OnlinePaSource().

◆ pa_stream_

PaStream* pa_stream_
private

◆ rb_size_

int32 rb_size_
private

Definition at line 116 of file online-audio-source.h.

Referenced by OnlinePaSource::OnlinePaSource().

◆ report_interval_

uint32 report_interval_
private

Definition at line 121 of file online-audio-source.h.

Referenced by OnlinePaSource::Callback(), and OnlinePaSource::Read().

◆ ring_buffer_

char* ring_buffer_
private

◆ sample_rate_

uint32 sample_rate_
private

Definition at line 115 of file online-audio-source.h.

Referenced by OnlinePaSource::OnlinePaSource().

◆ samples_lost_

uint32 samples_lost_
private

Definition at line 124 of file online-audio-source.h.

Referenced by OnlinePaSource::Callback(), and OnlinePaSource::Read().

◆ timed_out_

bool timed_out_
private

Definition at line 113 of file online-audio-source.h.

Referenced by OnlinePaSource::Read().

◆ timeout_

uint32 timeout_
private

Definition at line 111 of file online-audio-source.h.

Referenced by OnlinePaSource::Read().


The documentation for this class was generated from the following files: