summaryrefslogtreecommitdiffstats
path: root/hardware/audiooutput.cpp
blob: 46091dad792b3a56e9d083b8960a081192d5ccc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel                                                   //
//                                                                               //
// This program is free software; you can redistribute it and/or modify          //
// it under the terms of the GNU General Public License as published by          //
// the Free Software Foundation as version 3 of the License, or                  //
//                                                                               //
// This program is distributed in the hope that it will be useful,               //
// but WITHOUT ANY WARRANTY; without even the implied warranty of                //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
// GNU General Public License V3 for more details.                               //
//                                                                               //
// You should have received a copy of the GNU General Public License             //
// along with this program. If not, see <http://www.gnu.org/licenses/>.          //
///////////////////////////////////////////////////////////////////////////////////

#include <QMessageBox>
#include "audiooutput.h"
#include "audiofifo.h"

int AudioOutput::callbackHelper(
	const void *inputBuffer,
	void *outputBuffer,
	unsigned long framesPerBuffer,
	const PaStreamCallbackTimeInfo* timeInfo,
	PaStreamCallbackFlags statusFlags,
	void *userData)
{
	AudioOutput* audioOutput = (AudioOutput*)userData;

	if(outputBuffer == NULL)
		return 0;

	return audioOutput->callback(inputBuffer, outputBuffer, framesPerBuffer, timeInfo, statusFlags);
}

int AudioOutput::callback(
	const void* inputBuffer,
	void* outputBuffer,
	unsigned long framesPerBuffer,
	const PaStreamCallbackTimeInfo* timeInfo,
	PaStreamCallbackFlags statusFlags)
{
	Q_UNUSED(inputBuffer);
	Q_UNUSED(timeInfo);
	Q_UNUSED(statusFlags);

	uint needed = framesPerBuffer;
	uint done = m_audioFifo->read((quint8*)outputBuffer, needed, 1);

	if(needed > done) {
		memset(((quint8*)outputBuffer) + done * 4, 0x00, (needed - done) * 4);
		qDebug("************* inserted %d samples *************", needed - done);
		//m_rateCorrection = 0.99;
	}

	m_streamStartTime += (PaTime)framesPerBuffer / (PaTime)m_sampleRate;

	return 0;
}

AudioOutput::AudioOutput() :
	m_stream(NULL),
	m_audioFifo(NULL),
	m_sampleRate(0)
{
}

AudioOutput::~AudioOutput()
{
	stop();
}

bool AudioOutput::start(int device, int rate, AudioFifo* audioFifo)
{
	if(m_stream != NULL)
		stop();

	PaStreamParameters outputParameters;
	const PaStreamInfo* streamInfo;
	PaError err;

	m_audioFifo = audioFifo;

	outputParameters.device = device;
	outputParameters.channelCount = 2;
	outputParameters.sampleFormat = paInt16;
	outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
	outputParameters.hostApiSpecificStreamInfo = NULL;
	qDebug("AudioOutput: open");
	if((err = Pa_OpenStream(&m_stream, NULL, &outputParameters, rate, 1024, paClipOff, callbackHelper, this)) != paNoError)
		goto failed;

	qDebug("AudioOutput: start");
	m_streamStartTime = Pa_GetStreamTime(m_stream);
	if((err = Pa_StartStream(m_stream)) != paNoError)
		goto failed;

	streamInfo = Pa_GetStreamInfo(m_stream);
	m_sampleRate = streamInfo->sampleRate;

	qDebug("AudioOutput: playback has started (%s @ %d Hz)", Pa_GetDeviceInfo(outputParameters.device)->name, rate);
	return true;

failed:
	qCritical("AudioOutput: playback failed: %s (%d)", Pa_GetErrorText(err), err);
	Pa_CloseStream(m_stream);
	m_stream = NULL;
	m_sampleRate = 0;
	return false;
}

void AudioOutput::stop()
{
	if(m_stream != NULL) {
		Pa_StopStream(m_stream);
		Pa_CloseStream(m_stream);
		m_stream = NULL;
		m_sampleRate = 0;
		qDebug("AudioOutput: stopped");
	}
}

int AudioOutput::bufferedSamples()
{
	return (Pa_GetStreamTime(m_stream) - m_streamStartTime) * m_sampleRate;
}