-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.cpp
145 lines (130 loc) · 3.12 KB
/
mixer.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
/*
* REminiscence - Flashback interpreter
* Copyright (C) 2005-2019 Gregory Montoir (cyx@users.sourceforge.net)
*/
#include "mixer.h"
#include "systemstub.h"
#include "util.h"
Mixer::Mixer(FileSystem *fs, SystemStub *stub)
: _stub(stub), _musicType(MT_NONE), _mod(this, fs), _sfx(this) {
_musicTrack = -1;
_backgroundMusicType = MT_NONE;
}
void Mixer::init() {
memset(_channels, 0, sizeof(_channels));
_premixHook = 0;
_stub->startAudio(Mixer::mixCallback, this);
}
void Mixer::free() {
setPremixHook(0, 0);
stopAll();
_stub->stopAudio();
}
void Mixer::setPremixHook(PremixHook premixHook, void *userData) {
debug(DBG_SND, "Mixer::setPremixHook()");
LockAudioStack las(_stub);
_premixHook = premixHook;
_premixHookData = userData;
}
void Mixer::play(const uint8_t *data, uint32_t len, uint16_t freq, uint8_t volume) {
debug(DBG_SND, "Mixer::play(%d, %d)", freq, volume);
LockAudioStack las(_stub);
MixerChannel *ch = 0;
for (int i = 0; i < NUM_CHANNELS; ++i) {
MixerChannel *cur = &_channels[i];
if (cur->active) {
if (cur->chunk.data == data) {
cur->chunkPos = 0;
return;
}
} else {
ch = cur;
break;
}
}
if (ch) {
ch->active = true;
ch->volume = volume;
ch->chunk.data = data;
ch->chunk.len = len;
ch->chunkPos = 0;
ch->chunkInc = (freq << FRAC_BITS) / _stub->getOutputSampleRate();
}
}
bool Mixer::isPlaying(const uint8_t *data) const {
debug(DBG_SND, "Mixer::isPlaying");
LockAudioStack las(_stub);
for (int i = 0; i < NUM_CHANNELS; ++i) {
const MixerChannel *ch = &_channels[i];
if (ch->active && ch->chunk.data == data) {
return true;
}
}
return false;
}
uint32_t Mixer::getSampleRate() const {
return _stub->getOutputSampleRate();
}
void Mixer::stopAll() {
debug(DBG_SND, "Mixer::stopAll()");
LockAudioStack las(_stub);
for (uint8_t i = 0; i < NUM_CHANNELS; ++i) {
_channels[i].active = false;
}
}
static bool isMusicSfx(int num) {
return (num >= 68 && num <= 75);
}
void Mixer::playMusic(int num) {
debug(DBG_SND, "Mixer::playMusic(%d)", num);
if (isMusicSfx(num)) { // level action sequence
_sfx.play(num);
if (_sfx._playing) {
_musicType = MT_SFX;
}
} else { // cutscene
_mod.play(num);
if (_mod._playing) {
_musicType = MT_MOD;
}
}
}
void Mixer::stopMusic() {
debug(DBG_SND, "Mixer::stopMusic()");
switch (_musicType) {
case MT_NONE:
break;
case MT_MOD:
_mod.stop();
break;
case MT_SFX:
_sfx.stop();
break;
}
_musicType = MT_NONE;
}
void Mixer::mix(int16_t *out, int len) {
if (_premixHook) {
if (!_premixHook(_premixHookData, out, len)) {
_premixHook = 0;
_premixHookData = 0;
}
}
for (uint8_t i = 0; i < NUM_CHANNELS; ++i) {
MixerChannel *ch = &_channels[i];
if (ch->active) {
for (int pos = 0; pos < len; ++pos) {
if ((ch->chunkPos >> FRAC_BITS) >= (ch->chunk.len - 1)) {
ch->active = false;
break;
}
const int sample = ch->chunk.getPCM(ch->chunkPos >> FRAC_BITS) * ch->volume / Mixer::MAX_VOLUME;
out[pos] = ADDC_S16(out[pos], S8_to_S16(sample));
ch->chunkPos += ch->chunkInc;
}
}
}
}
void Mixer::mixCallback(void *param, int16_t *buf, int len) {
((Mixer *)param)->mix(buf, len);
}