AudioFileWriteBytes 完成其工作所需的时间太长

发布于 2024-10-20 09:01:40 字数 1798 浏览 7 评论 0原文

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

我有一组示例,正在尝试将其写入 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云胡 2024-10-27 09:01:40

为什么需要循环?例如,您不能一次性写出所有示例吗

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

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);

?

等待我真够勒 2024-10-27 09:01:40

也许每次调用 AudioFileWriteBytes 时写入的字节数太小。 bytesToWrite 有多大?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文