Luc*_* P. 5 c audio volume wav noise
我正在编写一个用于编辑 Wav 音频文件的 C 程序。我已将所有文件数据加载到一个无符号整数值数组 (UINT16_T) 中。
现在,我想减少文件的体积。我认为减少单个值的值(一定百分比)就足够了。但是如果我这样做,我会得到一个带有噪音的音频文件(我想我明白被称为“静态”或“点击噪音”)
为什么?哪个是正确的程序?
谢谢你!
这是受影响的代码段:
FILE* fp;
FILE* fp2;
/*Size of my file*/
#define BUFFER_SIZE 28242852
/*Array with file data*/
unsigned char *buffer;
/*Array used for converting two bytes in an unsigned int*/
unsigned char uintBytes[2];
/*The unsigned int obtained*/
uint16_t * conv;
/*The new value calculated*/
uint16_t nuovoValore;
/*Array used for the reverse conversion, form UINT to bytes*/
unsigned char* nuovoValArray;
for(i=44; i<BUFFER_SIZE;i++){
if(i%2==0){
/*I read 2 bytes form the array and "convert" it in an unsigned int*/
uintBytes[0]=buffer[i];
uintBytes[1]=buffer[i+1];
conv=(uint16_t *) &uintBytes[0];
/*Calculate the new value (-30%) to write in the new file*/
nuovoValore= *conv - ((float)*conv*30/100);
if(nuovoValore<0) nuovoValore=0;
nuovoValArray=malloc(2);
memset(nuovoValArray,'\0',2);
nuovoValArray=(unsigned char*)&nuovoValore;
/*Write the two bytes of the new file*/
fwrite(&nuovoValArray[0], 1, 1, fp2);
fwrite(&nuovoValArray[1], 1, 1, fp2);
}
}
Run Code Online (Sandbox Code Playgroud)
为了简单起见,请在编译程序之前检查音频文件的所有规格。普通.wav文件具有以下属性:
因此,请确保您正在解析的音频文件包含这些属性。一旦您确认这些属性对于您的音频文件是通用的,您就可以开始测试。如果您的文件不包含这些属性,您可能需要考虑使用Audacity或类似的东西来制作测试.wav文件。
你的代码有点奇怪。首先将数据转换为 a char,然后转换为 to int,最后转换为 into float。这会给你带来一些严重的错误。所有这些数据类型的大小都不同。Float也有完全不同的二进制格式。A intof value65可以是 a floatof -34564.23(或类似的东西)。只需使用
int16_t.
我还看到您为代码打开了两个文件 - 不要打扰,因为它会使代码更大。让您的代码尽可能简单,直到它满足您的要求 - 然后添加辅助属性。
另外,你fwrites已经写了fwrite (&nuovoValArray[0], 1, 1, fp2),但应该是这样fwrite (&nuovoValArray[0], 2, 1, fp2),因为 的大小int16_t是 2 个字节而不是 1。
当谈到减少文件的体积时,这里有一个应该有效的通用方法:
samp[i](16位或2字节)samp[i] -= (int16_t) (samp[i] * percent);i这是一段可能有帮助的代码片段:
// open file
// read into char * fileBuffer
int sampleCount = ((fileSize - dataOffset) / sizeof (int16_t));
int16_t * samp = (int16_t *) &fileBuffer[dataOffset];
float percent = 0.6f;
for (int i = 0; i < sampleCount; i++){
samp[i] -= (int16_t) (samp[i] * percent); // Should work +/- values
}
// save file
Run Code Online (Sandbox Code Playgroud)
我之前编写过一个应用程序,用于绘制.wav文件图形以进行波形分析。为了了解文件格式,我必须阅读此页面 - 它也应该对您有所帮助。