summaryrefslogtreecommitdiffstats
path: root/apps/plugins/xworld/mixer.c
blob: de7536cd369c7b62ff04bfa06e72c2d825ebf34b (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 Franklin Wei, Benjamin Brown
 * Copyright (C) 2004 Gregory Montoir
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

#include "mixer.h"
#include "serializer.h"
#include "sys.h"

static int8_t ICODE_ATTR addclamp(int a, int b) {
    int add = a + b;
    if (add < -128) {
        add = -128;
    }
    else if (add > 127) {
        add = 127;
    }
    return (int8_t)add;
}

void mixer_create(struct Mixer* mx, struct System *stub)
{
    mx->sys = stub;
}

static void mixer_mixCallback(void *param, uint8_t *buf, int len);

void mixer_init(struct Mixer* mx) {
    rb->memset(mx->_channels, 0, sizeof(mx->_channels));
    if(!mx->sys)
    {
        error("in mixer sys is NULL");
    }
    mx->_mutex = sys_createMutex(mx->sys);
    sys_startAudio(mx->sys, mixer_mixCallback, mx);
}

void mixer_free(struct Mixer* mx) {
    mixer_stopAll(mx);
    sys_stopAudio(mx->sys);
    sys_destroyMutex(mx->sys, mx->_mutex);
}

void mixer_playChannel(struct Mixer* mx, uint8_t channel, const struct MixerChunk *mc, uint16_t freq, uint8_t volume) {
    debug(DBG_SND, "mixer_playChannel(%d, %d, %d)", channel, freq, volume);
    assert(channel < AUDIO_NUM_CHANNELS);

    /* FW: the mutex code was converted 1:1 from C++ to C, leading to the ugly calls */
    /*     to constructors/destructors as seen here */

    struct MutexStack_t ms;
    MutexStack(&ms, mx->sys, mx->_mutex);

    struct MixerChannel *ch = &mx->_channels[channel];
    ch->active = true;
    ch->volume = volume;
    ch->chunk = *mc;
    ch->chunkPos = 0;
    ch->chunkInc = (freq << 8) / sys_getOutputSampleRate(mx->sys);

    MutexStack_destroy(&ms);
}

void mixer_stopChannel(struct Mixer* mx, uint8_t channel) {
    debug(DBG_SND, "mixer_stopChannel(%d)", channel);
    assert(channel < AUDIO_NUM_CHANNELS);

    struct MutexStack_t ms;
    MutexStack(&ms, mx->sys, mx->_mutex);

    mx->_channels[channel].active = false;

    MutexStack_destroy(&ms);
}

void mixer_setChannelVolume(struct Mixer* mx, uint8_t channel, uint8_t volume) {
    debug(DBG_SND, "mixer_setChannelVolume(%d, %d)", channel, volume);
    assert(channel < AUDIO_NUM_CHANNELS);

    struct MutexStack_t ms;
    MutexStack(&ms, mx->sys, mx->_mutex);

    mx->_channels[channel].volume = volume;

    MutexStack_destroy(&ms);
}

void mixer_stopAll(struct Mixer* mx) {
    debug(DBG_SND, "mixer_stopAll()");

    struct MutexStack_t ms;
    MutexStack(&ms, mx->sys, mx->_mutex);

    for (uint8_t i = 0; i < AUDIO_NUM_CHANNELS; ++i) {
        mx->_channels[i].active = false;
    }

    MutexStack_destroy(&ms);
}

/* Mx is SDL callback. Called in order to populate the buf with len bytes. */
/* The mixer iterates through all active channels and combine all sounds. */

/* Since there is no way to know when SDL will ask for a buffer fill, we need */
/* to synchronize with a mutex so the channels remain stable during the execution */
/* of this method. */
static void ICODE_ATTR mixer_mix(struct Mixer* mx, int8_t *buf, int len) {
    int8_t *pBuf;

    struct MutexStack_t ms;
    MutexStack(&ms, mx->sys, mx->_mutex);

    /* Clear the buffer since nothing guarantees we are receiving clean memory. */
    rb->memset(buf, 0, len);

    for (uint8_t i = 0; i < AUDIO_NUM_CHANNELS; ++i) {
        struct MixerChannel *ch = &mx->_channels[i];
        if (!ch->active)
            continue;

        pBuf = buf;
        for (int j = 0; j < len; ++j, ++pBuf) {

            uint16_t p1, p2;
            uint16_t ilc = (ch->chunkPos & 0xFF);
            p1 = ch->chunkPos >> 8;
            ch->chunkPos += ch->chunkInc;

            if (ch->chunk.loopLen != 0) {
                if (p1 == ch->chunk.loopPos + ch->chunk.loopLen - 1) {
                    debug(DBG_SND, "Looping sample on channel %d", i);
                    ch->chunkPos = p2 = ch->chunk.loopPos;
                } else {
                    p2 = p1 + 1;
                }
            } else {
                if (p1 == ch->chunk.len - 1) {
                    debug(DBG_SND, "Stopping sample on channel %d", i);
                    ch->active = false;
                    break;
                } else {
                    p2 = p1 + 1;
                }
            }
            /* interpolate */
            int8_t b1 = *(int8_t *)(ch->chunk.data + p1);
            int8_t b2 = *(int8_t *)(ch->chunk.data + p2);
            int8_t b = (int8_t)((b1 * (0xFF - ilc) + b2 * ilc) >> 8);

            /* set volume and clamp */
            *pBuf = addclamp(*pBuf, (int)b * ch->volume / 0x40);  /* 0x40=64 */
        }

    }

    MutexStack_destroy(&ms);
}

static void ICODE_ATTR mixer_mixCallback(void *param, uint8_t *buf, int len) {
    debug(DBG_SND, "mixer_mixCallback");
    mixer_mix((struct Mixer*)param, (int8_t *)buf, len);
}

void mixer_saveOrLoad(struct Mixer* mx, struct Serializer *ser) {
    sys_lockMutex(mx->sys, mx->_mutex);
    for (int i = 0; i < AUDIO_NUM_CHANNELS; ++i) {
        struct MixerChannel *ch = &mx->_channels[i];
        struct Entry entries[] = {
            SE_INT(&ch->active, SES_BOOL, VER(2)),
            SE_INT(&ch->volume, SES_INT8, VER(2)),
            SE_INT(&ch->chunkPos, SES_INT32, VER(2)),
            SE_INT(&ch->chunkInc, SES_INT32, VER(2)),
            SE_PTR(&ch->chunk.data, VER(2)),
            SE_INT(&ch->chunk.len, SES_INT16, VER(2)),
            SE_INT(&ch->chunk.loopPos, SES_INT16, VER(2)),
            SE_INT(&ch->chunk.loopLen, SES_INT16, VER(2)),
            SE_END()
        };
        ser_saveOrLoadEntries(ser, entries);
    }
    sys_unlockMutex(mx->sys, mx->_mutex);
};