Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. let genContext = Unmanaged.passRetained(self).toOpaque()
  2. var genCallbackStruct = AURenderCallbackStruct(inputProc: genCallback,
  3. inputProcRefCon: genContext)
  4. AudioUnitSetProperty(mixerUnit!, kAudioUnitProperty_SetRenderCallback,
  5. kAudioUnitScope_Input, 1, &genCallbackStruct,
  6. UInt32(MemoryLayout<AURenderCallbackStruct>.size))
  7.  
  8. var inputCallbackStruct = AURenderCallbackStruct(inputProc: recordingCallback,
  9. inputProcRefCon: context)
  10. AudioUnitSetProperty(remoteIOUnit!, kAudioOutputUnitProperty_SetInputCallback,
  11. kAudioUnitScope_Global, 0, &inputCallbackStruct,
  12. UInt32(MemoryLayout<AURenderCallbackStruct>.size))
  13.  
  14. let recordingCallback: AURenderCallback = { (
  15. inRefCon,
  16. ioActionFlags,
  17. inTimeStamp,
  18. inBusNumber,
  19. frameCount,
  20. ioData ) -> OSStatus in
  21.  
  22. let audioObject = unsafeBitCast(inRefCon, to: AudioEngine.self)
  23.  
  24. var err: OSStatus = noErr
  25.  
  26. var bufferList = AudioBufferList(
  27. mNumberBuffers: 1,
  28. mBuffers: AudioBuffer(
  29. mNumberChannels: UInt32(1),
  30. mDataByteSize: 512,
  31. mData: nil))
  32.  
  33. if let au: AudioUnit = audioObject.remoteIOUnit! {
  34. err = AudioUnitRender(au,
  35. ioActionFlags,
  36. inTimeStamp,
  37. inBusNumber,
  38. frameCount,
  39. &bufferList)
  40. }
  41.  
  42. let data = Data(bytes: bufferList.mBuffers.mData!, count: Int(bufferList.mBuffers.mDataByteSize))
  43. let samples = data.withUnsafeBytes {
  44. UnsafeBufferPointer<Int16>(start: $0, count: data.count / MemoryLayout<Int16>.size)
  45. }
  46. let factor = Float(Int16.max)
  47. var floats: [Float] = Array(repeating: 0.0, count: samples.count)
  48. for i in 0..<samples.count {
  49. floats[i] = (Float(samples[i]) / factor)
  50. }
  51.  
  52. var j = audioObject.in1BufIndex
  53. let m = audioObject.in1BufSize
  54. for i in 0..<(floats.count) {
  55. audioObject.in1Buf[j] = Float(floats[I])
  56.  
  57. j += 1 ; if j >= m { j = 0 }
  58. }
  59. audioObject.in1BufIndex = j
  60. audioObject.inputCallbackFrameSize = Int(frameCount)
  61. audioObject.callbackcount += 1
  62. var WindowSize = totalRecordSize / Int(frameCount)
  63. if audioObject.callbackcount == WindowSize {
  64.  
  65. audioObject.running = false
  66.  
  67. }
  68.  
  69. return 0
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement