shithub: sox

Download patch

ref: 19631f4068ff658d8cdd22a917f2da5f643f97ce
parent: 0aff9d901692e0252a5947ae63a2ed3f4ebd5259
author: cbagwell <cbagwell>
date: Sat Feb 19 16:36:34 EST 2011

Add support for specifying non-default audio device on coreaudio.

--- a/ChangeLog
+++ b/ChangeLog
@@ -40,6 +40,7 @@
     (Samuli Suominen) 
   o Allow dynamic loading of lame to be enabled even if lame header files
     are not installed.  (Doug Cook)
+  o Support specifying audio device other than default on OSX (cbagwell)
 
 Effects:
 
@@ -47,12 +48,12 @@
   o Fix -w option on stats effect. (Ronald Sprouse)
   o Fix segfault with some ladspa plugins (Thor Andreassen)
   o Optionally look for png.h in libpng directory to support OpenBSD
-    packaging.  Helps enable spectrograph effect.
-  o libpng15 requires application to include zlib.h header file.
+    packaging.  Helps enable spectrograph effect. (cbagwell)
+  o libpng15 requires application to include zlib.h header file. (cbagwell)
     Add this to spectrograph effect. [3184238]
   o Enable LADSPA effects on all platforms without any external
     dependencies.  Mainly useful for Linux, Windows and OS X which have
-    binaries readily available.
+    binaries readily available. (cbagwell)
 
 Other new features:
 
--- a/soxformat.7
+++ b/soxformat.7
@@ -246,10 +246,16 @@
 .TP
 \fBcoreaudio\fR (optional)
 Mac OSX CoreAudio device driver: supports both playing and recording
-audio.  Examples:
+audio.  If a filename is not specific or if the name is "default" then
+the default audio device is selected.  Any other name will be used
+to select a specific device.  The valid names can be seen in the
+System Preferences->Sound menu and then under the Output and Input tabs.
+
+Examples:
 .EX
 	sox infile \-t coreaudio
 	sox infile \-t coreaudio default
+	sox infile \-t coreaudio "Internal Speakers"
 .EE
 See also
 .BR play (1),
--- a/src/coreaudio.c
+++ b/src/coreaudio.c
@@ -129,14 +129,51 @@
   int32_t buf_size;
   int rc;
 
-  property_size = sizeof(ac->adid);
-  if (is_input)
-    status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice,
-                                      &property_size, &ac->adid);
+  if (strncmp(ft->filename, "default", (size_t)7) == 0)
+  {
+      property_size = sizeof(ac->adid);
+      if (is_input)
+	  status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &property_size, &ac->adid);
+      else
+	  status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &property_size, &ac->adid);
+  }
   else
-    status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
-                                      &property_size, &ac->adid);
+  {
+      Boolean is_writable;
+      status = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &property_size, &is_writable);
 
+      if (status == noErr)
+      {
+	  int device_count = property_size/sizeof(AudioDeviceID);
+	  AudioDeviceID *devices;
+
+	  devices = malloc(property_size);
+    	  status = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &property_size, devices);
+
+	  if (status == noErr)
+	  {
+	      int i;
+	      for (i = 0; i < property_size/sizeof(AudioDeviceID); i++)
+	      {
+		  char name[256];
+		  status = AudioDeviceGetProperty(devices[i],0,false,kAudioDevicePropertyDeviceName,&property_size,&name);
+
+		  lsx_report("Found Audio Device \"%s\"\n",name);
+
+		  /* String returned from OS is truncated so only compare
+		   * as much as returned.
+		   */
+		  if (strncmp(name,ft->filename,strlen(name)) == 0)
+		  {
+		      ac->adid = devices[i];
+		      break;
+		  }
+	      }
+	  }
+	  free(devices);
+      }
+  }
+
   if (status || ac->adid == kAudioDeviceUnknown)
   {
     lsx_fail_errno(ft, SOX_EPERM, "can not open audio device");
@@ -165,9 +202,10 @@
   ft->signal.rate = 44100;
   ft->encoding.bits_per_sample = 32;
 
-  /* TODO: My limited experience with hardware can only get floats working which a fixed sample
-   * rate and stereo.  I know that is a limitiation of audio device I have so this may not be
-   * standard operating orders.  If some hardware supports setting sample rates and channel counts
+  /* TODO: My limited experience with hardware can only get floats working
+   * withh a fixed sample rate and stereo.  I know that is a limitiation of
+   * audio device I have so this may not be standard operating orders.
+   * If some hardware supports setting sample rates and channel counts
    * then should do that over resampling and mixing.
    */
 #if  0