Guest User

Untitled

a guest
Oct 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. @interface AudioTapProcessor : NSObject
  2.  
  3. @property (nonatomic, strong) AVPlayerItem *item;
  4. @property (nonatomic, strong) id<AudioProcessorDelegate> delegate;
  5.  
  6. - (instancetype)initWithDelegate:(id<AudioProcessorDelegate>)delegate
  7. item:(AVPlayerItem *)item;
  8. - (void)startProcessing;
  9. - (void)stopProcessing;
  10.  
  11. @end
  12.  
  13. void init(MTAudioProcessingTapRef tap, void *clientInfo, void
  14. **tapStorageOut) {
  15. *tapStorageOut = clientInfo;
  16. }
  17.  
  18. void finalize(MTAudioProcessingTapRef tap) {}
  19.  
  20. void prepare(
  21. MTAudioProcessingTapRef tap,
  22. CMItemCount maxFrames,
  23. const AudioStreamBasicDescription *processingFormat
  24. ) {}
  25.  
  26. void unprepare(MTAudioProcessingTapRef tap) {}
  27.  
  28. void process(
  29. MTAudioProcessingTapRef tap,
  30. CMItemCount numberFrames,
  31. MTAudioProcessingTapFlags flags,
  32. AudioBufferList *bufferListInOut,
  33. CMItemCount *numberFramesOut,
  34. MTAudioProcessingTapFlags *flagsOut
  35. ) {
  36. //Random crashes here if I declare the delegate weak
  37. //Something like AUDeferredRenderer-0x7ff8f448ef (364): EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  38. AudioTapProcessor *processor = (__bridge AudioTapProcessor *)MTAudioProcessingTapGetStorage(tap);
  39.  
  40. OSStatus err = MTAudioProcessingTapGetSourceAudio(tap, numberFrames, bufferListInOut, flagsOut, NULL, numberFramesOut);
  41.  
  42. AudioBuffer *pBuffer = &bufferListInOut->mBuffers[0];
  43. UInt32 frameLength = pBuffer->mDataByteSize / sizeof(float);
  44. float *pData = (float *)pBuffer->mData;
  45.  
  46. if (err == noErr && processor) {
  47. if ([processor.delegate
  48. respondsToSelector:@selector(updateWith:withSize:)]) {
  49. [processor.delegate updateWith:pData withSize:frameLength];
  50. }
  51. }
  52. }
  53.  
  54. - (void)stopProcessing
  55. {
  56. [self.item removeObserver:self forKeyPath:@"status"];
  57. AVMutableAudioMixInputParameters *params =
  58. (AVMutableAudioMixInputParameters *) _item.audioMix.inputParameters[0];
  59. MTAudioProcessingTapRef tap = params.audioTapProcessor;
  60. self.item.audioMix = nil;
  61. CFRelease(tap);
  62. //By doing this the tap processor does call its unprepare and finalize methods, so it is being deallocated fine.
  63. }
  64.  
  65. var processor: AudioTapProcessor!
  66.  
  67. override func prepareForPlayback() {
  68. super.prepareForPlayback()
  69. if processor == nil {
  70. processor = AudioTapProcessor(delegate: self, item: item)
  71. processor.startProcessing()
  72. }
  73. }
  74.  
  75. override func viewWillDisappear(_ animated: Bool) {
  76. super.viewWillDisappear(animated)
  77. player.pause()
  78. }
  79.  
  80. deinit {
  81. //I tried to do this early in the lifecycle(viewWillDissapear) and it is the same thing.
  82. processor.stopProcessing()
  83. }
Add Comment
Please, Sign In to add comment