src/platform/qt/AudioDevice.cpp (view raw)
1#include "AudioDevice.h"
2
3extern "C" {
4#include "gba.h"
5#include "gba-audio.h"
6}
7
8using namespace QGBA;
9
10AudioDevice::AudioDevice(GBAAudio* audio, QObject* parent)
11 : QIODevice(parent)
12 , m_audio(audio)
13{
14 setOpenMode(ReadOnly);
15}
16
17void AudioDevice::setFormat(const QAudioFormat& format) {
18 // TODO: merge where the fudge rate exists
19 float fudgeRate = 16853760.0f / GBA_ARM7TDMI_FREQUENCY;
20 m_ratio = format.sampleRate() / (float) (m_audio->sampleRate * fudgeRate);
21}
22
23qint64 AudioDevice::readData(char* data, qint64 maxSize) {
24 if (maxSize > 0xFFFFFFFF) {
25 maxSize = 0xFFFFFFFF;
26 }
27
28 return GBAAudioResampleNN(m_audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
29}
30
31qint64 AudioDevice::writeData(const char*, qint64) {
32 return 0;
33}
34
35AudioThread::AudioThread(QObject* parent)
36 : QThread(parent)
37{
38 // Nothing to do
39}
40
41void AudioThread::setInput(GBAAudio* input) {
42 m_input = input;
43}
44
45void AudioThread::shutdown() {
46 m_audioOutput->stop();
47 quit();
48}
49
50void AudioThread::run() {
51 QAudioFormat format;
52 format.setSampleRate(44100);
53 format.setChannelCount(2);
54 format.setSampleSize(16);
55 format.setCodec("audio/pcm");
56 format.setByteOrder(QAudioFormat::LittleEndian);
57 format.setSampleType(QAudioFormat::SignedInt);
58
59 AudioDevice device(m_input);
60 m_audioOutput = new QAudioOutput(format);
61 m_audioOutput->setBufferSize(1024);
62 device.setFormat(m_audioOutput->format());
63 m_audioOutput->start(&device);
64
65 exec();
66}