gpt4 book ai didi

c++ - 如何使用 coreAudio 将标准化浮点值数组写入 AIFF?

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:49 25 4
gpt4 key购买 nike

我试过下面的代码,但在执行它时给出了'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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com