我有一个名为_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)
有人知道出了什么问题吗?提前谢谢。
答案 0 :(得分:1)
废弃我在问题中发布的代码。我在这里找到了解决方案:How to write array of float values to audio file in 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];
答案 1 :(得分:0)
这是一堆相当毛茸茸的代码。
这一行:
AudioBufferList *buffers =
(AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));
对我来说很可疑。它正在分配一个固定大小的缓冲区,即sizeof(AudioBufferList)和sizeof(AudioBuffer)的总和
这两个变量有哪些类型?
Sizeof返回结构的大小,而不是它指向的内容的大小。我猜你需要一个缓冲区,分配给一个浮点数的大小乘以一个值,该值保存数组中浮点数的总数。 (这是_recordingLength?)
这段代码来自哪里?你能详细解释一下它应该做些什么吗?你的花车阵列来自哪里?什么告诉你的代码有多少字节?数据存储在内存中的哪个位置?什么将值缓冲区写入磁盘?
您说您的文件已创建并且长度为4个字节。如果你试图存储大约390000个花车,这听起来确实是一个问题。