Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let genContext = Unmanaged.passRetained(self).toOpaque()
- var genCallbackStruct = AURenderCallbackStruct(inputProc: genCallback,
- inputProcRefCon: genContext)
- AudioUnitSetProperty(mixerUnit!, kAudioUnitProperty_SetRenderCallback,
- kAudioUnitScope_Input, 1, &genCallbackStruct,
- UInt32(MemoryLayout<AURenderCallbackStruct>.size))
- var inputCallbackStruct = AURenderCallbackStruct(inputProc: recordingCallback,
- inputProcRefCon: context)
- AudioUnitSetProperty(remoteIOUnit!, kAudioOutputUnitProperty_SetInputCallback,
- kAudioUnitScope_Global, 0, &inputCallbackStruct,
- UInt32(MemoryLayout<AURenderCallbackStruct>.size))
- let recordingCallback: AURenderCallback = { (
- inRefCon,
- ioActionFlags,
- inTimeStamp,
- inBusNumber,
- frameCount,
- ioData ) -> OSStatus in
- let audioObject = unsafeBitCast(inRefCon, to: AudioEngine.self)
- var err: OSStatus = noErr
- var bufferList = AudioBufferList(
- mNumberBuffers: 1,
- mBuffers: AudioBuffer(
- mNumberChannels: UInt32(1),
- mDataByteSize: 512,
- mData: nil))
- if let au: AudioUnit = audioObject.remoteIOUnit! {
- err = AudioUnitRender(au,
- ioActionFlags,
- inTimeStamp,
- inBusNumber,
- frameCount,
- &bufferList)
- }
- let data = Data(bytes: bufferList.mBuffers.mData!, count: Int(bufferList.mBuffers.mDataByteSize))
- let samples = data.withUnsafeBytes {
- UnsafeBufferPointer<Int16>(start: $0, count: data.count / MemoryLayout<Int16>.size)
- }
- let factor = Float(Int16.max)
- var floats: [Float] = Array(repeating: 0.0, count: samples.count)
- for i in 0..<samples.count {
- floats[i] = (Float(samples[i]) / factor)
- }
- var j = audioObject.in1BufIndex
- let m = audioObject.in1BufSize
- for i in 0..<(floats.count) {
- audioObject.in1Buf[j] = Float(floats[I])
- j += 1 ; if j >= m { j = 0 }
- }
- audioObject.in1BufIndex = j
- audioObject.inputCallbackFrameSize = Int(frameCount)
- audioObject.callbackcount += 1
- var WindowSize = totalRecordSize / Int(frameCount)
- if audioObject.callbackcount == WindowSize {
- audioObject.running = false
- }
- return 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement