-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtaudio_wrapper.cpp
76 lines (57 loc) · 1.4 KB
/
rtaudio_wrapper.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
extern "C" {
#include "ckv.h"
}
#include "rtaudio/RtAudio.h"
static RtAudio audio;
static AudioCallback callback;
static
int
render(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData)
{
if(status)
std::cerr << "[ckv] Stream underflow detected!" << std::endl;
callback((double *)outputBuffer, (double *)inputBuffer, nBufferFrames, streamTime, userData);
return 0;
}
extern "C" {
/* returns 0 on failure */
int
start_audio(AudioCallback _callback, int sample_rate, void *data)
{
if(audio.getDeviceCount() < 1) {
std::cout << "No audio devices found!\n";
return 0;
}
RtAudio::StreamParameters iparams, oparams;
/* configure input (microphone) */
iparams.deviceId = audio.getDefaultInputDevice();
iparams.nChannels = 1;
iparams.firstChannel = 0;
/* configure output */
oparams.deviceId = audio.getDefaultOutputDevice();
oparams.nChannels = 2;
oparams.firstChannel = 0;
unsigned int bufferFrames = 256;
callback = _callback;
try {
audio.openStream(&oparams, &iparams, RTAUDIO_FLOAT64 /* double */, sample_rate, &bufferFrames, &render, data);
audio.startStream();
} catch(RtError& e) {
e.printMessage();
return 0;
}
return 1;
}
void
stop_audio(void)
{
try {
audio.stopStream();
} catch(RtError& e) {
e.printMessage();
}
if(audio.isStreamOpen())
audio.closeStream();
}
} // extern "C"