【问题标题】:Filling up float ** from int16_t *从 int16_t * 填充 float **
【发布时间】:2020-06-19 11:42:36
【问题描述】:

我需要将 int16_t* 转换为浮点数 **

我需要它们通过一个需要输入缓冲区的函数,例如:

const float * const * const buf,

浮动分配**

  micsbuf = (float **)malloc(CHANNELS * sizeof(float *));
  farbuf = (float **)malloc(CHANNELS*sizeof(float *));
  for(int c1 = 0;c1 < CHANNELS;c1++)
  {
    micsbuf[c1] = (float *)malloc(BUFFER_LENGTH*sizeof(float));
    farbuf[c1] = (float *)malloc(BUFFER_LENGTH*sizeof(float));

  }

其余代码:

while(!feof(infile) || !feof(outfile))
  {
    NreadNear = fillBufferIn(infile, &bufferIn, bytes);
    NreadFar = fillBufferOut(outfile,&bufferOut,bytes);

    int16_t *bufferInCasted = (int16_t *)bufferIn;
    int16_t *bufferOutCasted = (int16_t *)bufferOut;

    for(int i = 0 ; i < 480 ; i++)
    {
      for(int c2 = 0 ; c2 < CHANNELS ; c2 ++)
      {
        micsbuf[c2][i] = (float )bufferInCasted[i];
        micsbuf[c2][1+i*2] = (float )bufferInCasted[1+i*2];
        farbuf[c2][i] = (float )bufferOutCasted[i];
        farbuf[c2][1+i*2] = (float )bufferOutCasted[1+i*2];
      }
      printf("I:  %d\t", i);
      printf("Left Sample Near %d\t", bufferInCasted[i]);
      printf("Right Sample Near %d\t\t", bufferInCasted[1+i*2]);
      printf("Left Sample Far %d\t", bufferOutCasted[i]);
      printf("Right Sample Far %d\n\n", bufferOutCasted[1+i*2]);
    }
  }

问题是,当我执行需要它的函数时,该函数会导致 Seg.Fault。它是专有库。

/**
 * @brief 
 * Should fill up the buffer;
 * @param mics 
 * @param bufferIn 
 * @return size_t 
 */ 
size_t fillBufferIn(FILE *in, void **buffer, size_t bytes) //1920 byte
{ 
  size_t Nread;
  /**
   * @brief 
   * Should return a buffer with float[][] so that it can read and write
   * Each time it read sizeof of two int16_t which are the left channel and right channel
   * 
   */
  *buffer = (void *)calloc(480,2*sizeof(int16_t));
  Nread = fread(*buffer, 2*sizeof(int16_t), 480, in);
  return 2*sizeof(int16_t)*Nread;
}

/**
 * @brief 
 * Should fill up the buffer
 * @param out 
 * @param bufferOut 
 * @param bytes 
 * @return size_t 
 */
size_t fillBufferOut(FILE *out, void **bufferOut, size_t bytes)
{
  *bufferOut = (void *)calloc(480,2*sizeof(int16_t));
  Nread = fread(*bufferOut, 2*sizeof(int16_t), 480, out);
  return 2*sizeof(int16_t)*Nread;
}

【问题讨论】:

  • 有什么问题?
  • 当我通过一个使用它的函数时,它会导致 seg.fault
  • 请确保您的问题中有正确的问题陈述。 micsbuf[c2][1+i*2] 看起来不对。
  • 你确定你不会在函数bufferInCasted[1+i*2];中使用bufferInCasted[1+i*2];(void *)calloc(480,2*sizeof(int16_t));它的平均480个元素的数组大小为2 * sizeof(int16_t)

标签: c malloc


【解决方案1】:
  1. micsbuf[c1]farbuf[c1]malloced 块只有所需大小的一半。您需要为BUFFER_LENGTH 左/右样本对提供空间。这是一个更正的版本:

      for(int c1 = 0;c1 < CHANNELS;c1++)
      {
         micsbuf[c1] = (float *)malloc(BUFFER_LENGTH*2*sizeof(float));
         farbuf[c1] = (float *)malloc(BUFFER_LENGTH*2*sizeof(float));
    
      }
    
  2. 填写和访问左右样本对的代码不正确。它会覆盖较早的样本并留下一些未填充的样本。左声道的索引应从i 更改为i*2,以便它们位于右声道1+i*2 的索引之前。这是一个更正的版本:

        for(int i = 0 ; i < 480 ; i++)
        {
          for(int c2 = 0 ; c2 < CHANNELS ; c2 ++)
          {
            micsbuf[c2][i*2] = (float )bufferInCasted[i*2];
            micsbuf[c2][1+i*2] = (float )bufferInCasted[1+i*2];
            farbuf[c2][i*2] = (float )bufferOutCasted[i*2];
            farbuf[c2][1+i*2] = (float )bufferOutCasted[1+i*2];
          }
          printf("I:  %d\t", i);
          printf("Left Sample Near %d\t", bufferInCasted[i*2]);
          printf("Right Sample Near %d\t\t", bufferInCasted[1+i*2]);
          printf("Left Sample Far %d\t", bufferOutCasted[i*2]);
          printf("Right Sample Far %d\n\n", bufferOutCasted[1+i*2]);
        }
    

    如果正确,您还应该考虑将循环中的幻数 480 替换为 BUFFER_LENGTH 或其他一些符号常量。

【讨论】:

  • 还是一样的结果Seg.Fault
  • @GentBinaku 也许您还需要将bufferInbufferOut 的大小加倍?
  • 我正在一个平台上工作,该平台有一个来自音频源的 1920 字节缓冲区,每 4 个字节代表一个 int16_t*,这意味着我在这些字节中有 480 个左声道和 480 个右声道。那么 bufferIn 和 bufferOut 是指向那些内存地址的空指针
  • @GentBinaku 每4个字节代表一个int16_t*?你不是说它代表2个int16_ts吗?
  • 没错,就是两个频道
【解决方案2】:

问题在于micsbuf[c2][1+i*2] = (float )bufferInCasted[1+i*2]; 行。您在micsbuf 分配子数组,大小为CHANNELS*sizeof(float *) 字节,即CHANNELS*4。而你的 i 从 1 到 480 不等。CHANNELS 变量应该足够高,否则会显示错误,因为你正试图访问未分配给该变量的内存。

【讨论】:

  • 那是因为我有两个音频信号通道,所以它实际上是浮动[2][480]。每次我读取 480*2*int16_t 即 1920 字节
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 2011-09-10
  • 2020-03-02
  • 2013-01-27
  • 2014-09-17
  • 2013-11-03
相关资源
最近更新 更多