/
audioIO.h
204 lines (166 loc) · 6.15 KB
/
audioIO.h
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Virtual Choir Rehearsal Room Copyright (C) 2020 Lukas Ondracek <ondracek.lukas@gmail.com>, use under GNU GPLv3
#include <time.h>
#include <portaudio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef __WIN32__
#include <pa_win_wasapi.h>
#endif
bool aioConnectAudio(PaStream **paInputStream, PaStream **paOutputStream, bool forceDefault, PaStreamCallback *inputCallback, PaStreamCallback *outputCallback, int *inputChannelsPtr) {
PaError err;
PaDeviceIndex inputIndex, outputIndex;
bool useWasapiExclusive = 0;
#ifdef __WIN32__
struct PaWasapiStreamInfo wasapiInfo = {
.size = sizeof(PaWasapiStreamInfo),
.hostApiType = paWASAPI,
.version = 1,
.flags = paWinWasapiExclusive | paWinWasapiThreadPriority | paWinWasapiExplicitSampleFormat | paWinWasapiPolling,
.channelMask = 0,
.hostProcessorOutput = NULL,
.hostProcessorInput = NULL,
.threadPriority = eThreadPriorityProAudio };
PaHostApiIndex wasapiIndex = Pa_HostApiTypeIdToHostApiIndex(paWASAPI);
#else
struct {} wasapiInfo;
#endif
if (!forceDefault) printf("[space] default settings [c] custom settings\n");
if (forceDefault || (ttyReadKey() != 'c')) {
inputIndex = Pa_GetDefaultInputDevice();
outputIndex = Pa_GetDefaultOutputDevice();
#ifdef __WIN32__
if (wasapiIndex >= 0) {
const PaHostApiInfo *wasapiInfo = Pa_GetHostApiInfo(wasapiIndex);
inputIndex = wasapiInfo->defaultInputDevice;
outputIndex = wasapiInfo->defaultOutputDevice;
useWasapiExclusive = true;
}
#endif
} else {
printf("List of available APIs and their devices:\n");
{
PaHostApiIndex apiCnt = Pa_GetHostApiCount();
PaHostApiIndex apiDefault = Pa_GetDefaultHostApi();
for (PaHostApiIndex apiIndex = 0; apiIndex < apiCnt; apiIndex++) {
const PaHostApiInfo *apiInfo = Pa_GetHostApiInfo(apiIndex);
printf(" %s API%s\n", apiInfo->name, (apiIndex == apiDefault ? ", system default" : ""));
// apiInfo -> defaultInputDevice, deviceCount
for (int apiDeviceIndex = 0; apiDeviceIndex < apiInfo->deviceCount; apiDeviceIndex++) {
PaDeviceIndex deviceIndex = Pa_HostApiDeviceIndexToDeviceIndex(apiIndex, apiDeviceIndex);
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(deviceIndex);
printf(" %3d: %s, %.0f Hz\n", deviceIndex, deviceInfo->name, deviceInfo->defaultSampleRate);
if (deviceInfo->maxInputChannels > 0) {
printf(" IN: %3d channels%s\n",
deviceInfo->maxInputChannels,
(deviceIndex == apiInfo->defaultInputDevice ? ", default input" : ""));
}
if (deviceInfo->maxOutputChannels > 0) {
printf(" OUT: %3d channels%s\n",
deviceInfo->maxOutputChannels,
(deviceIndex == apiInfo->defaultOutputDevice ? ", default output" : ""));
}
}
}
}
printf("\n");
/*
printf("Default input device id: %d\nDefault output device id: %d\nDefault minimal latency: 0\n\n",
Pa_GetDefaultInputDevice(),Pa_GetDefaultOutputDevice());
*/
inputIndex = ttyPromptInt("Choose input device id");
outputIndex = ttyPromptInt("Choose output device id");
#ifdef __WIN32__
const PaDeviceInfo *inputInfo = Pa_GetDeviceInfo(inputIndex);
const PaDeviceInfo *outputInfo = Pa_GetDeviceInfo(outputIndex);
if ((inputInfo->hostApi == wasapiIndex) && (outputInfo->hostApi == wasapiIndex)) {
useWasapiExclusive = (ttyPromptKey("Use WASAPI exclusive mode (y/n)", "yn") == 'y');
}
#endif
}
{
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(inputIndex);
if (!deviceInfo) return false;
*inputChannelsPtr = deviceInfo->maxInputChannels;
if (*inputChannelsPtr > 2) *inputChannelsPtr = 2; // 1 is sufficient but there were issues with it
}
const PaStreamParameters inputParameters = {
.device = inputIndex,
.channelCount = *inputChannelsPtr,
.sampleFormat = paInt16,
.suggestedLatency = 0,
.hostApiSpecificStreamInfo = (useWasapiExclusive ? &wasapiInfo : NULL)};
err = Pa_OpenStream( paInputStream,
&inputParameters, NULL,
SAMPLE_RATE, MONO_BLOCK_SIZE, paNoFlag,
inputCallback, NULL);
if (err != paNoError) {
printf("%s\n", Pa_GetErrorText(err));
return false;
}
const PaStreamParameters outputParameters = {
.device = outputIndex,
.channelCount = 2,
.sampleFormat = paInt16,
.suggestedLatency = 0,
.hostApiSpecificStreamInfo = (useWasapiExclusive ? &wasapiInfo : NULL)};
err = Pa_OpenStream( paOutputStream,
NULL, &outputParameters,
SAMPLE_RATE, MONO_BLOCK_SIZE, paNoFlag,
outputCallback, NULL);
if (err != paNoError) {
printf("%s\n", Pa_GetErrorText(err));
return false;
}
Pa_StartStream(*paOutputStream);
Pa_StartStream(*paInputStream);
return true;
}
// --- measuring latency ---
#define WAIT_SAMPLES (10 * MONO_BLOCK_SIZE)
#define BEEP_SAMPLES (20 * MONO_BLOCK_SIZE)
uint64_t aioLatSqSums[BEEP_SAMPLES];
uint64_t aioLatSqSum = 0;
size_t aioLatPos = 0;
uint64_t aioLatMaxDiff = 0;
uint64_t aioLatMaxDiffPos = 0;
int64_t aioLatBufferLatBlocksSum = 0;
void aioLatBlock(sample_t *block, int bufferLatBlocks) {
for (size_t i = 0; i < MONO_BLOCK_SIZE; i++) {
aioLatSqSum += (uint64_t)block[i] * block[i];
if (aioLatPos > BEEP_SAMPLES) {
uint64_t diff = aioLatSqSum - aioLatSqSums[aioLatPos % BEEP_SAMPLES];
if (diff > aioLatMaxDiff) {
aioLatMaxDiff = diff;
aioLatMaxDiffPos = aioLatPos - BEEP_SAMPLES;
}
}
aioLatSqSums[aioLatPos++ % BEEP_SAMPLES] = aioLatSqSum;
if (aioLatPos < WAIT_SAMPLES) {
block[i] = 0;
} else if (aioLatPos < WAIT_SAMPLES + BEEP_SAMPLES) {
block[i] = fmod(aioLatPos * 440.0 / SAMPLE_RATE, 1) < 0.5 ? -30000 : 30000;
} else {
block[i] = 0;
}
}
aioLatBufferLatBlocksSum += bufferLatBlocks;
}
double aioLatReset() {
double ret = 0;
if (aioLatPos > BEEP_SAMPLES + WAIT_SAMPLES) {
ret = ((double)aioLatMaxDiffPos - WAIT_SAMPLES) / SAMPLE_RATE * 1000;
double bufferLat = (double)aioLatBufferLatBlocksSum / (aioLatPos / MONO_BLOCK_SIZE) * MONO_BLOCK_SIZE / SAMPLE_RATE * 1000;
ret -= bufferLat;
double signalToNoise =
((double)aioLatMaxDiff / BEEP_SAMPLES) /
((double)(aioLatSqSum - aioLatMaxDiff) / (aioLatPos - BEEP_SAMPLES));
if ((signalToNoise < 5) || (ret < 0)) ret = 0;
}
aioLatSqSum = 0;
aioLatPos = 0;
aioLatMaxDiff = 0;
aioLatMaxDiffPos = 0;
aioLatBufferLatBlocksSum = 0;
return ret;
}