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
|
$NetBSD: patch-af,v 1.1.1.1 2004/05/14 15:42:15 dillo Exp $
This includes sound-20040508.diff from the master site.
--- mixer.cpp.orig Sun May 9 21:04:46 2004
+++ mixer.cpp
@@ -0,0 +1,117 @@
+/* Raw - Another World Interpreter
+ * 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 program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "mixer.h"
+#include "systemstub.h"
+
+
+static int8 addclamp(int a, int b) {
+ int add = a + b;
+ if (add < -128) {
+ add = -128;
+ }
+ else if (add > 127) {
+ add = 127;
+ }
+ return (int8)add;
+}
+
+Mixer::Mixer(SystemStub *stub)
+ : _stub(stub) {
+}
+
+void Mixer::init() {
+ memset(_channels, 0, sizeof(_channels));
+ _stub->startAudio(Mixer::mixCallback, this);
+}
+
+void Mixer::free() {
+ _stub->stopAudio();
+}
+
+void Mixer::playChannel(uint8 channel, const MixerChunk *mc, uint16 freq, uint8 volume) {
+ debug(DBG_SND, "Mixer::playChannel(%d, %d, %d)", channel, freq, volume);
+ assert(channel < NUM_CHANNELS);
+ _stub->lockAudio();
+ MixerChannel *ch = &_channels[channel];
+ ch->active = true;
+ ch->volume = volume;
+ ch->chunk = *mc;
+ ch->chunkPos = 0;
+ ch->chunkInc = (freq << 8) / _stub->getOutputSampleRate();
+ _stub->unlockAudio();
+}
+
+void Mixer::stopChannel(uint8 channel) {
+ debug(DBG_SND, "Mixer::stopChannel(%d)", channel);
+ assert(channel < NUM_CHANNELS);
+ _stub->lockAudio();
+ _channels[channel].active = false;
+ _stub->unlockAudio();
+}
+
+void Mixer::stopAll() {
+ debug(DBG_SND, "Mixer::stopAll()");
+ _stub->lockAudio();
+ for (uint8 i = 0; i < NUM_CHANNELS; ++i) {
+ _channels[i].active = false;
+ }
+ _stub->unlockAudio();
+}
+
+void Mixer::mix(int8 *buf, int len) {
+ memset(buf, 0, len);
+ for (uint8 i = 0; i < NUM_CHANNELS; ++i) {
+ MixerChannel *ch = &_channels[i];
+ if (ch->active) {
+ int8 *pBuf = buf;
+ for (int j = 0; j < len; ++j, ++pBuf) {
+ uint16 p1, p2;
+ uint16 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 b1 = *(int8 *)(ch->chunk.data + p1);
+ int8 b2 = *(int8 *)(ch->chunk.data + p2);
+ int8 b = (int8)((b1 * (0xFF - ilc) + b2 * ilc) >> 8);
+ // set volume and clamp
+ *pBuf = addclamp(*pBuf, (int)b * ch->volume / 0x40);
+ }
+ }
+ }
+}
+
+void Mixer::mixCallback(void *param, uint8 *buf, int len) {
+ ((Mixer *)param)->mix((int8 *)buf, len);
+}
|