我正在尝试将一些音频数据保存到 WAV 文件中——我有通常在 RemoteIO 中使用的音频数据,但我现在正在尝试实现一个函数来保存数据。我知道音频数据是有效的,所以这不是问题——如果我可以得到一个正确长度的空 WAV 文件,我可以稍后用数据填充它。
现在,代码创建了文件,它看起来是正确的字节长度,但显然它的格式不正确,因为 OSX、QuickTime、iTunes 等无法识别它(他们看到文件,无法确定长度,或播放它)
NSURL * tvarFilename = [savePanel URL];
NSLog(@"doSaveAs filename = %@",tvarFilename);
//try to create an audio file there
AudioFileID mRecordFile;
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 2;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 4;
audioFormat.mBytesPerFrame = 4;
OSStatus status = AudioFileCreateWithURL((CFURLRef)tvarFilename, kAudioFileWAVEType, &audioFormat, kAudioFileFlags_EraseFile, &mRecordFile);
int beatsToRecord = 4; //temporary
int bpm = 120;
double intervalInSamples = (double) 60 / bpm;
intervalInSamples *= (double)44100;
int inNumberFrames = (intervalInSamples * beatsToRecord);
UInt32 frameBuffer[inNumberFrames];
int sampleTime = 0;
UInt32 thisSubBuffer[inNumberFrames];
for (int i = 0; i < inNumberFrames; i++) { frameBuffer[i] = 0; }
UInt32 bytesToWrite = inNumberFrames * sizeof(UInt32);
status = AudioFileWriteBytes(mRecordFile, false, 0, &bytesToWrite, &frameBuffer);