我正在将OS X上的外部解码库中的音频写入AIFF文件,我可以使用 OSSwapInt32()交换数据的字节顺序。
生成的AIFF文件( 16位PCM立体声)确实播放,但左右声道已交换。
在编写每个缓冲区时,有没有办法交换频道?
以下是相关循环:
do
{
xmp_get_frame_info(writer_context, &writer_info);
if (writer_info.loop_count > 0)
break;
writeModBuffer.mBuffers[0].mDataByteSize = writer_info.buffer_size;
writeModBuffer.mBuffers[0].mNumberChannels = inputFormat.mChannelsPerFrame;
// Set up our buffer to do the endianness swap
void *new_buffer;
new_buffer = malloc((writer_info.buffer_size) * inputFormat.mBytesPerFrame);
int *ourBuffer = writer_info.buffer;
int *ourNewBuffer = new_buffer;
memset(new_buffer, 0, writer_info.buffer_size);
int i;
for (i = 0; i <= writer_info.buffer_size; i++)
{
ourNewBuffer[i] = OSSwapInt32(ourBuffer[i]);
};
writeModBuffer.mBuffers[0].mData = ourNewBuffer;
frame_size = writer_info.buffer_size / inputFormat.mBytesPerFrame;
err = ExtAudioFileWrite(writeModRef, frame_size, &writeModBuffer);
} while (xmp_play_frame(writer_context) == 0);
答案 0 :(得分:0)
此解决方案非常特定于2声道音频。我选择在循环更改字节顺序的同时执行此操作以避免额外的循环。我正在经历循环1/2数量并且每次迭代处理两个样本。样本是交错的,因此我从奇数样本索引复制到偶数样本索引,反之亦然。
for (i = 0; i <= writer_info.buffer_size/2; i++)
{
ourNewBuffer[i*2] = OSSwapInt32(ourBuffer[i*2 + 1]);
ourNewBuffer[i*2 + 1] = OSSwapInt32(ourBuffer[i*2]);
};
另一种方法是使用表查找进行通道映射。