From d4ed26e49ac57d9fd41cad56c632c27e7006b3ff Mon Sep 17 00:00:00 2001 From: Ross Bencina Date: Sat, 3 Sep 2016 16:50:27 +1000 Subject: [PATCH] add Pa_GetAvailableHostApisCount(), fix test to use it. reshuffle code --- include/portaudio.h | 64 ++++++++++++++++++++++------------- src/common/pa_front.c | 50 ++++++++++++++++----------- test/patest_select_hostapis.c | 23 +++++++++---- 3 files changed, 89 insertions(+), 48 deletions(-) diff --git a/include/portaudio.h b/include/portaudio.h index 311d0b0..e05bece 100644 --- a/include/portaudio.h +++ b/include/portaudio.h @@ -275,6 +275,46 @@ typedef enum PaHostApiTypeId } PaHostApiTypeId; +/** Returns the number of compiled-in host APIs in this build of PortAudio. + + The returned value is large enough that it can be used to dimension + arrays passed to Pa_GetAvailableHostApis and Pa_GetSelectedHostApis. + + FIXME REVIEW The "Available" name should match whatever is chosen for + Pa_GetAvailableHostApis + + @see Pa_GetAvailableHostApis, Pa_GetSelectedHostApis +*/ +int Pa_GetAvailableHostApisCount( void ); + + +/** Returns the type ids of all compiled-in host APIs in initialization order. + + Note that the compiled-in host APIs are not necessarily those that are + installed on the system. It is possible for compiled-in dynamically loaded + host APIs to be not installed on the system, and it is possible for + installed audio APIs to not be supported (compiled in to) PortAudio. + + @param hostApiTypes (IN/OUT) An array that will be filled with + host API identifiers belonging to the PaHostApiTypeId enumeration. + + @param count (OUT) The number of host APIs, returned even on error. + + @param countAvailable The number of available elements in the hostApiTypes array. + + FIXME REVIEW: Consider a different name for this function, both "available" + and "supported" are ambiguous between what is available/supported on the + target platform and what is compiled into PA. Keep in mind that + Pa_IsFormatSupported refers to formats supported by a device. + Proposals: + GetConfiguredHostApis, GetCompiledHostApis + NOTE: also fix name oPa_GetAvailableHostApisCount + + @see Pa_SelectHostApis +*/ +PaError Pa_GetAvailableHostApis( PaHostApiTypeId *hostApiTypes, int countAvailable, int *count ); + + /** Select host APIs and their initialization order. This function may only be called prior to calling Pa_Initialize() @@ -303,6 +343,7 @@ typedef enum PaHostApiTypeId */ PaError Pa_SelectHostApis( const PaHostApiTypeId *hostApiTypes, int count ); + /** Returns the type ids of the selected host APIs in initialization order. @param hostApiTypes (IN/OUT) An array that will be filled with @@ -322,29 +363,6 @@ PaError Pa_SelectHostApis( const PaHostApiTypeId *hostApiTypes, int count ); */ PaError Pa_GetSelectedHostApis( PaHostApiTypeId *hostApiTypes, int countAvailable, int *count ); -/** Returns the type ids of all compiled-in host APIs in initialization order. - - Note that the compiled-in host APIs are not necessarily those that are - installed on the target system. - - @param hostApiTypes (IN/OUT) An array that will be filled with - host API identifiers belonging to the PaHostApiTypeId enumeration. - - @param count (OUT) The number of host APIs, returned even on error. - - @param countAvailable The number of available elements in the hostApiTypes array. - - FIXME REVIEW: Consider a different name for this function, both "available" - and "supported" are ambiguous between what is available/supported on the - target platform and what is compiled into PA. Keep in mind that - Pa_IsFormatSupported refers to formats supported by a device. - Proposals: - GetConfiguredHostApis, GetCompiledHostApis - - @see Pa_SelectHostApis -*/ -PaError Pa_GetAvailableHostApis( PaHostApiTypeId *hostApiTypes, int countAvailable, int *count ); - /** A structure containing information about a particular host API. */ diff --git a/src/common/pa_front.c b/src/common/pa_front.c index de74457..ebc9c10 100644 --- a/src/common/pa_front.c +++ b/src/common/pa_front.c @@ -181,6 +181,7 @@ static int CountHostApiInitializers( void ) return result; } + static const PaUtilHostApiInitializerEntry* FindHostApiInitializerEntry( PaHostApiTypeId hostApiType ) { int i = 0; @@ -195,6 +196,33 @@ static const PaUtilHostApiInitializerEntry* FindHostApiInitializerEntry( PaHostA return NULL; } + +int Pa_GetAvailableHostApisCount( void ) +{ + return CountHostApiInitializers(); +} + + +PaError Pa_GetAvailableHostApis( PaHostApiTypeId *hostApiTypes, int countAvailable, int *count ) +{ + int i; + int initializerCount = CountHostApiInitializers(); + + *count = initializerCount; + if( countAvailable >= initializerCount ) + { + for( i=0; i < initializerCount; ++i ) + hostApiTypes[i] = paHostApiInitializers[i].hostApiType; + } + else + { + return paInsufficientMemory; + } + + return paNoError; +} + + static int CountSelectedHostApis() { if( selectedHostApiTypes_ == NULL ) @@ -203,6 +231,7 @@ static int CountSelectedHostApis() return selectedHostApiCount_; } + static const PaUtilHostApiInitializerEntry* GetSelectedHostApi( int index ) { if( selectedHostApiTypes_ == NULL ) @@ -211,6 +240,7 @@ static const PaUtilHostApiInitializerEntry* GetSelectedHostApi( int index ) return FindHostApiInitializerEntry( selectedHostApiTypes_[index] ); } + PaError Pa_SelectHostApis( const PaHostApiTypeId *hostApiTypes, int count ) { int i, j; @@ -280,6 +310,7 @@ PaError Pa_SelectHostApis( const PaHostApiTypeId *hostApiTypes, int count ) return paNoError; } + PaError Pa_GetSelectedHostApis( PaHostApiTypeId *hostApiTypes, int countAvailable, int *count ) { if( selectedHostApiTypes_ == NULL ) @@ -301,25 +332,6 @@ PaError Pa_GetSelectedHostApis( PaHostApiTypeId *hostApiTypes, int countAvailabl } } -PaError Pa_GetAvailableHostApis( PaHostApiTypeId *hostApiTypes, int countAvailable, int *count ) -{ - int i; - int initializerCount = CountHostApiInitializers(); - - *count = initializerCount; - if( countAvailable >= initializerCount ) - { - for( i=0; i < initializerCount; ++i ) - hostApiTypes[i] = paHostApiInitializers[i].hostApiType; - } - else - { - return paInsufficientMemory; - } - - return paNoError; -} - static void TerminateHostApis( void ) { diff --git a/test/patest_select_hostapis.c b/test/patest_select_hostapis.c index a50ef60..ceacf48 100644 --- a/test/patest_select_hostapis.c +++ b/test/patest_select_hostapis.c @@ -42,6 +42,8 @@ */ #include #include +#include + #include "portaudio.h" /*******************************************************************/ @@ -50,13 +52,19 @@ int main(void) { int i, j, k; int availableHostApiCount, scratchHostApiCount; -#define MAX_HOST_API_COUNT 100 - PaHostApiTypeId availableHostApis[MAX_HOST_API_COUNT]; - PaHostApiTypeId scratchHostApis[MAX_HOST_API_COUNT]; + int maxHostApiCount; + PaHostApiTypeId *availableHostApis; + PaHostApiTypeId *scratchHostApis; PaError err; + maxHostApiCount = Pa_GetAvailableHostApisCount(); + availableHostApis = (PaHostApiTypeId*)malloc( sizeof(PaHostApiTypeId) * maxHostApiCount ); + assert( availableHostApis != NULL ); + scratchHostApis = (PaHostApiTypeId*)malloc( sizeof(PaHostApiTypeId) * maxHostApiCount ); + assert( scratchHostApis != NULL ); + availableHostApiCount = 0; - err = Pa_GetAvailableHostApis( availableHostApis, MAX_HOST_API_COUNT, &availableHostApiCount ); + err = Pa_GetAvailableHostApis( availableHostApis, maxHostApiCount, &availableHostApiCount ); assert( err == paNoError ); assert( availableHostApiCount > 0 ); @@ -83,7 +91,7 @@ int main(void) assert( err == paNoError ); /* read back and verify selected apis*/ - err = Pa_GetSelectedHostApis( scratchHostApis, MAX_HOST_API_COUNT, &scratchHostApiCount ); + err = Pa_GetSelectedHostApis( scratchHostApis, maxHostApiCount, &scratchHostApiCount ); assert( err == paNoError ); assert( scratchHostApiCount == 1 ); assert( scratchHostApis[0] == hostApiType ); @@ -112,7 +120,7 @@ int main(void) assert( err == paNoError ); /* read back and verify selected apis*/ - err = Pa_GetSelectedHostApis( scratchHostApis, MAX_HOST_API_COUNT, &scratchHostApiCount ); + err = Pa_GetSelectedHostApis( scratchHostApis, maxHostApiCount, &scratchHostApiCount ); assert( err == paNoError ); assert( scratchHostApiCount == i ); for( j = 0; j < i; ++j ) @@ -145,4 +153,7 @@ int main(void) Pa_Terminate(); } + + free(availableHostApis); + free(scratchHostApis); } -- 2.43.0