aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/midi/soft.c
blob: 6566b411e0fa0f2cd521ca7014b883d4539b8592 (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
129
130
131
132
133
134
135
136
137
138
139
140

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "alMain.h"
#include "alError.h"
#include "evtqueue.h"
#include "alu.h"

#include "midi/base.h"


typedef struct SSynth {
    DERIVE_FROM_TYPE(MidiSynth);
} SSynth;

static void SSynth_mixSamples(SSynth *self, ALuint SamplesToDo, ALfloat (*restrict DryBuffer)[BUFFERSIZE]);

static void SSynth_Construct(SSynth *self, ALCdevice *device);
static void SSynth_Destruct(SSynth *self);
static DECLARE_FORWARD3(SSynth, MidiSynth, ALenum, selectSoundfonts, ALCcontext*, ALsizei, const ALuint*)
static DECLARE_FORWARD1(SSynth, MidiSynth, void, setGain, ALfloat)
static DECLARE_FORWARD(SSynth, MidiSynth, void, stop)
static DECLARE_FORWARD(SSynth, MidiSynth, void, reset)
static void SSynth_update(SSynth *self, ALCdevice *device);
static void SSynth_process(SSynth *self, ALuint SamplesToDo, ALfloat (*restrict DryBuffer)[BUFFERSIZE]);
DECLARE_DEFAULT_ALLOCATORS(SSynth)
DEFINE_MIDISYNTH_VTABLE(SSynth);


static void SSynth_Construct(SSynth *self, ALCdevice *device)
{
    MidiSynth_Construct(STATIC_CAST(MidiSynth, self), device);
    SET_VTABLE2(SSynth, MidiSynth, self);
}

static void SSynth_Destruct(SSynth* UNUSED(self))
{
}


static void SSynth_update(SSynth* UNUSED(self), ALCdevice* UNUSED(device))
{
}


static void SSynth_mixSamples(SSynth* UNUSED(self), ALuint UNUSED(SamplesToDo), ALfloatBUFFERSIZE *restrict UNUSED(DryBuffer))
{
}


static void SSynth_processQueue(SSynth *self, ALuint64 time)
{
    EvtQueue *queue = &STATIC_CAST(MidiSynth, self)->EventQueue;

    while(queue->pos < queue->size && queue->events[queue->pos].time <= time)
        queue->pos++;
}

static void SSynth_process(SSynth *self, ALuint SamplesToDo, ALfloat (*restrict DryBuffer)[BUFFERSIZE])
{
    MidiSynth *synth = STATIC_CAST(MidiSynth, self);
    ALenum state = synth->State;
    ALuint64 curtime;
    ALuint total = 0;

    if(state == AL_INITIAL)
        return;
    if(state != AL_PLAYING)
    {
        SSynth_mixSamples(self, SamplesToDo, DryBuffer);
        return;
    }

    curtime = MidiSynth_getTime(synth);
    while(total < SamplesToDo)
    {
        ALuint64 time, diff;
        ALint tonext;

        time = MidiSynth_getNextEvtTime(synth);
        diff = maxu64(time, curtime) - curtime;
        if(diff >= MIDI_CLOCK_RES || time == UINT64_MAX)
        {
            /* If there's no pending event, or if it's more than 1 second
             * away, do as many samples as we can. */
            tonext = INT_MAX;
        }
        else
        {
            /* Figure out how many samples until the next event. */
            tonext  = (ALint)((diff*synth->SampleRate + (MIDI_CLOCK_RES-1)) / MIDI_CLOCK_RES);
            tonext -= total;
            /* For efficiency reasons, try to mix a multiple of 64 samples
             * (~1ms @ 44.1khz) before processing the next event. */
            tonext = (tonext+63) & ~63;
        }

        if(tonext > 0)
        {
            ALuint todo = mini(tonext, SamplesToDo-total);
            SSynth_mixSamples(self, todo, DryBuffer);
            total += todo;
            tonext -= todo;
        }
        if(total < SamplesToDo && tonext <= 0)
            SSynth_processQueue(self, time);
    }

    synth->SamplesDone += SamplesToDo;
    synth->ClockBase += (synth->SamplesDone/synth->SampleRate) * MIDI_CLOCK_RES;
    synth->SamplesDone %= synth->SampleRate;
}


MidiSynth *SSynth_create(ALCdevice *device)
{
    SSynth *synth;

    /* This option is temporary. Once this synth is in a more usable state, a
     * more generic selector should be used. */
    if(!GetConfigValueBool("midi", "internal-synth", 0))
    {
        TRACE("Not using internal MIDI synth\n");
        return NULL;
    }

    synth = SSynth_New(sizeof(*synth));
    if(!synth)
    {
        ERR("Failed to allocate SSynth\n");
        return NULL;
    }
    SSynth_Construct(synth, device);
    return STATIC_CAST(MidiSynth, synth);
}