我試圖將一些音頻數據保存到WAV文件 - 我有我通常在RemoteIO中使用過的音頻數據,但我現在試圖實現一個函數保存數據。我知道音頻數據是有效的,所以這不是一個問題 - 如果我只能得到一個正確長度的空WAV文件,我可以在稍後填入數據。Objective C生成(空)WAV文件
現在,代碼創建文件,它看起來是正確的字節長度,但顯然它沒有正確格式化,因爲OSX,QuickTime,iTunes等無法識別它(他們看到文件,可以「T確定的長度,或播放)
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);
您應該使用十六進制編輯器檢查文件,並查看它們與標準的不同之處。 –
是的 - 我認爲這是我的下一步。現在潛入它 –
好吧,看起來問題是標題 - 其餘數據正在被正確寫入。 所以,換句話說,AudioFileCreateWithURL()調用是什麼導致的主要問題,它看起來像 –