Debian Codegraph @

src/audio/pa.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 "../ta-log.h"
22 #include "pa.h"
23 #ifdef _WIN32
24 #include <pa_win_wasapi.h>
25 #endif
26 #include <fmt/printf.h>
27
28 int taPAProcess(const void* in, void* out, unsigned long nframes, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags flags, void* inst) {
29 TAAudioPA* instance=(TAAudioPA*)inst;
30 return instance->onProcess(in,out,nframes,timeInfo,flags);
31 }
32
33 int TAAudioPA::onProcess(const void* in, void* out, unsigned long nframes, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags flags) {
34 for (int i=0; i<desc.inChans; i++) {
35 if (nframes>desc.bufsize) {
36 delete[] inBufs[i];
37 inBufs[i]=new float[nframes];
38 }
39 }
40 for (int i=0; i<desc.outChans; i++) {
41 if (nframes>desc.bufsize) {
42 delete[] outBufs[i];
43 outBufs[i]=new float[nframes];
44 }
45 }
46 if (nframes!=desc.bufsize) {
47 desc.bufsize=nframes;
48 }
49
50 if (audioProcCallback!=NULL) {
51 if (midiIn!=NULL) midiIn->gather();
52 audioProcCallback(audioProcCallbackUser,inBufs,outBufs,desc.inChans,desc.outChans,desc.bufsize);
53 }
54 float* fbuf=(float*)out;
55 for (size_t j=0; j<desc.bufsize; j++) {
56 for (size_t i=0; i<desc.outChans; i++) {
57 fbuf[j*desc.outChans+i]=outBufs[i][j];
58 }
59 }
60 return 0;
61 }
62
63 void* TAAudioPA::getContext() {
64 return (void*)&ac;
65 }
66
67 bool TAAudioPA::quit() {
68 if (!initialized) return false;
69
70 Pa_CloseStream(ac);
71
72 if (running) {
73 running=false;
74 }
75
76 for (int i=0; i<desc.outChans; i++) {
77 delete[] outBufs[i];
78 }
79
80 delete[] outBufs;
81
82 initialized=false;
83 return true;
84 }
85
86 bool TAAudioPA::setRun(bool run) {
87 if (!initialized) return false;
88 if (running==run) return running;
89 PaError status;
90 if (run) {
91 status=Pa_StartStream(ac);
92 } else {
93 status=Pa_StopStream(ac);
94 }
95 if (status!=paNoError) {
96 logW("error while setting run status: %s",Pa_GetErrorText(status));
97 return running;
98 }
99 running=run;
100 return running;
101 }
102
103 std::vector<String> TAAudioPA::listAudioDevices() {
104 std::vector<String> ret;
105 if (!audioSysStarted) {
106 PaError status=Pa_Initialize();
107 if (status!=paNoError) {
108 logE("could not initialize PortAudio to list audio devices");
109 return ret;
110 } else {
111 audioSysStarted=true;
112 }
113 }
114
115 int count=Pa_GetDeviceCount();
116 if (count<0) return ret;
117
118 for (int i=0; i<count; i++) {
119 const PaDeviceInfo* devInfo=Pa_GetDeviceInfo(i);
120 if (devInfo==NULL) continue;
121 if (devInfo->maxOutputChannels<1) continue;
122
123 String devName;
124
125 const PaHostApiInfo* driverInfo=Pa_GetHostApiInfo(devInfo->hostApi);
126 if (driverInfo==NULL) {
127 devName+="[???] ";
128 } else {
129 devName+=fmt::sprintf("[%s] ",driverInfo->name);
130 }
131
132 if (devInfo->name!=NULL) {
133 devName+=devInfo->name;
134 ret.push_back(devName);
135 }
136 }
137
138 return ret;
139 }
140
141 bool TAAudioPA::init(TAAudioDesc& request, TAAudioDesc& response) {
142 if (initialized) {
143 logE("audio already initialized");
144 return false;
145 }
146 PaError status;
147
148 if (!audioSysStarted) {
149 status=Pa_Initialize();
150 if (status!=paNoError) {
151 logE("could not initialize PortAudio");
152 return false;
153 } else {
154 audioSysStarted=true;
155 }
156 }
157
158 desc=request;
159 desc.outFormat=TA_AUDIO_FORMAT_F32;
160
161 const PaDeviceInfo* devInfo=NULL;
162 const PaHostApiInfo* driverInfo=NULL;
163 int outDeviceID=0;
164
165 if (desc.deviceName.empty()) {
166 outDeviceID=Pa_GetDefaultOutputDevice();
167 devInfo=Pa_GetDeviceInfo(outDeviceID);
168 if (devInfo!=NULL) {
169 driverInfo=Pa_GetHostApiInfo(devInfo->hostApi);
170 }
171 } else {
172 int count=Pa_GetDeviceCount();
173 bool found=false;
174 if (count<0) {
175 logE("audio device not found");
176 return false;
177 }
178
179 for (int i=0; i<count; i++) {
180 devInfo=Pa_GetDeviceInfo(i);
181 if (devInfo==NULL) continue;
182 if (devInfo->maxOutputChannels<1) continue;
183
184 String devName;
185
186 driverInfo=Pa_GetHostApiInfo(devInfo->hostApi);
187 if (driverInfo==NULL) {
188 devName+="[???] ";
189 } else {
190 devName+=fmt::sprintf("[%s] ",driverInfo->name);
191 }
192
193 if (devInfo->name!=NULL) {
194 devName+=devInfo->name;
195 } else {
196 continue;
197 }
198
199 if (devName==desc.deviceName) {
200 outDeviceID=i;
201 found=true;
202 break;
203 }
204 }
205 if (!found) {
206 logE("audio device not found");
207 return false;
208 }
209 }
210
211 // check output channels and sample rate
212 if (devInfo!=NULL) {
213 if (desc.outChans>devInfo->maxOutputChannels) desc.outChans=devInfo->maxOutputChannels;
214 }
215
216 PaStreamParameters outParams;
217 outParams.device=outDeviceID;
218 outParams.channelCount=desc.outChans;
219 outParams.sampleFormat=paFloat32;
220 outParams.suggestedLatency=(double)(desc.bufsize*desc.fragments)/desc.rate;
221 outParams.hostApiSpecificStreamInfo=NULL;
222
223 if (driverInfo!=NULL) {
224 #ifdef _WIN32
225 if (driverInfo->type==paWASAPI) {
226 logV("setting WASAPI-specific flags");
227 PaWasapiStreamInfo* wasapiInfo=new PaWasapiStreamInfo;
228 memset(wasapiInfo,0,sizeof(PaWasapiStreamInfo));
229 wasapiInfo->size=sizeof(PaWasapiStreamInfo);
230 wasapiInfo->hostApiType=paWASAPI;
231 wasapiInfo->version=1;
232 wasapiInfo->flags=paWinWasapiThreadPriority;
233 wasapiInfo->threadPriority=eThreadPriorityProAudio;
234 wasapiInfo->streamCategory=eAudioCategoryMedia;
235 wasapiInfo->streamOption=eStreamOptionRaw;
236
237 if (desc.wasapiEx) {
238 wasapiInfo->flags|=paWinWasapiExclusive;
239 }
240
241 outParams.hostApiSpecificStreamInfo=wasapiInfo;
242 }
243 #endif
244 }
245
246 logV("opening audio device...");
247 status=Pa_OpenStream(
248 &ac,
249 NULL,
250 &outParams,
251 desc.rate,
252 0,
253 paClipOff|paDitherOff,
254 taPAProcess,
255 this
256 );
257 if (status!=paNoError) {
258 logE("could not open audio device: %s",Pa_GetErrorText(status));
259 return false;
260 }
261
262 desc.deviceName=devInfo->name;
263 desc.inChans=0;
264
265 if (desc.outChans>0) {
266 outBufs=new float*[desc.outChans];
267 for (int i=0; i<desc.outChans; i++) {
268 outBufs[i]=new float[desc.bufsize];
269 }
270 }
271
272 response=desc;
273 initialized=true;
274 return true;
275 }