0
votes

I'm writing an emulator that requires me to continuously push generated raw audio samples to a QAudioOutput. This is achieved by writing bytes to the underlying QIODevice like so:

Qt_speaker::Qt_speaker()
{
    QAudioFormat fmt;
    fmt.setSampleRate(44100);
    fmt.setChannelCount(1);
    fmt.setSampleSize(sizeof(uint8_t)*8);
    fmt.setCodec("audio/pcm");
    fmt.setByteOrder(QAudioFormat::LittleEndian);
    fmt.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    if (!info.isFormatSupported(fmt)) {
        qWarning() << "Raw audio format not supported by backend, cannot play audio.\n"
                      "If you're running a Debian system, try manually installing Qt's"
                      "multimedia plugins with\n"
                      "\t'sudo apt-get install libqt5multimedia5-plugins'";
        toggle(false);
        return;
    }
    output_ = new QAudioOutput(fmt, nullptr);
    output_->setBufferSize(44100);
    //connect(output_, SIGNAL(stateChanged(QAudio::State)),
            //this, SLOT(output_state_changed(QAudio::State)));
    device_ = output_->start();

}

void Qt_speaker::push_samples(const gameboy::Raw_audio &a)
{
    device_->write(reinterpret_cast<const char *>(a.data()), a.size());
}

I want to keep track of the number of samples queued because the sound drives the speed of emulation, and consequently, of the raw audio samples being generated. Trying to call QIODevice::read() always returns 0. How can I do this?

1

1 Answers

-1
votes

I think you should notify signal of QAudioInput in order to behave according to the samples "consumed" by QAudioInput.