我正在尝试通过用0重写备用字节对(2个字节)来从扬声器的一侧播放声音。我在HEX编辑器中检出,并且备用字节对确实有重复。
我制作了一个小程序来做同样的事情。但是,它不起作用。你能指出我在这里做错了什么吗?
int main()
{
FILE *file, *fileWrite;
unsigned char *buffer;
unsigned long fileLen;
int n;
file = fopen("sound.dat", "rb");
fileWrite = fopen("sound1.dat", "w+");
// Get the File Length
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
buffer = (unsigned char *)malloc(fileLen+1);
n = fread(buffer, fileLen, 1, file);
printf("%s\n", &buffer);
int i;
for(i = 0 ;i < fileLen; i = i + 4)
{
buffer[i+2] = '\0';
buffer[i+3] = '\0';
// printf("%.2X %.2X %.2X %.2X \n", (int)buffer[i],(int)buffer[i+1], (int)buffer[i+2],(int)buffer[i+3]);
}
n = fwrite(buffer, fileLen, 1, fileWrite);
fclose(file);
fclose(fileWrite);
}
更新:如果十六进制读取
4F D0 4F D0 FF 05 FF 05
我想成功
4F D0 00 00 FF 05 00 00
已解决!!!
任何人都可以帮我解决这个问题:Audio File: Playing data through one Speaker Only?
答案 0 :(得分:1)
您的循环限制已关闭:
for(i = 0 ;i < fileLen; i = i + 4)
应该是
for(i = 0 ;i < fileLen - 3; i = i + 4)
否则,分配给buffer[i + 3]
将覆盖缓冲区末尾的内存中的内容。这样可以解决问题吗?