Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. - (id) init {
  2. self = [super init];
  3.  
  4. sizeof(allowBluetoothInput), &allowBluetoothInput);
  5.  
  6.  
  7. // You can adjust the latency of RemoteIO (and, in fact, any other audio framework) by setting the kAudioSessionProperty_PreferredHardwareIOBufferDuration property
  8. float aBufferLength = 0.005; // In seconds
  9. AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(aBufferLength), &aBufferLength);
  10.  
  11. OSStatus status;
  12.  
  13. // Describe audio component
  14. AudioComponentDescription desc;
  15. desc.componentType = kAudioUnitType_Output;
  16. desc.componentSubType = kAudioUnitSubType_RemoteIO;
  17. desc.componentFlags = 0;
  18. desc.componentFlagsMask = 0;
  19. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  20.  
  21.  
  22. // set line in as preferred
  23. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  24. NSString *preferredPortType = AVAudioSessionPortLineIn;
  25. for (AVAudioSessionPortDescription *desc in audioSession.availableInputs) {
  26. if ([desc.portType isEqualToString: AVAudioSessionPortLineIn] ||
  27. // [desc.portType isEqualToString: AVAudioSessionPortBuiltInMic] ||
  28. [desc.portType isEqualToString: AVAudioSessionPortHeadsetMic])
  29. {
  30. [audioSession setPreferredInput:desc error:nil];
  31.  
  32. }
  33. }
  34.  
  35. // Get component
  36. AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);
  37.  
  38. // Get audio units
  39. status = AudioComponentInstanceNew(inputComponent, &audioUnit);
  40. checkStatus(status);
  41.  
  42. // Enable IO for recording
  43. UInt32 flag = 1;
  44. status = AudioUnitSetProperty(audioUnit,
  45. kAudioOutputUnitProperty_EnableIO,
  46. kAudioUnitScope_Input,
  47. kInputBus,
  48. &flag,
  49. sizeof(flag));
  50. checkStatus(status);
  51.  
  52. // Enable IO for playback
  53. status = AudioUnitSetProperty(audioUnit,
  54. kAudioOutputUnitProperty_EnableIO,
  55. kAudioUnitScope_Output,
  56. kOutputBus,
  57. &flag,
  58. sizeof(flag));
  59. checkStatus(status);
  60.  
  61. // Describe format
  62. AudioStreamBasicDescription audioFormat;
  63. audioFormat.mSampleRate = 44100.00;
  64. audioFormat.mFormatID = kAudioFormatLinearPCM; // kAudioFormatMPEG4AAC_ELD
  65. audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  66. audioFormat.mFramesPerPacket = 1;
  67. audioFormat.mChannelsPerFrame = 1;
  68. audioFormat.mBitsPerChannel = 16;
  69. audioFormat.mBytesPerPacket = 2;
  70. audioFormat.mBytesPerFrame = 2;
  71.  
  72. // Apply format
  73. status = AudioUnitSetProperty(audioUnit,
  74. kAudioUnitProperty_StreamFormat,
  75. kAudioUnitScope_Output,
  76. kInputBus,
  77. &audioFormat,
  78. sizeof(audioFormat));
  79. checkStatus(status);
  80. status = AudioUnitSetProperty(audioUnit,
  81. kAudioUnitProperty_StreamFormat,
  82. kAudioUnitScope_Input,
  83. kOutputBus,
  84. &audioFormat,
  85. sizeof(audioFormat));
  86. checkStatus(status);
  87.  
  88.  
  89. // Set input callback
  90. AURenderCallbackStruct callbackStruct;
  91. callbackStruct.inputProc = recordingCallback;
  92. callbackStruct.inputProcRefCon = self;
  93. status = AudioUnitSetProperty(audioUnit,
  94. kAudioOutputUnitProperty_SetInputCallback,
  95. kAudioUnitScope_Global,
  96. kInputBus,
  97. &callbackStruct,
  98. sizeof(callbackStruct));
  99. checkStatus(status);
  100.  
  101. // Set output callback
  102. callbackStruct.inputProc = playbackCallback;
  103. callbackStruct.inputProcRefCon = self;
  104. status = AudioUnitSetProperty(audioUnit,
  105. kAudioUnitProperty_SetRenderCallback,
  106. kAudioUnitScope_Global,
  107. kOutputBus,
  108. &callbackStruct,
  109. sizeof(callbackStruct));
  110. checkStatus(status);
  111.  
  112. // Disable buffer allocation for the recorder (optional - do this if we want to pass in our own)
  113. flag = 0;
  114. status = AudioUnitSetProperty(audioUnit,
  115. kAudioUnitProperty_ShouldAllocateBuffer,
  116. kAudioUnitScope_Output,
  117. kInputBus,
  118. &flag,
  119. sizeof(flag));
  120.  
  121. // Allocate our own buffers (1 channel, 16 bits per sample, thus 16 bits per frame, thus 2 bytes per frame).
  122. // Practice learns the buffers used contain 512 frames, if this changes it will be fixed in processAudio.
  123. tempBuffer.mNumberChannels = 1;
  124. tempBuffer.mDataByteSize = 512 * 2;
  125. tempBuffer.mData = malloc( 512 * 2 );
  126.  
  127. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetoothA2DP error:NULL];
  128.  
  129. // Initialise
  130. AudioSessionSetActive(true);
  131.  
  132. NSTimeInterval outLat = [[AVAudioSession sharedInstance] outputLatency];
  133. NSTimeInterval inLat = [[AVAudioSession sharedInstance] inputLatency];
  134.  
  135. status = AudioUnitInitialize(audioUnit);
  136. checkStatus(status);
  137.  
  138. return self;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement