-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (129 loc) · 4.21 KB
/
main.cpp
File metadata and controls
159 lines (129 loc) · 4.21 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
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <portaudio.h>
#define SAMPLE_RATE 44100
#define FRAMES_PER_BUFFER 512
static void checkErr(PaError err) {
if (err != paNoError) {
printf("PortAudio error: %s\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
}
static inline float max(float a, float b) {
return a > b ? a : b;
}
static int patestCallback(
const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags,
void* userData
) {
float* in = (float*)inputBuffer;
(void)outputBuffer;
int dispSize = 100;
printf("\r");
float vol_l = 0;
float vol_r = 0;
for (unsigned long i = 0; i < framesPerBuffer * 2; i += 2) {
vol_l = max(vol_l, fabs(in[i]));
vol_r = max(vol_r, fabs(in[i+1]));
}
for (int i = 0; i < dispSize; i++) {
float barProportion = i / (float)dispSize;
if (barProportion <= vol_l && barProportion <= vol_r) {
printf("█");
} else if (barProportion <= vol_l) {
printf("▀");
} else if (barProportion <= vol_r) {
printf("▄");
} else {
printf(" ");
}
}
fflush(stdout);
return paContinue;
}
void listDevices() {
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("There are no available audio devices on this machine.\n");
exit(EXIT_SUCCESS);
}
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);
// Print the host API
const PaHostApiInfo* hostApiInfo = Pa_GetHostApiInfo(deviceInfo->hostApi);
printf(" host API: %s\n", hostApiInfo->name);
}
printf("\nDefault input device: %d\n", Pa_GetDefaultInputDevice());
printf("Default output device: %d\n", Pa_GetDefaultOutputDevice());
}
int main(int argc, char* argv[]) {
PaError err;
err = Pa_Initialize();
checkErr(err);
// List devices if requested
if (argc > 1 && strcmp(argv[1], "-l") == 0) {
listDevices();
Pa_Terminate();
return EXIT_SUCCESS;
}
// Determine which device to use
int device = 0; // Default to first device
if (argc > 1) {
device = atoi(argv[1]);
}
// Determine duration
int duration = 10; // Default 10 seconds
if (argc > 2) {
duration = atoi(argv[2]);
}
// Print which device we're using
const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo(device);
printf("Using device %d: %s\n", device, deviceInfo->name);
PaStreamParameters inputParameters;
PaStreamParameters outputParameters;
memset(&inputParameters, 0, sizeof(inputParameters));
inputParameters.channelCount = 2;
inputParameters.device = device;
inputParameters.hostApiSpecificStreamInfo = NULL;
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency = deviceInfo->defaultLowInputLatency;
// For desktop audio capture, we only need input
PaStream* stream;
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL, // No output
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paNoFlag,
patestCallback,
NULL
);
checkErr(err);
printf("Starting to capture audio for %d seconds...\n", duration);
printf("Volume meter: \n");
err = Pa_StartStream(stream);
checkErr(err);
Pa_Sleep(duration * 1000);
err = Pa_StopStream(stream);
checkErr(err);
err = Pa_CloseStream(stream);
checkErr(err);
err = Pa_Terminate();
checkErr(err);
printf("\nAudio capture complete.\n");
return EXIT_SUCCESS;
}