this repo has no description
at fixPythonPipStalling 206 lines 7.8 kB view raw
1/* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved. 2 3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. 4 ("Apple") in consideration of your agreement to the following terms, and your 5 use, installation, modification or redistribution of this Apple software 6 constitutes acceptance of these terms. If you do not agree with these terms, 7 please do not use, install, modify or redistribute this Apple software. 8 9 In consideration of your agreement to abide by the following terms, and subject 10 to these terms, Apple grants you a personal, non-exclusive license, under Apple�s 11 copyrights in this original Apple software (the "Apple Software"), to use, 12 reproduce, modify and redistribute the Apple Software, with or without 13 modifications, in source and/or binary forms; provided that if you redistribute 14 the Apple Software in its entirety and without modifications, you must retain 15 this notice and the following text and disclaimers in all such redistributions of 16 the Apple Software. Neither the name, trademarks, service marks or logos of 17 Apple Computer, Inc. may be used to endorse or promote products derived from the 18 Apple Software without specific prior written permission from Apple. Except as 19 expressly stated in this notice, no other rights or licenses, express or implied, 20 are granted by Apple herein, including but not limited to any patent rights that 21 may be infringed by your derivative works or by other works in which the Apple 22 Software may be incorporated. 23 24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO 25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN 28 COMBINATION WITH YOUR PRODUCTS. 29 30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR 31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION 34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT 35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN 36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37*/ 38/*============================================================================= 39 CAChannelMapper.cpp 40 41=============================================================================*/ 42 43#include "CAChannelMapper.h" 44#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__) 45 #include <AudioToolbox/AudioToolbox.h> 46#else 47 #include <AudioToolbox.h> 48#endif 49 50static void DefaultChannelLayout(CAAudioChannelLayout &layout, UInt32 nchannels) 51{ 52 // we really can't do a sensible downmix without valid source/destination channel layouts 53 if (nchannels == 1) 54 layout = CAAudioChannelLayout(kAudioChannelLayoutTag_Mono); 55 else if (nchannels == 2) 56 layout = CAAudioChannelLayout(kAudioChannelLayoutTag_Stereo); 57} 58 59CAChannelMapper::CAChannelMapper( const CAStreamBasicDescription &srcFormat, 60 const CAStreamBasicDescription &destFormat, 61 const CAAudioChannelLayout * inSrcLayout, 62 const CAAudioChannelLayout * inDestLayout) : 63 mSrcNChannels(srcFormat.mChannelsPerFrame), 64 mDestNChannels(destFormat.mChannelsPerFrame) 65{ 66 if (inSrcLayout && inSrcLayout->IsValid()) 67 mSrcLayout = *inSrcLayout; 68 else 69 DefaultChannelLayout(mSrcLayout, srcFormat.mChannelsPerFrame); 70 71 if (inDestLayout && inDestLayout->IsValid()) 72 mDestLayout = *inDestLayout; 73 else 74 DefaultChannelLayout(mDestLayout, destFormat.mChannelsPerFrame); 75} 76 77CAChannelMapper::~CAChannelMapper() 78{ 79 if (mMatrixMixer) 80 CloseComponent(mMatrixMixer); 81} 82 83OSStatus CAChannelMapper::OpenMixer(double sampleRate) 84{ 85 CAComponent comp(kAudioUnitType_Mixer, kAudioUnitSubType_MatrixMixer, kAudioUnitManufacturer_Apple); 86 OSStatus err; 87 88 err = CAAudioUnit::Open(comp, mMatrixMixer); 89 if (err) return err; 90 91 CAStreamBasicDescription fmt; 92 fmt.mSampleRate = sampleRate; 93 UInt32 nbuses = 1; 94 95 err = mMatrixMixer.SetProperty(kAudioUnitProperty_BusCount, kAudioUnitScope_Input, 0, &nbuses, sizeof(UInt32)); 96 if (err) return err; 97 err = mMatrixMixer.SetProperty(kAudioUnitProperty_BusCount, kAudioUnitScope_Output, 0, &nbuses, sizeof(UInt32)); 98 if (err) return err; 99 100 fmt.SetCanonical(mSrcNChannels, false); 101 err = mMatrixMixer.SetFormat(kAudioUnitScope_Input, 0, fmt); 102 if (err) return err; 103 104 fmt.SetCanonical(mDestNChannels, false); 105 err = mMatrixMixer.SetFormat(kAudioUnitScope_Output, 0, fmt); 106 if (err) return err; 107 108 // set render callback 109 AURenderCallbackStruct input; 110 input.inputProc = MixerInputProc; 111 input.inputProcRefCon = this; 112 err = mMatrixMixer.SetProperty( 113 kAudioUnitProperty_SetRenderCallback, 114 kAudioUnitScope_Global, 115 0, 116 &input, 117 sizeof(input)); 118 119 err = mMatrixMixer.Initialize(); 120 if (err) return err; 121 122 return ResetMixer(); 123} 124 125OSStatus CAChannelMapper::ResetMixer() 126{ 127 int nin = mSrcNChannels, nout = mDestNChannels; 128 int i, j; 129 130 // set global, input and output volumes 131 mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, 0xFFFFFFFF, 1.); 132 for (i = 0; i < nout; ++i) { 133 mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, 0xFFFF0000 | i, 1.); 134 } 135 for (i = 0; i < nin; ++i) { 136 mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, (i<<16) | 0xFFFF, 1.); 137 } 138 // set crosspoint volumes 139 for (i = 0; i < nin; ++i) { 140 for (j = 0; j < nout; ++j) { 141 mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, (i<<16) | j, 0.); 142 } 143 } 144 return noErr; 145} 146 147OSStatus CAChannelMapper::ConfigureDownmix() 148{ 149 OSStatus err = ResetMixer(); 150 if (err) 151 return err; 152 153 const AudioChannelLayout *layouts[] = { &mSrcLayout.Layout(), &mDestLayout.Layout() }; 154 UInt32 propSize; 155 err = AudioFormatGetPropertyInfo(kAudioFormatProperty_MatrixMixMap, sizeof(layouts), layouts, &propSize); 156 if (err) 157 return err; 158 159 void *mixmap = malloc(propSize); 160 err = AudioFormatGetProperty(kAudioFormatProperty_MatrixMixMap, sizeof(layouts), layouts, &propSize, mixmap); 161 if (!err) { 162 mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, 0xFFFFFFFF, 1.); 163 int nin = mSrcNChannels, nout = mDestNChannels; 164 int i, j; 165 // set the crosspoint volumes 166 Float32 *val = (Float32 *)mixmap; 167 for (i = 0; i < nin; ++i) { 168 for (j = 0; j < nout; ++j) { 169 mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, (i<<16) | j, *val++); 170 } 171 } 172 } 173 free(mixmap); 174 return noErr; 175} 176 177OSStatus CAChannelMapper::ConnectChannelToChannel(UInt32 inChannel, UInt32 outChannel) 178{ 179 return mMatrixMixer.SetParameter(kMatrixMixerParam_Volume, kAudioUnitScope_Global, 180 (inChannel << 16) | outChannel, 1.); 181} 182 183OSStatus CAChannelMapper::Mix(const AudioBufferList *src, AudioBufferList *dest, UInt32 nFrames) 184{ 185 mMixInputBufferList = src; 186 AudioUnitRenderActionFlags flags = 0; 187 AudioTimeStamp ts; 188 ts.mFlags = 0; 189 return AudioUnitRender(mMatrixMixer.AU(), &flags, &ts, 0, nFrames, dest); 190} 191 192OSStatus CAChannelMapper::MixerInputProc( 193 void * inRefCon, 194 AudioUnitRenderActionFlags *ioActionFlags, 195 const AudioTimeStamp * inTimeStamp, 196 UInt32 inBusNumber, 197 UInt32 inNumberFrames, 198 AudioBufferList * ioData) 199{ 200 CAChannelMapper *This = static_cast<CAChannelMapper *>(inRefCon); 201 const AudioBufferList *mixInputBufferList = This->mMixInputBufferList; 202 UInt32 copySize = sizeof(UInt32) + (mixInputBufferList->mNumberBuffers * sizeof(AudioBuffer)); 203 memcpy(ioData, mixInputBufferList, copySize); 204 205 return noErr; 206}