Jack2 1.9.12

JackPortAudioDevices.cpp
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "JackPortAudioDevices.h"
21 #include "JackError.h"
22 #include <stdlib.h>
23
24 using namespace std;
25
26 PortAudioDevices::PortAudioDevices()
27 {
28 PaError err;
29 PaDeviceIndex id;
30 jack_log("Initializing PortAudio...");
31 if ((err = Pa_Initialize()) == paNoError) {
32 fNumHostApi = Pa_GetHostApiCount();
33 fNumDevice = Pa_GetDeviceCount();
34 fDeviceInfo = new PaDeviceInfo*[fNumDevice];
35 for (id = 0; id < fNumDevice; id++) {
36 fDeviceInfo[id] = const_cast<PaDeviceInfo*>(Pa_GetDeviceInfo(id));
37 }
38 fHostName = new string[fNumHostApi];
39 for (id = 0; id < fNumHostApi; id++) {
40 fHostName[id] = string(Pa_GetHostApiInfo(id)->name);
41 }
42 } else {
43 jack_error("JackPortAudioDriver::Pa_Initialize error = %s", Pa_GetErrorText(err));
44 }
45 }
46
47 PortAudioDevices::~PortAudioDevices()
48 {
49 jack_log("Terminate PortAudio...");
50 Pa_Terminate();
51
52 delete[] fDeviceInfo;
53 delete[] fHostName;
54 }
55
56 PaDeviceIndex PortAudioDevices::GetNumDevice()
57 {
58 return fNumDevice;
59 }
60
61 PaDeviceInfo* PortAudioDevices::GetDeviceInfo(PaDeviceIndex id)
62 {
63 return fDeviceInfo[id];
64 }
65
66 string PortAudioDevices::GetDeviceName(PaDeviceIndex id)
67 {
68 return string(fDeviceInfo[id]->name);
69 }
70
71 string PortAudioDevices::GetHostFromDevice(PaDeviceInfo* device)
72 {
73 return fHostName[device->hostApi];
74 }
75
76 string PortAudioDevices::GetHostFromDevice(PaDeviceIndex id)
77 {
78 return fHostName[fDeviceInfo[id]->hostApi];
79 }
80
81 string PortAudioDevices::GetFullName(PaDeviceIndex id)
82 {
83 string hostname = GetHostFromDevice(id);
84 string devicename = GetDeviceName(id);
85 //some hostname are quite long...use shortcuts
86 if (hostname.compare("Windows DirectSound") == 0) {
87 hostname = string("DirectSound");
88 }
89 return (hostname + "::" + devicename);
90 }
91
92 string PortAudioDevices::GetFullName(std::string hostname, std::string devicename)
93 {
94 //some hostname are quite long...use shortcuts
95 if (hostname.compare("Windows DirectSound") == 0) {
96 hostname = string("DirectSound");
97 }
98 return (hostname + "::" + devicename);
99 }
100
101 PaDeviceInfo* PortAudioDevices::GetDeviceFromFullName(string fullname, PaDeviceIndex& id, bool isInput)
102 {
103 PaDeviceInfo* ret = NULL;
104 //no driver to find
105 if (fullname.size() == 0) {
106 return NULL;
107 }
108 //first get host and device names from fullname
109 string::size_type separator = fullname.find("::", 0);
110
111 if (separator == string::npos) {
112 return NULL;
113 }
114
115 char* hostname = (char*)malloc(separator + 9);
116 fill_n(hostname, separator + 9, 0);
117 fullname.copy(hostname, separator);
118
119 //we need the entire hostname, replace shortcuts
120 if (strcmp(hostname, "DirectSound") == 0) {
121 strcpy(hostname, "Windows DirectSound");
122 }
123 string devicename = fullname.substr(separator + 2);
124 //then find the corresponding device
125 for (PaDeviceIndex dev_id = 0; dev_id < fNumDevice; dev_id++) {
126 bool flag = (isInput) ? (fDeviceInfo[dev_id]->maxInputChannels > 0) : (fDeviceInfo[dev_id]->maxOutputChannels > 0);
127 if ((GetHostFromDevice(dev_id).compare(hostname) == 0)
128 && (GetDeviceName(dev_id).compare(devicename) == 0)
129 && flag) {
130 id = dev_id;
131 ret = fDeviceInfo[dev_id];
132 }
133 }
134 free(hostname);
135 return ret;
136 }
137
138 void PortAudioDevices::PrintSupportedStandardSampleRates(const PaStreamParameters* inputParameters, const PaStreamParameters* outputParameters)
139 {
140 static double standardSampleRates[] =
141 {
142 8000.0, 9600.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0,
143 44100.0, 48000.0, 88200.0, 96000.0, 192000.0, -1 /* negative terminated list */
144 };
145 int i, printCount;
146 PaError err;
147
148 printCount = 0;
149 for (i = 0; standardSampleRates[i] > 0; i++) {
150 err = Pa_IsFormatSupported(inputParameters, outputParameters, standardSampleRates[i]);
151 if (err == paFormatIsSupported) {
152 if (printCount == 0) {
153 jack_info("\t%8.2f", standardSampleRates[i]);
154 printCount = 1;
155 } else if (printCount == 4) {
156 jack_info(",\n\t%8.2f", standardSampleRates[i]);
157 printCount = 1;
158 } else {
159 jack_info(", %8.2f", standardSampleRates[i]);
160 ++printCount;
161 }
162 }
163 }
164 if (!printCount) {
165 jack_info("None");
166 } else {
167 jack_info("\n");
168 }
169 }
170
171 int PortAudioDevices::GetInputDeviceFromName(const char* devicename, PaDeviceIndex& id, int& max_input)
172 {
173 string fullname = string(devicename);
174 PaDeviceInfo* device = GetDeviceFromFullName(fullname, id, true);
175 if (device) {
176 max_input = device->maxInputChannels;
177 } else {
178 id = Pa_GetDefaultInputDevice();
179 if (fullname.size()) {
180 jack_error("Can't open %s, PortAudio will use default input device.", devicename);
181 }
182 if (id == paNoDevice) {
183 return -1;
184 }
185 max_input = GetDeviceInfo(id)->maxInputChannels;
186 }
187 return id;
188 }
189
190 int PortAudioDevices::GetOutputDeviceFromName(const char* devicename, PaDeviceIndex& id, int& max_output)
191 {
192 string fullname = string(devicename);
193 PaDeviceInfo* device = GetDeviceFromFullName(fullname, id, false);
194 if (device) {
195 max_output = device->maxOutputChannels;
196 } else {
197 id = Pa_GetDefaultOutputDevice();
198 if (fullname.size()) {
199 jack_error("Can't open %s, PortAudio will use default output device.", devicename);
200 }
201 if (id == paNoDevice) {
202 return -1;
203 }
204 max_output = GetDeviceInfo(id)->maxOutputChannels;
205 }
206 return id;
207 }
208
209 int PortAudioDevices::GetPreferredBufferSize(PaDeviceIndex id)
210 {
211 #if defined(WIN32) && defined(HAVE_ASIO)
212 /* ASIO specific latency information */
213 if (Pa_GetHostApiInfo(fDeviceInfo[id]->hostApi)->type == paASIO) {
214 long minLatency, maxLatency, preferredLatency, granularity;
215
216 PaAsio_GetAvailableBufferSizes(id, &minLatency, &maxLatency, &preferredLatency, &granularity);
217
218 jack_info("ASIO minimum buffer size = %ld", minLatency);
219 jack_info("ASIO maximum buffer size = %ld", maxLatency);
220 jack_info("ASIO preferred buffer size = %ld", preferredLatency);
221
222 if (granularity == -1) {
223 jack_info("ASIO buffer granularity = power of 2");
224 } else {
225 jack_info("ASIO buffer granularity = %ld", granularity);
226 }
227
228 return preferredLatency;
229 } else
230 #endif
231 {
232 return 512; // Non ASIO driver, returns generic value
233 }
234 }
235
236 void PortAudioDevices::DisplayDevicesNames()
237 {
238 PaDeviceIndex id;
239 PaStreamParameters inputParameters, outputParameters;
240 jack_info("********************** Devices list, %d detected **********************", fNumDevice);
241
242 for (id = 0; id < fNumDevice; id++) {
243 jack_info("-------- device #%d ------------------------------------------------", id);
244
245 if (id == Pa_GetDefaultInputDevice()) {
246 jack_info("[ Default Input ]");
247 } else if (id == Pa_GetHostApiInfo(fDeviceInfo[id]->hostApi)->defaultInputDevice) {
248 const PaHostApiInfo *host_info = Pa_GetHostApiInfo(fDeviceInfo[id]->hostApi);
249 jack_info("[ Default %s Input ]", host_info->name);
250 }
251
252 if (id == Pa_GetDefaultOutputDevice()) {
253 jack_info("[ Default Output ]");
254 } else if (id == Pa_GetHostApiInfo(fDeviceInfo[id]->hostApi)->defaultOutputDevice) {
255 const PaHostApiInfo *host_info = Pa_GetHostApiInfo(fDeviceInfo[id]->hostApi);
256 jack_info("[ Default %s Output ]", host_info->name);
257 }
258
259 /* print device info fields */
260 jack_info("Name = %s", GetFullName(id).c_str());
261 jack_info("Max inputs = %d", fDeviceInfo[id]->maxInputChannels);
262 jack_info("Max outputs = %d", fDeviceInfo[id]->maxOutputChannels);
263
264 #if defined(WIN32) && defined(HAVE_ASIO)
265 /* ASIO specific latency information */
266 if (Pa_GetHostApiInfo(fDeviceInfo[id]->hostApi)->type == paASIO) {
267 long minLatency, maxLatency, preferredLatency, granularity;
268
269 PaAsio_GetAvailableBufferSizes(id, &minLatency, &maxLatency, &preferredLatency, &granularity);
270
271 jack_info("ASIO minimum buffer size = %ld", minLatency);
272 jack_info("ASIO maximum buffer size = %ld", maxLatency);
273 jack_info("ASIO preferred buffer size = %ld", preferredLatency);
274
275 if (granularity == -1) {
276 jack_info("ASIO buffer granularity = power of 2");
277 } else {
278 jack_info("ASIO buffer granularity = %ld", granularity);
279 }
280 }
281 #endif
282 jack_info("Default sample rate = %8.2f", fDeviceInfo[id]->defaultSampleRate);
283
284 /* poll for standard sample rates */
285 inputParameters.device = id;
286 inputParameters.channelCount = fDeviceInfo[id]->maxInputChannels;
287 inputParameters.sampleFormat = paInt16;
288 inputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */
289 inputParameters.hostApiSpecificStreamInfo = NULL;
290
291 outputParameters.device = id;
292 outputParameters.channelCount = fDeviceInfo[id]->maxOutputChannels;
293 outputParameters.sampleFormat = paInt16;
294 outputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */
295 outputParameters.hostApiSpecificStreamInfo = NULL;
296 }
297 jack_info("**************************** End of list ****************************");
298 }
299
300 bool PortAudioDevices::IsDuplex(PaDeviceIndex id)
301 {
302 //does the device has in and out facilities
303 if (fDeviceInfo[id]->maxInputChannels && fDeviceInfo[id]->maxOutputChannels) {
304 return true;
305 }
306 //else is another complementary device ? (search in devices with the same name)
307 for (PaDeviceIndex i = 0; i < fNumDevice; i++) {
308 if ((i != id) && (GetDeviceName(i) == GetDeviceName(id))) {
309 if ((fDeviceInfo[i]->maxInputChannels && fDeviceInfo[id]->maxOutputChannels)
310 || (fDeviceInfo[i]->maxOutputChannels && fDeviceInfo[id]->maxInputChannels)) {
311 return true;
312 }
313 }
314 }
315 //then the device isn't full duplex
316 return false;
317 }
Pa_Initialize
PaError Pa_Initialize(void)
Pa_GetDefaultInputDevice
PaDeviceIndex Pa_GetDefaultInputDevice(void)
Pa_GetDeviceCount
PaDeviceIndex Pa_GetDeviceCount(void)
PaDeviceInfo
Definition: portaudio.h:437
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
PaHostApiInfo::name
const char * name
Definition: portaudio.h:242
jack_info
SERVER_EXPORT void jack_info(const char *fmt,...)
Definition: JackError.cpp:100
PaHostApiInfo
Definition: portaudio.h:236
Pa_IsFormatSupported
PaError Pa_IsFormatSupported(const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate)
PaDeviceIndex
int PaDeviceIndex
Definition: portaudio.h:153
PaStreamParameters::hostApiSpecificStreamInfo
void * hostApiSpecificStreamInfo
Definition: portaudio.h:515
PaAsio_GetAvailableBufferSizes
PaError PaAsio_GetAvailableBufferSizes(PaDeviceIndex device, long *minBufferSizeFrames, long *maxBufferSizeFrames, long *preferredBufferSizeFrames, long *granularity)
PaStreamParameters::sampleFormat
PaSampleFormat sampleFormat
Definition: portaudio.h:495
PaError
int PaError
Definition: portaudio.h:63
paInt16
#define paInt16
Definition: portaudio.h:427
PaStreamParameters::suggestedLatency
PaTime suggestedLatency
Definition: portaudio.h:508
PaStreamParameters
Definition: portaudio.h:475
paNoDevice
#define paNoDevice
Definition: portaudio.h:161
Pa_GetHostApiInfo
const PaHostApiInfo * Pa_GetHostApiInfo(PaHostApiIndex hostApi)
Pa_GetHostApiCount
PaHostApiIndex Pa_GetHostApiCount(void)
Pa_GetDeviceInfo
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
Pa_GetDefaultOutputDevice
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaStreamParameters::device
PaDeviceIndex device
Definition: portaudio.h:482
paFormatIsSupported
#define paFormatIsSupported
Definition: portaudio.h:522
Pa_GetErrorText
const char * Pa_GetErrorText(PaError errorCode)
PaStreamParameters::channelCount
int channelCount
Definition: portaudio.h:489
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
Pa_Terminate
PaError Pa_Terminate(void)