reference return for API names
[rtaudio-cdist.git] / RtAudio.cpp
index db3b7cb70f615b7b110a326f42742120e62ad31c..07a63144d375a745ff4dfc35e75ffd2e935c4bb7 100644 (file)
@@ -45,7 +45,6 @@
 #include <cstdlib>
 #include <cstring>
 #include <climits>
-#include <cctype>
 #include <cmath>
 #include <algorithm>
 
@@ -99,125 +98,94 @@ std::string RtAudio :: getVersion( void )
   return RTAUDIO_VERSION;
 }
 
-void RtAudio :: getCompiledApi( std::vector<RtAudio::Api> &apis )
+// Define API names.
+// TODO: replace with initializer list in C++11.
+// The order here will control the order of RtAudio's API search in
+// the constructor.
+// Have to maintain a separate list of API enum identifiers since map
+// doesn't preserve insertion order.
+static std::pair< RtAudio::ApiNameMap, std::vector<RtAudio::Api> > init_ApiNames()
 {
-  apis.clear();
-
-  // The order here will control the order of RtAudio's API search in
-  // the constructor.
+  RtAudio::ApiNameMap names;
+  std::vector<RtAudio::Api> apis;
 #if defined(__UNIX_JACK__)
-  apis.push_back( UNIX_JACK );
+  names["jack"] = std::pair<RtAudio::Api, std::string>(RtAudio::UNIX_JACK, "Jack");
+  apis.push_back(RtAudio::UNIX_JACK);
 #endif
 #if defined(__LINUX_PULSE__)
-  apis.push_back( LINUX_PULSE );
+  names["pulse"] = std::pair<RtAudio::Api, std::string>(RtAudio::LINUX_PULSE, "Pulse");
+  apis.push_back(RtAudio::LINUX_PULSE);
 #endif
 #if defined(__LINUX_ALSA__)
-  apis.push_back( LINUX_ALSA );
+  names["alsa"] = std::pair<RtAudio::Api, std::string>(RtAudio::LINUX_ALSA, "ALSA");
+  apis.push_back(RtAudio::LINUX_ALSA);
 #endif
 #if defined(__LINUX_OSS__)
-  apis.push_back( LINUX_OSS );
+  names["oss"] = std::pair<RtAudio::Api, std::string>(RtAudio::LINUX_OSS, "OSS");
+  apis.push_back(RtAudio::LINUX_OSS);
 #endif
 #if defined(__WINDOWS_ASIO__)
-  apis.push_back( WINDOWS_ASIO );
+  names["asio"] = std::pair<RtAudio::Api, std::string>(RtAudio::WINDOWS_ASIO, "ASIO");
+  apis.push_back(RtAudio::WINDOWS_ASIO);
 #endif
 #if defined(__WINDOWS_WASAPI__)
-  apis.push_back( WINDOWS_WASAPI );
+  names["wasapi"] = std::pair<RtAudio::Api, std::string>(RtAudio::WINDOWS_WASAPI, "WASAPI");
+  apis.push_back(RtAudio::WINDOWS_WASAPI);
 #endif
 #if defined(__WINDOWS_DS__)
-  apis.push_back( WINDOWS_DS );
+  names["ds"] = std::pair<RtAudio::Api, std::string>(RtAudio::WINDOWS_DS, "DirectSound");
+  apis.push_back(RtAudio::WINDOWS_DS);
 #endif
 #if defined(__MACOSX_CORE__)
-  apis.push_back( MACOSX_CORE );
+  names["core"] = std::pair<RtAudio::Api, std::string>(RtAudio::MACOSX_CORE, "CoreAudio");
+  apis.push_back(RtAudio::MACOSX_CORE);
 #endif
 #if defined(__RTAUDIO_DUMMY__)
-  apis.push_back( RTAUDIO_DUMMY );
+  names["dummy"] = std::pair<RtAudio::Api, std::string>(RtAudio::RTAUDIO_DUMMY, "Dummy");
+  apis.push_back(RtAudio::RTAUDIO_DUMMY);
 #endif
+  return std::make_pair(names, apis);
 }
 
-const std::string &RtAudio :: getCompiledApiName( RtAudio::Api api )
+const RtAudio::ApiNameMap RtAudio::apiNames(init_ApiNames().first);
+const std::vector<RtAudio::Api> RtAudio::compiledApis(init_ApiNames().second);
+
+void RtAudio :: getCompiledApi( std::vector<RtAudio::Api> &apis )
 {
-#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( "PulseAudio" );
-    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( "DirectSound" );
-    return name;
-  }
-#endif
-#if defined(__MACOSX_CORE__)
-  if ( api == MACOSX_CORE ) {
-    static std::string name( "CoreAudio" );
-    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;
+  apis = compiledApis;
 }
 
-RtAudio::Api RtAudio :: getCompiledApiByName( const std::string &name )
+const std::vector<RtAudio::Api>& RtAudio :: getCompiledApi()
 {
-  unsigned int api_number = RtAudio::UNSPECIFIED;
-  size_t nameLength = name.size();
-
-  if ( nameLength == 0 )
-    return RtAudio::UNSPECIFIED;
-
-  while ( api_number <= RtAudio::RTAUDIO_DUMMY ) {
-    const std::string &otherName =
-      getCompiledApiName((RtAudio::Api)api_number);
+  return compiledApis;
+}
 
-    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]);
+static const std::string unknown_api_name = "";
+static const std::string unknown_api_display_name = "Unknown";
 
-    if ( equal )
-      return (RtAudio::Api)api_number;
+const std::string& RtAudio :: getCompiledApiName( RtAudio::Api api )
+{
+  ApiNameMap::const_iterator it;
+  for (it = apiNames.begin(); it != apiNames.end(); it++)
+    if (it->second.first == api)
+      return it->first;
+  return unknown_api_name;
+}
 
-    ++api_number;
-  }
+const std::string& RtAudio :: getCompiledApiDisplayName( RtAudio::Api api )
+{
+  ApiNameMap::const_iterator it;
+  for (it = apiNames.begin(); it != apiNames.end(); it++)
+    if (it->second.first == api)
+      return it->second.second;
+  return unknown_api_display_name;
+}
 
+RtAudio::Api RtAudio :: getCompiledApiByName( const std::string &name )
+{
+  if (apiNames.find(name) == apiNames.end())
     return RtAudio::UNSPECIFIED;
+  return apiNames.at(name).first;
 }
 
 void RtAudio :: openRtApi( RtAudio::Api api )
@@ -5208,6 +5176,8 @@ Exit:
 // Various revisions for RtAudio 4.0 by Gary Scavone, April 2007
 // Changed device query structure for RtAudio 4.0.7, January 2010
 
+#include <windows.h>
+#include <process.h>
 #include <mmsystem.h>
 #include <mmreg.h>
 #include <dsound.h>