Eric Brotto
Eric Brotto

Reputation: 54281

AudioFileWriteBytes is taking way too long to do its job

This problem may be a bit too vast and nebulous for this space, but I'll give it a go.

I have an array of samples that I'm trying to write to a .wav file on my iOS and it is taking up to a minute and a half to do. Here is the loop where the drag is occurring:

for (int i=0; i< 1430529; i++)  // 1430529 is the length of the array of samples 
    {

        SInt16 sample;

        sample = sample_array[i];



        audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);

        sampleCount++;
    }

Any ideas?


EDIT 1

If it helps, this is the code that precedes it:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];




// THIS IS MY BIT TO CONVERT THE LOCATION TO NSSTRING
NSString *filePath = [[NSString alloc]init];
filePath = [NSString stringWithUTF8String:location];

// HERE I WANT TO REMOVE THE FILE NAME FROM THE LOCATION.
NSString *truncatedFilePath = filePath;

truncatedFilePath = [truncatedFilePath stringByReplacingOccurrencesOfString:@"/recordedFile.wav"
                                    // withString:@"/newFile.caf"];
                                    withString:@"/recordedFile.wav"];




NSLog(truncatedFilePath);



NSURL *fileURL = [NSURL fileURLWithPath:truncatedFilePath];



AudioStreamBasicDescription asbd;


memset(&asbd,0, sizeof(asbd));

asbd.mSampleRate = SAMPLE_RATE;
asbd.mFormatID = kAudioFormatLinearPCM;


asbd.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;


asbd.mBitsPerChannel = 16;
asbd.mChannelsPerFrame = 1;
asbd.mFramesPerPacket = 1;
asbd.mBytesPerFrame = 2;
asbd.mBytesPerPacket = 2;






AudioFileID audioFile;

OSStatus audioErr = noErr;

audioErr = AudioFileCreateWithURL((CFURLRef)fileURL, kAudioFileWAVEType, &asbd, kAudioFileFlags_EraseFile, &audioFile);

assert (audioErr == noErr);


long sampleCount = 0;
UInt32 bytesToWrite = 2;

Upvotes: 2

Views: 891

Answers (2)

Paul R
Paul R

Reputation: 213190

Why do you need the loop ? Can't you write all the samples in one go, e.g.

    numSamples = 1430529;
    bytesToWrite = numSamples * 2;
    audioErr = AudioFileWriteBytes(audioFile, false, 0, &bytesToWrite, sample_array);

?

Upvotes: 2

Nick Ruiz
Nick Ruiz

Reputation: 1445

Perhaps the number of bytes you are writing at each call to AudioFileWriteBytes is too small. How large is bytesToWrite?

Upvotes: 1

Related Questions