summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Holl <dholl@subspacedynamics.com>2017-04-15 08:04:05 -0700
committerDavid Holl <dholl@subspacedynamics.com>2017-04-24 10:37:34 -0700
commit85729e81156d0b6849d12f09aa2d83357078e6da (patch)
tree8edc2bf40943874247dfeaf428728f9f26fb9d4e
parent1ceba2671c0a572e78170a9049b8d230bdc67685 (diff)
Created CoreAudioSettings
-rw-r--r--sounddevice.py121
1 files changed, 120 insertions, 1 deletions
diff --git a/sounddevice.py b/sounddevice.py
index 4e08fb9..88c57fc 100644
--- a/sounddevice.py
+++ b/sounddevice.py
@@ -232,6 +232,33 @@ PaHostApiTypeId Pa_GetStreamHostApiType( PaStream* stream );
PaError Pa_GetSampleSize( PaSampleFormat format );
void Pa_Sleep( long msec );
+/* pa_mac_core.h */
+
+typedef int32_t SInt32;
+typedef struct
+{
+ unsigned long size;
+ PaHostApiTypeId hostApiType;
+ unsigned long version;
+ unsigned long flags;
+ SInt32 const * channelMap;
+ unsigned long channelMapSize;
+} PaMacCoreStreamInfo;
+void PaMacCore_SetupStreamInfo( PaMacCoreStreamInfo *data, unsigned long flags );
+void PaMacCore_SetupChannelMap( PaMacCoreStreamInfo *data, const SInt32 * const channelMap, unsigned long channelMapSize );
+const char *PaMacCore_GetChannelName( int device, int channelIndex, bool input );
+#define paMacCoreChangeDeviceParameters 0x01
+#define paMacCoreFailIfConversionRequired 0x02
+#define paMacCoreConversionQualityMin 0x0100
+#define paMacCoreConversionQualityMedium 0x0200
+#define paMacCoreConversionQualityLow 0x0300
+#define paMacCoreConversionQualityHigh 0x0400
+#define paMacCoreConversionQualityMax 0x0000
+#define paMacCorePlayNice 0x00
+#define paMacCorePro 0x01
+#define paMacCoreMinimizeCPUButPlayNice 0x0100
+#define paMacCoreMinimizeCPU 0x0101
+
/* pa_win_waveformat.h */
typedef unsigned long PaWinWaveFormatChannelMask;
@@ -2182,7 +2209,7 @@ class default(object):
See Also
--------
- AsioSettings, WasapiSettings
+ AsioSettings, CoreAudioSettings, WasapiSettings
"""
samplerate = None
@@ -2349,6 +2376,98 @@ class AsioSettings(object):
channelSelectors=self._selectors))
+class CoreAudioSettings(object):
+
+ def __init__(self, channel_map=None, change_device_parameters=False,
+ fail_if_conversion_required=False, conversion_quality='max'):
+ """Mac Core Audio-specific input/output settings.
+
+ Objects of this class can be used as *extra_settings* argument
+ to `Stream()` (and variants) or as `default.extra_settings`.
+
+ Parameters
+ ----------
+ channel_map : sequence of int, optional
+ Support for opening only specific channels of a Core Audio
+ device. Note that *channel_map* is treated differently
+ between input and output channels.
+
+ For input devices, *channel_map* is a list of integers
+ specifying the (zero-based) channel numbers to use.
+
+ For output devices, *channel_map* must have the same length
+ as the number of output channels of the device. Specify
+ unused channels with -1, and a 0-based index for any desired
+ channels.
+
+ See the example below. For additional information, see the
+ `PortAudio documentation`__.
+
+ __ https://app.assembla.com/spaces/portaudio/git/source/
+ master/src/hostapi/coreaudio/notes.txt
+ change_device_parameters : bool, optional
+ If ``True``, allows PortAudio to change things like the
+ device's frame size, which allows for much lower latency,
+ but might disrupt the device if other programs are using it,
+ even when you are just querying the device. ``False`` is
+ the default.
+ fail_if_conversion_required : bool, optional
+ In combination with the above flag, ``True`` causes the
+ stream opening to fail, unless the exact sample rates are
+ supported by the device.
+ conversion_quality : {'min', 'low', 'medium', 'high', 'max'}, optional
+ This sets Core Audio's sample rate conversion quality.
+ ``'max'`` is the default.
+
+ Example
+ -------
+ This example assumes a device having 6 input and 6 output
+ channels. Input is from the second and fourth channels, and
+ output is to the device's third and fifth channels:
+
+ >>> import sounddevice as sd
+ >>> ca_in = sd.CoreAudioSettings(channel_map=[1, 3])
+ >>> ca_out = sd.CoreAudioSettings(channel_map=[-1, -1, 0, -1, 1, -1])
+ >>> sd.playrec(..., channels=2, extra_settings=(ca_in, ca_out))
+
+ """
+ conversion_dict = {
+ 'min': _lib.paMacCoreConversionQualityMin,
+ 'low': _lib.paMacCoreConversionQualityLow,
+ 'medium': _lib.paMacCoreConversionQualityMedium,
+ 'high': _lib.paMacCoreConversionQualityHigh,
+ 'max': _lib.paMacCoreConversionQualityMax,
+ }
+
+ # Minimal checking on channel_map to catch errors that might
+ # otherwise go unnoticed:
+ if isinstance(channel_map, int):
+ raise TypeError('channel_map must be a list or tuple')
+
+ try:
+ self._flags = conversion_dict[conversion_quality.lower()]
+ except (KeyError, AttributeError):
+ raise ValueError('conversion_quality must be one of ' +
+ repr(list(conversion_dict)))
+ if change_device_parameters:
+ self._flags |= _lib.paMacCoreChangeDeviceParameters
+ if fail_if_conversion_required:
+ self._flags |= _lib.paMacCoreFailIfConversionRequired
+
+ # this struct must be kept alive!
+ self._streaminfo = _ffi.new('PaMacCoreStreamInfo*')
+ _lib.PaMacCore_SetupStreamInfo(self._streaminfo, self._flags)
+
+ if channel_map is not None:
+ # this array must be kept alive!
+ self._channel_map = _ffi.new('SInt32[]', channel_map)
+ if len(self._channel_map) == 0:
+ raise TypeError('channel_map must not be empty')
+ _lib.PaMacCore_SetupChannelMap(self._streaminfo,
+ self._channel_map,
+ len(self._channel_map))
+
+
class WasapiSettings(object):
def __init__(self, exclusive=False):