-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordAudio.cpp
More file actions
69 lines (59 loc) · 1.88 KB
/
RecordAudio.cpp
File metadata and controls
69 lines (59 loc) · 1.88 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
#include "PortAudioCallbacks.cpp"
#include "client.h"
#include "portaudio.h"
#include <cstdint>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <vector>
#define SAMPLE_RATE (3000)
#define LATENCY_MS (300)
#define FRAMES_PER_BUFFER (SAMPLE_RATE * LATENCY_MS / 1000)
int main() {
PortAudioCallbacks callback;
PaError err = Pa_Initialize();
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
return 1;
}
PaStream *stream;
PaStreamParameters inputParameters;
inputParameters.device = Pa_GetDefaultInputDevice();
if (inputParameters.device == paNoDevice) {
std::cerr << "Error: No default input device." << std::endl;
Pa_Terminate();
return 1;
}
inputParameters.channelCount = 1;
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency =
Pa_GetDeviceInfo(inputParameters.device)->defaultHighInputLatency;
// inputParameters.suggestedLatency =
// Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(&stream, &inputParameters, NULL, SAMPLE_RATE,
FRAMES_PER_BUFFER, paClipOff, callback.recordCallback, 0);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
Pa_Terminate();
return 1;
}
err = Pa_StartStream(stream);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
Pa_CloseStream(stream);
Pa_Terminate();
return 1;
}
std::cout << "Recording... Press Enter to stop." << std::endl;
std::cin.get();
err = Pa_CloseStream(stream);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
Pa_Terminate();
return 1;
}
Pa_Terminate();
std::cout << "Recording finished." << std::endl;
return 0;
}