0

I need to convert a int16_t* in to an float **

I need them to pass through a function which requires and input buffer like:

const float * const * const buf,

Allocation of float**

  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));

  }

Rest of the code:

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]);
    }
  }

The problem is that when I put through the function which requires it, that function results in Seg.Fault. It's proprietary library.

/**
 * @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;
}
Gent Binaku
  • 55
  • 11

2 Answers2

1
  1. Your malloced blocks for micsbuf[c1] and farbuf[c1] are only half the size they need to be. You need room for BUFFER_LENGTH left/right pairs of samples. Here is a corrected version:

      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. The code for filling in and accessing the left/right pairs of samples is incorrect. It overwrites earlier samples and leaves some samples not filled in. The indices for the left channel should be changed from i to i*2 so that they are just before the indices for the right channel 1+i*2. Here is a corrected version:

        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]);
        }
    

    You should also consider replacing the magic number 480 in the loop with BUFFER_LENGTH if that is correct, or some other symbolic constant.

Ian Abbott
  • 15,083
  • 19
  • 33
0

The problem is with the line micsbuf[c2][1+i*2] = (float )bufferInCasted[1+i*2];. You allocate the sub array at micsbuf, a size CHANNELS*sizeof(float *) bytes, which is CHANNELS*4. And your i vary from 1 to 480. The CHANNELS variable should be high enough otherwise it will show the error, as you are trying to access the memory not allocated to that variable.

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
  • That's because I have two channels of audio signal, so it float[2][480] actually. Each time I read 480*2*int16_t which is 1920 bytes – Gent Binaku Jun 19 '20 at 12:07