如何从缓冲区播放QAudioOutput对象?

时间:2016-10-12 11:11:28

标签: c++ qt

int main(int argc, char *argv[])
{
    QCoreApplication b(argc, argv);

    QBuffer *buffer;
    QAudioOutput *a;

    QAudioFormat format;
    format.setSampleRate(8000);
    format.setChannelCount(1);
    format.setSampleSize(8);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    if (info.isFormatSupported(format))
    {
        cout << "Format supported" << endl;
    }
    else
    {
        cout << "Format not supported" << endl;
    }

    char *data = (char*)malloc(32768 * sizeof(char));

    //generating a sound
    for (int i = 0; i<256; ++i)
    {
        for (int j = 0; j<128; ++j)
        {
            data[i * 128 + j] = (char)j;
        }
    }

    cout << "Created samples" << endl;

    //copying into the buffer
    buffer = new QBuffer;
    buffer->open(QIODevice::ReadWrite);
    buffer->seek(0);
    buffer->write(data, 32768);
    cout << "Filled buffer" << endl;

    //playing
    QThread thr;
    a = new QAudioOutput(format);
    //a->moveToThread(&thr);

    //thr.start();
    //QMetaObject::invokeMethod(a, "start", Q_ARG(QIODevice*, buffer));

    a->start(buffer);

    system("pause");

    return b.exec();
}

我正在尝试让我的控制台应用程序输出声音,但我无法弄清楚为什么我的QAudioOutput对象不会这样做。我把代码放在上面。你能告诉我我做错了什么吗? 附:如果我将该矢量写入文件并将其作为原始声音播放,我可以听到低频嗡嗡声。

1 个答案:

答案 0 :(得分:0)

首先,你肯定不应该在你的主循环之前{@ 1}} @Hayt提到。

其次,在你写完数据后,你应该寻求开始。

system("pause")