-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjack.cpp
118 lines (100 loc) · 2.84 KB
/
jack.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
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
#include "darmstadt.h"
int audio(jack_nframes_t count, void* arg) {
return ((JACKAudioEngine*)arg)->process(count);
}
int JACKAudioEngine::process(jack_nframes_t count) {
float* in[8];
for (int i=0; i<chan; i++) {
in[i]=(float*)jack_port_get_buffer(ai[i],count);
}
for (size_t i=0; i<count; i++) {
for (int j=0; j<chan; j++) ap.data[collPos*chan+j]=in[j][i];
collPos++;
if (collPos>=1024) {
// upload this buffer
collPos=0;
ap.time=curTime(CLOCK_MONOTONIC);
apqueue.push(new AudioPacket(ap));
if (wantBlank) {
wantBlank=false;
memset(ap.data,0,count*sizeof(float)*chan);
apqueue.push(new AudioPacket(ap));
}
}
}
return 0;
}
bool JACKAudioEngine::init(string dn) {
size_t splitPos;
string portName;
wantBlank=false;
chan=0;
collPos=0;
ac=jack_client_open("darmstadt",JackNoStartServer,&as,NULL);
if (ac==NULL) {
logW("JACK server not running or something.\n");
return false;
}
name=jack_get_client_name(ac);
jack_set_process_callback(ac,audio,this);
// select monitor if no device specified
devName=dn;
if (devName=="") {
devName="system:monitor_1|system:monitor_2";
}
// create ports
splitPos=0;
while ((splitPos=devName.find('|',splitPos))!=string::npos) {
if (chan>=8) {
logW("darmstadt only records a maximum of 8 audio channels.\n");
break;
}
portName=strFormat("in_%d",chan);
ai[chan++]=jack_port_register(ac,portName.c_str(),JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,0);
splitPos++;
}
portName=strFormat("in_%d",chan);
ai[chan++]=jack_port_register(ac,portName.c_str(),JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,0);
return true;
}
bool JACKAudioEngine::start() {
string portName, inPortName;
size_t splitPos, initPos;
int curChan;
jack_activate(ac);
// connect ports
splitPos=0; initPos=0; curChan=0;
while ((splitPos=devName.find('|',splitPos))!=string::npos) {
portName=strFormat("in_%d",curChan++);
inPortName=devName.substr(initPos,splitPos-initPos);
logD("connect %s to %s\n",inPortName.c_str(),portName.c_str());
jack_connect(ac,inPortName.c_str(),(name+":"+portName).c_str());
splitPos++;
initPos=splitPos;
}
splitPos=devName.size();
portName=strFormat("in_%d",curChan++);
inPortName=devName.substr(initPos,splitPos-initPos);
logD("connect %s to %s\n",inPortName.c_str(),portName.c_str());
jack_connect(ac,inPortName.c_str(),(name+":"+portName).c_str());
return true;
}
AudioPacket* JACKAudioEngine::read() {
AudioPacket* ret=NULL;
if (apqueue.empty()) {
return NULL;
} else {
ret=apqueue.front();
apqueue.pop();
return ret;
}
}
int JACKAudioEngine::sampleRate() {
return jack_get_sample_rate(ac);
}
int JACKAudioEngine::channels() {
return chan;
}
const char* JACKAudioEngine::engineName() {
return "JACK";
}