summaryrefslogtreecommitdiff
path: root/map65/pa_get_device_info.c
blob: 934ddc943775fcc57ed5389dde0c054c40adbb63 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <portaudio.h>
#include <stdio.h>
#include <string.h>

#define MAX_LATENCY 20

PaStream *in_stream;
//------------------------------------------------------- pa_get_device_info
int pa_get_device_info (int  n,
                        void *pa_device_name,
                        void *pa_device_hostapi, 
			double *pa_device_max_speed,
			double *pa_device_min_speed,
			int *pa_device_max_bytes,
			int *pa_device_min_bytes,
			int *pa_device_max_channels,
			int *pa_device_min_channels )
{

  (void) n ;
  (void) pa_device_name;
  (void) pa_device_hostapi; 
  (void) pa_device_max_speed;
  (void) pa_device_min_speed;
  (void) pa_device_max_bytes;
  (void) pa_device_min_bytes;
  (void) pa_device_max_channels;
  (void) pa_device_min_channels;
  const PaDeviceInfo *deviceInfo;
  PaError pa_err;
  PaStreamParameters inputParameters;
  int i,j, speed_warning;
  int minBytes, maxBytes;
  double maxStandardSampleRate;
  double minStandardSampleRate;
  int minInputChannels;
  int maxInputChannels;

// negative terminated  list
  static double standardSampleRates[] = {8000.0, 9600.0, 
        11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0,
        44100.0, 48000.0, 88200.0, 96000.0, 192000.0, -1};
// *******************************************************


  *pa_device_max_speed=0;
  *pa_device_min_speed=0;
  *pa_device_max_bytes=0;
  *pa_device_min_bytes=0;
  *pa_device_max_channels=0;
  *pa_device_min_channels=0;
  minInputChannels=0;
  if(n >= Pa_GetDeviceCount() ) return -1;
  deviceInfo = Pa_GetDeviceInfo(n);
  if (deviceInfo->maxInputChannels==0) return -1; 
  snprintf((char*)pa_device_name,
         128,
         "%.127s",
         deviceInfo->name);

snprintf((char*)pa_device_hostapi,
         128,
         "%.127s",
         Pa_GetHostApiInfo(deviceInfo->hostApi)->name);

  speed_warning=0;

// bypass bug in Juli@ ASIO driver: 
// this driver hangs after a Pa_IsFormatSupported call 
  i = strncmp(deviceInfo->name, "ASIO 2.0 - ESI Juli@", 19);
  if (i == 0) {
    minStandardSampleRate=44100;
    maxStandardSampleRate=192000;
    minBytes=1;
    maxBytes=4;
    maxInputChannels= deviceInfo->maxInputChannels;
    minInputChannels= 1;
    goto end_pa_get_device_info;
  }

// Investigate device capabilities.
// Check min and max samplerates  with 16 bit data.
  maxStandardSampleRate=0;
  minStandardSampleRate=0;
  inputParameters.device = n;
  inputParameters.channelCount = deviceInfo->maxInputChannels;
  inputParameters.sampleFormat = paInt16;
  inputParameters.suggestedLatency = 0;
  inputParameters.hostApiSpecificStreamInfo = NULL;

// ************************************************************************
//filter for portaudio Windows hostapi's with non experts.
//only allow ASIO or WASAPI or WDM-KS
  i = strncmp(Pa_GetHostApiInfo(deviceInfo->hostApi)->name, "ASIO", 4);
  if (i==0 ) goto end_filter_hostapi;
  i = strncmp(Pa_GetHostApiInfo(deviceInfo->hostApi)->name, 
	      "Windows WASAPI", 14);
  if (i==0 ) goto end_filter_hostapi;
  i = strncmp(Pa_GetHostApiInfo(deviceInfo->hostApi)->name, 
	      "Windows WDM-KS", 14);
  if (i==0 ) goto end_filter_hostapi;
  speed_warning=1;
end_filter_hostapi:;

// ************************************************************************
  i=0;
  while(standardSampleRates[i] > 0 && minStandardSampleRate==0) {
    pa_err=Pa_IsFormatSupported(&inputParameters, NULL, 
				standardSampleRates[i] );
    if(pa_err == paDeviceUnavailable) return -1;
    if(pa_err == paInvalidDevice) return -1;
    if(pa_err == paFormatIsSupported ) { 
      minStandardSampleRate=standardSampleRates[i];
    }
    i++;
  }
  if(minStandardSampleRate == 0) return -1;
  j=i;
  while(standardSampleRates[i] > 0 ) i++;
  i--;
  while(i >= j && maxStandardSampleRate==0) {
      pa_err=Pa_IsFormatSupported(&inputParameters, NULL, 
				  standardSampleRates[i] );
    if(pa_err == paDeviceUnavailable) return -1;
    if(pa_err == paInvalidDevice) return -1;
    if( pa_err == paFormatIsSupported ) { 
      maxStandardSampleRate=standardSampleRates[i];
    }
    i--;
  }

// check if min SampleRate  = max SampleRate 
  if(maxStandardSampleRate==0 && (minStandardSampleRate != 0)) {
    maxStandardSampleRate= minStandardSampleRate;
  }

// check min and max bytes
  minBytes=2;
  maxBytes=2;
  inputParameters.sampleFormat = paUInt8;
  pa_err=Pa_IsFormatSupported(&inputParameters, NULL, 
				maxStandardSampleRate );
  if( pa_err == paFormatIsSupported ) { 
    minBytes=1;
  }
    inputParameters.sampleFormat = paInt32;
    pa_err=Pa_IsFormatSupported(&inputParameters, NULL, 
				maxStandardSampleRate );
  if( pa_err == paFormatIsSupported ) { 
    maxBytes=4;
  }

// check min channel count
  maxInputChannels= deviceInfo->maxInputChannels;
  inputParameters.channelCount = 1;
  inputParameters.sampleFormat = paInt16;
  pa_err=paFormatIsSupported+32000;
  while(pa_err != paFormatIsSupported &&
	  ( inputParameters.channelCount < (maxInputChannels+1)) ) {
    pa_err=Pa_IsFormatSupported(&inputParameters, NULL, 
				maxStandardSampleRate );
    inputParameters.channelCount++;
  }
  if( pa_err == paFormatIsSupported ) {
    minInputChannels=inputParameters.channelCount-1;
  } else {
    return -1;
  }

end_pa_get_device_info:;

  *pa_device_max_speed=maxStandardSampleRate;
  *pa_device_min_speed=minStandardSampleRate;
  *pa_device_max_bytes=maxBytes;
  *pa_device_min_bytes=minBytes;
  *pa_device_max_channels= maxInputChannels;
  *pa_device_min_channels= minInputChannels;

  return speed_warning;
}