在为健全的开发编写代码时,重要的是要注意每个样本的大小、将样本存储为二进制数据的字节顺序,以及是否需要向数据写入标头,或者如果它是原始的,则无标头。
如果您的目标是填充 a QBuffer
,您可以将其写入 trough QDataStream
,如果您愿意,可以将其读回。
在我的回答中,我将假设 Little Endian,而不是浮点数,我将使用 16 位有符号整数样本、1 个通道和 8000Hz 频率。
我提供了一个简单的音调发生器示例,请根据您的需要进行调整!
让我们看看以下控制台示例:
#include <QtCore>
#include <QtMultimedia>
static QBuffer m_float_buffer;
void toneGenerator()
{
QDataStream write_stream(&m_float_buffer);
write_stream.setVersion(QDataStream::Qt_5_0); //Protocol for version 5.0
write_stream.setByteOrder(QDataStream::LittleEndian);
//Tone generator from http://www.cplusplus.com/forum/general/129827/
const unsigned int samplerate = 8000;
const unsigned short channels = 1;
const double pi = M_PI;
const qint16 amplitude = qint16(INT16_MAX * 0.5);
const unsigned short n_frequencies = 8;
const unsigned short n_seconds_each = 1;
float frequencies[n_frequencies] = {55.0, 110.0, 220.0, 440.0, 880.0, 1760.0, 3520.0, 7040.0};
const int n_samples = channels * samplerate * n_frequencies * n_seconds_each;
int index = n_samples / n_frequencies;
for (unsigned short i = 0; i < n_frequencies; i++)
{
float freq = frequencies[i];
float d = (samplerate / freq);
int c = 0;
for (int j = index * i; j < index * (i + 1); j++)
{
float deg = 360.0f / d;
write_stream << qint16(qSin((c++ * double(deg)) * pi / 180.0) * amplitude);
}
}
}
void dataPlay()
{
QAudioFormat format;
format.setCodec("audio/pcm");
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format))
{
qDebug() << "Raw audio format not supported by backend, cannot play audio.";
return;
}
QAudioOutput audio(format);
QEventLoop loop;
QObject::connect(&audio, &QAudioOutput::stateChanged, &audio, [&](const QAudio::State state){
if (state != QAudio::ActiveState)
loop.quit();
});
audio.start(&m_float_buffer);
loop.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Opening buffer...";
m_float_buffer.open(QBuffer::ReadWrite);
qDebug() << "\nGenerating...";
toneGenerator();
//Back to beginning, now for reading
m_float_buffer.seek(0);
qDebug() << "\nPlaying...";
dataPlay();
qDebug() << "\nQBuffer size:" << m_float_buffer.size() << "bytes";
return a.exec();
}