使用ExtAudioFile编写ALAC文件会导致文件大于AIFF

时间:2013-12-18 01:30:47

标签: macos core-audio

我正在尝试从内存中保存的数据中编写ALAC文件。如果我将文件写为AIFF,则文件为43.5MB。如果我将文件写为ALAC,则为73.9MB。这两个文件播放完美,但显然ALAC应该小于AIFF。 (很明显,当我使用iTunes将AIFF转换为ALAC时,生成的文件为28MB,这与我对无损压缩格式的期望大致相同)

Finder's file info

明显的区别是ALAC每个样本缺少Bits。

我是否错过了一些压缩设置,或者设置了哪些会使ALAC文件变小?

数据作为非交错浮点存储在内存中,这是处理格式设置的代码。

_outputFormat.mSampleRate = 44100
_outputFormat.mFormatID = <either kAudioFormatLinearPCM or kAudioFormatAppleLossless>;
_outputFormat.mChannelsPerFrame = 2;

if (_outputFormat.mFormatID == kAudioFormatLinearPCM) {
    _outputFormat.mBitsPerChannel = 16;
    _outputFormat.mBytesPerPacket = _outputFormat.mChannelsPerFrame * (_outputFormat.mBitsPerChannel / 8);
    _outputFormat.mFramesPerPacket = 1;
    _outputFormat.mBytesPerFrame = _outputFormat.mBytesPerPacket;
    _outputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;

    if (_isBigEndian) {
        _outputFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
    }
} else {
    size = sizeof(_outputFormat);
    err = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &_outputFormat);
    if (check_status_is_error(err, "AudioFileGetProperty")) {
        self = nil;
        return nil;
    }
}

CFURLRef urlRef = (__bridge CFURLRef)_url;

err = ExtAudioFileCreateWithURL(urlRef,
                                <either kAudioFileAIFFType or kAudioFileM4AType>,
                                &(_outputFormat),
                                NULL, kAudioFileFlags_EraseFile, &(_outputFile));
if (check_status_is_error(err, "ExtAudioFileCreateWithURL")) {
    self = nil;
    return nil;
}

AudioStreamBasicDescription clientFormat;

clientFormat.mFormatID = kAudioFormatLinearPCM;
clientFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical;
clientFormat.mSampleRate = _outputFormat.mSampleRate;
clientFormat.mChannelsPerFrame = _outputFormat.mChannelsPerFrame;
clientFormat.mFramesPerPacket = 1;
clientFormat.mBytesPerFrame = 4;
clientFormat.mBytesPerPacket = 4;
clientFormat.mBitsPerChannel = 8 * sizeof(AudioUnitSampleType);

size = sizeof(clientFormat);
err = ExtAudioFileSetProperty(_outputFile, 
                              kExtAudioFileProperty_ClientDataFormat,
                              size, &clientFormat);

1 个答案:

答案 0 :(得分:1)

解决方案是在保存ALAC文件时将AudioStreamBasicDescription结构的mFormatFlags设置为包含kAppleLosslessFormatFlag_16BitSourceData