我有以下代码将原始音频文件转换为wav文件:
int MainWindow::rawToWav(const char *rawfn, const char *wavfn, long samplingRate)
{
long chunksize=0x10;
int statusCollect = 0;
struct
{
unsigned short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
} fmt;
errno_t err;
FILE *raw;
err = fopen_s(&raw, rawfn, "rb");
if(!raw)
return -2;
fseek(raw, 0, SEEK_END);
long bytes = ftell(raw);
fseek(raw, 0, SEEK_SET);
long samplecount = bytes/2;
long riffsize = samplecount*2+0x24;
long datasize = samplecount*2;
FILE *wav;
err = fopen_s(&wav, wavfn, "wb");
if(!wav)
{
fclose(raw);
return -3;
}
fwrite( "RIFF", 1, 4, wav );
fwrite( &riffsize, 4, 1, wav );
fwrite( "WAVEfmt ", 1, 8, wav );
fwrite( &chunksize, 4, 1, wav );
int bitsPerSample = 16;
int bytesPerSample = bitsPerSample/8;
int channel = 2;
fmt.wFormatTag = 1; // PCM
fmt.wChannels = channel; // MONO
fmt.dwSamplesPerSec = samplingRate;
fmt.dwAvgBytesPerSec = samplingRate*channel*bytesPerSample; // 16 bit
fmt.wBlockAlign = channel*bytesPerSample;
fmt.wBitsPerSample = bitsPerSample;
fwrite( &fmt, sizeof(fmt), 1, wav );
fwrite( "data", 1, 4, wav );
fwrite( &datasize, 4, 1, wav );
short buff[1024];
while( !feof(raw) )
{
int cnt=fread(buff,2,1024,raw);
if( cnt == 0 )
break;
fwrite(buff,2,cnt,wav);
}
statusCollect = fclose( raw );
statusCollect += fclose( wav );
return statusCollect;
}
我希望完全相同的操作不在FILE类型数据上,而是在QByteArray类型上。
所以我尝试以下方式。首先,我尝试在QByteArray对象上添加wav标头。然后我在程序上上传原始音频文件。然后我将原始数据添加到我已经添加了wav头的QByteArray对象。然后我将QByteArray保存为wav文件。
void MainWindow::rawToWavQT(QByteArray* arr, long samplingRate, long sizeOfRawFile)
{
long chunksize=0x10;
struct
{
unsigned short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
} fmt;
long samplecount = sizeOfRawFile/2;
long riffsize = samplecount*2+0x24;
long datasize = samplecount*2;
arr->append("RIFF");// fwrite( "RIFF", 1, 4, wav );
arr->append((const char*)&riffsize);// fwrite( &riffsize, 4, 1, wav );
arr->append("WAVEfmt ");// fwrite( "WAVEfmt ", 1, 8, wav );
arr->append((const char*)&chunksize);// fwrite( &chunksize, 4, 1, wav );
int bitsPerSample = 16;
int bytesPerSample = bitsPerSample/8;
int channel = 2;
fmt.wFormatTag = 1; // PCM
fmt.wChannels = channel; // MONO
fmt.dwSamplesPerSec = samplingRate;
fmt.dwAvgBytesPerSec = samplingRate*channel*bytesPerSample; // 16 bit
fmt.wBlockAlign = channel*bytesPerSample;
fmt.wBitsPerSample = bitsPerSample;
arr->append((const char*)&fmt);// fwrite( &fmt, sizeof(fmt), 1, wav );
arr->append("data");// fwrite( "data", 1, 4, wav );
arr->append((const char*)&datasize);// fwrite( &datasize, 4, 1, wav );
}
但是这种向QByteArray添加标头然后添加原始数据的方式并没有产生正确类型的wav文件,因为它无法播放。从第二个函数来看,我认为我做错了什么?我在第二个函数中执行与第一个函数相同的操作但是在QByteArray对象上而不是在FILE对象上?
感谢。
答案 0 :(得分:2)
您的数据连接函数只停留在第一个空字节,丢失重要数据并中断对齐。相反,您希望以与fwrite
类似的方式指定要追加的长度。
解决方案:将您的append()
语句从单个const char *
参数替换为使用您的数据和数据长度构建的QByteArray
。
更改
arr->append((const char*)&riffsize);
到
arr->append(QByteArray((const char*)&riffsize, 4));
,类似于其他原始数据append
语句。