this repo has no description
at fixPythonPipStalling 191 lines 7.0 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 CAAudioFilePlayer.cpp 40 41=============================================================================*/ 42 43#include "CAAudioFilePlayer.h" 44#include "CAXException.h" 45 46CAAudioFilePlayer::CAAudioFilePlayer(int nBuffers, UInt32 bufferSizeFrames) : 47 CAAudioFileReader(nBuffers, bufferSizeFrames), 48 mOutputUnit(NULL) 49{ 50 // open output unit 51 Component comp; 52 ComponentDescription desc; 53 54 desc.componentType = kAudioUnitType_Output; 55 desc.componentSubType = kAudioUnitSubType_DefaultOutput; 56 desc.componentManufacturer = kAudioUnitManufacturer_Apple; 57 desc.componentFlags = 0; 58 desc.componentFlagsMask = 0; 59 comp = FindNextComponent(NULL, &desc); 60 61 // if we can't find the default one, look explicitly for AUHAL 62 if(comp == NULL) 63 { 64 desc.componentType = kAudioUnitType_Output; 65 desc.componentSubType = kAudioUnitSubType_HALOutput; 66 desc.componentManufacturer = kAudioUnitManufacturer_Apple; 67 desc.componentFlags = 0; 68 desc.componentFlagsMask = 0; 69 comp = FindNextComponent(NULL, &desc); 70 } 71 72 XThrowIf(comp == NULL, -1, "find audio output unit"); 73 XThrowIfError(OpenAComponent(comp, &mOutputUnit), "open audio output unit"); 74 XThrowIfError(AudioUnitInitialize(mOutputUnit), "initialize audio output unit"); 75 76#if 0 77 AudioStreamBasicDescription theFormat; 78 UInt32 theSize = sizeof(AudioStreamBasicDescription); 79 AudioUnitGetProperty(mOutputUnit, 'sfmt', kAudioUnitScope_Output, 0, &theFormat, &theSize); 80 81 theFormat.mChannelsPerFrame = 6; 82 theFormat.mBytesPerFrame = theFormat.mChannelsPerFrame * (theFormat.mBitsPerChannel / 8); 83 theFormat.mBytesPerPacket = theFormat.mBytesPerFrame; 84 AudioUnitSetProperty(mOutputUnit, 'sfmt', kAudioUnitScope_Output, 0, &theFormat, theSize); 85#endif 86} 87 88CAAudioFilePlayer::~CAAudioFilePlayer() 89{ 90 if (mOutputUnit) 91 CloseComponent(mOutputUnit); 92} 93 94void CAAudioFilePlayer::SetFile(const FSRef &inFile) 95{ 96 Stop(); 97 CAAudioFileReader::SetFile(inFile); 98 SetupChannelMapping(); 99} 100 101void CAAudioFilePlayer::SetupChannelMapping() 102{ 103 // set render callback 104 AURenderCallbackStruct input; 105 input.inputProc = InputProc; 106 input.inputProcRefCon = this; 107 XThrowIfError(AudioUnitSetProperty( 108 mOutputUnit, 109 kAudioUnitProperty_SetRenderCallback, 110 kAudioUnitScope_Global, 111 0, 112 &input, 113 sizeof(input)), "connect input proc to output unit"); 114 115 const CAStreamBasicDescription &fmt = GetFile().GetClientDataFormat(); 116 XThrowIfError(AudioUnitSetProperty( 117 mOutputUnit, 118 kAudioUnitProperty_StreamFormat, 119 kAudioUnitScope_Input, 120 0, 121 (void *)&fmt, 122 sizeof(AudioStreamBasicDescription)), "set audio output format"); 123 const CAAudioChannelLayout &layoutObj = GetFile().GetFileChannelLayout(); 124 if (layoutObj.IsValid()) { 125 const AudioChannelLayout *layout = layoutObj; 126 /*OSStatus err =*/ AudioUnitSetProperty( 127 mOutputUnit, 128 kAudioUnitProperty_AudioChannelLayout, 129 kAudioUnitScope_Input, 130 0, 131 layout, 132 layoutObj.Size()); 133 // $$$ FIXME: why does this fail sometimes? 134 } 135} 136 137void CAAudioFilePlayer::Start() 138{ 139 if (IsRunning()) 140 Stop(); 141 if (ReachedEndOfStream()) 142 SetCurrentPosition(0.); 143 CAAudioFileReader::Start(); 144 XThrowIfError(AudioOutputUnitStart(mOutputUnit), "start audio output unit"); 145} 146 147void CAAudioFilePlayer::Stop() 148{ 149 if (IsRunning()) { 150 XThrowIfError(AudioOutputUnitStop(mOutputUnit), "stop audio output unit"); 151 CAAudioFileReader::Stop(); 152 } 153} 154 155void CAAudioFilePlayer::SetVolume(double volume) 156{ 157 AudioUnitSetParameter(mOutputUnit, kHALOutputParam_Volume, 158 kAudioUnitScope_Global, 0, volume, 0); 159} 160 161OSStatus CAAudioFilePlayer::GetPlayBuffer( 162 UInt32 inNumberFrames, 163 AudioBufferList * ioData) 164{ 165 UInt32 nFrames = inNumberFrames; 166 PullBuffer(nFrames, ioData); 167// printf("read %ld frames\n", nFrames); 168 if (nFrames < inNumberFrames) { 169 AudioBuffer *buf = ioData->mBuffers; 170 UInt32 bytesPerFrame = buf->mNumberChannels * sizeof(Float32); 171 for (UInt32 i = 0; i < ioData->mNumberBuffers; ++i, ++buf) { 172 memset((Byte *)buf->mData + nFrames * bytesPerFrame, 0, 173 (inNumberFrames - nFrames) * bytesPerFrame); 174 } 175 } 176 return noErr; 177} 178 179OSStatus CAAudioFilePlayer::InputProc( 180 void * inRefCon, 181 AudioUnitRenderActionFlags *ioActionFlags, 182 const AudioTimeStamp * inTimeStamp, 183 UInt32 inBusNumber, 184 UInt32 inNumberFrames, 185 AudioBufferList * ioData) 186{ 187 CAAudioFilePlayer *This = static_cast<CAAudioFilePlayer *>(inRefCon); 188 return This->GetPlayBuffer(inNumberFrames, ioData); 189} 190 191