|
| 1 | +// Copyright AudioKit. All Rights Reserved. Revision History at http://github.com/AudioKit/AudioKit/ |
| 2 | + |
| 3 | +import AudioToolbox |
| 4 | +import AVFoundation |
| 5 | +import CAudioKitEX |
| 6 | +import AudioKit |
| 7 | + |
| 8 | +/// AudioUnit which instantiates a DSP kernel based on the componentSubType. |
| 9 | +open class AudioKitAU: AUAudioUnit { |
| 10 | + // MARK: AUAudioUnit Overrides |
| 11 | + |
| 12 | + private var inputBusArray: [AUAudioUnitBus] = [] |
| 13 | + private var outputBusArray: [AUAudioUnitBus] = [] |
| 14 | + private var internalBuffers: [AVAudioPCMBuffer] = [] |
| 15 | + |
| 16 | + // Default supported channel capabilities |
| 17 | + public var supportedLeftChannelCount: NSNumber = 2 |
| 18 | + public var supportedRightChannelCount: NSNumber = 2 |
| 19 | + |
| 20 | + override public var channelCapabilities: [NSNumber]? { |
| 21 | + return [supportedLeftChannelCount, supportedRightChannelCount] |
| 22 | + } |
| 23 | + |
| 24 | + /// Allocate the render resources |
| 25 | + override public func allocateRenderResources() throws { |
| 26 | + try super.allocateRenderResources() |
| 27 | + |
| 28 | + if let inputFormat = inputBusArray.first?.format { |
| 29 | + |
| 30 | + // we don't need to allocate a buffer if we can process in place |
| 31 | + if !canProcessInPlace || inputBusArray.count > 1 { |
| 32 | + for i in inputBusArray.indices { |
| 33 | + if let buffer = AVAudioPCMBuffer(pcmFormat: inputFormat, frameCapacity: maximumFramesToRender) { |
| 34 | + setBufferDSP(dsp, buffer.mutableAudioBufferList, i) |
| 35 | + internalBuffers.append(buffer) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if let outputFormat = outputBusArray.first?.format { |
| 42 | + allocateRenderResourcesDSP(dsp, outputFormat.channelCount, outputFormat.sampleRate) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Delllocate Render Resources |
| 47 | + override public func deallocateRenderResources() { |
| 48 | + super.deallocateRenderResources() |
| 49 | + deallocateRenderResourcesDSP(dsp) |
| 50 | + internalBuffers = [] |
| 51 | + } |
| 52 | + |
| 53 | + /// Reset the DSP |
| 54 | + override public func reset() { |
| 55 | + resetDSP(dsp) |
| 56 | + } |
| 57 | + |
| 58 | + private lazy var auInputBusArray: AUAudioUnitBusArray = { |
| 59 | + AUAudioUnitBusArray(audioUnit: self, busType: .input, busses: inputBusArray) |
| 60 | + }() |
| 61 | + |
| 62 | + /// Input busses |
| 63 | + override public var inputBusses: AUAudioUnitBusArray { |
| 64 | + return auInputBusArray |
| 65 | + } |
| 66 | + |
| 67 | + private lazy var auOutputBusArray: AUAudioUnitBusArray = { |
| 68 | + AUAudioUnitBusArray(audioUnit: self, busType: .output, busses: outputBusArray) |
| 69 | + }() |
| 70 | + |
| 71 | + /// Output bus array |
| 72 | + override public var outputBusses: AUAudioUnitBusArray { |
| 73 | + return auOutputBusArray |
| 74 | + } |
| 75 | + |
| 76 | + /// Internal render block |
| 77 | + override public var internalRenderBlock: AUInternalRenderBlock { |
| 78 | + internalRenderBlockDSP(dsp) |
| 79 | + } |
| 80 | + |
| 81 | + private var _parameterTree: AUParameterTree? |
| 82 | + |
| 83 | + /// Parameter tree |
| 84 | + override public var parameterTree: AUParameterTree? { |
| 85 | + get { return _parameterTree } |
| 86 | + set { |
| 87 | + _parameterTree = newValue |
| 88 | + |
| 89 | + _parameterTree?.implementorValueObserver = { [unowned self] parameter, value in |
| 90 | + setParameterValueDSP(self.dsp, parameter.address, value) |
| 91 | + } |
| 92 | + |
| 93 | + _parameterTree?.implementorValueProvider = { [unowned self] parameter in |
| 94 | + getParameterValueDSP(self.dsp, parameter.address) |
| 95 | + } |
| 96 | + |
| 97 | + _parameterTree?.implementorStringFromValueCallback = { parameter, value in |
| 98 | + if let value = value { |
| 99 | + return String(format: "%.2f", value.pointee) |
| 100 | + } else { |
| 101 | + return "Invalid" |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// Whether the unit can process in place |
| 108 | + override public var canProcessInPlace: Bool { |
| 109 | + return canProcessInPlaceDSP(dsp) |
| 110 | + } |
| 111 | + |
| 112 | + /// Set in order to bypass processing |
| 113 | + override public var shouldBypassEffect: Bool { |
| 114 | + get { return getBypassDSP(dsp) } |
| 115 | + set { setBypassDSP(dsp, newValue) } |
| 116 | + } |
| 117 | + |
| 118 | + // MARK: Lifecycle |
| 119 | + |
| 120 | + /// DSP Reference |
| 121 | + public private(set) var dsp: DSPRef? |
| 122 | + |
| 123 | + /// Initialize with component description and options |
| 124 | + /// - Parameters: |
| 125 | + /// - componentDescription: Audio Component Description |
| 126 | + /// - options: Audio Component Instantiation Options |
| 127 | + /// - Throws: error |
| 128 | + override public init(componentDescription: AudioComponentDescription, |
| 129 | + options: AudioComponentInstantiationOptions = []) throws { |
| 130 | + try super.init(componentDescription: componentDescription, options: options) |
| 131 | + |
| 132 | + // Create pointer to C++ DSP code. |
| 133 | + dsp = akCreateDSP(componentDescription.componentSubType) |
| 134 | + assert(dsp != nil) |
| 135 | + |
| 136 | + // create audio bus connection points |
| 137 | + let format = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2)! |
| 138 | + for _ in 0..<inputBusCountDSP(dsp) { |
| 139 | + inputBusArray.append(try AUAudioUnitBus(format: format)) |
| 140 | + } |
| 141 | + |
| 142 | + // All AudioKit nodes have one output bus. |
| 143 | + outputBusArray.append(try AUAudioUnitBus(format: format)) |
| 144 | + |
| 145 | + parameterTree = AUParameterTree.createTree(withChildren: []) |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + deinit { |
| 150 | + deleteDSP(dsp) |
| 151 | + } |
| 152 | + |
| 153 | + // MARK: AudioKit |
| 154 | + |
| 155 | + /// Trigger something within the audio unit |
| 156 | + public func trigger(note: MIDINoteNumber, velocity: MIDIVelocity) { |
| 157 | + #if !os(tvOS) |
| 158 | + guard let midiBlock = scheduleMIDIEventBlock else { |
| 159 | + fatalError("Attempt to trigger audio unit which doesn't respond to MIDI.") |
| 160 | + } |
| 161 | + let event = MIDIEvent(noteOn: note, velocity: velocity, channel: 0) |
| 162 | + event.data.withUnsafeBufferPointer { ptr in |
| 163 | + guard let ptr = ptr.baseAddress else { return } |
| 164 | + midiBlock(AUEventSampleTimeImmediate, 0, event.data.count, ptr) |
| 165 | + } |
| 166 | + #endif |
| 167 | + } |
| 168 | + |
| 169 | + /// Trigger something within the audio unit |
| 170 | + public func trigger() { |
| 171 | + trigger(note: 64, velocity: 127) |
| 172 | + } |
| 173 | + |
| 174 | + /// Turn off the the trigger for a gate |
| 175 | + public func detrigger() { |
| 176 | + trigger(note: 64, velocity: 0) |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + /// Create an array of values to use as waveforms or other things inside an audio unit |
| 181 | + /// - Parameters: |
| 182 | + /// - wavetable: Array of float values |
| 183 | + /// - index: Optional index at which to set the table (useful for multiple waveform audio units) |
| 184 | + public func setWavetable(_ wavetable: [AUValue], index: Int = 0) { |
| 185 | + setWavetableDSP(dsp, wavetable, wavetable.count, Int32(index)) |
| 186 | + } |
| 187 | + |
| 188 | + /// Set wave table |
| 189 | + /// - Parameters: |
| 190 | + /// - data: A pointer to the data |
| 191 | + /// - size: Size of the table |
| 192 | + /// - index: Index at which to set the value |
| 193 | + public func setWavetable(data: UnsafePointer<AUValue>?, size: Int, index: Int = 0) { |
| 194 | + setWavetableDSP(dsp, data, size, Int32(index)) |
| 195 | + } |
| 196 | +} |
0 commit comments