GENMIDI.OP2 OPL2 sound bank: C++11/STL parsing library

GENMIDI.OP2 is a sound bank file used by players based on DMX sound library to play MIDI files with General MIDI instruments using OPL2 sound chip (which was commonly installed on popular AdLib and Sound Blaster sound cards).

Major users of DMX sound library include:

  • Original Doom game engine (and games based on it: Heretic, Hexen, Strife, Chex Quest)
  • Raptor: Call of the Shadows

File extension

op2

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

References

This page hosts a formal specification of GENMIDI.OP2 OPL2 sound bank 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++11/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.op2", 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:
    genmidi_op2_t data(&ks);
    

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

data.magic() // => get magic

C++11/STL source code to parse GENMIDI.OP2 OPL2 sound bank

genmidi_op2.h

#pragma once

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

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

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

/**
 * GENMIDI.OP2 is a sound bank file used by players based on DMX sound
 * library to play MIDI files with General MIDI instruments using OPL2
 * sound chip (which was commonly installed on popular AdLib and Sound
 * Blaster sound cards).
 * 
 * Major users of DMX sound library include:
 * 
 * * Original Doom game engine (and games based on it: Heretic, Hexen, Strife, Chex Quest)
 * * Raptor: Call of the Shadows
 * \sa http://www.fit.vutbr.cz/~arnost/muslib/op2_form.zip Source
 * \sa https://doom.fandom.com/wiki/GENMIDI Source
 * \sa https://moddingwiki.shikadi.net/wiki/OP2_Bank_Format Source
 */

class genmidi_op2_t : public kaitai::kstruct {

public:
    class instrument_entry_t;
    class instrument_t;
    class op_settings_t;

    genmidi_op2_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, genmidi_op2_t* p__root = nullptr);

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

public:
    ~genmidi_op2_t();

    class instrument_entry_t : public kaitai::kstruct {

    public:

        instrument_entry_t(kaitai::kstream* p__io, genmidi_op2_t* p__parent = nullptr, genmidi_op2_t* p__root = nullptr);

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

    public:
        ~instrument_entry_t();

    private:
        uint16_t m_flags;
        uint8_t m_finetune;
        uint8_t m_note;
        std::unique_ptr<std::vector<std::unique_ptr<instrument_t>>> m_instruments;
        genmidi_op2_t* m__root;
        genmidi_op2_t* m__parent;

    public:
        uint16_t flags() const { return m_flags; }
        uint8_t finetune() const { return m_finetune; }

        /**
         * MIDI note for fixed instruments, 0 otherwise
         */
        uint8_t note() const { return m_note; }
        std::vector<std::unique_ptr<instrument_t>>* instruments() const { return m_instruments.get(); }
        genmidi_op2_t* _root() const { return m__root; }
        genmidi_op2_t* _parent() const { return m__parent; }
    };

    class instrument_t : public kaitai::kstruct {

    public:

        instrument_t(kaitai::kstream* p__io, genmidi_op2_t::instrument_entry_t* p__parent = nullptr, genmidi_op2_t* p__root = nullptr);

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

    public:
        ~instrument_t();

    private:
        std::unique_ptr<op_settings_t> m_op1;
        uint8_t m_feedback;
        std::unique_ptr<op_settings_t> m_op2;
        uint8_t m_unused;
        int16_t m_base_note;
        genmidi_op2_t* m__root;
        genmidi_op2_t::instrument_entry_t* m__parent;

    public:
        op_settings_t* op1() const { return m_op1.get(); }

        /**
         * Feedback/AM-FM (both operators)
         */
        uint8_t feedback() const { return m_feedback; }
        op_settings_t* op2() const { return m_op2.get(); }
        uint8_t unused() const { return m_unused; }

        /**
         * Base note offset
         */
        int16_t base_note() const { return m_base_note; }
        genmidi_op2_t* _root() const { return m__root; }
        genmidi_op2_t::instrument_entry_t* _parent() const { return m__parent; }
    };

    /**
     * OPL2 settings for one operator (carrier or modulator)
     */

    class op_settings_t : public kaitai::kstruct {

    public:

        op_settings_t(kaitai::kstream* p__io, genmidi_op2_t::instrument_t* p__parent = nullptr, genmidi_op2_t* p__root = nullptr);

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

    public:
        ~op_settings_t();

    private:
        uint8_t m_trem_vibr;
        uint8_t m_att_dec;
        uint8_t m_sust_rel;
        uint8_t m_wave;
        uint8_t m_scale;
        uint8_t m_level;
        genmidi_op2_t* m__root;
        genmidi_op2_t::instrument_t* m__parent;

    public:

        /**
         * Tremolo/vibrato/sustain/KSR/multi
         */
        uint8_t trem_vibr() const { return m_trem_vibr; }

        /**
         * Attack rate/decay rate
         */
        uint8_t att_dec() const { return m_att_dec; }

        /**
         * Sustain level/release rate
         */
        uint8_t sust_rel() const { return m_sust_rel; }

        /**
         * Waveform select
         */
        uint8_t wave() const { return m_wave; }

        /**
         * Key scale level
         */
        uint8_t scale() const { return m_scale; }

        /**
         * Output level
         */
        uint8_t level() const { return m_level; }
        genmidi_op2_t* _root() const { return m__root; }
        genmidi_op2_t::instrument_t* _parent() const { return m__parent; }
    };

