/
sfx.cpp
304 lines (253 loc) · 8.44 KB
/
sfx.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//-------------------------------------------------------------------------
/*
Copyright (C) 2010-2019 EDuke32 developers and contributors
Copyright (C) 2019 Nuke.YKT
This file is part of NBlood.
NBlood is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//-------------------------------------------------------------------------
#include "ns.h" // Must come before everything else!
#include <string.h>
#include "build.h"
#include "blood.h"
#include "raze_sound.h"
BEGIN_BLD_NS
class BloodSoundEngine : public RazeSoundEngine
{
// client specific parts of the sound engine go in this class.
void CalcPosVel(int type, const void* source, const float pt[3], int channum, int chanflags, FSoundID chanSound, FVector3* pos, FVector3* vel, FSoundChan* channel) override;
TArray<uint8_t> ReadSound(int lumpnum) override;
public:
BloodSoundEngine()
{
S_Rolloff.RolloffType = ROLLOFF_Doom;
S_Rolloff.MinDistance = 170; // these are the numbers I got when uncrunching the original sound code.
S_Rolloff.MaxDistance = 850;
}
void StopChannel(FSoundChan* chan) override
{
if (chan && chan->SysChannel != nullptr && !(chan->ChanFlags & CHANF_EVICTED) && chan->SourceType == SOURCE_Actor)
{
chan->Source = nullptr;
chan->SourceType = SOURCE_Unattached;
}
SoundEngine::StopChannel(chan);
}
};
//==========================================================================
//
//
//
//==========================================================================
TArray<uint8_t> BloodSoundEngine::ReadSound(int lumpnum)
{
auto wlump = fileSystem.OpenFileReader(lumpnum);
return wlump.Read();
}
void BloodSoundEngine::CalcPosVel(int type, const void* source, const float pt[3], int channum, int chanflags, FSoundID chanSound, FVector3* pos, FVector3* vel, FSoundChan*)
{
if (pos != nullptr && type != SOURCE_None)
{
FVector3 camera;
if (gMe && gMe->actor) camera = GetSoundPos(&gMe->actor->spr.pos);
else camera = { 0, 0, 0 }; // don't crash if there is no player.
if (vel) vel->Zero();
if (type == SOURCE_Unattached)
{
pos->X = pt[0];
pos->Y = pt[1];
pos->Z = pt[2];
}
else if (type == SOURCE_Actor)
{
assert(source != nullptr);
auto actor = (DBloodActor*)source;
// Engine expects velocity in units per second, not units per tic.
if (vel) *vel = { actor->vel.X * (30 / 65536.f), actor->vel.Z * (-30 / 65536.f), actor->vel.Y * (-30 / 65536.f) };
*pos = GetSoundPos(&actor->spr.pos);
}
else if (type == SOURCE_Ambient)
{
*pos = camera; // just to be safe. Ambient sounds are in the world but unpositioned
}
if ((chanflags & CHANF_LISTENERZ))
{
pos->Y = camera.Y;
}
}
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void GameInterface::UpdateSounds()
{
SoundListener listener;
if (gMe->actor)
{
listener.angle = -gMe->actor->spr.ang * float(BAngRadian); // Build uses a period of 2048.
listener.velocity.Zero();
listener.position = GetSoundPos(&gMe->actor->spr.pos);
listener.valid = true;
}
else
{
listener.position.Zero();
listener.valid = false;
}
listener.underwater = false;
// This should probably use a real environment instead of the pitch hacking in S_PlaySound3D.
// listenactor->waterlevel == 3;
//assert(primaryLevel->Zones.Size() > listenactor->Sector->ZoneNumber);
listener.Environment = 0;// primaryLevel->Zones[listenactor->Sector->ZoneNumber].Environment;
listener.ListenerObject = gMe;
soundEngine->SetListener(listener);
soundEngine->UpdateSounds(I_GetTime());
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
FSoundID getSfx(FSoundID soundId, float& attenuation, int& pitch, int& relvol)
{
auto udata = soundEngine->GetUserData(soundId);
if (pitch < 0) pitch = udata ? udata[0] : 0x10000;
if (relvol < 0) relvol = 0;
else if (relvol == 0) relvol = udata && udata[2] ? udata[2] : 80;
if (relvol > 255) relvol = 255;
// Limit the attenuation. More than 2.0 is simply too much.
attenuation = relvol > 0 ? clamp(80.f / relvol, 0.f, 2.f) : 1.f;
return soundId;
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void sfxPlay3DSound(int x, int y, int z, int soundId, sectortype* pSector)
{
if (!SoundEnabled() || soundId < 0) return;
auto sid = soundEngine->FindSoundByResID(soundId);
if (sid == 0) return;
vec3_t xyz = { x, y, z };
auto svec = GetSoundPos(&xyz);
float attenuation;
int pitch = -1;
int relvol = 0;
sid = getSfx(sid, attenuation, pitch, relvol);
auto sfx = soundEngine->GetSfx(sid);
EChanFlags flags = CHANF_OVERLAP;
if (sfx && sfx->LoopStart >= 0) flags |= CHANF_LOOP;
auto chan = soundEngine->StartSound(SOURCE_Unattached, nullptr, &svec, -1, flags, sid, (0.8f / 80.f) * relvol, attenuation, nullptr, pitch / 65536.f);
if (chan) chan->UserData = sectnum(pSector);
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void sfxPlay3DSoundCP(DBloodActor* pActor, int soundId, int playchannel, int playflags, int pitch, int volume)
{
if (!SoundEnabled() || soundId < 0 || !pActor) return;
auto sid = soundEngine->FindSoundByResID(soundId);
if (sid == 0) return;
auto svec = GetSoundPos(&pActor->spr.pos);
float attenuation;
sid = getSfx(sid, attenuation, pitch, volume);
if (volume == -1) volume = 80;
if (playchannel >= 0)
{
playchannel++; // This is to make 0 a valid channel value.
if (soundEngine->EnumerateChannels([=](FSoundChan* chan) -> int
{
if (chan->SourceType != SOURCE_Actor) return false; // other source types are not our business.
if (chan->EntChannel == playchannel && (chan->Source == pActor || (playflags & FX_GlobalChannel) != 0))
{
if ((playflags & FX_ChannelMatch) != 0 && chan->EntChannel == playchannel)
return true;
if ((playflags & FX_SoundMatch) != 0 && chan->OrgID == sid)
return true;
soundEngine->StopChannel(chan);
return -1;
}
return false;
})) return;
}
auto sfx = soundEngine->GetSfx(sid);
EChanFlags flags = playchannel == -1 ? CHANF_OVERLAP : CHANF_NONE;
if (sfx && sfx->LoopStart >= 0)
{
flags |= CHANF_LOOP;
flags &= ~CHANF_OVERLAP;
}
soundEngine->StartSound(SOURCE_Actor, pActor, &svec, playchannel, flags, sid, volume * (0.8f / 80.f), attenuation, nullptr, pitch / 65536.f);
}
void sfxPlay3DSound(DBloodActor* pActor, int soundId, int a3, int a4)
{
sfxPlay3DSoundCP(pActor, soundId, a3, a4, -1);
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void sfxKill3DSound(DBloodActor* pActor, int a2, int a3)
{
if (!pActor)
return;
if (a2 >= 0) a2++;
auto sid = soundEngine->FindSoundByResID(a3);
soundEngine->EnumerateChannels([=](FSoundChan* channel)
{
if (channel->SourceType == SOURCE_Actor && channel->Source == pActor && (a2 < 0 || a2 == channel->EntChannel) && (a3 < 0 || sid == channel->OrgID))
{
soundEngine->StopChannel(channel);
}
return false;
});
}
void sfxKillAllSounds(void)
{
if (soundEngine) soundEngine->EnumerateChannels([](FSoundChan* channel)
{
if (channel->SourceType == SOURCE_Actor || channel->SourceType == SOURCE_Unattached) soundEngine->StopChannel(channel);
return false;
});
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void sfxSetReverb(bool toggle)
{
if (toggle)
{
FX_SetReverb(128);
FX_SetReverbDelay(10);
}
else
FX_SetReverb(0);
}
void sfxSetReverb2(bool toggle)
{
if (toggle)
{
FX_SetReverb(128);
FX_SetReverbDelay(20);
}
else
FX_SetReverb(0);
}
END_BLD_NS