Use short lower-case names as API identifiers
authorJP Cimalando <jpcima@users.noreply.github.com>
Tue, 7 Aug 2018 22:36:22 +0000 (00:36 +0200)
committerJP Cimalando <jpcima@users.noreply.github.com>
Tue, 7 Aug 2018 22:43:31 +0000 (00:43 +0200)
RtAudio.cpp
RtAudio.h
rtaudio_c.cpp
rtaudio_c.h
tests/apinames.cpp

index db3b7cb70f615b7b110a326f42742120e62ad31c..82c78bd05ceaf0de628446c473ee4e24198aa0c6 100644 (file)
@@ -45,7 +45,6 @@
 #include <cstdlib>
 #include <cstring>
 #include <climits>
-#include <cctype>
 #include <cmath>
 #include <algorithm>
 
@@ -136,6 +135,66 @@ void RtAudio :: getCompiledApi( std::vector<RtAudio::Api> &apis )
 
 const std::string &RtAudio :: getCompiledApiName( RtAudio::Api api )
 {
+#if defined(__UNIX_JACK__)
+  if ( api == UNIX_JACK ) {
+    static std::string name( "jack" );
+    return name;
+  }
+#endif
+#if defined(__LINUX_PULSE__)
+  if ( api == LINUX_PULSE ) {
+    static std::string name( "pulse" );
+    return name;
+  }
+#endif
+#if defined(__LINUX_ALSA__)
+  if ( api == LINUX_ALSA ) {
+    static std::string name( "alsa" );
+    return name;
+  }
+#endif
+#if defined(__LINUX_OSS__)
+  if ( api == LINUX_OSS ) {
+    static std::string name( "oss" );
+    return name;
+  }
+#endif
+#if defined(__WINDOWS_ASIO__)
+  if ( api == WINDOWS_ASIO ) {
+    static std::string name( "asio" );
+    return name;
+  }
+#endif
+#if defined(__WINDOWS_WASAPI__)
+  if ( api == WINDOWS_WASAPI ) {
+    static std::string name( "wasapi" );
+    return name;
+  }
+#endif
+#if defined(__WINDOWS_DS__)
+  if ( api == WINDOWS_DS ) {
+    static std::string name( "ds" );
+    return name;
+  }
+#endif
+#if defined(__MACOSX_CORE__)
+  if ( api == MACOSX_CORE ) {
+    static std::string name( "core" );
+    return name;
+  }
+#endif
+#if defined(__RTAUDIO_DUMMY__)
+  if ( api == RTAUDIO_DUMMY ) {
+    static std::string name( "dummy" );
+    return name;
+  }
+#endif
+  static std::string name;
+  return name;
+}
+
+const std::string &RtAudio :: getCompiledApiDisplayName( RtAudio::Api api )
+{
 #if defined(__UNIX_JACK__)
   if ( api == UNIX_JACK ) {
     static std::string name( "JACK" );
@@ -180,13 +239,13 @@ const std::string &RtAudio :: getCompiledApiName( RtAudio::Api api )
 #endif
 #if defined(__MACOSX_CORE__)
   if ( api == MACOSX_CORE ) {
-    static std::string name( "CoreAudio" );
+    static std::string name( "Core Audio" );
     return name;
   }
 #endif
 #if defined(__RTAUDIO_DUMMY__)
   if ( api == RTAUDIO_DUMMY ) {
-    static std::string name( "Dummy" );
+    static std::string name( "RtAudio Dummy" );
     return name;
   }
 #endif
@@ -206,18 +265,13 @@ RtAudio::Api RtAudio :: getCompiledApiByName( const std::string &name )
     const std::string &otherName =
       getCompiledApiName((RtAudio::Api)api_number);
 
-    bool equal = nameLength == otherName.size();
-    for ( size_t i = 0; equal && i < nameLength; ++i )
-      equal = tolower((unsigned char)name[i]) ==
-        tolower((unsigned char)otherName[i]);
-
-    if ( equal )
+    if ( name == otherName )
       return (RtAudio::Api)api_number;
 
     ++api_number;
   }
 
-    return RtAudio::UNSPECIFIED;
+  return RtAudio::UNSPECIFIED;
 }
 
 void RtAudio :: openRtApi( RtAudio::Api api )
index 91124cf104e85c3a5e8b9e885d233f30f2210fcd..449e0d7dcc9323b47b955ebc7c23a8b2187f4c45 100644 (file)
--- a/RtAudio.h
+++ b/RtAudio.h
@@ -399,11 +399,21 @@ class RTAUDIO_DLL_PUBLIC RtAudio
 
   //! Return the name of a specified compiled audio API.
   /*!
+    This obtains a short lower-case name used for identification purposes.
+    This value is guaranteed to remain identical across library versions.
     If the API is unknown or not compiled, this function will return
     the empty string.
   */
   static const std::string &getCompiledApiName( RtAudio::Api api );
 
+  //! Return the display name of a specified compiled audio API.
+  /*!
+    This obtains a long name used for display purposes.
+    If the API is unknown or not compiled, this function will return
+    the empty string.
+  */
+  static const std::string &getCompiledApiDisplayName( RtAudio::Api api );
+
   //! Return the compiled audio API having the given name.
   /*!
     A case insensitive comparison will check the specified name
index f831655470c9842eed646fc7fcb518c7d0eb80d9..ec849414a4cdd6e860dccea76daa237018ac238c 100644 (file)
@@ -55,6 +55,12 @@ const char *rtaudio_compiled_api_name(rtaudio_api_t api) {
     return name.empty() ? NULL : name.c_str();
 }
 
+const char *rtaudio_compiled_api_display_name(rtaudio_api_t api)
+{
+    const std::string &name = RtAudio::getCompiledApiDisplayName((RtAudio::Api)api);
+    return name.empty() ? NULL : name.c_str();
+}
+
 rtaudio_api_t rtaudio_compiled_api_by_name(const char *name) {
     RtAudio::Api api = RtAudio::UNSPECIFIED;
     if (name) {
index 589994aacc3e9fe9ef9f2c70b3fdf606ab394d27..893917cdb3a2fba7accb98d84108ac42c9371afc 100644 (file)
@@ -103,6 +103,7 @@ typedef struct rtaudio *rtaudio_t;
 RTAUDIOAPI const char *rtaudio_version(void);
 RTAUDIOAPI const rtaudio_api_t *rtaudio_compiled_api(void);
 RTAUDIOAPI const char *rtaudio_compiled_api_name(rtaudio_api_t api);
+RTAUDIOAPI const char *rtaudio_compiled_api_display_name(rtaudio_api_t api);
 RTAUDIOAPI rtaudio_api_t rtaudio_compiled_api_by_name(const char *name);
 
 RTAUDIOAPI const char *rtaudio_error(rtaudio_t audio);
index b915ad068be4f26ba8da69e54c47c1da024c5062..db7a2582a5f19d84412bb14644757d34328e7072 100644 (file)
@@ -10,8 +10,8 @@
 /******************************************/
 
 #include "RtAudio.h"
-#include <cstdlib>
 #include <cctype>
+#include <cstdlib>
 #include <iostream>
 
 int main() {
@@ -26,7 +26,12 @@ int main() {
             std::cerr << "Invalid name for API " << (int)apis[i] << "\n";
             exit(1);
         }
-        std::cout << "* " << (int)apis[i] << ": '" << name << "'\n";
+        const std::string &displayName = RtAudio::getCompiledApiDisplayName(apis[i]);
+        if (displayName.empty()) {
+            std::cerr << "Invalid display name for API " << (int)apis[i] << "\n";
+            exit(1);
+        }
+        std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n";
     }
 
     // ensure unknown APIs return the empty string
@@ -36,9 +41,14 @@ int main() {
             std::cerr << "Bad string for invalid API\n";
             exit(1);
         }
+        const std::string &displayName = RtAudio::getCompiledApiDisplayName((RtAudio::Api)-1);
+        if (!displayName.empty()) {
+            std::cerr << "Bad display string for invalid API\n";
+            exit(1);
+        }
     }
 
-    // try getting API identifier by case-insensitive name
+    // try getting API identifier by name
     std::cout << "API identifiers by name:\n";
     for ( size_t i = 0; i < apis.size() ; ++i ) {
         std::string name = RtAudio::getCompiledApiName(apis[i]);
@@ -47,23 +57,19 @@ int main() {
             exit( 1 );
         }
         std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
+
         for ( size_t j = 0; j < name.size(); ++j )
             name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]);
-        if ( RtAudio::getCompiledApiByName(name) != apis[i] ) {
-            std::cerr << "Bad identifier for API '" << name << "'\n";
+        RtAudio::Api api = RtAudio::getCompiledApiByName(name);
+        if ( api != RtAudio::UNSPECIFIED ) {
+            std::cerr << "Identifier " << (int)api << " for invalid API '" << name << "'\n";
             exit( 1 );
         }
-        std::cout << "* '" << name << "': " << (int)apis[i] << "\n";
     }
 
     // try getting an API identifier by unknown name
     {
         RtAudio::Api api;
-        api = RtAudio::getCompiledApiByName("ALSO");
-        if ( api != RtAudio::UNSPECIFIED ) {
-            std::cerr << "Bad identifier for unknown API name\n";
-            exit( 1 );
-        }
         api = RtAudio::getCompiledApiByName("");
         if ( api != RtAudio::UNSPECIFIED ) {
             std::cerr << "Bad identifier for unknown API name\n";