#pragma once

#include "sequencer.hpp"
#include "midisynth.hpp"
#include "kmp_pi.h"

class KbFmMidiDecoder : output
{
private:
    enum{
        MAX_NUM_PORTS = 4
    };
    fm_note_factory m_note_factory;
    synthesizer     m_synthesizer;
    sequencer       m_sequencer;
    synthesizer*    m_synthesizers[MAX_NUM_PORTS];
    int_least32_t  *m_pMixingBuffer;
    DWORD m_dwSampleRate;
    DWORD m_dwNumPorts;
    DWORD m_dwCurrentSample;
    DWORD m_dwLastSample;
    BOOL  m_bSeeking;
    void  __fastcall Close(void);
    void midi_message(int port, uint_least32_t message);
    void sysex_message(int port, const void* data, size_t size);
    void meta_event(int type, const void* data, size_t size);
    void reset();
public:
    KbFmMidiDecoder(void);
    ~KbFmMidiDecoder(void);
    BOOL  __fastcall Open(const char *cszFileName, SOUNDINFO *pInfo);
    DWORD __fastcall SetPosition(DWORD dwPos);
    DWORD __fastcall Render(BYTE *pBuffer, DWORD dwSize);
};
