Use an ambisonics-based panning method
[openal-soft.git] / Alc / midi / base.h
blob157f2399aeae9558155f0bccbaab9a12fb814832
1 #ifndef AL_MIDI_BASE_H
2 #define AL_MIDI_BASE_H
4 #include "alMain.h"
5 #include "atomic.h"
6 #include "evtqueue.h"
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 struct ALsoundfont;
14 typedef size_t (*ReaderCb)(void *ptr, size_t size, void *stream);
15 typedef struct Reader {
16 ReaderCb cb;
17 void *ptr;
18 int error;
19 } Reader;
20 inline size_t Reader_read(Reader *self, void *buf, size_t len)
22 size_t got = (!self->error) ? self->cb(buf, len, self->ptr) : 0;
23 if(got < len) self->error = 1;
24 return got;
26 #define READERR(x_) ((x_)->error)
28 ALboolean loadSf2(Reader *stream, struct ALsoundfont *sfont, ALCcontext *context);
31 #define MIDI_CLOCK_RES U64(1000000000)
34 struct MidiSynthVtable;
36 typedef struct MidiSynth {
37 EvtQueue EventQueue;
39 ALuint64 ClockBase;
40 ALuint SamplesDone;
41 ALuint SampleRate;
43 /* NOTE: This rwlock is for the state and soundfont. The EventQueue and
44 * related must instead use the device lock as they're used in the mixer
45 * thread.
47 RWLock Lock;
49 struct ALsoundfont **Soundfonts;
50 ALsizei NumSoundfonts;
52 volatile ALfloat Gain;
53 volatile ALenum State;
55 const struct MidiSynthVtable *vtbl;
56 } MidiSynth;
58 void MidiSynth_Construct(MidiSynth *self, ALCdevice *device);
59 void MidiSynth_Destruct(MidiSynth *self);
60 ALenum MidiSynth_selectSoundfonts(MidiSynth *self, ALCcontext *context, ALsizei count, const ALuint *ids);
61 inline void MidiSynth_setGain(MidiSynth *self, ALfloat gain) { self->Gain = gain; }
62 inline ALfloat MidiSynth_getGain(const MidiSynth *self) { return self->Gain; }
63 inline void MidiSynth_setState(MidiSynth *self, ALenum state) { ExchangeInt(&self->State, state); }
64 inline ALenum MidiSynth_getState(const MidiSynth *self) { return self->State; }
65 void MidiSynth_stop(MidiSynth *self);
66 inline void MidiSynth_reset(MidiSynth *self) { MidiSynth_stop(self); }
67 inline ALuint64 MidiSynth_getTime(const MidiSynth *self)
68 { return self->ClockBase + (self->SamplesDone*MIDI_CLOCK_RES/self->SampleRate); }
69 inline ALuint64 MidiSynth_getNextEvtTime(const MidiSynth *self)
71 if(self->EventQueue.pos == self->EventQueue.size)
72 return UINT64_MAX;
73 return self->EventQueue.events[self->EventQueue.pos].time;
75 void MidiSynth_setSampleRate(MidiSynth *self, ALuint srate);
76 inline void MidiSynth_update(MidiSynth *self, ALCdevice *device)
77 { MidiSynth_setSampleRate(self, device->Frequency); }
78 ALenum MidiSynth_insertEvent(MidiSynth *self, ALuint64 time, ALuint event, ALsizei param1, ALsizei param2);
79 ALenum MidiSynth_insertSysExEvent(MidiSynth *self, ALuint64 time, const ALbyte *data, ALsizei size);
82 struct MidiSynthVtable {
83 void (*const Destruct)(MidiSynth *self);
85 ALenum (*const selectSoundfonts)(MidiSynth *self, ALCcontext *context, ALsizei count, const ALuint *ids);
87 void (*const setGain)(MidiSynth *self, ALfloat gain);
89 void (*const stop)(MidiSynth *self);
90 void (*const reset)(MidiSynth *self);
92 void (*const update)(MidiSynth *self, ALCdevice *device);
93 void (*const process)(MidiSynth *self, ALuint samples, ALfloat (*restrict DryBuffer)[BUFFERSIZE]);
95 void (*const Delete)(void *ptr);
98 #define DEFINE_MIDISYNTH_VTABLE(T) \
99 DECLARE_THUNK(T, MidiSynth, void, Destruct) \
100 DECLARE_THUNK3(T, MidiSynth, ALenum, selectSoundfonts, ALCcontext*, ALsizei, const ALuint*) \
101 DECLARE_THUNK1(T, MidiSynth, void, setGain, ALfloat) \
102 DECLARE_THUNK(T, MidiSynth, void, stop) \
103 DECLARE_THUNK(T, MidiSynth, void, reset) \
104 DECLARE_THUNK1(T, MidiSynth, void, update, ALCdevice*) \
105 DECLARE_THUNK2(T, MidiSynth, void, process, ALuint, ALfloatBUFFERSIZE*restrict) \
106 static void T##_MidiSynth_Delete(void *ptr) \
107 { T##_Delete(STATIC_UPCAST(T, MidiSynth, (MidiSynth*)ptr)); } \
109 static const struct MidiSynthVtable T##_MidiSynth_vtable = { \
110 T##_MidiSynth_Destruct, \
112 T##_MidiSynth_selectSoundfonts, \
113 T##_MidiSynth_setGain, \
114 T##_MidiSynth_stop, \
115 T##_MidiSynth_reset, \
116 T##_MidiSynth_update, \
117 T##_MidiSynth_process, \
119 T##_MidiSynth_Delete, \
123 MidiSynth *SSynth_create(ALCdevice *device);
124 MidiSynth *FSynth_create(ALCdevice *device);
125 MidiSynth *DSynth_create(ALCdevice *device);
127 MidiSynth *SynthCreate(ALCdevice *device);
129 #ifdef __cplusplus
131 #endif
133 #endif /* AL_MIDI_BASE_H */