double buf[1000];
double value;
double *ptr = &value;
for(i=0; i < no_of_iterations; i++) {
for(j=0; j < chunkSize; j++) {
num_bytes_read = fread(ptr,1,pcm_sample_size,fptr); //read one sample
if(num_bytes_read == 2) {
sum_sq += (*ptr) * (*ptr); //calculate power of each sample
buf[j] = *ptr;
}
else {
flag = 0;
break;
}
}
if(!flag) {
printf("exiting loop");
break;
}
power = sum_sq/chunkSize;//calculate rms value of signals for chunkSize samples
if(power < threshold) //compare with some value
printf("power is lower than threshold"); //silence-don't write
else { //write
ret = fwrite(buf,1,pcm_sample_size,optr);
if(ret != 1)
printf("error in fwrite %d", ret);
}
}
printf("done");
fclose(fptr);
fclose(optr);
以上是我根据某些条件将一些 pcm 样本写入文件的代码,但我收到 fread 错误。控件不进入 if(num_bytes_read==2) 块。我认为错误是因为我想读取大小为 2 个字节的 pcm 样本,我需要在某个地方存储它。我可以使用什么数据类型来存储 2 字节 pcm 值(pcm 值不是 int 值)。请指教。