Create a white noise sound track using AVAssetWriter

I am currently using AVAssetWriter to create Quicktime mov. The video track is generated correctly. Now I would like to add the mp4a trash soundtrack. The sound may just be white. I am mainly interested in the mov file, which contains both video and audio tracks.

How to configure a CMSampleBufferRef that just contains white noise in mp4a format? Here is what I have tried so far:

 CMSampleBufferRef garbageAudioSampleBuffer = NULL; AudioStreamBasicDescription audioFormat; audioFormat.mSampleRate = 44100; audioFormat.mFormatID = kAudioFormatLinearPCM; audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; audioFormat.mFramesPerPacket = 1; audioFormat.mChannelsPerFrame = 2; audioFormat.mBitsPerChannel = 16; audioFormat.mBytesPerPacket = 4; audioFormat.mBytesPerFrame = 4; CMAudioFormatDescriptionRef audioFormatDescrip = NULL; CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &audioFormat, 0, NULL, 0, NULL, NULL, &audioFormatDescrip ); CMSampleBufferCreate(kCFAllocatorDefault, NULL, YES, NULL, NULL, NULL, // audioFormatDescrip, 0, 0, NULL, 0, NULL, &garbageAudioSampleBuffer ); if(myAVAssetAudioWriterInput isReadyForMoreMediaData) [myAVAssetAudioWriterInput appendSampleBuffer:garbageAudioSampleBuffer]; 

When NULL is passed for audioFormatDescrip, the mov file is generated successfully, but contains only the video track (and the absence of an audio track). When I actually pass audioFormatDescrip, the mov file seems corrupted. I probably should really pass some samples, but I'm not sure how to do this.

Note. I checked that appendSampleBuffer returns YES (for brevity, I skipped this code).

+4
source share
1 answer

There are many problems in your code:

  • White noise has a statistical definition; you create undefined noise.
  • You are not passing audioFormatDescription to CMSampleBufferCreate
  • You complicate your life by using CMSampleBufferCreate instead of CMAudioSampleBufferCreateWithPacketDescriptions (you don't have package descriptions, so just minus zero and 0)
  • You need to say when your sound buffer is timestamped
  • Your sound buffer is 0 frames long. This is too short.
  • and finally, you need to add sound buffers sufficient for the entire duration of your movie.

CoreMedia code can be complicated because there are hundreds of arguments that should be in order. For your purposes, this may be a little too general, so why not get a loop-like piece of white noise and assemble it using AVMutableComposition? This will give you a movie and sound file, which can then be pinned together using another AVMutableComposition and AVAssetExportSession.

+3
source

Source: https://habr.com/ru/post/1379030/


All Articles