我已经尝试了以下代码,但是在执行时会给出“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;
}

最佳答案

AIFF类型不支持 float 数据,您需要创建一个AIFF-C文件。因此,当您调用AudioFileCreateWithURL时,您将使用kAudioFileAIFCType

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

关于c++ - 如何使用coreAudio将标准化浮点值数组写入AIFF?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12159418/

10-10 02:35