Я написал код на C ++ в Qt, чтобы генерировать звуковой сигнал двигателя с частотой дискретизации 16384
каждую секунду.Так что мой таймер работает каждую секунду, и я получаю новый вектор размером 16384
каждую секунду.Я пытаюсь воспроизвести этот сигнал, используя QAudioOutput
.Я получаю аудио выход каждую секунду, но не непрерывный звук.
qreal sampleRate = 16384;
qreal duration = 1.000;
qreal frequency = 1000;
// --- generate a QVector<double> xengine that contains a 16384 samples of data ---
// --- transfer QVector data to QByteArray
QByteArray* byteBuffer = new QByteArray();
int n = xengine.size();
byteBuffer->resize(n);
for (int i = 0; i < xengine.size(); i++)
{
qreal y = xengine[i]; // transfer data to y
*byteBuffer)[i] = (qint16)y; enter code here
}
// use qint16 (instead of quint16), because our waveform goes above and below zeros.
// create and setup a QAudioFormat object
QAudioFormat audioFormat;
audioFormat.setSampleRate(static_cast<int>(sampleRate));
audioFormat.setChannelCount(1);
audioFormat.setSampleSize(8);
audioFormat.setCodec("audio/pcm");
audioFormat.setByteOrder(QAudioFormat::LittleEndian);
audioFormat.setSampleType(QAudioFormat::SignedInt);
// create a QAudioDeviceInfo object, to make sure that our audioFormat is supported by the device
QAudioDeviceInfo deviceInfo(QAudioDeviceInfo::defaultOutputDevice());
if(!deviceInfo.isFormatSupported(audioFormat))
{
qWarning() << "Raw audio format not supported by backend,
cannot play audio.";
return;
}
QBuffer* input = new QBuffer(byteBuffer);
input->open(QIODevice::ReadOnly);
QAudioOutput* audio = new QAudioOutput(audioFormat, this);
audio->start(input);`