aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/midi/base.h
blob: 72e83b7d08e92744c1bf5126ca286cdecb5fb30a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef AL_MIDI_BASE_H
#define AL_MIDI_BASE_H

#include "alMain.h"
#include "atomic.h"
#include "evtqueue.h"

#ifdef __cplusplus
extern "C" {
#endif

struct ALsoundfont;

typedef size_t (*ReaderCb)(void *ptr, size_t size, void *stream);
typedef struct Reader {
    ReaderCb cb;
    void *ptr;
    int error;
} Reader;
#define READ(x_, buf_, len_) ((x_)->cb((buf_), (len_), (x_)->ptr))
#define READERR(x_)          ((x_)->error)

ALboolean loadSf2(Reader *stream, struct ALsoundfont *sfont, ALCcontext *context);


struct MidiSynthVtable;

typedef struct MidiSynth {
    EvtQueue EventQueue;

    ALuint64 LastEvtTime;
    ALuint64 NextEvtTime;
    ALdouble SamplesSinceLast;
    ALdouble SamplesToNext;

    ALdouble SamplesPerTick;

    /* NOTE: This rwlock is for the state and soundfont. The EventQueue and
     * related must instead use the device lock as they're used in the mixer
     * thread.
     */
    RWLock Lock;

    struct ALsoundfont **Soundfonts;
    ALsizei NumSoundfonts;

    volatile ALfloat Gain;
    volatile ALenum State;

    const struct MidiSynthVtable *vtbl;
} MidiSynth;

void MidiSynth_Construct(MidiSynth *self, ALCdevice *device);
void MidiSynth_Destruct(MidiSynth *self);
struct ALsoundfont *MidiSynth_getDefSoundfont(ALCcontext *context);
ALenum MidiSynth_selectSoundfonts(MidiSynth *self, ALCcontext *context, ALsizei count, const ALuint *ids);
inline void MidiSynth_setGain(MidiSynth *self, ALfloat gain) { self->Gain = gain; }
inline ALfloat MidiSynth_getGain(const MidiSynth *self) { return self->Gain; }
inline void MidiSynth_setState(MidiSynth *self, ALenum state) { ExchangeInt(&self->State, state); }
void MidiSynth_stop(MidiSynth *self);
inline void MidiSynth_reset(MidiSynth *self) { MidiSynth_stop(self); }
ALuint64 MidiSynth_getTime(const MidiSynth *self);
inline ALuint64 MidiSynth_getNextEvtTime(const MidiSynth *self)
{
    if(self->EventQueue.pos == self->EventQueue.size)
        return UINT64_MAX;
    return self->EventQueue.events[self->EventQueue.pos].time;
}
void MidiSynth_setSampleRate(MidiSynth *self, ALdouble srate);
inline void MidiSynth_update(MidiSynth *self, ALCdevice *device)
{ MidiSynth_setSampleRate(self, device->Frequency); }
ALenum MidiSynth_insertEvent(MidiSynth *self, ALuint64 time, ALuint event, ALsizei param1, ALsizei param2);
ALenum MidiSynth_insertSysExEvent(MidiSynth *self, ALuint64 time, const ALbyte *data, ALsizei size);


struct MidiSynthVtable {
    void (*const Destruct)(MidiSynth *self);

    ALenum (*const selectSoundfonts)(MidiSynth *self, ALCcontext *context, ALsizei count, const ALuint *ids);

    void (*const setGain)(MidiSynth *self, ALfloat gain);
    void (*const setState)(MidiSynth *self, ALenum state);

    void (*const stop)(MidiSynth *self);
    void (*const reset)(MidiSynth *self);

    void (*const update)(MidiSynth *self, ALCdevice *device);
    void (*const process)(MidiSynth *self, ALuint samples, ALfloat (*restrict DryBuffer)[BUFFERSIZE]);

    void (*const Delete)(MidiSynth *self);
};

#define DEFINE_MIDISYNTH_VTABLE(T)                                            \
DECLARE_THUNK(T, MidiSynth, void, Destruct)                                   \
DECLARE_THUNK3(T, MidiSynth, ALenum, selectSoundfonts, ALCcontext*, ALsizei, const ALuint*) \
DECLARE_THUNK1(T, MidiSynth, void, setGain, ALfloat)                          \
DECLARE_THUNK1(T, MidiSynth, void, setState, ALenum)                          \
DECLARE_THUNK(T, MidiSynth, void, stop)                                       \
DECLARE_THUNK(T, MidiSynth, void, reset)                                      \
DECLARE_THUNK1(T, MidiSynth, void, update, ALCdevice*)                        \
DECLARE_THUNK2(T, MidiSynth, void, process, ALuint, ALfloatBUFFERSIZE*restrict) \
DECLARE_THUNK(T, MidiSynth, void, Delete)                                     \
                                                                              \
static const struct MidiSynthVtable T##_MidiSynth_vtable = {                  \
    T##_MidiSynth_Destruct,                                                   \
                                                                              \
    T##_MidiSynth_selectSoundfonts,                                           \
    T##_MidiSynth_setGain,                                                    \
    T##_MidiSynth_setState,                                                   \
    T##_MidiSynth_stop,                                                       \
    T##_MidiSynth_reset,                                                      \
    T##_MidiSynth_update,                                                     \
    T##_MidiSynth_process,                                                    \
                                                                              \
    T##_MidiSynth_Delete,                                                     \
}


MidiSynth *FSynth_create(ALCdevice *device);
MidiSynth *DSynth_create(ALCdevice *device);

MidiSynth *SynthCreate(ALCdevice *device);

#ifdef __cplusplus
}
#endif

#endif /* AL_MIDI_BASE_H */