aboutsummaryrefslogtreecommitdiff
path: root/pd/portaudio_v18/docs/pa_drivermodel.c.txt
blob: 956f664c68c8e49745849ab4ad51d1d7230c5f2b (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
	This file contains the host-neutral code for implementing multiple driver model 
	support in PortAudio.

	It has not been compiled, but it is supplied only for example purposes at this stage.

	TODO: use of CHECK_DRIVER_MODEL is bogus in some instances since some 
		of those functions don't return a PaError


*/

#include "pa_drivermodel.h.txt"


#ifndef PA_MULTIDRIVER 
/* single driver support, most functions will stay in the implementation files */

PaDriverModelID Pa_CountDriverModels()
{
	return 1;
}

/*
Perhaps all drivers should define this with this signature
const PaDriverModelInfo* Pa_GetDriverModelInfo( PaDriverModelID driverModelID )
{
}
*/

PaDeviceID Pa_DriverModelDefaultInputDeviceID( PaDriverModelID driverModelID )
{
	return Pa_GetDefaultInputDeviceID();
}


PaDeviceID Pa_DriverModelDefaultOutputDeviceID( PaDriverModelID driverModelID )
{
	return Pa_GetDefaultInputDeviceID();
}

/*
Perhaps all drivers should define with this signature
int Pa_DriverModelMinNumBuffers( PaDriverModelID driverModelID, int framesPerBuffer, double sampleRate )
{

}
*/

int Pa_DriverModelCountDevices( PaDriverModelID driverModelID )
{
	return Pa_CountDevices();
}

PaDeviceID Pa_DriverModelGetDeviceID(PaDriverModelID driverModelID, int perDriverModelIndex )
{
	return perDriverModelIndex;
}


#else
/* multidriver support */


typedef PaError (*PaInitializeFunPtr)( PaDriverModelImplementation** );

/*
	the initializers array is a static table of function pointers
	to all the available driverModels on the current platform.

	the order of pointers in the array is important. the first
	pointer is always considered to be the "default" driver model.
*/

static PaInitializeFunPtr driverModelInitializers[] = {
#ifdef WINDOWS
	PaWin32WMME_MultiDriverInitialize,
	PaWin32DS_MultiDriverInitialize,
	PaASIO_MultiDriverInitialize
#endif
#ifdef MAC
	PaMacSM_MultiDriverInitialize,
	PaMacCA_MultiDriverInitialize,
	PaASIO_MultiDriverInitialize
#endif
/* other platforms here */
	(PaInitializeFunPtr*)0 /* NULL terminate the list */
}; 


/*
	the driverModels array is a dynamically created table of
	currently available driverModels.
*/
static PaDriverModelImplementation* driverModels = 0;
static int numDriverModels = 0;


#define PA_CHECK_INITIALIZED\
	if( driverModels == 0 )
		return paLibraryNotInitialised

#define PA_CHECK_DRIVER_MODEL_ID( id )
	if( id < 0 || id >= numDriverModels )
		return paBadDriverModelID;


/*
	ConvertPublicDeviceIdToImplementationDeviceId converts deviceId
	from a public device id, to a device id for a particular 
	PortAudio implementation. On return impl will either point
	to a valid implementation or will be NULL.
*/
static void ConvertPublicDeviceIDToImplementationDeviceID( 
	PaDriverModelImplementation *impl, PaDeviceID deviceID )
{
	int i, count;

	impl = NULL;

	for( i=0; i < numDriverModels; ++i ){
		count = driverModels[i]->countDevices();
		if( deviceID < count ){
			impl = driverModels[i];
			return NULL;			
		}else{
			deviceID -= count;
		}
	}
}

static PaDeviceID ConvertImplementationDeviceIDToPublicDeviceID( 
		PaDriverModelID driverModelID, PaDeviceID deviceID )
{
	int i;

	for( i=0; i < driverModelID; ++i )
		deviceID += driverModels[i]->countDevices();
}


PaError Pa_Initialize( void )
{
	PaError result = paNoError;
	int i, initializerCount;
	PaDriverModelImplementation *impl;

	if( driverModels != 0 )
		return paAlreadyInitialized;

	/* count the number of driverModels */
	initializerCount=0;
	while( driverModelInitializers[initializerCount] != 0 ){
		++initializerCount;
	}

	driverModels = malloc( sizeof(PaDriverModelImplementation*) * initializerCount );
	if( driverModels == NULL )
		return paInsufficientMemory;

	numDriverModels = 0;
	for( i=0; i<initializerCount; ++i ){
		result = (*driverModelInitializers[i])( &impl );
		if( result == paNoError ){
			driverModels[numDriverModels] = impl;
			++numDriverModels;
		}else{
			// TODO: terminate the drivers which have already been initialized.
		}
	}

	return result;
}



PaError Pa_Terminate( void )
{
	int i;

	PA_CHECK_INITIALIZED;

	/*
		rather than require each implementation to do it separately we could
		keep track of all open streams and close them here
	*/

	for( i=0; i<numDriverModels; ++i )
		driverModels[i]->terminate( driverModels[i] );
}


long Pa_GetHostError( void )
{
	PA_CHECK_INITIALIZED;
	
	under construction. depends on error text proposal.
}


const char *Pa_GetErrorText( PaError errnum )
{
	PA_CHECK_INITIALIZED;

	under construction. may need to call driver model specific code 
	depending on how the error text proposal pans out.
}



int Pa_CountDevices()
{
	int i, result;

	PA_CHECK_INITIALIZED;

	result = 0;
	for( i=0; i < numDriverModels; ++i )
		result += driverModels[i]->countDevices();
	
	return result;
}


PaDeviceID Pa_GetDefaultInputDeviceID( void )
{
	PA_CHECK_INITIALIZED;

	return driverModels[0]->getDefaultInputDeviceID();
}

PaDeviceID Pa_GetDefaultOutputDeviceID( void )
{
	PA_CHECK_INITIALIZED;

	return driverModels[0]->getDefaultInputDeviceID();
}


const PaDeviceInfo* Pa_GetDeviceInfo( PaDeviceID deviceID )
{
	PaDriverModelImplementation *impl;	

	PA_CHECK_INITIALIZED;

	ConvertPublicDeviceIDToImplementationDeviceID( impl, deviceID );
	if( impl == NULL )
		return paInvalidDeviceID;

	return impl->getDeviceInfo( deviceID );
}

/* NEW MULTIPLE DRIVER MODEL FUNCTIONS ---------------------------------- */

PaDriverModelID Pa_CountDriverModels()
{
	PA_CHECK_INITIALIZED;

	return numDriverModels;
}


const PaDriverModelInfo* Pa_GetDriverModelInfo( PaDriverModelID driverModelID )
{
	PA_CHECK_INITIALIZED;
	PA_CHECK_DRIVER_MODEL_ID( driverModelID );

	return driverModels[ driverModelID ]->getDriverModelInfo();
}


PaDeviceID Pa_DriverModelDefaultInputDeviceID( PaDriverModelID driverModelID )
{
	PA_CHECK_INITIALIZED;
	PA_CHECK_DRIVER_MODEL_ID( driverModelID );

	return ConvertImplementationDeviceIDToPublicDeviceID( driverModelID,
			driverModels[ driverModelID ]->getDefaultInputDeviceID();
}


PaDeviceID Pa_DriverModelDefaultOutputDeviceID( PaDriverModelID driverModelID )
{
	PA_CHECK_INITIALIZED;
	PA_CHECK_DRIVER_MODEL_ID( driverModelID );
	
	return ConvertImplementationDeviceIDToPublicDeviceID( driverModelID,
			driverModels[ driverModelID ]->getDefaultOutputDeviceID();
}


int Pa_DriverModelMinNumBuffers( PaDriverModelID driverModelID, int framesPerBuffer, double sampleRate )
{
	PA_CHECK_INITIALIZED;
	PA_CHECK_DRIVER_MODEL_ID( driverModelID );

	return driverModels[ driverModelID ]->getMinNumBuffers( int framesPerBuffer, double sampleRate );
}


int Pa_DriverModelCountDevices( PaDriverModelID driverModelID )
{
	PA_CHECK_INITIALIZED;
	PA_CHECK_DRIVER_MODEL_ID( driverModelID );

	return driverModels[ driverModelID ]->coundDevices();
}

PaDeviceID Pa_DriverModelGetDeviceID(PaDriverModelID driverModelID, int perDriverModelIndex )
{
	PA_CHECK_INITIALIZED;
	PA_CHECK_DRIVER_MODEL_ID( driverModelID );

	return ConvertImplementationDeviceIDToPublicDeviceID( driverModelID, perDriverModelIndex );
}

/* END NEW MULTIPLE DRIVER MODEL FUNCTIONS ------------------------------ */


PaError Pa_OpenStream( PortAudioStream** stream,
				PaDeviceID inputDevice,
				int numInputChannels,
				PaSampleFormat inputSampleFormat,
				void *inputDriverInfo,
				PaDeviceID outputDevice,
				int numOutputChannels,
				PaSampleFormat outputSampleFormat,
				void *outputDriverInfo,
				double sampleRate,
				unsigned long framesPerBuffer,
				unsigned long numberOfBuffers,
				PaStreamFlags streamFlags,
				PortAudioCallback *callback,
				void *userData )
{
	PaError result;
	PaDriverModelImplementation *inputImpl, *outputImpl, impl;

	PA_CHECK_INITIALIZED;
	
	if( inputDevice != paNoDevice ){
		ConvertPublicDeviceIDToImplementationDeviceID( inputImpl, inputDevice );
		if( inputImpl == NULL )
			return paInvalidDeviceID;
		else
			impl = inputImpl;	
	}

	if( outputDevice != paNoDevice ){
		ConvertPublicDeviceIDToImplementationDeviceID( outputImpl, outputDevice );
		if( outputImpl == NULL )
			return paInvalidDeviceID;
		else
			impl = outputImpl;
	}

	if( inputDevice != paNoDevice && outputDevice != paNoDevice ){	
		if( inputImpl != outputImpl )
			return paDevicesMustBelongToTheSameDriverModel;
	}

	
	result = impl->openStream( stream, inputDevice, numInputChannels, inputSampleFormat, inputDriverInfo,
				outputDevice, numOutputChannels, outputSampleFormat, outputDriverInfo,
				sampleRate, framesPerBuffer, numberOfBuffers, streamFlags, callback, userData );


	if( result == paNoError )
		((PaStreamImplementation*)stream)->magic = PA_STREAM_MAGIC;
	
	return result;
}


PaError Pa_OpenDefaultStream( PortAudioStream** stream,
				int numInputChannels,
				int numOutputChannels,
				PaSampleFormat sampleFormat,
				double sampleRate,
				unsigned long framesPerBuffer,
				unsigned long numberOfBuffers,
				PortAudioCallback *callback,
				void *userData )
{
	PaError result;
	int inputDevice = driverModels[0]->getDefaultInputDeviceID;
	int outputDevice = driverModels[0]->getDefaultOutputDeviceID;		
		
	result = driverModels[0]->openStream( stream, inputDevice, numInputChannels, sampleFormat, 0, 
								outputDevice, numOutputChannels, sampleFormat, 0, 
								sampleRate, framesPerBuffer, numberOfBuffers,
								streamFlags, callback, userData );

	if( result == paNoError )
		((PaStreamImplementation*)stream)->magic = PA_STREAM_MAGIC;
	
	return result;
}


PaError Pa_CloseStream( PortAudioStream* stream )
{
	PA_CHECK_INITIALIZED;

	PaError result = ((PaStreamImplementation*)stream)->close();

	if( result == PaNoError )
		((PaStreamImplementation*)stream)->magic = 0; /* clear magic number */

	return result;
}


PaError Pa_StartStream( PortAudioStream *stream );
{
	PA_CHECK_INITIALIZED;

	return ((PaStreamImplementation*)stream)->start();
}


PaError Pa_StopStream( PortAudioStream *stream );
{
	PA_CHECK_INITIALIZED;

	return ((PaStreamImplementation*)stream)->stop();
}


PaError Pa_AbortStream( PortAudioStream *stream );
{
	PA_CHECK_INITIALIZED;

	return ((PaStreamImplementation*)stream)->abort();
}


PaError Pa_StreamActive( PortAudioStream *stream )
{
	PA_CHECK_INITIALIZED;

	return ((PaStreamImplementation*)stream)->active();
}

PaTimestamp Pa_StreamTime( PortAudioStream *stream )
{
	PA_CHECK_INITIALIZED;

	return ((PaStreamImplementation*)stream)->time();
}


double Pa_StreamCPULoad( PortAudioStream* stream )
{
	PA_CHECK_INITIALIZED;

	return ((PaStreamImplementation*)stream)->cpuLoad();
}



int Pa_GetMinNumBuffers( PaDeviceID deviceID, int framesPerBuffer, double sampleRate )
{	
	PaDriverModelImplementation *impl;
	PA_CHECK_INITIALIZED;

	ConvertPublicDeviceIDToImplementationDeviceID( impl, deviceID );
	if( impl == NULL )
		return paInvalidDeviceID;

	return impl->getMinNumBuffers( framesPerBuffer, sampleRate );	
}


void Pa_Sleep( long msec )
{
	same as existing implementaion
}


PaError Pa_GetSampleSize( PaSampleFormat format )
{
	same as existing implementation
}

#endif /* PA_MULTIDRIVER */