AU audio format: C++98/STL parsing library

This page hosts a formal specification of AU audio format using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.au", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    au_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.magic() // => get magic

C++98/STL source code to parse AU audio format

au.h

#ifndef AU_H_
#define AU_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * The NeXT/Sun audio file format.
 * 
 * Sample files:
 * 
 * * <https://github.com/python/cpython/tree/b8a7daf077da/Lib/test/sndhdrdata>
 * * <ftp://ftp-ccrma.stanford.edu/pub/Lisp/sf.tar.gz>
 * * <https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/AU/Samples.html>
 * \sa https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/AU/AU.html Source
 * \sa http://soundfile.sapp.org/doc/NextFormat/ Source
 * \sa http://soundfile.sapp.org/doc/NextFormat/soundstruct.h Source
 * \sa https://github.com/andreiw/polaris/blob/deb47cb/usr/src/head/audio/au.h#L87-L112 Source
 * \sa https://github.com/libsndfile/libsndfile/blob/86c9f9eb/src/au.c#L39-L74 Source
 * \sa https://github.com/chirlu/sox/blob/dd8b63bd/src/au.c#L34-L49 Source
 * \sa https://github.com/mpruett/audiofile/blob/b62c902/libaudiofile/NeXT.cpp#L65-L96 Source
 */

class au_t : public kaitai::kstruct {

public:
    class header_t;

    enum encodings_t {
        ENCODINGS_MULAW_8 = 1,
        ENCODINGS_LINEAR_8 = 2,
        ENCODINGS_LINEAR_16 = 3,
        ENCODINGS_LINEAR_24 = 4,
        ENCODINGS_LINEAR_32 = 5,
        ENCODINGS_FLOAT = 6,
        ENCODINGS_DOUBLE = 7,
        ENCODINGS_FRAGMENTED = 8,
        ENCODINGS_NESTED = 9,
        ENCODINGS_DSP_CORE = 10,
        ENCODINGS_FIXED_POINT_8 = 11,
        ENCODINGS_FIXED_POINT_16 = 12,
        ENCODINGS_FIXED_POINT_24 = 13,
        ENCODINGS_FIXED_POINT_32 = 14,
        ENCODINGS_DISPLAY = 16,
        ENCODINGS_MULAW_SQUELCH = 17,
        ENCODINGS_EMPHASIZED = 18,
        ENCODINGS_COMPRESSED = 19,
        ENCODINGS_COMPRESSED_EMPHASIZED = 20,
        ENCODINGS_DSP_COMMANDS = 21,
        ENCODINGS_DSP_COMMANDS_SAMPLES = 22,
        ENCODINGS_ADPCM_G721 = 23,
        ENCODINGS_ADPCM_G722 = 24,
        ENCODINGS_ADPCM_G723_3 = 25,
        ENCODINGS_ADPCM_G723_5 = 26,
        ENCODINGS_ALAW_8 = 27,
        ENCODINGS_AES = 28,
        ENCODINGS_DELTA_MULAW_8 = 29
    };

    au_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, au_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~au_t();

    class header_t : public kaitai::kstruct {

    public:

        header_t(kaitai::kstream* p__io, au_t* p__parent = 0, au_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~header_t();

    private:
        uint32_t m_data_size;
        encodings_t m_encoding;
        uint32_t m_sample_rate;
        uint32_t m_num_channels;
        std::string m_comment;
        au_t* m__root;
        au_t* m__parent;

    public:

        /**
         * don't read this field, access `_root.len_data` instead
         * 
         * value `0xffff_ffff` means unspecified size
         */
        uint32_t data_size() const { return m_data_size; }
        encodings_t encoding() const { return m_encoding; }
        uint32_t sample_rate() const { return m_sample_rate; }

        /**
         * number of interleaved channels
         */
        uint32_t num_channels() const { return m_num_channels; }

        /**
         * Most resources claim that this field must be at least 4 bytes long.
         * However, most programs don't enforce it, and [Audacity](
         * https://www.audacityteam.org/) even generates .au files with this field
         * being 0-byte long. According to <https://nixdoc.net/man-pages/IRIX/man4/dmedia/next.4.html>,
         * "NeXT files require that this chunk be at least 4 bytes (chars) long,
         * whereas this chunk may be zerolength in a Sun .au file."
         * 
         * By convention, size should be a multiple of 4 -
         * see <https://github.com/chirlu/sox/blob/dd8b63bd/src/au.c#L132-L133>.
         * Page <http://soundfile.sapp.org/doc/NextFormat/> also mentions that for some
         * sound playing programs, this field must have an even byte size. So a multiple
         * of 4 is probably best for compatibility.
         * 
         * Must be null-terminated. It is usually an ASCII text string, but this space
         * might be also used to store application-specific binary (i.e. non-ASCII) data.
         */
        std::string comment() const { return m_comment; }
        au_t* _root() const { return m__root; }
        au_t* _parent() const { return m__parent; }
    };

private:
    bool f_len_data;
    int32_t m_len_data;

public:
    int32_t len_data();

private:
    std::string m_magic;
    uint32_t m_ofs_data;
    header_t* m_header;
    au_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_header;
    kaitai::kstream* m__io__raw_header;

public:
    std::string magic() const { return m_magic; }
    uint32_t ofs_data() const { return m_ofs_data; }
    header_t* header() const { return m_header; }
    au_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::string _raw_header() const { return m__raw_header; }
    kaitai::kstream* _io__raw_header() const { return m__io__raw_header; }
};

#endif  // AU_H_

au.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "au.h"
#include "kaitai/exceptions.h"

au_t::au_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, au_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_header = 0;
    m__io__raw_header = 0;
    f_len_data = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void au_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x2E\x73\x6E\x64", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x2E\x73\x6E\x64", 4), magic(), _io(), std::string("/seq/0"));
    }
    m_ofs_data = m__io->read_u4be();
    m__raw_header = m__io->read_bytes(((ofs_data() - 4) - 4));
    m__io__raw_header = new kaitai::kstream(m__raw_header);
    m_header = new header_t(m__io__raw_header, this, m__root);
}

au_t::~au_t() {
    _clean_up();
}

void au_t::_clean_up() {
    if (m__io__raw_header) {
        delete m__io__raw_header; m__io__raw_header = 0;
    }
    if (m_header) {
        delete m_header; m_header = 0;
    }
}

au_t::header_t::header_t(kaitai::kstream* p__io, au_t* p__parent, au_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void au_t::header_t::_read() {
    m_data_size = m__io->read_u4be();
    m_encoding = static_cast<au_t::encodings_t>(m__io->read_u4be());
    m_sample_rate = m__io->read_u4be();
    m_num_channels = m__io->read_u4be();
    if (!(num_channels() >= 1)) {
        throw kaitai::validation_less_than_error<uint32_t>(1, num_channels(), _io(), std::string("/types/header/seq/3"));
    }
    m_comment = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(m__io->read_bytes_full(), 0, false), std::string("ASCII"));
}

au_t::header_t::~header_t() {
    _clean_up();
}

void au_t::header_t::_clean_up() {
}

int32_t au_t::len_data() {
    if (f_len_data)
        return m_len_data;
    m_len_data = ((header()->data_size() == 4294967295UL) ? ((_io()->size() - ofs_data())) : (header()->data_size()));
    f_len_data = true;
    return m_len_data;
}