【问题标题】:How to change int16 array to vector<float>如何将 int16 数组更改为 vector<float>
【发布时间】:2019-08-19 06:04:21
【问题描述】:

我正在设置一个新的 ASR 服务器。音频数据来自客户端,音频数据格式为 int16 数组(PCM 数据),在 ASR 服务引擎之前应改为vector&lt;float&gt;

我尝试了很多次,但出现如下编译错误:

错误:从‘int’到 kaldi::MatrixResizeType’的无效转换 [-fpermissive] 错误:未在此范围内声明“向量” wave_data = 矢量(std::开始(pcm_bytes),std::end(pcm_bytes)); //测试; 错误:在“(”标记之前缺少模板参数 矢量wave_data = Vector(pcm_bytes, pcm_bytes + sizeof(pcm_bytes) / sizeof(pcm_bytes[0])); //测试;

请告诉我如何将数组更改为向量(int16 为浮点数)而不会出现编译错误。

我的源文件:

BaseFloat pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2];
opus_int16 out[MAX_FRAME_SIZE*CHANNELS]; //opus_int16

/* Convert to little-endian ordering. */
for(int i=0; i<CHANNELS*userinfo->asr_session_data->frame_size; i++)
{
  //pcm_byte
  pcm_bytes[2*i]=(BaseFloat)(out[i]&0xFF);
  pcm_bytes[2*i+1]=(BaseFloat)((out[i]>>8)&0xFF);
}
//vector init
Vector<BaseFloat> wave_data(CHANNELS*userinfo->asr_session_data->frame_size,0);
*wave_data = vector(std::begin(pcm_bytes), std::end(pcm_bytes));* //testing;  -> **error happend this line**    

我的矢量头文件:

template<typename Real>
class Vector: public VectorBase<Real> {
 public:
  /// Constructor that takes no arguments.  Initializes to empty.
  Vector(): VectorBase<Real>() {}

  /// Constructor with specific size.  Sets to all-zero by default
  /// if set_zero == false, memory contents are undefined.
  explicit Vector(const MatrixIndexT s,
                  MatrixResizeType resize_type = kSetZero)
      : VectorBase<Real>() {  Resize(s, resize_type);  }

  /// Copy constructor from CUDA vector
  /// This is defined in ../cudamatrix/cu-vector.h
  template<typename OtherReal>
  explicit Vector(const CuVectorBase<OtherReal> &cu);

  /// Copy constructor.  The need for this is controversial.
  Vector(const Vector<Real> &v) : VectorBase<Real>()  { //  (cannot be explicit)
    Resize(v.Dim(), kUndefined);
    this->CopyFromVec(v);
  }

  /// Copy-constructor from base-class, needed to copy from SubVector.
  explicit Vector(const VectorBase<Real> &v) : VectorBase<Real>() {
    Resize(v.Dim(), kUndefined);
    this->CopyFromVec(v);
  }

  /// Type conversion constructor.
  template<typename OtherReal>
  explicit Vector(const VectorBase<OtherReal> &v): VectorBase<Real>() {
    Resize(v.Dim(), kUndefined);
    this->CopyFromVec(v);
  }

// Took this out since it is unsafe : Arnab
//  /// Constructor from a pointer and a size; copies the data to a location
//  /// it owns.
//  Vector(const Real* Data, const MatrixIndexT s): VectorBase<Real>() {
//    Resize(s);
  //    CopyFromPtr(Data, s);
//  }


  /// Swaps the contents of *this and *other.  Shallow swap.
  void Swap(Vector<Real> *other);

  /// Destructor.  Deallocates memory.
  ~Vector() { Destroy(); }

};

【问题讨论】:

  • 你有什么理由要实现自己的向量而不是使用std::vector
  • 我有一个开源代码,不是我的代码,而是开源使用他们的代码。
  • 好的...您如何访问Vector 中的数据?我猜它是在VectorBase 中定义的。

标签: c++ arrays vector


【解决方案1】:

排队

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

vector 显然是指std::vector(可能在某处有using namespace std; - 一个坏主意,但经常发生)。 std::vector 是一个标准模板,需要指定元素类型(例如vector&lt;int&gt;(...)vector&lt;float&gt;(...)

在代码中没有指定类型;这就是错误的意思。

注意:C++17 为构造函数添加了模板参数推导,因为错误消息看起来您的编译器还不支持它。

此外,Vector 类(注意大写 V)似乎没有接受std::vector 的构造函数,除非VectorBase 有一个接受std::vector 的非显式构造函数。

【讨论】:

  • @L.F.:添加了一个关于 C++17 编译器应该接受它的注释
猜你喜欢
  • 1970-01-01
  • 2013-03-11
  • 2019-04-23
  • 2014-06-28
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多