changeset 3790:8f8209f8da6d SDL-ryan-multiple-audio-device

Added audio device enumeration for 1.3.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 03 Oct 2006 20:07:50 +0000
parents e2f68b579a01
children be33495e4d97
files include/SDL_audio.h src/audio/SDL_audio.c src/audio/SDL_sysaudio.h src/audio/macosx/SDL_coreaudio.c
diffstat 4 files changed, 84 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_audio.h	Tue Oct 03 20:05:33 2006 +0000
+++ b/include/SDL_audio.h	Tue Oct 03 20:07:50 2006 +0000
@@ -235,6 +235,9 @@
  * Get the human-readable name of a specific audio device.
  *  Must be a value between 0 and (number of audio devices-1).
  *  Only valid after a successfully initializing the audio subsystem.
+ *  The values returned by this function reflect the latest call to
+ *  SDL_GetNumAudioDevices(); recall that function to redetect available
+ *  hardware.
  */
 extern DECLSPEC const char *SDLCALL SDL_GetAudioDevice(int index,
                                                        int iscapture);
--- a/src/audio/SDL_audio.c	Tue Oct 03 20:05:33 2006 +0000
+++ b/src/audio/SDL_audio.c	Tue Oct 03 20:07:50 2006 +0000
@@ -455,6 +455,34 @@
     return current_audio.name;
 }
 
+
+int
+SDL_GetNumAudioDevices(int iscapture)
+{
+    if (!SDL_WasInit(SDL_INIT_AUDIO) || !current_audio.impl.DetectDevices) {
+        return -1;
+    }
+    return current_audio.impl.DetectDevices(iscapture);
+}
+
+
+const char *
+SDL_GetAudioDevice(int index, int iscapture)
+{
+    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
+        SDL_SetError("Audio subsystem is not initialized");
+        return NULL;
+    }
+
+    if ((index < 0) && (!current_audio.impl.GetAudioDevice)) {
+        SDL_SetError("No such device");
+        return NULL;
+    }
+
+    return current_audio.impl.GetAudioDevice(index, iscapture);
+}
+
+
 static void
 close_audio_device(SDL_AudioDevice *device)
 {
--- a/src/audio/SDL_sysaudio.h	Tue Oct 03 20:05:33 2006 +0000
+++ b/src/audio/SDL_sysaudio.h	Tue Oct 03 20:07:50 2006 +0000
@@ -34,6 +34,8 @@
 /* !!! FIXME: rename these from "Audio" to "Device" ... */
 typedef struct SDL_AudioDriverImpl
 {
+    int (*DetectDevices)(int iscapture);
+    const char *(*GetAudioDevice)(int index, int iscapture);
     int (*OpenAudio) (_THIS, const char *devname, int iscapture);
     void (*ThreadInit) (_THIS); /* Called by audio thread at start */
     void (*WaitAudio) (_THIS);
--- a/src/audio/macosx/SDL_coreaudio.c	Tue Oct 03 20:05:33 2006 +0000
+++ b/src/audio/macosx/SDL_coreaudio.c	Tue Oct 03 20:07:50 2006 +0000
@@ -29,7 +29,7 @@
 #include "../SDL_sysaudio.h"
 #include "SDL_coreaudio.h"
 
-#define DEBUG_COREAUDIO 1
+#define DEBUG_COREAUDIO 0
 
 typedef struct COREAUDIO_DeviceList
 {
@@ -169,6 +169,22 @@
     }
 }
 
+static inline void
+build_device_lists(void)
+{
+    build_device_list(0, &outputDevices, &outputDeviceCount);
+    build_device_list(1, &inputDevices, &inputDeviceCount);
+}
+
+
+static inline void
+free_device_lists(void)
+{
+    free_device_list(&outputDevices, &outputDeviceCount);
+    free_device_list(&inputDevices, &inputDeviceCount);
+}
+
+
 static int
 find_device_id(const char *devname, int iscapture, AudioDeviceID *id)
 {
@@ -188,6 +204,8 @@
 
 /* Audio driver functions */
 
+static int COREAUDIO_DetectDevices(int iscapture);
+static const char *COREAUDIO_GetAudioDevice(int index, int iscapture);
 static int COREAUDIO_OpenAudio(_THIS, const char *devname, int iscapture);
 static void COREAUDIO_WaitAudio(_THIS);
 static void COREAUDIO_PlayAudio(_THIS);
@@ -206,11 +224,9 @@
 static int
 COREAUDIO_Init(SDL_AudioDriverImpl *impl)
 {
-    /* !!! FIXME: should these _really_ be static? */
-    build_device_list(0, &outputDevices, &outputDeviceCount);
-    build_device_list(1, &inputDevices, &inputDeviceCount);
-
     /* Set the function pointers */
+    impl->DetectDevices = COREAUDIO_DetectDevices;
+    impl->GetAudioDevice = COREAUDIO_GetAudioDevice;
     impl->OpenAudio = COREAUDIO_OpenAudio;
     impl->WaitAudio = COREAUDIO_WaitAudio;
     impl->PlayAudio = COREAUDIO_PlayAudio;
@@ -227,11 +243,39 @@
 };
 
 
+static int
+COREAUDIO_DetectDevices(int iscapture)
+{
+    if (iscapture) {
+        build_device_list(1, &inputDevices, &inputDeviceCount);
+        return inputDeviceCount;
+    } else {
+        build_device_list(0, &outputDevices, &outputDeviceCount);
+        return outputDeviceCount;
+    }
+
+    return 0;  /* shouldn't ever hit this. */
+}
+
+
+static const char *
+COREAUDIO_GetAudioDevice(int index, int iscapture)
+{
+    if ((iscapture) && (index < inputDeviceCount)) {
+        return inputDevices[index].name;
+    } else if ((!iscapture) && (index < outputDeviceCount)) {
+        return outputDevices[index].name;
+    }
+
+    SDL_SetError("No such device");
+    return NULL;
+}
+
+
 static void
 COREAUDIO_Deinitialize(void)
 {
-    free_device_list(&outputDevices, &outputDeviceCount);
-    free_device_list(&inputDevices, &inputDeviceCount);
+    free_device_lists();
 }