GENMIDI.OP2 OPL2 sound bank: C++98/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++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.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++98/STL source code to parse GENMIDI.OP2 OPL2 sound bank

genmidi_op2.h

#ifndef GENMIDI_OP2_H_
#define GENMIDI_OP2_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>
#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 = 0, genmidi_op2_t* p__root = 0);

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 = 0, genmidi_op2_t* p__root = 0);

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

    public:
        ~instrument_entry_t();

    private:
        uint16_t m_flags;
        uint8_t m_finetune;
        uint8_t m_note;
        std::vector<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<instrument_t*>* instruments() const { return m_instruments; }
        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 = 0, genmidi_op2_t* p__root = 0);

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

    public:
        ~instrument_t();

    private:
        op_settings_t* m_op1;
        uint8_t m_feedback;
        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; }

        /**
         * Feedback/AM-FM (both operators)
         */
        uint8_t feedback() const { return m_feedback; }
        op_settings_t* op2() const { return m_op2; }
        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 = 0, genmidi_op2_t* p__root = 0);

    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::vector<instrument_entry_t*>* m_instruments;
    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<instrument_entry_t*>* instruments() const { return m_instruments; }
    std::vector<std::string>* instrument_names() const { return m_instrument_names; }
    genmidi_op2_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // GENMIDI_OP2_H_

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 = 0;
    m_instrument_names = 0;

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

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 = new std::vector<instrument_entry_t*>();
    const int l_instruments = 175;
    for (int i = 0; i < l_instruments; i++) {
        m_instruments->push_back(new instrument_entry_t(m__io, this, m__root));
    }
    m_instrument_names = 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(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() {
    if (m_instruments) {
        for (std::vector<instrument_entry_t*>::iterator it = m_instruments->begin(); it != m_instruments->end(); ++it) {
            delete *it;
        }
        delete m_instruments; m_instruments = 0;
    }
    if (m_instrument_names) {
        delete m_instrument_names; m_instrument_names = 0;
    }
}

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 = 0;

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

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 = new std::vector<instrument_t*>();
    const int l_instruments = 2;
    for (int i = 0; i < l_instruments; i++) {
        m_instruments->push_back(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() {
    if (m_instruments) {
        for (std::vector<instrument_t*>::iterator it = m_instruments->begin(); it != m_instruments->end(); ++it) {
            delete *it;
        }
        delete m_instruments; m_instruments = 0;
    }
}

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 = 0;
    m_op2 = 0;

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

void genmidi_op2_t::instrument_t::_read() {
    m_op1 = new op_settings_t(m__io, this, m__root);
    m_feedback = m__io->read_u1();
    m_op2 = 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() {
    if (m_op1) {
        delete m_op1; m_op1 = 0;
    }
    if (m_op2) {
        delete m_op2; m_op2 = 0;
    }
}

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;

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

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() {
}