Guest User

Untitled

a guest
Jan 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. AudioBufferList *AEAllocateAndInitAudioBufferList(AudioStreamBasicDescription audioFormat, int frameCount) {
  2. int numberOfBuffers = audioFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved ? audioFormat.mChannelsPerFrame : 1;
  3. int channelsPerBuffer = audioFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved ? 1 : audioFormat.mChannelsPerFrame;
  4. int bytesPerBuffer = audioFormat.mBytesPerFrame * frameCount;
  5. AudioBufferList *audio = malloc(sizeof(AudioBufferList) + (numberOfBuffers - 1) * sizeof(AudioBuffer));
  6. if (!audio) {
  7. return NULL;
  8. }
  9. audio->mNumberBuffers = numberOfBuffers;
  10. for (int i = 0; i < numberOfBuffers; i++) {
  11. if (bytesPerBuffer > 0) {
  12. audio->mBuffers[i].mData = calloc(bytesPerBuffer, 1);
  13. if (!audio->mBuffers[i].mData) {
  14. for (int j = 0; j < i; j++) free(audio->mBuffers[j].mData);
  15. free(audio);
  16. return NULL;
  17. }
  18. } else {
  19. audio->mBuffers[i].mData = NULL;
  20. }
  21. audio->mBuffers[i].mDataByteSize = bytesPerBuffer;
  22. audio->mBuffers[i].mNumberChannels = channelsPerBuffer;
  23. }
  24. return audio;}
  25.  
  26. - (OSStatus)renderToBufferList:(AudioBufferList *)bufferList
  27. writeToFile:(ExtAudioFileRef)audioFile
  28. bufferLength:(NSUInteger)bufferLength
  29. timeStamp:(AudioTimeStamp *)timeStamp {
  30. [self clearBufferList:bufferList];
  31. AudioUnit outputUnit = self.engine.outputNode.audioUnit;
  32. OSStatus status =AudioUnitRender(outputUnit, 0, timeStamp, 0, (UInt32)bufferLength, bufferList);
  33.  
  34.  
  35.  
  36. float *data1 = bufferList->mBuffers[0].mData;
  37. float *data2 = bufferList->mBuffers[1].mData;;
  38.  
  39. for(int i=0; i<bufferLength/4; i++)
  40. {
  41. if(data1[i]!=0||data2[i]!=0)
  42. NSLog(@"%f - %f",data1[i],data2[i]);
  43. }
  44.  
  45. if (status != noErr) {
  46. NSLog(@"Can not render audio unit");
  47. return status;
  48. }
  49. timeStamp->mSampleTime += bufferLength;
  50. status = ExtAudioFileWrite(audioFile, (UInt32)bufferLength, bufferList);
  51. if (status != noErr)
  52. NSLog(@"Can not write audio to file");
  53.  
  54. - (NSString *)renderAudioAndWriteToFile {
  55. AVAudioOutputNode *outputNode = self.engine.outputNode;
  56. AudioStreamBasicDescription const *audioDescription = [outputNode outputFormatForBus:0].streamDescription;
  57. NSString *path = [self filePath];
  58. ExtAudioFileRef audioFile = [self createAndSetupExtAudioFileWithASBD:audioDescription andFilePath:path];
  59. if (!audioFile)
  60. return nil;
  61. AVURLAsset *asset = [AVURLAsset assetWithURL:self.file.url];
  62. NSTimeInterval duration = CMTimeGetSeconds(asset.duration);
  63. NSUInteger lengthInFrames = (NSUInteger) (duration * audioDescription->mSampleRate);
  64.  
  65. const NSUInteger kBufferLength = 1024; //3756;
  66.  
  67. AudioBufferList *bufferList = AEAllocateAndInitAudioBufferList(*audioDescription, kBufferLength);
  68. AudioTimeStamp timeStamp;
  69. memset (&timeStamp, 0, sizeof(timeStamp));
  70. timeStamp.mFlags = kAudioTimeStampSampleTimeValid;
  71. OSStatus status = noErr;
  72. for (NSUInteger i = kBufferLength; i < lengthInFrames; i += kBufferLength) {
  73. status = [self renderToBufferList:bufferList writeToFile:audioFile bufferLength:kBufferLength timeStamp:&timeStamp];
  74. if (status != noErr)
  75. break;
  76. }
  77. if (status == noErr && timeStamp.mSampleTime < lengthInFrames) {
  78. NSUInteger restBufferLength = (NSUInteger) (lengthInFrames - timeStamp.mSampleTime);
  79. AudioBufferList *restBufferList = AEAllocateAndInitAudioBufferList(*audioDescription, (Float32)restBufferLength);
  80. status = [self renderToBufferList:restBufferList writeToFile:audioFile bufferLength:restBufferLength timeStamp:&timeStamp];
  81. AEFreeAudioBufferList(restBufferList);
  82. }
  83.  
  84.  
  85. SInt64 fileLengthInFrames;
  86. UInt32 size = sizeof(SInt64);
  87. ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_FileLengthFrames, &size, &fileLengthInFrames);
  88. AEFreeAudioBufferList(bufferList);
  89. ExtAudioFileDispose(audioFile);
  90. if (status != noErr)
  91. [self showAlertWithTitle:@"Error" message:@"See logs for details"];
  92. else {
  93. NSLog(@"Finished writing to file at path: %@ n File size must be %f Mb", path,(tmpData.length/1024.0)/1024.0);
  94. [self showAlertWithTitle:@"Success!" message:@"Now you can play a result file"];
  95. }
  96. return path;
  97. }
Add Comment
Please, Sign In to add comment