private void AllocateCoreAudioBuffers(SystemAudioInputBuffer buffer) { //8.10 p173 - Calculate capture buffer size uint bufferSizeFrames = 0; int uintSize = Marshal.SizeOf(typeof(uint)); ErrorHandler.CheckError(AudioUnitService.GetProperty( buffer.AudioUnit.Handle, AudioDevicePropertyID.BufferFrameSize, AudioUnitScopeType.Global, AudioUnitConstant.Element.OUTPUT_BUS, out bufferSizeFrames, ref uintSize )); //8.11-8.12 p173-175 - Allocate AudioBuffers & RingBuffer buffer.AllocateCoreAudioBuffers(buffer.StreamFormat.Value.ChannelsPerFrame, (int)bufferSizeFrames); }
private AudioUnit CreateInputUnit(SystemAudioInputBuffer buffer) { // Create input unit - 8.4 - 8.13 - Learning Core Audio p169 - 176 //8.4 p169 - Create AudioUnit var component = GetHALAudioComponent(); buffer.AudioUnit = component.CreateAudioUnit(); buffer.AudioUnit.Initialize(); //8.5 p169-170 - Enable IO on Input AUHAL ErrorHandler.CheckError(buffer.AudioUnit.SetEnableIO(true, AudioUnitScopeType.Input, AudioUnitConstant.Element.INPUT_BUS)); ErrorHandler.CheckError(buffer.AudioUnit.SetEnableIO(false, AudioUnitScopeType.Output, AudioUnitConstant.Element.OUTPUT_BUS)); //8.7 p171 - Set current device on AUHAL ErrorHandler.CheckError(buffer.AudioUnit.SetCurrentDevice((uint)buffer.Device.DeviceID, AudioUnitScopeType.Global, AudioUnitConstant.Element.OUTPUT_BUS)); //8.8 p171-172 - Get format from AUHAL input var auHalFormat = buffer.AudioUnit.GetAudioFormat(AudioUnitScopeType.Output, AudioUnitConstant.Element.INPUT_BUS); //8.9 p172 - Use AUHAL input sample rate var auHalInputFormat = buffer.AudioUnit.GetAudioFormat(AudioUnitScopeType.Input, AudioUnitConstant.Element.INPUT_BUS); AudioStreamBasicDescription requestedFormat = auHalFormat; requestedFormat.SampleRate = auHalInputFormat.SampleRate; buffer.StreamFormat = requestedFormat; ErrorHandler.CheckError(buffer.AudioUnit.SetFormat(requestedFormat, AudioUnitScopeType.Output, AudioUnitConstant.Element.INPUT_BUS)); //8.10 - 8.11 - Calculate buffer size & allocate AudioBuffers AllocateCoreAudioBuffers(buffer); //8.13 p174 - Create Input Callback -- sets callback for the SystemAudioInputBuffer instance ErrorHandler.CheckError(buffer.AudioUnit.SetInputCallback(buffer.InputRenderProc, AudioUnitScopeType.Global, AudioUnitConstant.Element.INPUT_BUS)); // contradicts book-- need INPUT_BUS to get trigger callback return(buffer.AudioUnit); }