基於QT錄製PCM音訊例項詳細

༄yi笑奈何發表於2020-11-03
#include "QtAudio_input.h"
#include <QtWidgets/QApplication>
#include<QAudioInput>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
	QAudioFormat fmt;
	fmt.setSampleRate(44100);//樣本率
	fmt.setSampleSize(16);//樣本大小
	fmt.setChannelCount(2);//通道大小
	fmt.setCodec("audio/pcm");
	fmt.setByteOrder(QAudioFormat::LittleEndian);//小端模式
	fmt.setSampleType(QAudioFormat::UnSignedInt);

	QAudioInput *input = new QAudioInput(fmt);
	QIODevice *io=input->start();
	FILE *fp = fopen("out.pcm", "wb");
	char *buf = new char[1024];
	int total = 0;
	for (;;)
	{
		int br = input->bytesReady();
		if (br < 1024)continue;
		int len = io->read(buf, 1024);
		fwrite(buf, 1, len, fp);
		cout << len << "|";
		total += len;
		if (total > 10241 * 1024)
			break;
	}
	fclose(fp);

    return 0;
}

 

相關文章