1

我已经尝试过以下代码,但在执行时会给出“fmt?” 错误告诉我“此文件类型不支持数据格式”。不幸的是,如果要写入 AIFF,我在任何地方都找不到说明应如何设置 AudioStreamBasicDescription 的参考。

我已经用谷歌搜索了这个问题并阅读了我能找到的所有文档,但无济于事。以前有人做过这种事情吗?任何想法我做错了什么?

//  This path is just for testing
char * filePath = "/Users/user/Desktop/reverb.aiff";

//  This value will actually be set by earlier code depending on how many mics are defined    
int channels = 1;

CFURLRef audioFileURL = CFURLCreateFromFileSystemRepresentation(NULL,
                                                                (const UInt8 *)filePath,
                                                                strlen(filePath),
                                                                false);

AudioStreamBasicDescription audioFileDescription = {0};
audioFileDescription.mFormatID         = kAudioFormatLinearPCM;
audioFileDescription.mFormatFlags      = kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsBigEndian;
audioFileDescription.mBitsPerChannel   = sizeof(float) * 8;
audioFileDescription.mChannelsPerFrame = channels;
audioFileDescription.mBytesPerFrame    = (audioFileDescription.mBitsPerChannel / 8) * audioFileDescription.mChannelsPerFrame;
audioFileDescription.mFramesPerPacket  = 1;
audioFileDescription.mBytesPerPacket   = audioFileDescription.mBytesPerFrame * audioFileDescription.mFramesPerPacket;

AudioFileID audioFileID = 0;

OSStatus createFileStatus = AudioFileCreateWithURL(audioFileURL,
                                                   kAudioFileAIFFType,
                                                   &audioFileDescription,
                                                   kAudioFileFlags_EraseFile,
                                                   &audioFileID);

if (createFileStatus != 0) {
    cout << "Error creating output file" << endl;
    cout << "Error code: " << createFileStatus << ", ";

    //  hacky way of converting number back to kinda-readable enum code
    char chars[5];
    for (int i = 3; i >= 0; i--) {
        chars[i] = (char)createFileStatus;
        createFileStatus = createFileStatus >> 8;
    }
    char[4] = '\0';
    cout << chars << endl;
    return 1;
}
4

1 回答 1

1

AIFF 类型不支持浮点数据,您需要创建一个 AIFF-C 文件。所以当你打电话时AudioFileCreateWithURL你使用kAudioFileAIFCType.

Big Endian 和 float 互不兼容,不要指定 bigEndian。

于 2013-02-28T02:16:03.540 回答