how to change array to vector

97 views
Skip to first unread message

Yu Beomgon

unread,
Aug 19, 2019, 3:42:54 AM8/19/19
to kaldi-help

Hi,
I try to test with online2-tcp-nnet3-decode-faster.cc.
I modified the code for my test because I use web socket, not tcp socket.

web socket output and kaldi input format is different.
web socket output is int16 array, but kaldi input is float type user defined vector.
How can I change arrary to vector?

I try to for long times but compile error happened.

here is my sample code.

for(int i=0; i<CHANNELS*userinfo->asr_session_data->frame_size; i++)
{
pcm_bytes[2*i]=(BaseFloat)(out[i]&0xFF);
pcm_bytes[2*i+1]=(BaseFloat)((out[i]>>8)&0xFF);  
}

Vector<BaseFloat> wave_data;

1. 
wave_data.CopyFromPtr( pcm_bytes,  sizeof(pcm_bytes[0]));



2.  for (int i=0; i<CHANNELS*userinfo->asr_session_data->frame_size; i++)
{
//wave_data[i] = pcm_bytes[i];:wq
std::memcpy(pcm_bytes,wave_data,sizeof(pcm_bytes[0])*CHANNELS*userinfo->asr_session_data->frame_size)
}



3.                     wave_data = vector(std::begin(pcm_bytes), std::end(pcm_bytes));

please someone help me.
thanks and regard/

Dazhu Qiu

unread,
Aug 19, 2019, 4:10:09 AM8/19/19
to kaldi-help
I really didn't get your goal. kaldi::Vector is a type in kaldi, std::vector is a type in STL, which one do you want to copy the data to? (as you declared wave_data with Vector but then assign vector to it...)

If you wanna copy to kaldi::Vector, just use
SubVector<BaseFloat> sv(data, size); // data is a raw pointer.
Vector<BaseFloat> v(sv); // copy data here

if you wanna copy to std::vector, just use
vector<BaseFloat> v(data, size); // see http://www.cplusplus.com/reference/vector/vector/vector/




在 2019年8月19日星期一 UTC+8下午3:42:54,Yu Beomgon写道:

Yu Beomgon

unread,
Aug 19, 2019, 4:34:35 AM8/19/19
to kaldi-help

thank you for your help.
third is wrong, its just test.

by using below code, no error happened now.
SubVector<BaseFloat> sv(data, size); // data is a raw pointer.
Vector<BaseFloat> v(sv); // copy data here

thanks alot.


2019년 8월 19일 월요일 오후 5시 10분 9초 UTC+9, Dazhu Qiu 님의 말:

Dazhu Qiu

unread,
Aug 19, 2019, 4:43:43 AM8/19/19
to kaldi-help
That's great. Noted that CopyFromPtr requires you to allocate memory (with size of sz) to data_ before calling it, that's why you encounter error when you use the 1st version.
template<typename Real>
void VectorBase<Real>::CopyFromPtr(const Real *data, MatrixIndexT sz) {
  KALDI_ASSERT(dim_ == sz);
  std::memcpy(this->data_, data, Dim() * sizeof(Real));
}
 


在 2019年8月19日星期一 UTC+8下午4:34:35,Yu Beomgon写道:
Reply all
Reply to author
Forward
0 new messages