OnlineSpeexEncoder Class Reference

#include <online-speex-wrapper.h>

Collaboration diagram for OnlineSpeexEncoder:

Public Member Functions

 OnlineSpeexEncoder (const SpeexOptions &config)
 
 ~OnlineSpeexEncoder ()
 
void AcceptWaveform (int32 sample_rate, const VectorBase< BaseFloat > &waveform)
 
void InputFinished ()
 
void GetSpeexBits (std::vector< char > *spx_bits)
 

Private Member Functions

void Encode (const VectorBase< BaseFloat > &wave, std::vector< char > *speex_encoder_bits)
 

Private Attributes

int32 speex_frame_size_
 
int32 speex_encoded_frame_size_
 
SPEEXBITS speex_bits_
 
Vector< BaseFloatwaveform_remainder_
 
std::vector< char > speex_encoded_char_bits_
 
BaseFloat sample_rate_
 
bool input_finished_
 

Detailed Description

Definition at line 74 of file online-speex-wrapper.h.

Constructor & Destructor Documentation

◆ OnlineSpeexEncoder()

OnlineSpeexEncoder ( const SpeexOptions config)

Definition at line 25 of file online-speex-wrapper.cc.

References KALDI_ERR, SpeexOptions::sample_rate, OnlineSpeexEncoder::sample_rate_, OnlineSpeexEncoder::speex_bits_, SpeexOptions::speex_bits_frame_size, OnlineSpeexEncoder::speex_encoded_frame_size_, OnlineSpeexEncoder::speex_frame_size_, SpeexOptions::speex_quality, and SpeexOptions::speex_wave_frame_size.