private:
    std::string m_magic;
    std::unique_ptr<std::vector<std::unique_ptr<instrument_entry_t>>> m_instruments;
    std::unique_ptr<std::vector<std::string>> m_instrument_names;
    genmidi_op2_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }
    std::vector<std::unique_ptr<instrument_entry_t>>* instruments() const { return m_instruments.get(); }
    std::vector<std::string>* instrument_names() const { return m_instrument_names.get(); }
    genmidi_op2_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

genmidi_op2.cpp

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

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

genmidi_op2_t::genmidi_op2_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, genmidi_op2_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_instruments = nullptr;
    m_instrument_names = nullptr;
    _read();
}

void genmidi_op2_t::_read() {
    m_magic = m__io->read_bytes(8);
    if (!(magic() == std::string("\x23\x4F\x50\x4C\x5F\x49\x49\x23", 8))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x23\x4F\x50\x4C\x5F\x49\x49\x23", 8), magic(), _io(), std::string("/seq/0"));
    }
    m_instruments = std::unique_ptr<std::vector<std::unique_ptr<instrument_entry_t>>>(new std::vector<std::unique_ptr<instrument_entry_t>>());
    const int l_instruments = 175;
    for (int i = 0; i < l_instruments; i++) {
        m_instruments->push_back(std::move(std::unique_ptr<instrument_entry_t>(new instrument_entry_t(m__io, this, m__root))));
    }
    m_instrument_names = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
    const int l_instrument_names = 175;
    for (int i = 0; i < l_instrument_names; i++) {
        m_instrument_names->push_back(std::move(kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(kaitai::kstream::bytes_strip_right(m__io->read_bytes(32), 0), 0, false), std::string("ASCII"))));
    }
}

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

void genmidi_op2_t::_clean_up() {
}

genmidi_op2_t::instrument_entry_t::instrument_entry_t(kaitai::kstream* p__io, genmidi_op2_t* p__parent, genmidi_op2_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_instruments = nullptr;
    _read();
}

void genmidi_op2_t::instrument_entry_t::_read() {
    m_flags = m__io->read_u2le();
    m_finetune = m__io->read_u1();
    m_note = m__io->read_u1();
    m_instruments = std::unique_ptr<std::vector<std::unique_ptr<instrument_t>>>(new std::vector<std::unique_ptr<instrument_t>>());
    const int l_instruments = 2;
    for (int i = 0; i < l_instruments; i++) {
        m_instruments->push_back(std::move(std::unique_ptr<instrument_t>(new instrument_t(m__io, this, m__root))));
    }
}

genmidi_op2_t::instrument_entry_t::~instrument_entry_t() {
    _clean_up();
}

void genmidi_op2_t::instrument_entry_t::_clean_up() {
}

genmidi_op2_t::instrument_t::instrument_t(kaitai::kstream* p__io, genmidi_op2_t::instrument_entry_t* p__parent, genmidi_op2_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_op1 = nullptr;
    m_op2 = nullptr;
    _read();
}

void genmidi_op2_t::instrument_t::_read() {
    m_op1 = std::unique_ptr<op_settings_t>(new op_settings_t(m__io, this, m__root));
    m_feedback = m__io->read_u1();
    m_op2 = std::unique_ptr<op_settings_t>(new op_settings_t(m__io, this, m__root));
    m_unused = m__io->read_u1();
    m_base_note = m__io->read_s2le();
}

genmidi_op2_t::instrument_t::~instrument_t() {
    _clean_up();
}

void genmidi_op2_t::instrument_t::_clean_up() {
}

genmidi_op2_t::op_settings_t::op_settings_t(kaitai::kstream* p__io, genmidi_op2_t::instrument_t* p__parent, genmidi_op2_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void genmidi_op2_t::op_settings_t::_read() {
    m_trem_vibr = m__io->read_u1();
    m_att_dec = m__io->read_u1();
    m_sust_rel = m__io->read_u1();
    m_wave = m__io->read_u1();
    m_scale = m__io->read_u1();
    m_level = m__io->read_u1();
}

genmidi_op2_t::op_settings_t::~op_settings_t() {
    _clean_up();
}

void genmidi_op2_t::op_settings_t::_clean_up() {
}