diff options
author | Chris Robinson <[email protected]> | 2013-12-17 23:03:34 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2013-12-17 23:03:34 -0800 |
commit | 0653680690688fd0c9e4331bf1c5a5cd6490e270 (patch) | |
tree | c91a02b8e72de51d92ced184efb1b04b50f27915 /Alc | |
parent | 0095a59fc96faa1d1cf0c8ede9105b4770b240bc (diff) |
Move the base MidiSynth to a separate file
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/ALc.c | 2 | ||||
-rw-r--r-- | Alc/ALu.c | 3 | ||||
-rw-r--r-- | Alc/midi/base.c | 241 | ||||
-rw-r--r-- | Alc/midi/base.h | 113 | ||||
-rw-r--r-- | Alc/midi/dummy.c | 2 | ||||
-rw-r--r-- | Alc/midi/fluidsynth.c | 3 |
6 files changed, 360 insertions, 4 deletions
@@ -30,7 +30,6 @@ #include "alMain.h" #include "alSource.h" #include "alListener.h" -#include "alMidi.h" #include "alThunk.h" #include "alSource.h" #include "alBuffer.h" @@ -40,6 +39,7 @@ #include "alu.h" #include "backends/base.h" +#include "midi/base.h" /************************************************ @@ -31,12 +31,13 @@ #include "alBuffer.h" #include "alListener.h" #include "alAuxEffectSlot.h" -#include "alMidi.h" #include "alu.h" #include "bs2b.h" #include "mixer_defs.h" +#include "midi/base.h" + struct ChanMap { enum Channel channel; diff --git a/Alc/midi/base.c b/Alc/midi/base.c new file mode 100644 index 00000000..46c9b3d2 --- /dev/null +++ b/Alc/midi/base.c @@ -0,0 +1,241 @@ + +#include "config.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> + +#include "midi/base.h" + +#include "alMain.h" +#include "alError.h" +#include "evtqueue.h" +#include "rwlock.h" +#include "alu.h" + + +/* Microsecond resolution */ +#define TICKS_PER_SECOND (1000000) + +/* MIDI events */ +#define SYSEX_EVENT (0xF0) + + +void MidiSynth_Construct(MidiSynth *self, ALCdevice *device) +{ + InitEvtQueue(&self->EventQueue); + + RWLockInit(&self->Lock); + + self->Gain = 1.0f; + self->State = AL_INITIAL; + + self->LastEvtTime = 0; + self->NextEvtTime = UINT64_MAX; + self->SamplesSinceLast = 0.0; + self->SamplesToNext = 0.0; + + self->SamplesPerTick = (ALdouble)device->Frequency / TICKS_PER_SECOND; +} + +void MidiSynth_Destruct(MidiSynth *self) +{ + ResetEvtQueue(&self->EventQueue); +} + +const char *MidiSynth_getFontName(const MidiSynth* UNUSED(self), const char *filename) +{ + if(!filename || !filename[0]) + filename = getenv("ALSOFT_SOUNDFONT"); + if(!filename || !filename[0]) + filename = GetConfigValue("midi", "soundfont", ""); + if(!filename[0]) + WARN("No default soundfont found\n"); + + return filename; +} + +extern inline void MidiSynth_setGain(MidiSynth *self, ALfloat gain); +extern inline ALfloat MidiSynth_getGain(const MidiSynth *self); +extern inline void MidiSynth_setState(MidiSynth *self, ALenum state); + +void MidiSynth_stop(MidiSynth *self) +{ + ResetEvtQueue(&self->EventQueue); + + self->LastEvtTime = 0; + self->NextEvtTime = UINT64_MAX; + self->SamplesSinceLast = 0.0; + self->SamplesToNext = 0.0; +} + +extern inline void MidiSynth_reset(MidiSynth *self); + +ALuint64 MidiSynth_getTime(const MidiSynth *self) +{ + ALuint64 time = self->LastEvtTime + (self->SamplesSinceLast/self->SamplesPerTick); + return clampu(time, self->LastEvtTime, self->NextEvtTime); +} + +extern inline ALuint64 MidiSynth_getNextEvtTime(const MidiSynth *self); + +void MidiSynth_setSampleRate(MidiSynth *self, ALdouble srate) +{ + ALdouble sampletickrate = srate / TICKS_PER_SECOND; + + self->SamplesSinceLast = self->SamplesSinceLast * sampletickrate / self->SamplesPerTick; + self->SamplesToNext = self->SamplesToNext * sampletickrate / self->SamplesPerTick; + self->SamplesPerTick = sampletickrate; +} + +extern inline void MidiSynth_update(MidiSynth *self, ALCdevice *device); + +ALenum MidiSynth_insertEvent(MidiSynth *self, ALuint64 time, ALuint event, ALsizei param1, ALsizei param2) +{ + MidiEvent entry; + ALenum err; + + entry.time = time; + entry.event = event; + entry.param.val[0] = param1; + entry.param.val[1] = param2; + + err = InsertEvtQueue(&self->EventQueue, &entry); + if(err != AL_NO_ERROR) return err; + + if(entry.time < self->NextEvtTime) + { + self->NextEvtTime = entry.time; + + self->SamplesToNext = (self->NextEvtTime - self->LastEvtTime) * self->SamplesPerTick; + self->SamplesToNext -= self->SamplesSinceLast; + } + + return AL_NO_ERROR; +} + +ALenum MidiSynth_insertSysExEvent(MidiSynth *self, ALuint64 time, const ALbyte *data, ALsizei size) +{ + MidiEvent entry; + ALenum err; + + entry.time = time; + entry.event = SYSEX_EVENT; + entry.param.sysex.size = size; + entry.param.sysex.data = malloc(size); + if(!entry.param.sysex.data) + return AL_OUT_OF_MEMORY; + memcpy(entry.param.sysex.data, data, size); + + err = InsertEvtQueue(&self->EventQueue, &entry); + if(err != AL_NO_ERROR) + { + free(entry.param.sysex.data); + return err; + } + + if(entry.time < self->NextEvtTime) + { + self->NextEvtTime = entry.time; + + self->SamplesToNext = (self->NextEvtTime - self->LastEvtTime) * self->SamplesPerTick; + self->SamplesToNext -= self->SamplesSinceLast; + } + + return AL_NO_ERROR; +} + + +void InitEvtQueue(EvtQueue *queue) +{ + queue->events = NULL; + queue->maxsize = 0; + queue->size = 0; + queue->pos = 0; +} + +void ResetEvtQueue(EvtQueue *queue) +{ + ALsizei i; + for(i = 0;i < queue->size;i++) + { + if(queue->events[i].event == SYSEX_EVENT) + { + free(queue->events[i].param.sysex.data); + queue->events[i].param.sysex.data = NULL; + } + } + + free(queue->events); + queue->events = NULL; + queue->maxsize = 0; + queue->size = 0; + queue->pos = 0; +} + +ALenum InsertEvtQueue(EvtQueue *queue, const MidiEvent *evt) +{ + ALsizei pos; + + if(queue->maxsize == queue->size) + { + if(queue->pos > 0) + { + /* Queue has some stale entries, remove them to make space for more + * events. */ + for(pos = 0;pos < queue->pos;pos++) + { + if(queue->events[pos].event == SYSEX_EVENT) + { + free(queue->events[pos].param.sysex.data); + queue->events[pos].param.sysex.data = NULL; + } + } + memmove(&queue->events[0], &queue->events[queue->pos], + (queue->size-queue->pos)*sizeof(queue->events[0])); + queue->size -= queue->pos; + queue->pos = 0; + } + else + { + /* Queue is full, double the allocated space. */ + void *temp = NULL; + ALsizei newsize; + + newsize = (queue->maxsize ? (queue->maxsize<<1) : 16); + if(newsize > queue->maxsize) + temp = realloc(queue->events, newsize * sizeof(queue->events[0])); + if(!temp) + return AL_OUT_OF_MEMORY; + + queue->events = temp; + queue->maxsize = newsize; + } + } + + pos = queue->pos; + if(queue->size > 0) + { + ALsizei high = queue->size - 1; + while(pos < high) + { + ALsizei mid = pos + (high-pos)/2; + if(queue->events[mid].time < evt->time) + pos = mid + 1; + else + high = mid; + } + while(pos < queue->size && queue->events[pos].time <= evt->time) + pos++; + + if(pos < queue->size) + memmove(&queue->events[pos+1], &queue->events[pos], + (queue->size-pos)*sizeof(queue->events[0])); + } + + queue->events[pos] = *evt; + queue->size++; + + return AL_NO_ERROR; +} diff --git a/Alc/midi/base.h b/Alc/midi/base.h new file mode 100644 index 00000000..bba24baa --- /dev/null +++ b/Alc/midi/base.h @@ -0,0 +1,113 @@ +#ifndef ALMIDI_H +#define ALMIDI_H + +#include "alMain.h" +#include "atomic.h" +#include "evtqueue.h" + +#ifdef __cplusplus +extern "C" { +#endif + +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; + + volatile ALfloat Gain; + volatile ALenum State; + + const struct MidiSynthVtable *vtbl; +} MidiSynth; + +void MidiSynth_Construct(MidiSynth *self, ALCdevice *device); +void MidiSynth_Destruct(MidiSynth *self); +const char *MidiSynth_getFontName(const MidiSynth *self, const char *filename); +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); + + ALboolean (*const isSoundfont)(MidiSynth *self, const char *filename); + ALenum (*const loadSoundfont)(MidiSynth *self, const char *filename); + + 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_THUNK1(T, MidiSynth, ALboolean, isSoundfont, const char*) \ +DECLARE_THUNK1(T, MidiSynth, ALenum, loadSoundfont, const char*) \ +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_isSoundfont, \ + T##_MidiSynth_loadSoundfont, \ + 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 /* ALMIDI_H */ diff --git a/Alc/midi/dummy.c b/Alc/midi/dummy.c index 029db2e7..4e53270e 100644 --- a/Alc/midi/dummy.c +++ b/Alc/midi/dummy.c @@ -6,13 +6,13 @@ #include <string.h> #include <limits.h> -#include "alMidi.h" #include "alMain.h" #include "alError.h" #include "evtqueue.h" #include "rwlock.h" #include "alu.h" +#include "midi/base.h" typedef struct DSynth { DERIVE_FROM_TYPE(MidiSynth); diff --git a/Alc/midi/fluidsynth.c b/Alc/midi/fluidsynth.c index c2cde00c..91c7229d 100644 --- a/Alc/midi/fluidsynth.c +++ b/Alc/midi/fluidsynth.c @@ -10,13 +10,14 @@ #include <fluidsynth.h> -#include "alMidi.h" #include "alMain.h" #include "alError.h" #include "evtqueue.h" #include "rwlock.h" #include "alu.h" +#include "midi/base.h" + /* MIDI events */ #define SYSEX_EVENT (0xF0) |