forked from chrisrouck/tutorial-cpp-audio-capture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppAudioCapture.cpp
More file actions
204 lines (156 loc) · 4.47 KB
/
CppAudioCapture.cpp
File metadata and controls
204 lines (156 loc) · 4.47 KB
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
#include <cstring>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <portaudio.h>
#include <portaudiocpp/Exception.hxx>
#define SAMPLE_RATE 44100.0
#define FRAMES_PER_BUFFER 512
#define NUM_CHANNELS 2
using namespace std;
static void checkErr(PaError err){
if (err != paNoError){
printf("PortAudio error %s\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
}
// Full-block and half-block characters: `█`, `▀`, `▄`
static inline float max(float a, float b){
return a > b ? a : b;
}
static inline float absolute(float a){
return a > 0 ? a : -a;
}
static int paVMeterCallback(
const void *inputBuffer,
void *outputBuffer,
unsigned long int framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
unsigned long int statusFlag,
void *userData
){
float* in = (float*)inputBuffer;
(void*)outputBuffer;
int dispSize = 100;
printf("\r");
float vol_L = 0;
float vol_R = 0;
printf("\e[40m");
for (unsigned long i = 0; i < framesPerBuffer * 2; i += 2){
vol_L = max(vol_L, absolute((float)in[i]));
vol_R = max(vol_R, absolute((float)in[i+1]));
}
for (int i = 0; i < dispSize; i++){
float barProportion = i / (float)dispSize;
// Green: volume < 1/3
if (
(barProportion < vol_L && barProportion < vol_R) &&
(vol_L <= 0.33 && vol_R <= 0.33)
){
printf("\e[0;32m\e[40m█");
} else if (
barProportion < vol_L && vol_L <= 0.33
){
printf("\e[0;32m\e[40m▀");
} else if (barProportion < vol_R && vol_R <= 0.33
){
printf("\e[0;32m\e[40m▄");
// Yellow: volume < 2/3
} else if (
(barProportion <= vol_L && barProportion <= vol_R) &&
(vol_L > 0.33 && vol_R > 0.33) &&
(vol_L < 0.67 && vol_R < 0.67)
){
printf("\e[0;33m\e[40m█");
} else if (
barProportion <= vol_L && vol_L > 0.33 && vol_L < 0.67
){
printf("\e[0;33m\e[40m▀");
} else if (
barProportion <= vol_R && vol_R > 0.33 && vol_R < 0.67
){
printf("\e[0;33m\e[40m▄");
// Red: volume >= 2/3
} else if (
(barProportion <= vol_L && barProportion <= vol_R) &&
(vol_L >= 0.67 && vol_R >= 0.67)
){
printf("\e[0;31m\e[40m█");
} else if (
barProportion <= vol_L && vol_L >= 0.67
){
printf("\e[0;31m\e[40m▀");
} else if (
barProportion <= vol_R && vol_R >= 0.67
){
printf("\e[0;31m\e[40m▄");
// Black bar: 0 signal
} else {
printf("\e[40m ");
}
}
printf("\e[0m");
fflush(stdout);
return 0;
}
int main(int argc, char **argv) {
PaError err;
err = Pa_Initialize();
checkErr(err);
int numDevices = Pa_GetDeviceCount();
printf("Number of devices: %d\n", numDevices);
if (numDevices < 0){
printf("Error getting device count.\n");
exit(EXIT_FAILURE);
} else if (numDevices == 0){
printf("Error getting device count.\n");
exit(EXIT_FAILURE);
}
const PaDeviceInfo* deviceInfo;
for (int i = 0; i < numDevices; i++){
deviceInfo = Pa_GetDeviceInfo(i);
printf("Device %d\n", i);
printf(" name: %s\n", deviceInfo->name);
printf(" maxInputChannels: %d\n", deviceInfo->maxInputChannels);
printf(" maxOutputChannels: %d\n", deviceInfo->maxOutputChannels);
printf(" defaultSampleRate: %f\n", deviceInfo->defaultSampleRate);
}
int device = Pa_GetDefaultInputDevice();
PaStreamParameters inputParameters;
memset(&inputParameters, 0, sizeof(inputParameters));
inputParameters.channelCount = NUM_CHANNELS; //Pa_GetDeviceInfo(device)->maxInputChannels;
inputParameters.device = device;
inputParameters.hostApiSpecificStreamInfo = NULL;
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency = Pa_GetDeviceInfo(device)->defaultLowInputLatency;
printf("\n____________________________________________________________________________________________________\n");
printf("Input device: (%d) %s\n", device, deviceInfo->name);
printf("Audio captured signal level (L/R):\n");
printf("\e[0;32m1...5...10....15.......25.......33\e[0;33m.....40........50........60....66\e[0;31m........75...80.......90......100\e[0m\n");
PaStream* stream;
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL,
Pa_GetDeviceInfo(device)->defaultSampleRate,
FRAMES_PER_BUFFER,
paNoFlag,
paVMeterCallback,
NULL
);
checkErr(err);
err = Pa_StartStream(stream);
checkErr(err);
// Execute until <ENTER> is pressed
cin.get();
// Execute for 10 seconds
//Pa_Sleep(10 * 1000);
err = Pa_StopStream(stream);
checkErr(err);
err = Pa_CloseStream(stream);
checkErr(err);
err = Pa_Terminate();
checkErr(err);
printf("\n");
return EXIT_SUCCESS;
}