python - 使用 CoreAudio 获取正确的 FileLengthFrames

标签 python objective-c core-audio extaudiofile

我正在努力将我的 Python 代码转换为 Objective C 以在 iOS 设备上运行。读取音频文件的代码。在Python中,我使用AudioSegment来读取文件,结果是数组中的2个独立 channel 。

例如:

Left channel  [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]  //length = 10
Right channel [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]   //length = 10

所以Python的总长度是20

这是我在 objective-c 中获取音频输出的方法

float *audioTotal = malloc(fileLengthInFrames * sizeof(float));
SInt16 *inputFrames = (SInt16*)bufferList->mBuffers[0].mData;
for(int i = 0; i < fileLengthInFrames; ++i) {
    audioTotal[i] = (float)inputFrames[i];
    printf("%f ", audioTotal[i]);
}

输出是:

[-1, 1, -2, 2, -3, 3, -4, 4, -5, 5] // length = 10

因此 objective-c 的输出是左右 channel 混合的。所以我必须通过代码将它们分开:

if (clientFormat.mChannelsPerFrame > 1) {
        int indexLeft = 0;
        int indexRight = 0;
        float *leftAudio = malloc(fileLengthInFrames* sizeof(float));
        float *rightAudio = malloc(fileLengthInFrames * sizeof(float));
        for(int i = 0; i < fileLengthInFrames; i++) {
            if (i%2 == 0) {
                leftAudio[indexLeft] = audioTotal[i];
                printf("%f ", leftAudio[indexLeft]);
                indexLeft ++;
            } else {
                rightAudio[indexRight] = audioTotal[i];
                printf("%f ", rightAudio[indexRight]);
                indexRight ++;
            }
        }
}

现在我有 2 个与 objective-c 分离的 channel :

Left channel  [-1,-2,-3,-4,-5]  //length = 5
Right channel [ 1, 2, 3, 4, 5]   //length = 5

所以我从 objective-c 得到的总长度是 10,而 python 中是 20。 我的其余数据在哪里?我错过了一些步骤吗?或者配置错误? 感谢您的帮助。

最佳答案

当您有交错样本并且“通过代码分隔它们”时,您会忘记乘以 channelsPerBuffer (这似乎是交错的精明?),因此对于立体声您会丢失一半的样本。尝试将 for 循环更改为

for(int i = 0; i < fileLengthInFrames*channelsPerBuffer; i++) {
    // display left and right samples here ...
}

audioTotal 的长度也应为 fileLengthInFrames*channelsPerBuffer

附:如果客户端和文件采样率相同,为什么要重新计算fileLengthInFrames

关于python - 使用 CoreAudio 获取正确的 FileLengthFrames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645402/

相关文章:

python - 列名和相应的数据在 python 中不匹配

python - 如何使用 re.compile 和 re.findall 删除括号内的文本?

python - 有人可以解释一下 np.log 是做什么的吗?

ios - 在iOS中如何在后台运行状态下获取全局触摸事件通知?

swift - 具有非默认但仍为设备原生采样率的 AVAudioSinkNode

ios - 使耳机麦克风与iOS上的AVAudioRecorder配合使用

python - pycrypto 不会为 AES(CFB 模式)复制 NIST 测试向量

iphone - UINavigationBar 高度和横向模式

objective-c - 在 OS X 上获取活跃的网络连接

ios - AVAudioEngine schedule sample 准确的参数变化