This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
sfxplayer.cpp
226 lines (211 loc) · 6.18 KB
/
sfxplayer.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Another World engine rewrite
* Copyright (C) 2004-2005 Gregory Montoir ([email protected])
*/
#include "sfxplayer.h"
#include "mixer.h"
#include "resource.h"
#include "systemstub.h"
#include "util.h"
SfxPlayer::SfxPlayer(Resource *res)
: _res(res), _delay(0) {
_playing = false;
}
void SfxPlayer::setEventsDelay(uint16_t delay) {
debug(DBG_SND, "SfxPlayer::setEventsDelay(%d)", delay);
_delay = delay;
}
void SfxPlayer::loadSfxModule(uint16_t resNum, uint16_t delay, uint8_t pos) {
debug(DBG_SND, "SfxPlayer::loadSfxModule(0x%X, %d, %d)", resNum, delay, pos);
MemEntry *me = &_res->_memList[resNum];
if (me->status == Resource::STATUS_LOADED && me->type == Resource::RT_MUSIC) {
memset(&_sfxMod, 0, sizeof(SfxModule));
_sfxMod.curOrder = pos;
_sfxMod.numOrder = me->bufPtr[0x3F];
debug(DBG_SND, "SfxPlayer::loadSfxModule() curOrder = 0x%X numOrder = 0x%X", _sfxMod.curOrder, _sfxMod.numOrder);
_sfxMod.orderTable = me->bufPtr + 0x40;
if (delay == 0) {
_delay = READ_BE_UINT16(me->bufPtr);
} else {
_delay = delay;
}
_sfxMod.data = me->bufPtr + 0xC0;
debug(DBG_SND, "SfxPlayer::loadSfxModule() eventDelay = %d ms", _delay);
prepareInstruments(me->bufPtr + 2);
} else {
warning("SfxPlayer::loadSfxModule() ec=0x%X", 0xF8);
}
}
void SfxPlayer::prepareInstruments(const uint8_t *p) {
memset(_sfxMod.samples, 0, sizeof(_sfxMod.samples));
for (int i = 0; i < 15; ++i) {
SfxInstrument *ins = &_sfxMod.samples[i];
uint16_t resNum = READ_BE_UINT16(p); p += 2;
if (resNum != 0) {
ins->volume = READ_BE_UINT16(p);
MemEntry *me = &_res->_memList[resNum];
if (me->status == Resource::STATUS_LOADED && me->type == Resource::RT_SOUND) {
ins->data = me->bufPtr;
debug(DBG_SND, "Loaded instrument 0x%X n=%d volume=%d", resNum, i, ins->volume);
} else {
error("Error loading instrument 0x%X", resNum);
}
}
p += 2; // skip volume
}
}
void SfxPlayer::play(int rate) {
_playing = true;
_rate = rate;
_samplesLeft = 0;
memset(_channels, 0, sizeof(_channels));
}
static int16_t toS16(int a) {
if (a <= -128) {
return -32768;
} else if (a >= 127) {
return 32767;
} else {
const uint8_t u8 = (a ^ 0x80);
return ((u8 << 8) | u8) - 32768;
}
}
static void mixChannel(int16_t &s, SfxChannel *ch) {
if (ch->sampleLen == 0) {
return;
}
int pos1 = ch->pos.offset >> Frac::BITS;
ch->pos.offset += ch->pos.inc;
int pos2 = pos1 + 1;
if (ch->sampleLoopLen != 0) {
if (pos1 >= ch->sampleLoopPos + ch->sampleLoopLen - 1) {
pos2 = ch->sampleLoopPos;
ch->pos.offset = pos2 << Frac::BITS;
}
} else {
if (pos1 >= ch->sampleLen - 1) {
ch->sampleLen = 0;
return;
}
}
int sample = ch->pos.interpolate((int8_t)ch->sampleData[pos1], (int8_t)ch->sampleData[pos2]);
sample = s + toS16(sample * ch->volume / 64);
s = (sample < -32768 ? -32768 : (sample > 32767 ? 32767 : sample));
}
void SfxPlayer::mixSamples(int16_t *buf, int len) {
while (len != 0) {
if (_samplesLeft == 0) {
handleEvents();
const int samplesPerTick = _rate * (_delay * 60 * 1000 / kPaulaFreq) / 1000;
_samplesLeft = samplesPerTick;
}
int count = _samplesLeft;
if (count > len) {
count = len;
}
_samplesLeft -= count;
len -= count;
for (int i = 0; i < count; ++i) {
mixChannel(*buf, &_channels[0]);
mixChannel(*buf, &_channels[3]);
++buf;
mixChannel(*buf, &_channels[1]);
mixChannel(*buf, &_channels[2]);
++buf;
}
}
}
void SfxPlayer::readSamples(int16_t *buf, int len) {
if (_delay != 0) {
mixSamples(buf, len / 2);
}
}
void SfxPlayer::start() {
debug(DBG_SND, "SfxPlayer::start()");
_sfxMod.curPos = 0;
}
void SfxPlayer::stop() {
debug(DBG_SND, "SfxPlayer::stop()");
_playing = false;
}
void SfxPlayer::handleEvents() {
uint8_t order = _sfxMod.orderTable[_sfxMod.curOrder];
const uint8_t *patternData = _sfxMod.data + _sfxMod.curPos + order * 1024;
for (uint8_t ch = 0; ch < 4; ++ch) {
handlePattern(ch, patternData);
patternData += 4;
}
_sfxMod.curPos += 4 * 4;
debug(DBG_SND, "SfxPlayer::handleEvents() order = 0x%X curPos = 0x%X", order, _sfxMod.curPos);
if (_sfxMod.curPos >= 1024) {
_sfxMod.curPos = 0;
order = _sfxMod.curOrder + 1;
if (order == _sfxMod.numOrder) {
_playing = false;
}
_sfxMod.curOrder = order;
}
}
void SfxPlayer::handlePattern(uint8_t channel, const uint8_t *data) {
SfxPattern pat;
memset(&pat, 0, sizeof(SfxPattern));
pat.note_1 = READ_BE_UINT16(data + 0);
pat.note_2 = READ_BE_UINT16(data + 2);
if (pat.note_1 != 0xFFFD) {
uint16_t sample = (pat.note_2 & 0xF000) >> 12;
if (sample != 0) {
uint8_t *ptr = _sfxMod.samples[sample - 1].data;
if (ptr != 0) {
debug(DBG_SND, "SfxPlayer::handlePattern() preparing sample %d", sample);
pat.sampleVolume = _sfxMod.samples[sample - 1].volume;
pat.sampleStart = 8;
pat.sampleBuffer = ptr;
pat.sampleLen = READ_BE_UINT16(ptr) * 2;
uint16_t loopLen = READ_BE_UINT16(ptr + 2) * 2;
if (loopLen != 0) {
pat.loopPos = pat.sampleLen;
pat.loopLen = loopLen;
} else {
pat.loopPos = 0;
pat.loopLen = 0;
}
int16_t m = pat.sampleVolume;
uint8_t effect = (pat.note_2 & 0x0F00) >> 8;
if (effect == 5) { // volume up
uint8_t volume = (pat.note_2 & 0xFF);
m += volume;
if (m > 0x3F) {
m = 0x3F;
}
} else if (effect == 6) { // volume down
uint8_t volume = (pat.note_2 & 0xFF);
m -= volume;
if (m < 0) {
m = 0;
}
}
_channels[channel].volume = m;
pat.sampleVolume = m;
}
}
}
if (pat.note_1 == 0xFFFD) {
debug(DBG_SND, "SfxPlayer::handlePattern() _syncVar = 0x%X", pat.note_2);
*_syncVar = pat.note_2;
} else if (pat.note_1 == 0xFFFE) {
_channels[channel].sampleLen = 0;
} else if (pat.note_1 != 0 && pat.sampleBuffer != 0) {
assert(pat.note_1 >= 0x37 && pat.note_1 < 0x1000);
// convert Amiga period value to hz
const int freq = kPaulaFreq / (pat.note_1 * 2);
debug(DBG_SND, "SfxPlayer::handlePattern() adding sample freq = 0x%X", freq);
SfxChannel *ch = &_channels[channel];
ch->sampleData = pat.sampleBuffer + pat.sampleStart;
ch->sampleLen = pat.sampleLen;
ch->sampleLoopPos = pat.loopPos;
ch->sampleLoopLen = pat.loopLen;
ch->volume = pat.sampleVolume;
ch->pos.offset = 0;
ch->pos.inc = (freq << Frac::BITS) / _rate;
}
}