open(QIODevice::WriteOn-6ren">
gpt4 book ai didi

c++ - QtMultimedia,我如何以任何格式保存录音,如 mp3、wav 或其他格式?

转载 作者:太空狗 更新时间:2023-10-29 21:29:27 26 4
gpt4 key购买 nike

好吧,像我在教程中看到的那样,但我想保存为音乐播放器的一种可读格式。可能吗?

file   = new QFile("smth.raw");
file->open(QIODevice::WriteOnly | QIODevice::Truncate );

QAudioFormat format;
format.setFrequency(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format)) {
qWarning()<<"default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audioInput = new QAudioInput(format, this);

最佳答案

好吧,如果有人感兴趣,那就是要转换的算法。如果您有更好的想法,请回复!

 #include <stdio.h>
int rawToWav(const char *rawfn, const char *wavfn, long frequency)
{
long chunksize=0x10;

struct
{
unsigned short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
} fmt;

FILE *raw = fopen(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=fopen(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 );

fmt.wFormatTag = 1; // PCM
fmt.wChannels = 1; // MONO
fmt.dwSamplesPerSec = frequency*1;
fmt.dwAvgBytesPerSec = frequency*1*2; // 16 bit
fmt.wBlockAlign = 2;
fmt.wBitsPerSample = 16;

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);
}
fclose( raw );
fclose( wav );
}

关于c++ - QtMultimedia,我如何以任何格式保存录音,如 mp3、wav 或其他格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4811873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com