Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Audio Extension Toolbox function doesn't quite work anymore as is due to API changes. It now requires you to setup a category. When setting the export properties I was getting an error code of '?cat' (which the NSError will print out in decimal).</p> <p>Here is the code that works now in iOS 5.1. It is incredibly slow too, just by looking I'd say several times slower. It is also memory intensive since it appear to load the file into memory, which generates memory warnings for 10MB mp3 files.</p> <pre><code>-(void) scaleAudioFileAmplitude:(NSURL *)theURL withAmpScale:(float) ampScale { OSStatus err = noErr; ExtAudioFileRef audiofile; ExtAudioFileOpenURL((CFURLRef)theURL, &amp;audiofile); assert(audiofile); // get some info about the file's format. AudioStreamBasicDescription fileFormat; UInt32 size = sizeof(fileFormat); err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_FileDataFormat, &amp;size, &amp;fileFormat); // we'll need to know what type of file it is later when we write AudioFileID aFile; size = sizeof(aFile); err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_AudioFile, &amp;size, &amp;aFile); AudioFileTypeID fileType; size = sizeof(fileType); err = AudioFileGetProperty(aFile, kAudioFilePropertyFileFormat, &amp;size, &amp;fileType); // tell the ExtAudioFile API what format we want samples back in AudioStreamBasicDescription clientFormat; bzero(&amp;clientFormat, sizeof(clientFormat)); clientFormat.mChannelsPerFrame = fileFormat.mChannelsPerFrame; clientFormat.mBytesPerFrame = 4; clientFormat.mBytesPerPacket = clientFormat.mBytesPerFrame; clientFormat.mFramesPerPacket = 1; clientFormat.mBitsPerChannel = 32; clientFormat.mFormatID = kAudioFormatLinearPCM; clientFormat.mSampleRate = fileFormat.mSampleRate; clientFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved; err = ExtAudioFileSetProperty(audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &amp;clientFormat); // find out how many frames we need to read SInt64 numFrames = 0; size = sizeof(numFrames); err = ExtAudioFileGetProperty(audiofile, kExtAudioFileProperty_FileLengthFrames, &amp;size, &amp;numFrames); // create the buffers for reading in data AudioBufferList *bufferList = malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (clientFormat.mChannelsPerFrame - 1)); bufferList-&gt;mNumberBuffers = clientFormat.mChannelsPerFrame; //printf("bufferList-&gt;mNumberBuffers = %lu \n\n", bufferList-&gt;mNumberBuffers); for (int ii=0; ii &lt; bufferList-&gt;mNumberBuffers; ++ii) { bufferList-&gt;mBuffers[ii].mDataByteSize = sizeof(float) * numFrames; bufferList-&gt;mBuffers[ii].mNumberChannels = 1; bufferList-&gt;mBuffers[ii].mData = malloc(bufferList-&gt;mBuffers[ii].mDataByteSize); } // read in the data UInt32 rFrames = (UInt32)numFrames; err = ExtAudioFileRead(audiofile, &amp;rFrames, bufferList); // close the file err = ExtAudioFileDispose(audiofile); // process the audio for (int ii=0; ii &lt; bufferList-&gt;mNumberBuffers; ++ii) { float *fBuf = (float *)bufferList-&gt;mBuffers[ii].mData; for (int jj=0; jj &lt; rFrames; ++jj) { *fBuf = *fBuf * ampScale; fBuf++; } } // open the file for writing err = ExtAudioFileCreateWithURL((CFURLRef)theURL, fileType, &amp;fileFormat, NULL, kAudioFileFlags_EraseFile, &amp;audiofile); NSError *error = NULL; /*************************** You Need This Now ****************************/ AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryAudioProcessing error:&amp;error]; /************************* End You Need This Now **************************/ // tell the ExtAudioFile API what format we'll be sending samples in err = ExtAudioFileSetProperty(audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &amp;clientFormat); error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil]; NSLog(@"Error: %@", [error description]); // write the data err = ExtAudioFileWrite(audiofile, rFrames, bufferList); // close the file ExtAudioFileDispose(audiofile); // destroy the buffers for (int ii=0; ii &lt; bufferList-&gt;mNumberBuffers; ++ii) { free(bufferList-&gt;mBuffers[ii].mData); } free(bufferList); bufferList = NULL; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload