AudioFileWriteBytes正在花费太长时间来完成它的工作

时间:2011-03-03 10:24:34

标签: ios audio core-audio wav

这个问题对于这个空间来说可能有点过于庞大和模糊,但我会试一试。

我有一个样本数组,我正在尝试写入iOS上的.wav文件,这需要花费一分半钟的时间。这是发生拖动的循环:

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++;
    }

有什么想法吗?


编辑1

如果有帮助,这就是它之前的代码:

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;

2 个答案:

答案 0 :(得分:2)

为什么需要循环?你不能一次性写下所有的样本,例如

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

答案 1 :(得分:1)

每次调用AudioFileWriteBytes时,您写入的字节数可能太小。 bytesToWrite有多大?