1

我有一个名为 _recordingSamples 的 Float32 值数组,我希望将其写入音频文件。_recordingLength 的值约为 390000。我正在使用以下代码:

NSString *outputPath = @"/Users/evanjackson/output.caf";
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:outputPath contents:nil attributes:nil];

AudioStreamBasicDescription _audioFormat;
_audioFormat.mSampleRate = 44100.0;
_audioFormat.mFormatID = kAudioFormatLinearPCM;
_audioFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
_audioFormat.mChannelsPerFrame = 1;
_audioFormat.mBitsPerChannel = 32;
_audioFormat.mBytesPerFrame = 4;
_audioFormat.mFramesPerPacket = 1;
_audioFormat.mBytesPerPacket = _audioFormat.mFramesPerPacket * _audioFormat.mBytesPerFrame;

UInt32 lengthPropertySize = sizeof(SInt64);
ExtAudioFileRef filteredAudio;
NSURL *destinationURL = [NSURL URLWithString:outputPath];
OSStatus status = ExtAudioFileCreateWithURL((__bridge CFURLRef)destinationURL, kAudioFileCAFType, &_audioFormat, NULL, kAudioFileFlags_EraseFile, &filteredAudio);
printf("Status: %d\n", status);
//ExtAudioFileOpenURL((__bridge CFURLRef)[NSURL fileURLWithPath:outputPath], &filteredAudio);
const AudioStreamBasicDescription audioFormat = [del audioFormat];
status = ExtAudioFileSetProperty(filteredAudio, kExtAudioFileProperty_ClientDataFormat, sizeof(audioFormat), &audioFormat);
printf("Status: %d\n", status);
status = ExtAudioFileGetProperty(filteredAudio, kExtAudioFileProperty_FileLengthFrames, &lengthPropertySize, &_recordingLength);
printf("Status: %d\n", status);
AudioBufferList *buffers = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));
buffers->mNumberBuffers = 1;
for (int i = 0; i < buffers->mNumberBuffers; i++) {
    buffers->mBuffers[0].mData = _recordingSamples;
    buffers->mBuffers[0].mNumberChannels = 1;
    buffers->mBuffers[0].mDataByteSize = _recordingLength * sizeof(Float32);
}

status = ExtAudioFileWrite(filteredAudio, _recordingLength, buffers);
printf("Status: %d\n", status);

所有状态都返回 0,文件被创建(4 个字节),但 Quicktime 不会打开它并返回错误 -12842,编译器会传递以下消息:

libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
(lldb) 

有人知道出了什么问题吗?提前致谢。

4

2 回答 2

1

废弃我在问题中发布的代码。我在这里找到了解决方案:如何在 Core Audio 中将浮点值数组写入音频文件?. 看第一个答案。我去了给定的链接,下载了源代码,然后将 EAFWrite.h 和 EAFWrite.mm 添加到我的项目中。该类假设音频将从多个缓冲区(即:2D 数组)中读取,但需要它与 1D 数组一起使用,因此我修改了函数 writeToFloats,如下所示:

-(OSStatus) writeFloats:(long)numFrames fromArray:(float *)data
{
    OSStatus    err = noErr;

    if (!data)      return -1;
    if (!numFrames) return -1;

    AudioBufferList *abl = AllocateAudioBufferList(mStreamFormat.mChannelsPerFrame, numFrames*sizeof(short));
    if (!abl)       return -1;

    abl->mBuffers[0].mNumberChannels = 1;
    abl->mBuffers[0].mDataByteSize = numFrames*sizeof(short);
    short *buffer = (short*)abl->mBuffers[0].mData;
    for (long v = 0; v < numFrames; v++) {
        if (data[v] > 0.999)
            data[v] = 0.999;
        else if (data[v] < -1)
            data[v] = -1;
        buffer[v] = (short)(data[v]*32768.f);
    }

    abl->mBuffers[0].mData = buffer;

    err = ExtAudioFileWrite(mOutputAudioFile, numFrames, abl);

    DestroyAudioBufferList(abl);

    if(err != noErr)
    {
        char formatID[5];
        *(UInt32 *)formatID = CFSwapInt32HostToBig(err);
        formatID[4] = '\0';
        fprintf(stderr, "ExtAudioFileWrite FAILED! %d '%-4.4s'\n",(int)err, formatID);
        return err;
    }

    return err;

}

要调用此函数,您需要:

NSString *outputPath = @"outputFile.caf"
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:outputPath contents:nil attributes:nil];
NSURL* fileURL = [NSURL URLWithString:outputPath];

EAFWrite *writer = [[EAFWrite alloc] init];
[writer openFileForWrite:fileURL sr:44100.0 channels:1 wordLength:32 type:kAudioFileCAFType];
[writer writeFloats:_recordingLength fromArray:_recordingSamples];
于 2014-06-13T13:16:30.793 回答
0

那是一堆相当多毛的代码。

这一行:

AudioBufferList *buffers = 
  (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));

在我看来很可疑。它正在分配一个固定大小的缓冲区,即 sizeof(AudioBufferList) 和 sizeof(AudioBuffer)

这两个变量的类型是什么?

Sizeof 返回结构的大小,而不是它指向的内容的大小。我猜你需要一个分配给单个浮点大小的缓冲区乘以一个保存数组中浮点总数的值。(那是 _recordingLength 吗?)

这段代码是从哪里来的?你能详细解释一下它应该做什么吗?你的花车数组来自哪里?什么告诉你的代码有多少字节?数据存储在内存中的什么位置?是什么将值的缓冲区写入磁盘?

您说您的文件已创建并且长度为 4 个字节。如果您要存储一些 390000 个浮点数,那听起来肯定是个问题。

于 2014-06-12T16:00:48.410 回答