【问题标题】:Writing ALAC files with ExtAudioFile results in larger files than AIFF使用 ExtAudioFile 写入 ALAC 文件会产生比 AIFF 更大的文件
【发布时间】:2013-12-18 01:30:46
【问题描述】:

我正在尝试从内存中保存的数据写入 ALAC 文件。如果我将文件写为 AIFF,则文件为 43.5MB。如果我将文件写为 ALAC,它的大小为 73.9MB。这两个文件播放完美,但显然 ALAC 应该比 AIFF 小。 (顺便说一下,当我使用 iTunes 将 AIFF 转换为 ALAC 时,生成的文件为 28MB,这大致是我对无损压缩格式的预期)

明显的区别是 ALAC 缺少每个样本的比特数。

我是否遗漏了一些压缩设置,或者一些可以使 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);

【问题讨论】:

    标签: macos core-audio


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多