25  :
26  input_finished_(false) {
27  speex_frame_size_ = config.speex_bits_frame_size;
28  speex_encoded_frame_size_ = config.speex_wave_frame_size;
29  sample_rate_ = config.sample_rate;
30 
31  if(sizeof(speex_bits_) == 1) {
32  KALDI_ERR << "OnlineSpeexEncoder called but Speex not installed."
33  << "You should run tools/extras/install_speex.sh first, then "
34  << "re-run configure in src/ and then make Kaldi again.\n";
35  }
36 
37 #ifdef HAVE_SPEEX
38  speex_state_ = speex_encoder_init(&speex_wb_mode); // init speex with wideband mode
39  int32 tmp = config.speex_quality;
40  speex_encoder_ctl(speex_state_, SPEEX_SET_QUALITY, &tmp);
41  tmp = (int)(sample_rate_);
42  speex_encoder_ctl(speex_state_, SPEEX_SET_SAMPLING_RATE, &tmp);
43  speex_bits_init(&speex_bits_);
44 #endif
45 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ ~OnlineSpeexEncoder()

Definition at line 47 of file online-speex-wrapper.cc.

References OnlineSpeexEncoder::speex_bits_.

47  {
48 #ifdef HAVE_SPEEX
49  speex_bits_destroy(&speex_bits_);
50  speex_encoder_destroy(speex_state_);
51 #endif
52 }

Member Function Documentation

◆ AcceptWaveform()

void AcceptWaveform ( int32  sample_rate,
const VectorBase< BaseFloat > &  waveform 
)

Definition at line 54 of file online-speex-wrapper.cc.

References VectorBase< Real >::Dim(), OnlineSpeexEncoder::Encode(), OnlineSpeexEncoder::input_finished_, KALDI_ERR, VectorBase< Real >::Range(), Vector< Real >::Resize(), OnlineSpeexEncoder::sample_rate_, OnlineSpeexEncoder::speex_encoded_char_bits_, and OnlineSpeexEncoder::waveform_remainder_.

Referenced by main().

55  {
56  if (waveform.Dim() == 0) {
57  return; // Nothing to do.
58  }
59  if (input_finished_) {
60  KALDI_ERR << "AcceptWaveform called after InputFinished() was called.";
61  }
62  if (sample_rate != sample_rate_) {
63  KALDI_ERR << "Sampling frequency mismatch, expected "
64  << sample_rate_ << ", got " << sample_rate;
65  }
66 
67  Vector<BaseFloat> appended_wave;
68  const VectorBase<BaseFloat> &wave_to_use = (waveform_remainder_.Dim() != 0 ?
69  appended_wave : waveform);
70  if (waveform_remainder_.Dim() != 0) {
71  appended_wave.Resize(waveform_remainder_.Dim() +
72  waveform.Dim());
73  appended_wave.Range(0, waveform_remainder_.Dim()).CopyFromVec(
75  appended_wave.Range(waveform_remainder_.Dim(),
76  waveform.Dim()).CopyFromVec(waveform);
77  }
78  waveform_remainder_.Resize(0);
79 
80  std::vector<char> spx_bits;
81  Encode(wave_to_use, &spx_bits);
82 
83  if (spx_bits.size() > 0) {
85  spx_bits.begin(), spx_bits.end());
86  }
87 }
std::vector< char > speex_encoded_char_bits_
Vector< BaseFloat > waveform_remainder_
#define KALDI_ERR
Definition: kaldi-error.h:147
void Encode(const VectorBase< BaseFloat > &wave, std::vector< char > *speex_encoder_bits)

◆ Encode()

void Encode ( const VectorBase< BaseFloat > &  wave,
std::vector< char > *  speex_encoder_bits 
)
private

Definition at line 106 of file online-speex-wrapper.cc.

References VectorBase< Real >::Data(), VectorBase< Real >::Dim(), rnnlm::i, KALDI_ERR, kaldi::kCopyData, OnlineSpeexEncoder::speex_bits_, OnlineSpeexEncoder::speex_encoded_frame_size_, OnlineSpeexEncoder::speex_frame_size_, and OnlineSpeexEncoder::waveform_remainder_.

Referenced by OnlineSpeexEncoder::AcceptWaveform(), and OnlineSpeexEncoder::InputFinished().

107  {
108  if (wave.Dim() == 0) {
109  return;
110  }
111 
112  int32 to_encode = wave.Dim();
113  int32 has_encode = 0;
114  char cbits[200];
115  std::vector<char> encoded_bits;
116  while (to_encode > speex_encoded_frame_size_) {
117  SubVector<BaseFloat> wave_frame(wave, has_encode,
119  int32 nbytes = 0;
120 #ifdef HAVE_SPEEX
121  speex_bits_reset(&speex_bits_);
122  speex_encode(speex_state_, wave_frame.Data(), &speex_bits_);
123  nbytes = speex_bits_nbytes(&speex_bits_);
124  if (nbytes != speex_frame_size_) {
125  KALDI_ERR << "The number of bytes of Speex encoded frame mismatch,"
126  << "expected " << speex_frame_size_ << ", got " << nbytes;
127  }
128  nbytes = speex_bits_write(&speex_bits_, cbits, 200);
129 #endif
130 
131  int32 encoded_bits_len = encoded_bits.size();
132  encoded_bits.resize(encoded_bits_len + nbytes);
133  for (int32 i = 0; i < nbytes; i++) {
134  encoded_bits[i+encoded_bits_len] = cbits[i];
135  }
136 
137  has_encode += speex_encoded_frame_size_;
138  to_encode -= speex_encoded_frame_size_;
139  }
140 
141  if (to_encode > 0) {
142  SubVector<BaseFloat> wave_left(wave, has_encode, to_encode);
143  int32 dim = waveform_remainder_.Dim();
144  if (dim != 0) {
145  waveform_remainder_.Resize(dim + to_encode, kCopyData);
146  waveform_remainder_.Range(dim, to_encode).CopyFromVec(wave_left);
147  } else {
148  waveform_remainder_ = wave_left;
149  }
150  }
151 
152  *speex_encoder_bits = encoded_bits;
153 }
Vector< BaseFloat > waveform_remainder_
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ GetSpeexBits()

void GetSpeexBits ( std::vector< char > *  spx_bits)
inline

Definition at line 84 of file online-speex-wrapper.h.

Referenced by main().

84  { // call it after AcceptWaveform
85  *spx_bits = speex_encoded_char_bits_;
87  }
std::vector< char > speex_encoded_char_bits_

◆ InputFinished()

void InputFinished ( )

Definition at line 90 of file online-speex-wrapper.cc.

References OnlineSpeexEncoder::Encode(), OnlineSpeexEncoder::input_finished_, KALDI_ASSERT, VectorBase< Real >::Range(), OnlineSpeexEncoder::speex_encoded_char_bits_, OnlineSpeexEncoder::speex_encoded_frame_size_, and OnlineSpeexEncoder::waveform_remainder_.

Referenced by main().

90  {
91  input_finished_ = true;
92 
93  int32 dim = waveform_remainder_.Dim();
94  if (dim != 0) {
96  Vector<BaseFloat> wave_last(speex_encoded_frame_size_);
97  std::vector<char> spx_bits;
98  wave_last.Range(0, dim).CopyFromVec(waveform_remainder_);
99  Encode(wave_last, &spx_bits);
100 
102  spx_bits.begin(), spx_bits.end());
103  }
104 }
std::vector< char > speex_encoded_char_bits_
Vector< BaseFloat > waveform_remainder_
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Encode(const VectorBase< BaseFloat > &wave, std::vector< char > *speex_encoder_bits)

Member Data Documentation

◆ input_finished_

bool input_finished_
private

◆ sample_rate_

BaseFloat sample_rate_
private

◆ speex_bits_

◆ speex_encoded_char_bits_

std::vector<char> speex_encoded_char_bits_
private

◆ speex_encoded_frame_size_

int32 speex_encoded_frame_size_
private

◆ speex_frame_size_

◆ waveform_remainder_


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