OnlineSpeexDecoder Class Reference

#include <online-speex-wrapper.h>

Collaboration diagram for OnlineSpeexDecoder:

Public Member Functions

 OnlineSpeexDecoder (const SpeexOptions &config)
 
 ~OnlineSpeexDecoder ()
 
void AcceptSpeexBits (const std::vector< char > &spx_enc_bits)
 
void GetWaveform (Vector< BaseFloat > *waveform)
 

Private Member Functions

void Decode (const std::vector< char > &speex_char_bits, Vector< BaseFloat > *decoded_wav)
 

Private Attributes

int32 speex_frame_size_
 
int32 speex_decoded_frame_size_
 
SPEEXBITS speex_bits_
 
Vector< BaseFloatwaveform_
 
std::vector< char > speex_bits_remainder_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ OnlineSpeexDecoder()

OnlineSpeexDecoder ( const SpeexOptions config)

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

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

156  {
157  speex_frame_size_ = config.speex_bits_frame_size;
158  speex_decoded_frame_size_ = config.speex_wave_frame_size;
159 
160  if(sizeof(speex_bits_) == 1) {
161  KALDI_ERR << "OnlineSpeexEncoder called but Speex not installed."
162  << "You should run tools/extras/install_speex.sh first, then "
163  << "re-run configure in src/ and then make Kaldi again.\n";
164  }
165 
166 #ifdef HAVE_SPEEX
167  speex_state_ = speex_decoder_init(&speex_wb_mode); // init speex with wideband mode
168  int32 tmp = config.speex_quality;
169  speex_decoder_ctl(speex_state_, SPEEX_SET_QUALITY, &tmp);
170  tmp = (int)config.sample_rate;
171  speex_decoder_ctl(speex_state_, SPEEX_SET_SAMPLING_RATE, &tmp);
172  speex_bits_init(&speex_bits_);
173 #endif
174 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ ~OnlineSpeexDecoder()

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

References OnlineSpeexEncoder::speex_bits_.

176  {
177 #ifdef HAVE_SPEEX
178  speex_decoder_destroy(speex_state_);
179  speex_bits_destroy(&speex_bits_);
180 #endif
181 }

Member Function Documentation

◆ AcceptSpeexBits()

void AcceptSpeexBits ( const std::vector< char > &  spx_enc_bits)

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

References VectorBase< Real >::Dim(), and kaldi::kCopyData.

Referenced by main().

183  {
184  if (spx_enc_bits.size() == 0) {
185  return; // Nothing to do
186  }
187 
188  std::vector<char> appended_bits;
189  const std::vector<char> &bits_to_use = (speex_bits_remainder_.size() != 0 ?
190  appended_bits : spx_enc_bits);
191  if (speex_bits_remainder_.size() != 0) {
192  appended_bits.insert(appended_bits.end(), speex_bits_remainder_.begin(),
193  speex_bits_remainder_.end());
194  appended_bits.insert(appended_bits.end(), spx_enc_bits.begin(),
195  spx_enc_bits.end());
196  }
197  speex_bits_remainder_.clear();
198 
199  Vector<BaseFloat> waveform;
200  Decode(bits_to_use, &waveform);
201  if (waveform.Dim() == 0) {
202  // Got nothing, maybe the decode has failed
203  return;
204  }
205  int32 last_wav_size = waveform_.Dim();
206  waveform_.Resize(last_wav_size + waveform.Dim(), kCopyData);
207  waveform_.Range(last_wav_size, waveform.Dim()).CopyFromVec(waveform);
208 }
kaldi::int32 int32
std::vector< char > speex_bits_remainder_
void Decode(const std::vector< char > &speex_char_bits, Vector< BaseFloat > *decoded_wav)

◆ Decode()

void Decode ( const std::vector< char > &  speex_char_bits,
Vector< BaseFloat > *  decoded_wav 
)
private

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

References VectorBase< Real >::Dim(), rnnlm::i, kaldi::kCopyData, Vector< Real >::Resize(), OnlineSpeexEncoder::speex_bits_, and OnlineSpeexEncoder::speex_frame_size_.

211  {
212  if (speex_char_bits.size() < speex_frame_size_) {
213  return; // Nothing to do, should never reach this
214  }
215  decoded_wav->Resize(0);
216 
217  char *cbits = new char[speex_frame_size_ + 10]();
219  int32 to_decode = speex_char_bits.size();
220  int32 has_decode = 0;
221 
222  while(to_decode > speex_frame_size_){
223  memcpy(cbits, &speex_char_bits[has_decode], speex_frame_size_);
224 #ifdef HAVE_SPEEX
225  speex_bits_read_from(&speex_bits_, cbits, speex_frame_size_);
226  speex_decode(speex_state_, &speex_bits_, wav);
227 #endif
228 
229  int32 dim = decoded_wav->Dim(); // expanding decoded_wav each frame
230  decoded_wav->Resize(dim + speex_decoded_frame_size_, kCopyData);
231  // Cannot use CopyFromPtr at this moment
232  // decoded_wav->Range(dim, speex_decoded_frame_size_).
233  // CopyFromPtr(wav, speex_decoded_frame_size_);
234  for (int32 i = 0; i < speex_decoded_frame_size_; i++) {
235  (*decoded_wav)(i+dim) = wav[i];
236  }
237 
238  has_decode += speex_frame_size_;
239  to_decode -= speex_frame_size_;
240  }
241 
242  if (to_decode > 0) {
244  speex_char_bits.begin() + has_decode, speex_char_bits.end());
245  }
246 
247  delete []cbits;
248  delete []wav;
249 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
std::vector< char > speex_bits_remainder_

◆ GetWaveform()

void GetWaveform ( Vector< BaseFloat > *  waveform)
inline

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

References Vector< Real >::Resize().

Referenced by main().

117  { // call it after AcceptSpeexBits
118  *waveform = waveform_;
119  waveform_.Resize(0);
120  }

Member Data Documentation

◆ speex_bits_

SPEEXBITS speex_bits_
private

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

◆ speex_bits_remainder_

std::vector<char> speex_bits_remainder_
private

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

◆ speex_decoded_frame_size_

int32 speex_decoded_frame_size_
private

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

◆ speex_frame_size_

int32 speex_frame_size_
private

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

◆ waveform_

Vector<BaseFloat> waveform_
private

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


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