-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiointerface.cpp
146 lines (132 loc) · 3.81 KB
/
audiointerface.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "audiointerface.h"
#include <QTimer>
#include <QDebug>
AudioInterface::AudioInterface( RtAudio* audioInterface, unsigned int sampleRate, unsigned int inputDevice, unsigned int outputDevice, QObject* /*parent*/ )
:SignalGenerator()
,m_started(false)
,m_ptrAudioInterace(audioInterface)
,m_sampleRate(sampleRate)
,m_inputDevice(inputDevice)
,m_outputDevice(outputDevice)
,m_scope(NULL)
,m_analyzer(NULL)
,m_out1(false)
,m_out2(false)
{
}
void AudioInterface::startAudio( RtAudio* audioInterface, unsigned int sampleRate )
{
m_ptrAudioInterace = audioInterface;
m_sampleRate = sampleRate;
if( m_ptrAudioInterace )
{
m_started = true;
m_cycle = 0;
// Set the same number of channels for both input and output.
unsigned int bufferFrames = 16384;
RtAudio::StreamParameters iParams, oParams;
iParams.deviceId = m_inputDevice;
iParams.nChannels = 2;
oParams.deviceId = m_outputDevice;
oParams.nChannels = 2;
RtAudio::StreamOptions options;
options.flags = RTAUDIO_SCHEDULE_REALTIME;
try
{
if( !m_ptrAudioInterace->isStreamOpen())
{
m_ptrAudioInterace->openStream( &oParams, &iParams, RTAUDIO_FLOAT64, m_sampleRate, &bufferFrames, &AudioInterfaceInOut, (void*) this );
}
}
catch ( RtAudioError& e )
{
e.printMessage();
exit( 0 );
}
m_ptrAudioInterace->startStream();
}
}
void AudioInterface::stopAudio()
{
if( m_ptrAudioInterace && m_started )
{
m_started = false;
m_ptrAudioInterace->stopStream();
if( m_scope )
{
QVector<double> channel1;
QVector<double> channel2;
m_scope->insertScopeData( channel1, channel2 );
}
if( m_analyzer )
{
QVector<double> channel1;
QVector<double> channel2;
m_analyzer->insertAnalyzerData( channel1, channel2 );
}
}
}
// Pass-through function.
int AudioInterfaceInOut( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double /*streamTime*/, RtAudioStreamStatus status, void *data )
{
AudioInterface* ai = (AudioInterface*) data;
double* outBuffer = (double*) outputBuffer;
double* inBuffer = (double*) inputBuffer;
QVector<double> channel1;
channel1.reserve(nBufferFrames);
QVector<double> channel2;
channel1.reserve(nBufferFrames);
if( status == RTAUDIO_INPUT_OVERFLOW )
{
qDebug() << "RTAUDIO_INPUT_OVERFLOW";
}
else if( status == RTAUDIO_OUTPUT_UNDERFLOW )
{
qDebug() << "RTAUDIO_OUTPUT_UNDERFLOW";
}
for( unsigned int i = 0; i < nBufferFrames; i++ )
{
double x = ai->compute( ai->sampleRate() );
double v = *inBuffer;
for ( int j= 0; j< 2; j++ )
{
if( j == 0 )
{
if( ai->isOut1() )
{
*outBuffer = x;
}
else
{
*outBuffer = 0;
}
channel1.push_back( (double) v );
}
else
{
if( ai->isOut2() )
{
*outBuffer = x;
}
else
{
*outBuffer = 0;
}
channel2.push_back( (double) v );
}
outBuffer++;
inBuffer++;
}
}
IScope* scope = ai->scope();
if( scope )
{
scope->insertScopeData( channel1, channel2 );
}
IAnalyzer* analyzer = ai->analyzer();
if( analyzer )
{
analyzer->insertAnalyzerData( channel1, channel2 );
}
return 0;
}