SFBAudioEngine Documentation

repository·main·Indexed 20 days ago

https://github.com/sbooth/sfbaudioengine

A comprehensive audio processing engine for Apple platforms (macOS, iOS, tvOS) providing capabilities for decoding, playback, encoding, and format conversion. It includes tools like AudioPlayer for file playback, AudioFile for metadata extraction, and AudioConverter for high-level audio format conversion via a decode-convert-encode pipeline.

Tokens
1.1K
Snippets
4
Records
9
Agent score
22%

What's inside SFBAudioEngine

  1. How audio conversion works in SFBAudioConverter

    main

    The SFBAudioConverter performs high-level conversion using a three-step pipeline:

    1. Decode: An audio decoder reads PCM audio from the source in its native processing format.
    2. Intermediate Conversion: The audio is converted to an intermediate PCM format.
    3. Encode: An audio encoder writes that intermediate PCM audio to the final desired format.

    Note: The decoder's processing format and the intermediate format must both be PCM, but they do not need to share the same sample rate, bit depth, channel count, or channel layout.

  2. How audio encoding works in SFBAudioEngine

    main

    Encoders process input data and write it to an SFBOutputTarget (file, buffer, or data).

    • All encoders implement the SFBAudioEncoding protocol.
    • PCM-consuming encoders additionally implement SFBPCMEncoding.
    • The processing format used by an encoder is determined by a combination of the desired output format and the encoder's specific settings.
  3. How audio decoding works in SFBAudioEngine

    main

    Decoders in SFBAudioEngine read data from an SFBInputSource (which can be a file, buffer, or data) and are categorized by their output type:

    • PCM Decoders: Implement SFBAudioDecoding and additionally SFBPCMDecoding. These produce Pulse Code Modulation output.
    • DSD Decoders: Implement SFBAudioDecoding and additionally SFBDSDDecoding. These produce Direct Stream Digital output.

    Decoder Decorators

    Several special subclasses can wrap (decorate) an underlying decoder:

    • SFBAudioRegionDecoder: For seekable inputs, allows playback and looping of a specific PCM decoder region.
    • SFBDoPDecoder: Provides DSD over PCM (DoP) output from a DSD decoder.
    • SFBDSDPCMDecoder: Provides PCM output from a DSD decoder.
  4. Convert audio files using AudioConverter

    main

    Simple File Conversion

    Convert one file to another by specifying the input and output URLs. The output format is automatically inferred from the file extension.

    let inputURL = URL(fileURLWithPath: "music.wav")
    let outputURL = URL(fileURLWithPath: "music.flac")
    try AudioConverter.convert(inputURL, to: outputURL)

    Complex Conversion to Data

    For more control, you can convert to a Data object using an AudioEncoder and an OutputTarget.

    let output = OutputTarget.makeForData()
    let encoder = try AudioEncoder(outputTarget: output, encoderName: .coreAudio)
    encoder.settings = [
        .coreAudioFileTypeID: kAudioFileM4AType,
        .coreAudioFormatID: kAudioFormatMPEG4AAC,
        .coreAudioAudioConverterPropertySettings: [kAudioConverterCodecQuality: kAudioConverterQuality_High]
    ]
    try AudioConverter.convert(inputURL, using: encoder)
    // Encoder output is in `output.data`
  5. Play an audio file with AudioPlayer

    main

    You can play an audio file using AudioPlayer. Note that only file URLs are supported.

    Important: Ensure you use a file URL, as network or other URL types are not supported for playback.

    import SFBAudioEngine
    let player = AudioPlayer()
    let url = URL(fileURLWithPath: "example.flac")
    try? player.play(url)
  6. Read audio properties and metadata

    main

    Use AudioFile to extract properties (like sample rate) and metadata (like title) from an audio file. Properties are read-only, while metadata is writable for most formats.

    if let audioFile = try? AudioFile(readingPropertiesAndMetadataFrom: url) {
        let sampleRate = audioFile.properties.sampleRate
        let title = audioFile.metadata.title
    }
  7. Musepack (MPC) licensing information

    main

    SFBAudioEngine utilizes Musepack components for audio processing. Note the following licensing distinction:

    • Decoder (libmpcdec): Distributed under a BSD license.
    • Encoder (libmpcenc): Distributed under the LGPL (GNU Lesser General Public License) version 2.1.

    When using or distributing software that incorporates these components, ensure compliance with the respective BSD and LGPL requirements, particularly regarding attribution and redistribution of source code for the encoder.