c++ - 核心音频指定要解码的音轨

标签 c++ audio core-audio

我能够使用Core Audio API成功获取音频文件的解码PCM数据。下面是简化后的代码,显示了如何执行此操作:

CFStringRef urlStr = CFStringCreateWithCString(kCFAllocatorDefault, "file.m4a", kCFStringEncodingUTF8);
CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, urlStr, kCFURLPOSIXPathStyle, false);    

ExtAudioFileOpenURL(urlRef, &m_audioFile);

bzero(&m_outputFormat, sizeof(AudioStreamBasicDescription));
m_outputFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
m_outputFormat.mSampleRate = m_inputFormat.mSampleRate;
m_outputFormat.mFormatID = kAudioFormatLinearPCM;
m_outputFormat.mChannelsPerFrame = m_inputFormat.mChannelsPerFrame;
m_outputFormat.mBytesPerFrame = sizeof(short) * m_outputFormat.mChannelsPerFrame;
m_outputFormat.mBitsPerChannel = sizeof(short) * 8;
m_outputFormat.mFramesPerPacket = 1;
m_outputFormat.mBytesPerPacket = m_outputFormat.mBytesPerFrame * m_outputFormat.mFramesPerPacket;


ExtAudioFileSetProperty(m_audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(m_outputFormat), &m_outputFormat)

short* transformData = new short[sampleCount];    

AudioBufferList fillBufList;
fillBufList.mNumberBuffers = 1;
fillBufList.mBuffers[0].mNumberChannels = channels;
fillBufList.mBuffers[0].mDataByteSize = m_sampleCount * sizeof(short);
fillBufList.mBuffers[0].mData = (void*)(&transformData[0]);

ExtAudioFileRead(m_audioFile, &m_frameCount, &fillBufList);

我对感兴趣,如何指定我要解码的音轨(假设媒体文件包含多个)?

最佳答案

一种方法是解码所有轨道,然后将所需的轨道数据(隔行立体声的每个其他样本等)提取(复制)到另一个缓冲区,阵列或文件中。与解码时间相比,额外的复制时间微不足道。

关于c++ - 核心音频指定要解码的音轨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47717072/

相关文章:

c++ - 读取由结构定义的二进制文件

java - 如何完全加载音频但稍后再播放?

cocoa - 无声音播放时核心音频 CPU 使用率高

macos - 将LPCM缓冲区转换为AAC以进行HTTP实时流传输

c++ - xcode:添加链接器标志和 "link binary with libraries"之间的区别

c++ - 如何终止使用 ShellExecute 启动的程序

c++ - 您可以在哪些开源项目中看到高级内存管理的使用?

Android 5.0 Lollipop 通知全音不播放

algorithm - 确定最佳音频质量

iphone - 从 IO 单元将音频写入磁盘