Debian Codegraph @

src/audio/jack.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <string.h>
21 #include "jack.h"
22 #include "../ta-log.h"
23
24 int taJACKonSampleRate(jack_nframes_t rate, void* inst) {
25 TAAudioJACK* in=(TAAudioJACK*)inst;
26 in->onSampleRate(rate);
27 return 0;
28 }
29
30 int taJACKonBufferSize(jack_nframes_t bufsize, void* inst) {
31 TAAudioJACK* in=(TAAudioJACK*)inst;
32 in->onBufferSize(bufsize);
33 return 0;
34 }
35
36 int taJACKProcess(jack_nframes_t nframes, void* inst) {
37 TAAudioJACK* in=(TAAudioJACK*)inst;
38 in->onProcess(nframes);
39 return 0;
40 }
41
42 void TAAudioJACK::onSampleRate(jack_nframes_t rate) {
43 if (sampleRateChanged!=NULL) {
44 sampleRateChanged(SampleRateChangeEvent(rate));
45 }
46 }
47
48 void TAAudioJACK::onBufferSize(jack_nframes_t bufsize) {
49 if (bufferSizeChanged!=NULL) {
50 bufferSizeChanged(BufferSizeChangeEvent(bufsize));
51 }
52 }
53
54 void TAAudioJACK::onProcess(jack_nframes_t nframes) {
55 for (int i=0; i<desc.inChans; i++) {
56 iInBufs[i]=(float*)jack_port_get_buffer(ai[i],nframes);
57 if (nframes>desc.bufsize) {
58 delete[] inBufs[i];
59 inBufs[i]=new float[nframes];
60 }
61 memcpy(iInBufs[i],inBufs[i],nframes*sizeof(float));
62 }
63 for (int i=0; i<desc.outChans; i++) {
64 if (nframes>desc.bufsize) {
65 delete[] outBufs[i];
66 outBufs[i]=new float[nframes];
67 }
68 }
69 if (audioProcCallback!=NULL) {
70 if (midiIn!=NULL) midiIn->gather();
71 audioProcCallback(audioProcCallbackUser,inBufs,outBufs,desc.inChans,desc.outChans,nframes);
72 }
73 for (int i=0; i<desc.outChans; i++) {
74 iOutBufs[i]=(float*)jack_port_get_buffer(ao[i],nframes);
75 memcpy(iOutBufs[i],outBufs[i],nframes*sizeof(float));
76 }
77 if (nframes!=desc.bufsize) {
78 desc.bufsize=nframes;
79 }
80 }
81
82 void* TAAudioJACK::getContext() {
83 return (void*)ac;
84 }
85
86 bool TAAudioJACK::quit() {
87 if (!initialized) return false;
88
89 if (running) {
90 running=false;
91 if (jack_deactivate(ac)) {
92 logE("could not deactivate!");
93 return false;
94 }
95 }
96
97 for (int i=0; i<desc.inChans; i++) {
98 jack_port_unregister(ac,ai[i]);
99 ai[i]=NULL;
100 delete[] inBufs[i];
101 }
102 for (int i=0; i<desc.outChans; i++) {
103 jack_port_unregister(ac,ao[i]);
104 ao[i]=NULL;
105 delete[] outBufs[i];
106 }
107
108 if (iInBufs!=NULL) delete[] iInBufs;
109 if (iOutBufs!=NULL) delete[] iOutBufs;
110 delete[] inBufs;
111 delete[] outBufs;
112 delete[] ai;
113 delete[] ao;
114
115 jack_client_close(ac);
116 ac=NULL;
117
118 initialized=false;
119
120 return true;
121 }
122
123 bool TAAudioJACK::setRun(bool run) {
124 if (!initialized) return false;
125 if (running==run) {
126 return running;
127 }
128 if (run) {
129 if (jack_activate(ac)) {
130 logE("could not activate!");
131 return false;
132 }
133
134 for (int i=0; i<desc.outChans; i++) {
135 jack_connect(ac,(desc.name+String(":out")+std::to_string(i)).c_str(),(String("system:playback_")+std::to_string(i+1)).c_str());
136 }
137 running=true;
138 } else {
139 if (jack_deactivate(ac)) {
140 logE("could not deactivate!");
141 return true;
142 }
143 running=false;
144 }
145 return running;
146 }
147
148 String TAAudioJACK::printStatus(jack_status_t status) {
149 String ret;
150 if (status&JackFailure) {
151 ret+="failure. ";
152 }
153 if (status&JackInvalidOption) {
154 ret+="invalid option ";
155 }
156 if (status&JackNameNotUnique) {
157 ret+="name not unique ";
158 }
159 if (status&JackServerStarted) {
160 ret+="server started ";
161 }
162 if (status&JackServerFailed) {
163 ret+="server failed ";
164 }
165 if (status&JackServerError) {
166 ret+="server error ";
167 }
168 if (status&JackNoSuchClient) {
169 ret+="no such client ";
170 }
171 if (status&JackLoadFailure) {
172 ret+="load failure ";
173 }
174 if (status&JackInitFailure) {
175 ret+="init failure ";
176 }
177 if (status&JackShmFailure) {
178 ret+="shared memory failure ";
179 }
180 if (status&JackVersionError) {
181 ret+="version error ";
182 }
183 if (status&JackBackendError) {
184 ret+="backend error ";
185 }
186 if (status&JackClientZombie) {
187 ret+="client is zombie ";
188 }
189 if (ret.empty()) {
190 ret="no status";
191 } else {
192 if (ret[ret.size()-1]==' ') {
193 ret.resize(ret.size()-1);
194 }
195 }
196 return ret;
197 }
198
199 bool TAAudioJACK::init(TAAudioDesc& request, TAAudioDesc& response) {
200 if (initialized) return false;
201 int haveJACK=have_libjack();
202 if (haveJACK==-1) {
203 logE("JACK library not initialized!");
204 return false;
205 }
206 if (haveJACK==-2) {
207 logE("JACK not installed!");
208 return false;
209 }
210 if (haveJACK!=0) {
211 logE("JACK symbol error!");
212 return false;
213 }
214 desc=request;
215 desc.outFormat=TA_AUDIO_FORMAT_F32;
216
217 jack_status_t as;
218 ac=jack_client_open(desc.name.c_str(),JackNoStartServer,&as);
219 if (ac==NULL) {
220 logE("error while opening client! (%s)",printStatus(as));
221 return false;
222 }
223
224 desc.name=String(jack_get_client_name(ac));
225 jack_set_sample_rate_callback(ac,taJACKonSampleRate,this);
226 jack_set_buffer_size_callback(ac,taJACKonBufferSize,this);
227 jack_set_process_callback(ac,taJACKProcess,this);
228
229 jack_nframes_t count=jack_get_buffer_size(ac);
230 desc.bufsize=count;
231 desc.fragments=1;
232
233 jack_nframes_t sampleRate=jack_get_sample_rate(ac);
234 desc.rate=sampleRate;
235
236 if (desc.inChans>0) {
237 inBufs=new float*[desc.inChans];
238 iInBufs=new float*[desc.inChans];
239 ai=new jack_port_t*[desc.inChans];
240 for (int i=0; i<desc.inChans; i++) {
241 ai[i]=jack_port_register(ac,(String("in")+std::to_string(i)).c_str(),JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput,0);
242 if (ai[i]==NULL) {
243 desc.inChans=i;
244 break;
245 }
246 inBufs[i]=new float[count];
247 }
248 }
249 if (desc.outChans>0) {
250 outBufs=new float*[desc.outChans];
251 iOutBufs=new float*[desc.outChans];
252 ao=new jack_port_t*[desc.outChans];
253 for (int i=0; i<desc.outChans; i++) {
254 ao[i]=jack_port_register(ac,(String("out")+std::to_string(i)).c_str(),JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,0);
255 if (ao[i]==NULL) {
256 desc.outChans=i;
257 break;
258 }
259 outBufs[i]=new float[count];
260 }
261 }
262
263 response=desc;
264 initialized=true;
265 return true;
266 }