【发布时间】: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)