summaryrefslogtreecommitdiff
path: root/apps/plugins/xworld/sfxplayer.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/xworld/sfxplayer.c')
-rw-r--r--apps/plugins/xworld/sfxplayer.c247
1 files changed, 247 insertions, 0 deletions
diff --git a/apps/plugins/xworld/sfxplayer.c b/apps/plugins/xworld/sfxplayer.c
new file mode 100644
index 0000000000..9bdc143d02
--- /dev/null
+++ b/apps/plugins/xworld/sfxplayer.c
@@ -0,0 +1,247 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2014 Franklin Wei, Benjamin Brown
11 * Copyright (C) 2004 Gregory Montoir
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "sfxplayer.h"
24#include "mixer.h"
25#include "resource.h"
26#include "serializer.h"
27#include "sys.h"
28
29void player_create(struct SfxPlayer* sfx, struct Mixer *mix, struct Resource *res, struct System *stub)
30{
31 sfx->mixer = mix;
32 sfx->res = res;
33 sfx->sys = stub;
34 sfx->_delay = 0;
35 sfx->_resNum = 0;
36}
37
38void player_init(struct SfxPlayer* sfx) {
39 debug(DBG_SND, "sys is 0x%08x", sfx->sys);
40 sfx->_mutex = sys_createMutex(sfx->sys);
41}
42
43void player_free(struct SfxPlayer* sfx) {
44 player_stop(sfx);
45 sys_destroyMutex(sfx->sys, sfx->_mutex);
46}
47
48void player_setEventsDelay(struct SfxPlayer* sfx, uint16_t delay) {
49 debug(DBG_SND, "player_setEventsDelay(%d)", delay);
50 struct MutexStack_t ms;
51 MutexStack(&ms, sfx->sys, sfx->_mutex);
52 sfx->_delay = delay * 60 / 7050;
53 MutexStack_destroy(&ms);
54}
55
56void player_loadSfxModule(struct SfxPlayer* sfx, uint16_t resNum, uint16_t delay, uint8_t pos) {
57
58 debug(DBG_SND, "player_loadSfxModule(0x%X, %d, %d)", resNum, delay, pos);
59 struct MutexStack_t ms;
60 MutexStack(&ms, sfx->sys, sfx->_mutex);
61
62
63 struct MemEntry *me = &sfx->res->_memList[resNum];
64
65 if (me->state == MEMENTRY_STATE_LOADED && me->type == RT_MUSIC) {
66 sfx->_resNum = resNum;
67 rb->memset(&sfx->_sfxMod, 0, sizeof(struct SfxModule));
68 sfx->_sfxMod.curOrder = pos;
69 sfx->_sfxMod.numOrder = READ_BE_UINT16(me->bufPtr + 0x3E);
70 debug(DBG_SND, "player_loadSfxModule() curOrder = 0x%X numOrder = 0x%X", sfx->_sfxMod.curOrder, sfx->_sfxMod.numOrder);
71 for (int i = 0; i < 0x80; ++i) {
72 sfx->_sfxMod.orderTable[i] = *(me->bufPtr + 0x40 + i);
73 }
74 if (delay == 0) {
75 sfx->_delay = READ_BE_UINT16(me->bufPtr);
76 } else {
77 sfx->_delay = delay;
78 }
79 sfx->_delay = sfx->_delay * 60 / 7050;
80 sfx->_sfxMod.data = me->bufPtr + 0xC0;
81 debug(DBG_SND, "player_loadSfxModule() eventDelay = %d ms", sfx->_delay);
82 player_prepareInstruments(sfx, me->bufPtr + 2);
83 } else {
84 warning("player_loadSfxModule() ec=0x%X", 0xF8);
85 }
86 MutexStack_destroy(&ms);
87}
88
89void player_prepareInstruments(struct SfxPlayer* sfx, const uint8_t *p) {
90
91 rb->memset(sfx->_sfxMod.samples, 0, sizeof(sfx->_sfxMod.samples));
92
93 for (int i = 0; i < 15; ++i) {
94 struct SfxInstrument *ins = &sfx->_sfxMod.samples[i];
95 uint16_t resNum = READ_BE_UINT16(p);
96 p += 2;
97 if (resNum != 0) {
98 ins->volume = READ_BE_UINT16(p);
99 struct MemEntry *me = &sfx->res->_memList[resNum];
100 if (me->state == MEMENTRY_STATE_LOADED && me->type == RT_SOUND) {
101 ins->data = me->bufPtr;
102 rb->memset(ins->data + 8, 0, 4);
103 debug(DBG_SND, "Loaded instrument 0x%X n=%d volume=%d", resNum, i, ins->volume);
104 } else {
105 error("Error loading instrument 0x%X", resNum);
106 }
107 }
108 p += 2; /* skip volume */
109 }
110}
111
112void player_start(struct SfxPlayer* sfx) {
113 debug(DBG_SND, "player_start()");
114 struct MutexStack_t ms;
115 MutexStack(&ms, sfx->sys, sfx->_mutex);
116 sfx->_sfxMod.curPos = 0;
117 sfx->_timerId = sys_addTimer(sfx->sys, sfx->_delay, player_eventsCallback, sfx);
118 MutexStack_destroy(&ms);
119}
120
121void player_stop(struct SfxPlayer* sfx) {
122 debug(DBG_SND, "player_stop()");
123 struct MutexStack_t ms;
124 MutexStack(&ms, sfx->sys, sfx->_mutex);
125 if (sfx->_resNum != 0) {
126 sfx->_resNum = 0;
127 sys_removeTimer(sfx->sys, sfx->_timerId);
128 }
129 MutexStack_destroy(&ms);
130}
131
132void player_handleEvents(struct SfxPlayer* sfx) {
133 struct MutexStack_t ms;
134 MutexStack(&ms, sfx->sys, sfx->_mutex);
135 uint8_t order = sfx->_sfxMod.orderTable[sfx->_sfxMod.curOrder];
136 const uint8_t *patternData = sfx->_sfxMod.data + sfx->_sfxMod.curPos + order * 1024;
137 for (uint8_t ch = 0; ch < 4; ++ch) {
138 player_handlePattern(sfx, ch, patternData);
139 patternData += 4;
140 }
141 sfx->_sfxMod.curPos += 4 * 4;
142 debug(DBG_SND, "player_handleEvents() order = 0x%X curPos = 0x%X", order, sfx->_sfxMod.curPos);
143 if (sfx->_sfxMod.curPos >= 1024) {
144 sfx->_sfxMod.curPos = 0;
145 order = sfx->_sfxMod.curOrder + 1;
146 if (order == sfx->_sfxMod.numOrder) {
147 sfx->_resNum = 0;
148 sys_removeTimer(sfx->sys, sfx->_timerId);
149 mixer_stopAll(sfx->mixer);
150 }
151 sfx->_sfxMod.curOrder = order;
152 }
153 MutexStack_destroy(&ms);
154}
155
156void player_handlePattern(struct SfxPlayer* sfx, uint8_t channel, const uint8_t *data) {
157 struct SfxPattern pat;
158 rb->memset(&pat, 0, sizeof(struct SfxPattern));
159 pat.note_1 = READ_BE_UINT16(data + 0);
160 pat.note_2 = READ_BE_UINT16(data + 2);
161 if (pat.note_1 != 0xFFFD) {
162 uint16_t sample = (pat.note_2 & 0xF000) >> 12;
163 if (sample != 0) {
164 uint8_t *ptr = sfx->_sfxMod.samples[sample - 1].data;
165 if (ptr != 0) {
166 debug(DBG_SND, "player_handlePattern() preparing sample %d", sample);
167 pat.sampleVolume = sfx->_sfxMod.samples[sample - 1].volume;
168 pat.sampleStart = 8;
169 pat.sampleBuffer = ptr;
170 pat.sampleLen = READ_BE_UINT16(ptr) * 2;
171 uint16_t loopLen = READ_BE_UINT16(ptr + 2) * 2;
172 if (loopLen != 0) {
173 pat.loopPos = pat.sampleLen;
174 pat.loopData = ptr;
175 pat.loopLen = loopLen;
176 } else {
177 pat.loopPos = 0;
178 pat.loopData = 0;
179 pat.loopLen = 0;
180 }
181 int16_t m = pat.sampleVolume;
182 uint8_t effect = (pat.note_2 & 0x0F00) >> 8;
183 if (effect == 5) { /* volume up */
184 uint8_t volume = (pat.note_2 & 0xFF);
185 m += volume;
186 if (m > 0x3F) {
187 m = 0x3F;
188 }
189 } else if (effect == 6) { /* volume down */
190 uint8_t volume = (pat.note_2 & 0xFF);
191 m -= volume;
192 if (m < 0) {
193 m = 0;
194 }
195 }
196 mixer_setChannelVolume(sfx->mixer, channel, m);
197 pat.sampleVolume = m;
198 }
199 }
200 }
201 if (pat.note_1 == 0xFFFD) {
202 debug(DBG_SND, "player_handlePattern() _scriptVars[0xF4] = 0x%X", pat.note_2);
203 *sfx->_markVar = pat.note_2;
204 } else if (pat.note_1 != 0) {
205 if (pat.note_1 == 0xFFFE) {
206 mixer_stopChannel(sfx->mixer, channel);
207 } else if (pat.sampleBuffer != 0) {
208 struct MixerChunk mc;
209 rb->memset(&mc, 0, sizeof(mc));
210 mc.data = pat.sampleBuffer + pat.sampleStart;
211 mc.len = pat.sampleLen;
212 mc.loopPos = pat.loopPos;
213 mc.loopLen = pat.loopLen;
214 /* convert amiga period value to hz */
215 uint16_t freq = 7159092 / (pat.note_1 * 2);
216 debug(DBG_SND, "player_handlePattern() adding sample freq = 0x%X", freq);
217 mixer_playChannel(sfx->mixer, channel, &mc, freq, pat.sampleVolume);
218 }
219 }
220}
221
222uint32_t player_eventsCallback(uint32_t interval, void *param) {
223 (void) interval;
224 debug(DBG_SND, "player_eventsCallback with interval %d ms and param 0x%08x", interval, param);
225 struct SfxPlayer *p = (struct SfxPlayer *)param;
226 player_handleEvents(p);
227 return p->_delay;
228}
229
230void player_saveOrLoad(struct SfxPlayer* sfx, struct Serializer *ser) {
231 sys_lockMutex(sfx->sys, sfx->_mutex);
232 struct Entry entries[] = {
233 SE_INT(&sfx->_delay, SES_INT8, VER(2)),
234 SE_INT(&sfx->_resNum, SES_INT16, VER(2)),
235 SE_INT(&sfx->_sfxMod.curPos, SES_INT16, VER(2)),
236 SE_INT(&sfx->_sfxMod.curOrder, SES_INT8, VER(2)),
237 SE_END()
238 };
239 ser_saveOrLoadEntries(ser, entries);
240 sys_unlockMutex(sfx->sys, sfx->_mutex);
241 if (ser->_mode == SM_LOAD && sfx->_resNum != 0) {
242 uint16_t delay = sfx->_delay;
243 player_loadSfxModule(sfx, sfx->_resNum, 0, sfx->_sfxMod.curOrder);
244 sfx->_delay = delay;
245 sfx->_timerId = sys_addTimer(sfx->sys, sfx->_delay, player_eventsCallback, sfx);
246 }
247}