IOS Core Audio - MP3 到 WAV 仅在 1 声道时工作,如何获得立体声?

IOS Core Audio - MP3 to WAV working only when 1 channel, how to get stereo?

目前正在接收一个 MP3 文件并输出一个 WAV。我的代码在一段时间内运行良好,但我现在想更改它,其中导出的 WAV 是一个 2 声道立体声文件。

问题就出在这里。这描述了所需的输出格式。

下面这段代码在之前运行良好(单声道):

AudioStreamBasicDescription outputFormat = new AudioStreamBasicDescription();
outputFormat.setFormat(AudioFormat.LinearPCM);
outputFormat.setFormatFlags(AudioFormatFlags.Canonical);
outputFormat.setBitsPerChannel(16);
outputFormat.setChannelsPerFrame(1);
outputFormat.setFramesPerPacket(1);
outputFormat.setBytesPerFrame(2);
outputFormat.setBytesPerPacket(2);
outputFormat.setSampleRate(pitch);

将其更改为 setChannelsPerFrame(2); 无效。不确定还需要更改什么?

错误是:

Launcher[318:12909] 224: SetDataFormat failed
Launcher[318:12909] 367: EXCEPTION (1718449215): "create audio file"

org.robovm.apple.corefoundation.OSStatusException: 1718449215
at org.robovm.apple.corefoundation.OSStatusException.throwIfNecessary(OSStatusException.java:53)
at org.robovm.apple.audiotoolbox.ExtAudioFile.create(ExtAudioFile.java:80)
at package.Launcher.mp3ToPCM(Launcher.java:1108)
...

有问题的行在哪里

outputFileExtAudio = ExtAudioFile.create(outputFileURL, AudioFileType.WAVE, outputFormat, null, AudioFileFlags.EraseFile);

但问题一定出在我的输出格式的 AudioStreamBasicDescription 上,因为这是唯一更改为“2 通道”的东西,突然它就不再有效了。

(这是 Java 代码,利用 RoboVM 转换为本机 IOS 代码。)

您还需要更新尺码。

在 Core Audio 中,一个样本是一个单一值,一帧是所有通道中的一个样本。对于PCM音频,单帧也是单包。

对于16位单声道音频,帧和样本是同义词,占用2个字节。对于 16 位立体声音频,一帧由两个样本(左和右)组成,每个样本占用 2 个字节,每帧占用 4 个字节。

AudioStreamBasicDescription 的值因描述的格式是否交错而略有不同。

你一般可以这样想 non-interleaved PCM AudioStreamBasicDescriptions:

asbd.mBytesPerFrame     = asbd.mBitsPerChannel / 8;

交错像这样:

asbd.mBytesPerFrame     = (asbd.mBitsPerChannel / 8) * asbd.mChannelsPerFrame;

两者

asbd.mFramesPerPacket   = 1;
asbd.mBytesPerPacket    = asbd.mBytesPerFrame;

AudioFormatFlags.Canonical 已弃用,但我在这里假设它等同于交错打包的本机字节序有符号整数。

所以对于你的情况,交错的 16 位立体声是:

AudioStreamBasicDescription outputFormat = new AudioStreamBasicDescription();
outputFormat.setFormat(AudioFormat.LinearPCM);
outputFormat.setFormatFlags(AudioFormatFlags.Canonical);

outputFormat.setSampleRate(pitch);
outputFormat.setChannelsPerFrame(2);
outputFormat.setBitsPerChannel(16);

outputFormat.setBytesPerFrame(4);
outputFormat.setFramesPerPacket(1);
outputFormat.setBytesPerPacket(4);

这里有两个辅助函数(在 C++ 中)显示了关系:

static AudioFormatFlags CalculateLPCMFlags(UInt32 validBitsPerChannel, UInt32 totalBitsPerChannel, bool isFloat, bool isBigEndian, bool isNonInterleaved)
{
    return (isFloat ? kAudioFormatFlagIsFloat : kAudioFormatFlagIsSignedInteger) | (isBigEndian ? ((UInt32)kAudioFormatFlagIsBigEndian) : 0) | ((validBitsPerChannel == totalBitsPerChannel) ? kAudioFormatFlagIsPacked : kAudioFormatFlagIsAlignedHigh) | (isNonInterleaved ? ((UInt32)kAudioFormatFlagIsNonInterleaved) : 0);
}

static void FillOutASBDForLPCM(AudioStreamBasicDescription *asbd, Float64 sampleRate, UInt32 channelsPerFrame, UInt32 validBitsPerChannel, UInt32 totalBitsPerChannel, bool isFloat, bool isBigEndian, bool isNonInterleaved)
{
    asbd->mFormatID = kAudioFormatLinearPCM;
    asbd->mFormatFlags = CalculateLPCMFlags(validBitsPerChannel, totalBitsPerChannel, isFloat, isBigEndian, isNonInterleaved);

    asbd->mSampleRate = sampleRate;
    asbd->mChannelsPerFrame = channelsPerFrame;
    asbd->mBitsPerChannel = validBitsPerChannel;

    asbd->mBytesPerPacket = (isNonInterleaved ? 1 : channelsPerFrame) * (totalBitsPerChannel / 8);
    asbd->mFramesPerPacket = 1;
    asbd->mBytesPerFrame = (isNonInterleaved ? 1 : channelsPerFrame) * (totalBitsPerChannel / 8);
}