2020-12-28 09:51:40 +01:00
|
|
|
#include "driver2.h"
|
|
|
|
#include "sound.h"
|
|
|
|
|
|
|
|
#include "camera.h"
|
|
|
|
#include "xmplay.h"
|
|
|
|
#include "mission.h"
|
|
|
|
#include "glaunch.h"
|
|
|
|
#include "gamesnd.h"
|
|
|
|
#include "xaplay.h"
|
|
|
|
#include "players.h"
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
#include "LIBSPU.H"
|
|
|
|
#include "LIBETC.H"
|
2020-04-09 15:55:21 +02:00
|
|
|
#include "LIBGTE.H"
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-05-22 10:51:20 +02:00
|
|
|
#include "INLINE_C.H"
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-09-30 07:05:35 +02:00
|
|
|
#include "RAND.H"
|
|
|
|
#include "STRINGS.H"
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
#define SPU_CHANNEL_COUNT 24
|
2020-12-08 19:21:47 +01:00
|
|
|
#define LSB_BANK_COUNT 7
|
|
|
|
|
|
|
|
typedef struct __LSBDinfo
|
|
|
|
{
|
|
|
|
int addr;
|
|
|
|
int memtop;
|
|
|
|
int count[LSB_BANK_COUNT];
|
|
|
|
int bnktop[LSB_BANK_COUNT];
|
|
|
|
int append;
|
|
|
|
} LSBDinfo;
|
|
|
|
|
|
|
|
typedef struct __pauseinfo
|
|
|
|
{
|
|
|
|
u_short pitch[MAX_SFX_CHANNELS];
|
|
|
|
u_short voll[MAX_SFX_CHANNELS];
|
|
|
|
u_short volr[MAX_SFX_CHANNELS];
|
|
|
|
u_short max;
|
|
|
|
u_short lev;
|
|
|
|
} pauseinfo;
|
|
|
|
|
|
|
|
static pauseinfo pause;
|
|
|
|
static LSBDinfo lsbTabs;
|
2020-10-28 09:17:27 +01:00
|
|
|
|
2020-12-08 17:45:14 +01:00
|
|
|
LONGVECTOR3 dummylong = { 0, 0, 0 };
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-08 18:12:17 +01:00
|
|
|
int bankaddr[2] = { 0 };
|
|
|
|
int banksize[2] = { 88064, 412672 };
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-04-04 15:42:29 +02:00
|
|
|
char banks[24] = { 0 };
|
|
|
|
|
2020-12-08 19:21:47 +01:00
|
|
|
SAMPLE_DATA samples[LSB_BANK_COUNT][35];
|
2020-10-28 09:17:27 +01:00
|
|
|
CHANNEL_DATA channels[MAX_SFX_CHANNELS]; // offset 0xDE480
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-04-09 15:55:21 +02:00
|
|
|
int master_volume = 0; // why need two?
|
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
int gMasterVolume = 0;
|
2020-08-22 17:13:42 +02:00
|
|
|
int gMusicVolume = -4000;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-05-12 17:49:21 +02:00
|
|
|
int Song_ID = -1;
|
|
|
|
int VABID = -1;
|
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
int gSoundMode = 1; // mono or stereo
|
|
|
|
|
|
|
|
int stop_sound_handler = 0;
|
|
|
|
int sound_paused = 0;
|
|
|
|
int music_paused = 0;
|
|
|
|
|
|
|
|
SpuCommonAttr sound_attr;
|
|
|
|
|
|
|
|
int vblcounter = 0; // vblank counter
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
void SoundHandler(void)
|
|
|
|
{
|
|
|
|
int ct;
|
|
|
|
int off;
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
if (stop_sound_handler || sound_paused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (ct = 0; ct < MAX_SFX_CHANNELS; ct++)
|
|
|
|
{
|
|
|
|
if (channels[ct].loop || channels[ct].time == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (--channels[ct].time == 0)
|
|
|
|
off |= SPU_KEYCH(ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (off)
|
|
|
|
SpuSetKey(0, off);
|
|
|
|
}
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
void VsyncProc(void)
|
|
|
|
{
|
|
|
|
vblcounter++;
|
|
|
|
|
|
|
|
if (Song_ID != -1)
|
|
|
|
XM_Update();
|
|
|
|
|
|
|
|
SoundHandler();
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void InitSound(void)
|
|
|
|
{
|
2020-04-08 20:07:11 +02:00
|
|
|
int i;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
SpuInit();
|
2020-12-08 19:21:47 +01:00
|
|
|
SpuInitMalloc(LSB_BANK_COUNT, banks);
|
2020-03-27 21:47:29 +01:00
|
|
|
SpuSetMute(1);
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2021-01-19 17:31:53 +01:00
|
|
|
AllocateReverb(SPU_REV_MODE_ROOM | SPU_REV_MODE_STUDIO_A, 16384);
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2021-01-19 17:31:53 +01:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2020-04-08 20:07:11 +02:00
|
|
|
bankaddr[i] = SpuMalloc(banksize[i]);
|
|
|
|
|
|
|
|
if (bankaddr[i] == -1)
|
|
|
|
{
|
2020-12-09 17:57:59 +01:00
|
|
|
printError("Failed to SpuMalloc! Exiting...\n");
|
2020-12-28 11:07:24 +01:00
|
|
|
#ifndef PSX
|
2020-03-27 21:47:29 +01:00
|
|
|
exit(-1);
|
2020-12-28 11:07:24 +01:00
|
|
|
#endif
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
2021-01-19 17:31:53 +01:00
|
|
|
}
|
2020-04-04 15:42:29 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
ResetSound();
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-05-26 14:21:40 +02:00
|
|
|
XM_OnceOffInit(GetVideoMode()); // [A] PAL or NTSC here
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
SetMasterVolume(gMasterVolume);
|
|
|
|
VSyncCallback(VsyncProc);
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
SpuSetMute(0);
|
|
|
|
}
|
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void ClearChannelFields(int channel)
|
|
|
|
{
|
2020-05-15 19:14:17 +02:00
|
|
|
channels[channel].loop = 0;
|
|
|
|
channels[channel].locked = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
channels[channel].time = 0;
|
|
|
|
channels[channel].samplerate = 0;
|
|
|
|
channels[channel].srcvolume = -10000;
|
2020-11-02 20:12:46 +01:00
|
|
|
channels[channel].srcpitch = 4096;
|
|
|
|
channels[channel].volumeScale = 4096;
|
|
|
|
channels[channel].dopplerScale = 4096;
|
2020-03-27 21:47:29 +01:00
|
|
|
channels[channel].srcposition = NULL;
|
|
|
|
channels[channel].position.vx = 0;
|
|
|
|
channels[channel].position.vy = 0;
|
|
|
|
channels[channel].position.vz = 0;
|
2020-12-08 17:45:14 +01:00
|
|
|
channels[channel].srcvelocity = (LONGVECTOR3*)dummylong;
|
2020-03-27 21:47:29 +01:00
|
|
|
channels[channel].player = 0;
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void ResetSound(void)
|
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
int ct;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
stop_sound_handler = 1;
|
|
|
|
sound_paused = 0;
|
|
|
|
music_paused = 0;
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
sound_attr.mvol.left = 16383;
|
|
|
|
sound_attr.mvol.right = 16383;
|
|
|
|
sound_attr.mask = SPU_COMMON_MVOLL | SPU_COMMON_MVOLR;
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
SpuSetCommonAttr(&sound_attr);
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
ct = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
do {
|
2020-12-28 11:07:24 +01:00
|
|
|
memset((u_char*)&channels[ct], 0, sizeof(CHANNEL_DATA));
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
channels[ct].attr.volmode.left = 0;
|
|
|
|
channels[ct].attr.volmode.right = 0;
|
|
|
|
channels[ct].attr.voice = SPU_KEYCH(ct);
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
ClearChannelFields(ct);
|
|
|
|
ct++;
|
|
|
|
} while (ct < MAX_SFX_CHANNELS);
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
ct = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
do {
|
2020-11-02 20:12:46 +01:00
|
|
|
SpuSetVoiceRR(ct, 6);
|
|
|
|
SpuSetVoiceAR(ct, 35);
|
|
|
|
|
|
|
|
ct++;
|
|
|
|
} while (ct < MAX_SFX_CHANNELS);
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
stop_sound_handler = 0;
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void SetReverbState(int on)
|
|
|
|
{
|
2020-06-16 20:19:40 +02:00
|
|
|
if (on == 0)
|
2020-11-02 20:12:46 +01:00
|
|
|
SpuSetReverbVoice(0, SPU_ALLCH);
|
2020-06-16 20:19:40 +02:00
|
|
|
else
|
2020-11-02 20:12:46 +01:00
|
|
|
SpuSetReverbVoice(1, SPU_ALLCH);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void SetReverbInGameState(int on)
|
|
|
|
{
|
2020-12-08 17:45:14 +01:00
|
|
|
int cl;
|
2020-09-01 20:30:42 +02:00
|
|
|
|
|
|
|
cl = 0xffff;
|
|
|
|
|
|
|
|
gSpeechQueue.reverb = on;
|
|
|
|
|
|
|
|
if (on && gSpeechQueue.is_playing)
|
2020-09-25 15:44:02 +02:00
|
|
|
cl &= ~SPU_KEYCH(gSpeechQueue.chan);
|
2020-09-01 20:30:42 +02:00
|
|
|
|
|
|
|
SpuSetReverbVoice(on, cl);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
int SetReverbChannelState(int ch, int on)
|
|
|
|
{
|
2020-12-08 17:45:14 +01:00
|
|
|
int cl;
|
2020-09-01 20:30:42 +02:00
|
|
|
int prev;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-09-01 20:30:42 +02:00
|
|
|
cl = SPU_KEYCH(ch);
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-09-01 20:30:42 +02:00
|
|
|
prev = SpuGetReverbVoice();
|
|
|
|
|
|
|
|
SpuSetReverbVoice(on, cl);
|
|
|
|
|
|
|
|
return (prev & cl) != 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
// [D] [T]
|
|
|
|
void SetMasterVolume(int vol)
|
|
|
|
{
|
|
|
|
int vl = MAX(0, 10000 + vol);
|
|
|
|
|
|
|
|
master_volume = vl + vl / 2 + vl / 8 + vl / 128;
|
|
|
|
gMasterVolume = vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
void SetXMVolume(int volume)
|
|
|
|
{
|
|
|
|
int vol;
|
|
|
|
|
|
|
|
vol = MIN(10000, MAX(0, 10000 + volume));
|
|
|
|
|
|
|
|
gMusicVolume = volume;
|
|
|
|
|
|
|
|
if (Song_ID == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
XM_SetMasterVol(Song_ID, (vol >> 7) + (vol >> 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
char SetPlayerOwnsChannel(int chan, char player)
|
|
|
|
{
|
|
|
|
char oldPlayer;
|
|
|
|
|
|
|
|
oldPlayer = channels[chan].player;
|
|
|
|
channels[chan].player = player;
|
|
|
|
|
|
|
|
return oldPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
int CalculateVolume(int channel)
|
|
|
|
{
|
|
|
|
int distance;
|
|
|
|
int fade;
|
|
|
|
int volume;
|
|
|
|
|
|
|
|
VECTOR *srcPos;
|
|
|
|
VECTOR *plPos;
|
|
|
|
VECTOR ofse;
|
|
|
|
|
|
|
|
volume = 10000 + channels[channel].srcvolume;
|
|
|
|
|
|
|
|
if (NumPlayers > 1 && NoPlayerControl == 0)
|
|
|
|
{
|
|
|
|
volume = (volume * 3 / 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
srcPos = channels[channel].srcposition;
|
|
|
|
|
|
|
|
if (srcPos != NULL)
|
|
|
|
{
|
|
|
|
plPos = &player[channels[channel].player].cameraPos;
|
|
|
|
|
|
|
|
ofse.vx = srcPos->vx - plPos->vx;
|
|
|
|
ofse.vy = srcPos->vy + plPos->vy;
|
|
|
|
ofse.vz = srcPos->vz - plPos->vz;
|
|
|
|
|
|
|
|
gte_ldlvl(&ofse);
|
|
|
|
gte_sqr0();
|
|
|
|
gte_stlvnl(&ofse);
|
|
|
|
|
|
|
|
distance = SquareRoot0(ofse.vx + ofse.vy + ofse.vz);
|
|
|
|
|
|
|
|
if (distance < 1024)
|
|
|
|
distance = 1024;
|
|
|
|
|
|
|
|
fade = 0xbd0000 / (distance + 2000);
|
|
|
|
|
|
|
|
distance = MAX(0, MIN(4096, distance - 16000));
|
|
|
|
|
|
|
|
if (distance != 0)
|
|
|
|
fade *= (4096 - distance) / 4096;
|
|
|
|
|
|
|
|
volume = (fade * volume) / 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
return volume - 10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int gSurround = 0;
|
|
|
|
|
|
|
|
// [D]
|
|
|
|
void UpdateVolumeAttributesS(int channel, int proximity)
|
|
|
|
{
|
|
|
|
int volume;
|
|
|
|
int vol;
|
|
|
|
int player_id;
|
|
|
|
VECTOR *pos;
|
|
|
|
VECTOR *cam_pos;
|
|
|
|
int dist;
|
|
|
|
int cam_ang, ang;
|
|
|
|
int damp;
|
|
|
|
|
|
|
|
|
|
|
|
player_id = channels[channel].player;
|
|
|
|
pos = channels[channel].srcposition;
|
|
|
|
cam_pos = &player[player_id].cameraPos;
|
|
|
|
cam_ang = player[player_id].snd_cam_ang;
|
|
|
|
|
|
|
|
volume = CalculateVolume(channel);
|
|
|
|
|
|
|
|
vol = MAX(0, 10000 + volume);
|
|
|
|
|
|
|
|
if (camera_change == 1 || old_camera_change == 1)
|
|
|
|
vol = 0;
|
|
|
|
|
|
|
|
vol = (vol + vol / 2 + vol / 8 + vol / 128) * master_volume / 16384;
|
|
|
|
|
|
|
|
channels[channel].attr.volume.left = vol;
|
|
|
|
channels[channel].attr.volume.right = gSurround ? -vol : vol; // HMM: why like that?
|
|
|
|
|
|
|
|
if (vol == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pos == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pos->vz == cam_pos->vz && cam_pos->vx == pos->vx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// HACK: player car sounds are not affected with stereo
|
|
|
|
if (player[player_id].cameraView == 2)
|
|
|
|
{
|
|
|
|
if (channel == player[player_id].skidding.chan || channel == player[player_id].wheelnoise.chan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (player_id == 0)
|
|
|
|
{
|
|
|
|
if (channel == 0 || channel == 1 || channel == 2)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (player_id == 1)
|
|
|
|
{
|
|
|
|
if (channel == 3 || channel == 4 || channel == 5)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calc angle relative to camera angle and source position
|
|
|
|
ang = 2048 - (((-ratan2(pos->vz - cam_pos->vz, cam_pos->vx - pos->vx) + 4096 + 3072) % 4096 - (cam_ang - 4096)) % 4096);
|
|
|
|
|
|
|
|
// wtf?
|
|
|
|
if (ABS(ang) >= 1024)
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
tmp = 2048 - ABS(ang);
|
|
|
|
|
|
|
|
if (ang < 1)
|
|
|
|
ang = -tmp;
|
|
|
|
else
|
|
|
|
ang = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proximity > 0)
|
|
|
|
{
|
|
|
|
int dx, dz;
|
|
|
|
|
|
|
|
dx = cam_pos->vx - pos->vx;
|
|
|
|
dz = cam_pos->vz - pos->vz;
|
|
|
|
|
|
|
|
dist = jsqrt(dx * dx + dz * dz);
|
|
|
|
|
|
|
|
if (dist >= 12000)
|
|
|
|
proximity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate spatial damp for specific side
|
|
|
|
if (ang > 0)
|
|
|
|
{
|
|
|
|
damp = ((vol * 9 / 10) * ang) / 1024;
|
|
|
|
|
|
|
|
if (proximity > 0)
|
|
|
|
damp = damp * dist / 12000;
|
|
|
|
|
|
|
|
channels[channel].attr.volume.left = MAX(0, vol - damp);
|
|
|
|
}
|
|
|
|
else if (ang < 0)
|
|
|
|
{
|
|
|
|
damp = ((vol * 9 / 10) * -ang) / 1024;
|
|
|
|
|
|
|
|
if (proximity > 0)
|
|
|
|
damp = damp * dist / 12000;
|
|
|
|
|
|
|
|
channels[channel].attr.volume.right = gSurround ? -MAX(0, vol - damp) : MAX(0, vol - damp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
void UpdateVolumeAttributesM(int channel)
|
|
|
|
{
|
|
|
|
int volume;
|
|
|
|
int vol;
|
|
|
|
|
|
|
|
volume = CalculateVolume(channel);
|
|
|
|
|
|
|
|
vol = MAX(0, 10000 + volume);
|
|
|
|
|
|
|
|
if (camera_change == 1 || old_camera_change == 1)
|
|
|
|
vol = 0;
|
|
|
|
|
|
|
|
vol = (vol + vol / 2 + vol / 8 + vol / 128) * master_volume / 16384;
|
|
|
|
|
|
|
|
channels[channel].attr.volume.left = vol;
|
|
|
|
channels[channel].attr.volume.right = vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [D] [T]
|
|
|
|
int CompleteSoundSetup(int channel, int bank, int sample, int pitch, int proximity)
|
|
|
|
{
|
|
|
|
int bpf;
|
|
|
|
int rate;
|
|
|
|
|
|
|
|
rate = samples[bank][sample].samplerate * pitch;
|
|
|
|
bpf = (rate / 4096) / 50;
|
|
|
|
|
|
|
|
if (bpf == 0)
|
|
|
|
{
|
|
|
|
channel = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gSoundMode == 1 && proximity != -1)
|
|
|
|
UpdateVolumeAttributesS(channel, proximity);
|
|
|
|
else
|
|
|
|
UpdateVolumeAttributesM(channel);
|
|
|
|
|
|
|
|
stop_sound_handler = 1;
|
|
|
|
|
|
|
|
channels[channel].attr.mask = SPU_VOICE_VOLL | SPU_VOICE_VOLR | SPU_VOICE_VOLMODEL | SPU_VOICE_VOLMODER | SPU_VOICE_PITCH | SPU_VOICE_WDSA;
|
|
|
|
channels[channel].attr.addr = samples[bank][sample].address;
|
|
|
|
channels[channel].attr.pitch = MIN(rate / 44100, 16383);
|
|
|
|
channels[channel].time = (samples[bank][sample].length / bpf) * 2 + 2;
|
|
|
|
channels[channel].loop = samples[bank][sample].loop;
|
|
|
|
|
|
|
|
channels[channel].samplerate = samples[bank][sample].samplerate;
|
|
|
|
|
|
|
|
if (sound_paused != 0)
|
|
|
|
{
|
|
|
|
channels[channel].attr.volume.left = 0;
|
|
|
|
channels[channel].attr.volume.right = 0;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
SpuSetVoiceAttr(&channels[channel].attr);
|
|
|
|
SpuSetKey(1, channels[channel].attr.voice);
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
stop_sound_handler = 0;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
return channel;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-12-09 09:33:57 +01:00
|
|
|
void ComputeDoppler(CHANNEL_DATA *ch)
|
2020-03-27 21:47:29 +01:00
|
|
|
{
|
2020-12-09 09:33:57 +01:00
|
|
|
int dist;
|
|
|
|
int seperationrate;
|
2020-04-09 15:55:21 +02:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
VECTOR *srcPos;
|
|
|
|
long* srcVel; // LONGVECTOR3
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
PLAYER *pl;
|
|
|
|
int dx, dy, dz;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
srcPos = ch->srcposition;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
if (srcPos == NULL)
|
|
|
|
{
|
|
|
|
ch->dopplerScale = ONE;
|
|
|
|
return;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
srcVel = (long*)ch->srcvelocity;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
pl = &player[ch->player];
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
dx = srcPos->vx - pl->cameraPos.vx;
|
|
|
|
dy = srcPos->vy + pl->cameraPos.vy;
|
|
|
|
dz = srcPos->vz - pl->cameraPos.vz;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
dist = jsqrt(dx * dx + dy * dy + dz * dz);
|
2020-04-27 01:28:49 +02:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
dx = (srcPos->vx - pl->cameraPos.vx) + FIXEDH(srcVel[0] - pl->camera_vel[0]);
|
|
|
|
dy = (srcPos->vy + pl->cameraPos.vy) + FIXEDH(srcVel[1] - pl->camera_vel[1]);
|
|
|
|
dz = (srcPos->vz - pl->cameraPos.vz) + FIXEDH(srcVel[2] - pl->camera_vel[2]);
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
seperationrate = jsqrt(dx * dx + dy * dy + dz * dz);
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
ch->dopplerScale = (seperationrate - dist) * -3 + ONE;
|
|
|
|
ch->cameradist = dist;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
if (ch->dopplerScale << 16 < 0)
|
|
|
|
ch->dopplerScale = 0;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
int StartSound(int channel, int bank, int sample, int volume, int pitch)
|
|
|
|
{
|
2020-04-26 09:06:59 +02:00
|
|
|
if (channel < 0)
|
2020-03-27 21:47:29 +01:00
|
|
|
channel = GetFreeChannel();
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
2020-05-15 19:14:17 +02:00
|
|
|
return -1;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
channels[channel].srcposition = NULL;
|
2020-05-15 19:14:17 +02:00
|
|
|
channels[channel].srcvolume = volume;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
return CompleteSoundSetup(channel, bank, sample, pitch, 0);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-12-08 17:45:14 +01:00
|
|
|
int Start3DTrackingSound(int channel, int bank, int sample, VECTOR *position, LONGVECTOR3* velocity)
|
2020-03-27 21:47:29 +01:00
|
|
|
{
|
2020-04-26 09:06:59 +02:00
|
|
|
if (channel < 0)
|
2020-03-27 21:47:29 +01:00
|
|
|
channel = GetFreeChannel();
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
2020-06-16 20:19:40 +02:00
|
|
|
return -1;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-06-16 20:19:40 +02:00
|
|
|
channels[channel].srcposition = position;
|
2020-12-08 17:45:14 +01:00
|
|
|
channels[channel].srcvelocity = velocity ? velocity : (LONGVECTOR3*)dummylong;
|
2020-11-02 20:12:46 +01:00
|
|
|
channels[channel].srcpitch = 4096;
|
2020-06-16 20:19:40 +02:00
|
|
|
channels[channel].srcvolume = 0;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
channel = CompleteSoundSetup(channel, bank, sample, 4096, 0);
|
2020-06-16 20:19:40 +02:00
|
|
|
|
|
|
|
ComputeDoppler(&channels[channel]);
|
2020-10-28 09:17:27 +01:00
|
|
|
SetChannelPitch(channel, 4096);
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-06-16 20:19:40 +02:00
|
|
|
return channel;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:46:06 +02:00
|
|
|
// [D]
|
2020-03-27 21:47:29 +01:00
|
|
|
int Start3DSoundVolPitch(int channel, int bank, int sample, int x, int y, int z, int volume, int pitch)
|
|
|
|
{
|
2020-04-27 01:28:49 +02:00
|
|
|
if (channel < 0)
|
2020-03-27 21:47:29 +01:00
|
|
|
channel = GetFreeChannel();
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
2020-06-16 20:19:40 +02:00
|
|
|
return -1;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-06-16 20:19:40 +02:00
|
|
|
channels[channel].srcposition = &channels[channel].position;
|
2020-12-08 17:45:14 +01:00
|
|
|
channels[channel].srcvelocity = (LONGVECTOR3*)dummylong;
|
2020-06-16 20:19:40 +02:00
|
|
|
channels[channel].position.vx = x;
|
|
|
|
channels[channel].position.vy = y;
|
|
|
|
channels[channel].position.vz = z;
|
|
|
|
channels[channel].srcvolume = volume;
|
|
|
|
channels[channel].srcpitch = pitch;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-06-16 20:19:40 +02:00
|
|
|
channel = CompleteSoundSetup(channel, bank, sample, pitch, 0);
|
|
|
|
|
|
|
|
ComputeDoppler(&channels[channel]);
|
|
|
|
SetChannelPitch(channel, pitch);
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-04-27 01:28:49 +02:00
|
|
|
return channel;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void SetChannelPitch(int channel, int pitch)
|
|
|
|
{
|
2020-12-08 17:45:14 +01:00
|
|
|
int rate;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
2020-05-15 19:14:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (sound_paused)
|
|
|
|
return;
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (channels[channel].time != 0 && pitch > 128)
|
2020-04-26 09:06:59 +02:00
|
|
|
{
|
2020-05-15 19:14:17 +02:00
|
|
|
channels[channel].srcpitch = pitch;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
rate = (channels[channel].samplerate * ((channels[channel].dopplerScale * pitch) / 4096)) / 44100;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
channels[channel].attr.mask = SPU_VOICE_PITCH;
|
|
|
|
channels[channel].attr.pitch = MIN(rate, 16383);
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
SpuSetVoiceAttr(&channels[channel].attr);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void SetChannelVolume(int channel, int volume, int proximity)
|
|
|
|
{
|
2020-10-28 09:17:27 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
2020-05-15 19:14:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (sound_paused == 0 && channels[channel].time != 0)
|
2020-04-26 09:06:59 +02:00
|
|
|
{
|
|
|
|
channels[channel].srcvolume = volume;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-04-26 09:06:59 +02:00
|
|
|
if (gSoundMode == 1)
|
2020-03-27 21:47:29 +01:00
|
|
|
UpdateVolumeAttributesS(channel, proximity);
|
2020-04-26 09:06:59 +02:00
|
|
|
else
|
2020-03-27 21:47:29 +01:00
|
|
|
UpdateVolumeAttributesM(channel);
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
channels[channel].attr.mask = SPU_VOICE_VOLL | SPU_VOICE_VOLR | SPU_VOICE_VOLMODEL | SPU_VOICE_VOLMODER;
|
2020-04-26 09:06:59 +02:00
|
|
|
SpuSetVoiceAttr(&channels[channel].attr);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-12-08 17:45:14 +01:00
|
|
|
void SetChannelPosition3(int channel, VECTOR *position, LONGVECTOR3* velocity, int volume, int pitch, int proximity)
|
2020-03-27 21:47:29 +01:00
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS)
|
2020-05-15 19:14:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (camera_change != 1 && old_camera_change != 1 && sound_paused == 0)
|
2020-04-26 09:06:59 +02:00
|
|
|
{
|
|
|
|
channels[channel].srcposition = position;
|
2020-12-08 17:45:14 +01:00
|
|
|
channels[channel].srcvelocity = velocity ? velocity : (LONGVECTOR3*)dummylong;
|
2020-04-26 09:06:59 +02:00
|
|
|
channels[channel].srcvolume = volume;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-04-26 09:06:59 +02:00
|
|
|
if (gSoundMode == 1)
|
2020-03-27 21:47:29 +01:00
|
|
|
UpdateVolumeAttributesS(channel, proximity);
|
2020-04-26 09:06:59 +02:00
|
|
|
else
|
2020-03-27 21:47:29 +01:00
|
|
|
UpdateVolumeAttributesM(channel);
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
channels[channel].attr.mask = SPU_VOICE_VOLL | SPU_VOICE_VOLR | SPU_VOICE_VOLMODEL | SPU_VOICE_VOLMODER;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
|
|
|
SpuSetVoiceAttr(&channels[channel].attr);
|
|
|
|
|
|
|
|
ComputeDoppler(&channels[channel]);
|
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
SetChannelPitch(channel, pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 18:08:42 +02:00
|
|
|
static int music_pause_max;
|
2020-05-12 16:53:26 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void PauseXM(void)
|
|
|
|
{
|
2020-05-12 16:53:26 +02:00
|
|
|
int fade;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (music_paused != 0)
|
|
|
|
return;
|
2020-05-12 16:53:26 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
music_pause_max = 0;
|
|
|
|
fade = 96;
|
2020-05-12 16:53:26 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
while (fade < music_pause_max) // [A] WTF?
|
|
|
|
fade += 96;
|
2020-05-14 15:11:44 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (Song_ID != -1) // [A] bug fix
|
|
|
|
XM_Pause(Song_ID);
|
|
|
|
|
|
|
|
music_paused = 1;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void PauseSFX(void)
|
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
int i, fade;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (sound_paused)
|
|
|
|
return;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
pause.max = 0;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// set the faded volume
|
|
|
|
for (i = 0; i < MAX_SFX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
int vl;
|
|
|
|
SpuGetVoiceVolume(i, (short*)&pause.voll[i], (short*)&pause.volr[i]);
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
vl = MAX(pause.voll[i], pause.volr[i]);
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (pause.max < vl)
|
|
|
|
pause.max = vl;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
pause.lev = gMasterVolume + 10000;
|
|
|
|
}
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
fade = 0;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// fade out each voice
|
|
|
|
while (fade < pause.max)
|
|
|
|
{
|
|
|
|
for (i = 0; i < MAX_SFX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
SpuSetVoiceVolume(i, MAX(0, pause.voll[i] - fade), MAX(0, pause.volr[i] - fade));
|
|
|
|
}
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
fade += 96;
|
|
|
|
}
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// pause by zeroing pitch
|
|
|
|
for (i = 0; i < MAX_SFX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
SpuGetVoicePitch(i, &pause.pitch[i]);
|
|
|
|
SpuSetVoicePitch(i, 0);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
2020-11-02 20:12:46 +01:00
|
|
|
|
|
|
|
sound_paused = 1;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void PauseSound(void)
|
|
|
|
{
|
|
|
|
SetReverbInGameState(0);
|
|
|
|
PauseXM();
|
|
|
|
PauseSFX();
|
|
|
|
PauseXA();
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void UnPauseXM(void)
|
|
|
|
{
|
2020-05-12 17:49:21 +02:00
|
|
|
if (Song_ID == -1)
|
|
|
|
return;
|
|
|
|
|
2020-05-12 16:53:26 +02:00
|
|
|
int fade;
|
|
|
|
|
|
|
|
if (music_paused != 0)
|
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
fade = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
do {
|
2020-08-13 18:08:42 +02:00
|
|
|
fade += 96;
|
2020-11-02 20:12:46 +01:00
|
|
|
} while (fade < music_pause_max); // [A] WTF?
|
2020-05-12 16:53:26 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
XM_Restart(Song_ID);
|
|
|
|
music_paused = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void UnPauseSFX(void)
|
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
int i, fade, vol;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (!sound_paused)
|
|
|
|
return;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// restore pitch
|
|
|
|
for (i = 0; i < MAX_SFX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
SpuSetVoicePitch(i, pause.pitch[i]);
|
|
|
|
}
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
vol = 10000 + gMasterVolume;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// restore volume levels
|
|
|
|
if (pause.lev != 0 && pause.lev != vol)
|
|
|
|
{
|
|
|
|
for (i = 0; i < MAX_SFX_CHANNELS; i++)
|
2020-05-02 04:13:50 +02:00
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
pause.voll[i] = pause.voll[i] * vol / pause.lev;
|
|
|
|
pause.volr[i] = pause.volr[i] * vol / pause.lev;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
2020-11-02 20:12:46 +01:00
|
|
|
}
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
fade = 0;
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// fade out each voice
|
|
|
|
while (fade < pause.max)
|
|
|
|
{
|
|
|
|
for (i = 0; i < MAX_SFX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
SpuSetVoiceVolume(i, MIN(fade, pause.voll[i]), MIN(fade, pause.volr[i]));
|
|
|
|
}
|
2020-05-02 04:13:50 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
fade += 96;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
2020-11-02 20:12:46 +01:00
|
|
|
|
|
|
|
sound_paused = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void UnPauseSound(void)
|
|
|
|
{
|
|
|
|
UnPauseXM();
|
|
|
|
UnPauseSFX();
|
|
|
|
ResumeXA();
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void StopChannel(int channel)
|
|
|
|
{
|
2020-05-15 19:14:17 +02:00
|
|
|
unsigned char lock;
|
|
|
|
int vsync;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS)
|
2020-05-15 19:14:17 +02:00
|
|
|
return;
|
2020-04-26 09:06:59 +02:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
lock = channels[channel].locked;
|
|
|
|
vsync = VSync(-1);
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
SpuSetKey(0, channels[channel].attr.voice);
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// PSX SPU keyoff sometimes does not work for first time due to it's nature
|
2020-05-15 19:14:17 +02:00
|
|
|
do {
|
|
|
|
if (SpuGetKeyStatus(channels[channel].attr.voice) == 0)
|
|
|
|
break;
|
|
|
|
} while (VSync(-1) - vsync < 8);
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
ClearChannelFields(channel);
|
2020-11-02 20:12:46 +01:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
channels[channel].locked = lock;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void StopAllChannels(void)
|
|
|
|
{
|
2020-05-15 19:14:17 +02:00
|
|
|
int ct;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-05-15 19:14:17 +02:00
|
|
|
ct = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
do {
|
2020-05-15 19:14:17 +02:00
|
|
|
StopChannel(ct);
|
2020-03-27 21:47:29 +01:00
|
|
|
VSync(0);
|
2020-05-15 19:14:17 +02:00
|
|
|
ct++;
|
2020-10-28 09:17:27 +01:00
|
|
|
} while (ct < MAX_SFX_CHANNELS);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void LockChannel(int channel)
|
|
|
|
{
|
2020-10-28 09:17:27 +01:00
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
2020-05-15 19:14:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
channels[channel].locked = 1;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void UnlockChannel(int c)
|
|
|
|
{
|
2020-10-28 09:17:27 +01:00
|
|
|
if (c < 0 || c >= MAX_SFX_CHANNELS) // [A]
|
2020-05-15 19:14:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
channels[c].locked = 0;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
int LoadSoundBank(char *address, int length, int bank)
|
|
|
|
{
|
2020-04-27 01:28:49 +02:00
|
|
|
int slength;
|
|
|
|
int spuaddress;
|
|
|
|
int num_samples;
|
2020-11-02 20:12:46 +01:00
|
|
|
int ct;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
spuaddress = bankaddr[bank];
|
2020-04-27 01:28:49 +02:00
|
|
|
|
|
|
|
num_samples = *(int *)address;
|
2020-11-02 20:12:46 +01:00
|
|
|
slength = num_samples * sizeof(SAMPLE_DATA) + sizeof(int);
|
2020-04-27 01:28:49 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// copy sample info
|
2020-12-28 11:07:24 +01:00
|
|
|
memcpy((u_char*)samples[bank], (u_char*)(address + sizeof(int)), num_samples * sizeof(SAMPLE_DATA));
|
2020-11-02 20:12:46 +01:00
|
|
|
|
|
|
|
// transfer sample data
|
|
|
|
SpuSetTransferMode(SPU_TRANSFER_BY_DMA);
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-04-27 01:28:49 +02:00
|
|
|
SpuSetTransferStartAddr(spuaddress);
|
|
|
|
SpuWrite((unsigned char*)address + slength, length - slength);
|
2020-11-02 20:12:46 +01:00
|
|
|
|
|
|
|
SpuIsTransferCompleted(SPU_TRANSFER_WAIT);
|
2020-04-27 01:28:49 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
for(ct = 0; ct < num_samples; ct++)
|
2020-04-27 01:28:49 +02:00
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
samples[bank][ct].address += spuaddress;
|
2020-12-09 09:33:57 +01:00
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-09 09:33:57 +01:00
|
|
|
return num_samples;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void UpdateXMSamples(int num_samps)
|
|
|
|
{
|
2020-05-13 15:56:19 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < num_samps; i++)
|
|
|
|
XM_SetVAGAddress(VABID, i, samples[0][i].address);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
int LoadSoundBankDynamic(char *address, int length, int dbank)
|
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
int i;
|
|
|
|
int slength;
|
2020-04-27 12:52:30 +02:00
|
|
|
int num_samples;
|
|
|
|
|
2020-04-08 16:46:06 +02:00
|
|
|
if (address == NULL)
|
|
|
|
{
|
2020-05-24 09:40:04 +02:00
|
|
|
switch (length)
|
|
|
|
{
|
2020-04-04 15:42:29 +02:00
|
|
|
case 0:
|
2020-05-24 09:40:04 +02:00
|
|
|
if (dbank == 0)
|
|
|
|
{
|
2020-04-04 15:42:29 +02:00
|
|
|
lsbTabs.append = 0;
|
2021-02-14 22:17:44 +01:00
|
|
|
printInfo("*---LSBD(): successful init---*\n");
|
2020-04-04 15:42:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
lsbTabs.memtop = lsbTabs.addr;
|
2021-02-14 22:17:44 +01:00
|
|
|
printInfo("*---LSBD(): saved memtop---*\n");
|
2020-04-04 15:42:29 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
lsbTabs.addr = lsbTabs.memtop;
|
2021-02-14 22:17:44 +01:00
|
|
|
printInfo("*---LSBD(): goneto memtop---*\n");
|
2020-04-04 15:42:29 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
lsbTabs.bnktop[dbank] = lsbTabs.count[dbank];
|
2021-02-14 22:17:44 +01:00
|
|
|
printInfo("*---LSBD(): saved banktop %d---*\n", lsbTabs.bnktop[dbank]);
|
2020-04-04 15:42:29 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
lsbTabs.count[dbank] = lsbTabs.bnktop[dbank];
|
2021-02-14 22:17:44 +01:00
|
|
|
printInfo("*---LSBD(): goneto banktop %d---*\n", lsbTabs.count[dbank]);
|
2020-04-04 15:42:29 +02:00
|
|
|
break;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// init bank samples
|
|
|
|
if (lsbTabs.append == 0)
|
|
|
|
{
|
|
|
|
lsbTabs.addr = bankaddr[1];
|
|
|
|
|
|
|
|
for(i = 0; i < 7; i++)
|
|
|
|
lsbTabs.count[i] = 0;
|
|
|
|
|
|
|
|
lsbTabs.append = 1;
|
|
|
|
}
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
num_samples = *(int *)address;
|
|
|
|
slength = num_samples * sizeof(SAMPLE_DATA) + sizeof(int);
|
2020-04-04 15:42:29 +02:00
|
|
|
|
2020-12-28 11:07:24 +01:00
|
|
|
memcpy((u_char*)(samples[dbank] + lsbTabs.count[dbank]), (u_char*)(address + sizeof(int)), num_samples * sizeof(SAMPLE_DATA));
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
SpuSetTransferMode(SPU_TRANSFER_BY_DMA);
|
2020-04-08 20:07:11 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
SpuSetTransferStartAddr(lsbTabs.addr);
|
|
|
|
SpuWrite((unsigned char*)address + slength, length - slength);
|
2020-04-04 15:42:29 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
SpuIsTransferCompleted(SPU_TRANSFER_WAIT);
|
2020-04-04 15:42:29 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// set proper address for samples
|
|
|
|
for(i = 0; i < num_samples; i++)
|
|
|
|
{
|
|
|
|
samples[dbank][lsbTabs.count[dbank]].address += lsbTabs.addr;
|
|
|
|
lsbTabs.count[dbank]++;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
2020-11-02 20:12:46 +01:00
|
|
|
|
|
|
|
lsbTabs.addr += length - slength;
|
2020-04-27 12:52:30 +02:00
|
|
|
|
|
|
|
return num_samples;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void FreeXM(void)
|
|
|
|
{
|
2020-05-12 16:53:26 +02:00
|
|
|
if (Song_ID == -1)
|
|
|
|
return;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-05-12 16:53:26 +02:00
|
|
|
XM_Exit();
|
|
|
|
XM_FreeAllSongIDs();
|
|
|
|
XM_CloseVAB2(VABID);
|
|
|
|
|
|
|
|
Song_ID = -1;
|
|
|
|
VABID = -1;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void StartXM(int reverb)
|
|
|
|
{
|
2020-05-12 16:53:26 +02:00
|
|
|
int ct;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
if (Song_ID == -1)
|
|
|
|
return;
|
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
XM_PlayStart(Song_ID, -1);
|
|
|
|
SpuSetReverb(1);
|
2020-05-12 16:53:26 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
for(ct = MAX_SFX_CHANNELS; ct < SPU_CHANNEL_COUNT; ct++)
|
|
|
|
{
|
2020-06-01 20:15:37 +02:00
|
|
|
SpuSetReverbVoice(reverb, SPU_VOICECH(ct));
|
2020-10-28 09:17:27 +01:00
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void StopXM(void)
|
|
|
|
{
|
2020-05-12 16:53:26 +02:00
|
|
|
XM_PlayStop(Song_ID);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-11-02 22:05:23 +01:00
|
|
|
int GetFreeChannel(int force)
|
2020-03-27 21:47:29 +01:00
|
|
|
{
|
|
|
|
int channel;
|
2020-06-16 20:19:40 +02:00
|
|
|
int it;
|
2020-11-02 20:12:46 +01:00
|
|
|
int least;
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-10-28 09:17:27 +01:00
|
|
|
char status[SPU_CHANNEL_COUNT];
|
2020-11-02 20:12:46 +01:00
|
|
|
|
2020-04-05 00:53:41 +02:00
|
|
|
SpuGetAllKeysStatus(status);
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
channel = 0;
|
2020-06-16 20:19:40 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// find free channel
|
2020-11-06 23:51:00 +01:00
|
|
|
while (channel < MAX_SFX_CHANNELS && (channels[channel].locked || status[channel] != SPU_OFF && status[channel] != SPU_RESET))
|
2020-04-08 16:46:06 +02:00
|
|
|
{
|
2020-06-16 20:19:40 +02:00
|
|
|
channel++;
|
2020-11-02 20:12:46 +01:00
|
|
|
}
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (channel < MAX_SFX_CHANNELS)
|
|
|
|
return channel;
|
2020-11-02 22:05:23 +01:00
|
|
|
|
|
|
|
if (!force)
|
|
|
|
return -1;
|
2020-11-02 20:12:46 +01:00
|
|
|
|
|
|
|
// if not found free channels - free already playing one
|
|
|
|
channel = -1;
|
|
|
|
least = 0;
|
|
|
|
|
|
|
|
for (it = 0; it < MAX_SFX_CHANNELS; it++)
|
|
|
|
{
|
|
|
|
if (channels[it].locked || channels[it].loop)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (channel == -1 || channels[it].time < least)
|
2020-04-08 16:46:06 +02:00
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
channel = it;
|
|
|
|
least = channels[it].time;
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 16:46:06 +02:00
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
if (channel != -1)
|
|
|
|
StopChannel(channel);
|
|
|
|
|
2020-03-27 21:47:29 +01:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
void AllocateReverb(long mode, long depth)
|
|
|
|
{
|
2020-09-01 20:30:42 +02:00
|
|
|
SpuReverbAttr r_attr;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-09-01 20:30:42 +02:00
|
|
|
if (SpuReserveReverbWorkArea(1) == 1)
|
|
|
|
{
|
2020-11-02 20:12:46 +01:00
|
|
|
r_attr.mode = mode | SPU_REV_MODE_CLEAR_WA;
|
|
|
|
r_attr.mask = SPU_REV_MODE | SPU_REV_DEPTHL | SPU_REV_DEPTHR;
|
2020-09-01 20:30:42 +02:00
|
|
|
r_attr.depth.left = depth;
|
|
|
|
r_attr.depth.right = r_attr.depth.left;
|
|
|
|
|
|
|
|
SpuSetReverbModeParam(&r_attr);
|
|
|
|
SpuSetReverbDepth(&r_attr);
|
2020-03-27 21:47:29 +01:00
|
|
|
SpuSetReverb(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:12:46 +01:00
|
|
|
// [D] [T]
|
2020-03-27 21:47:29 +01:00
|
|
|
int FESound(int sample)
|
|
|
|
{
|
2020-11-02 20:18:17 +01:00
|
|
|
int channel;
|
|
|
|
channel = GetFreeChannel();
|
|
|
|
|
|
|
|
if (channel < 0 || channel >= MAX_SFX_CHANNELS) // [A]
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
channels[channel].srcposition = NULL;
|
2020-12-08 17:45:14 +01:00
|
|
|
channels[channel].srcvelocity = (LONGVECTOR3*)dummylong;
|
2020-11-02 20:18:17 +01:00
|
|
|
channels[channel].srcvolume = 4096;
|
|
|
|
|
|
|
|
return CompleteSoundSetup(channel, 1, sample, 2048, -1);
|
2020-03-27 21:47:29 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:50:28 +01:00
|
|
|
// [D] [T]
|
|
|
|
int jsqrt(u_int a)
|
|
|
|
{
|
|
|
|
int b0;
|
|
|
|
int b1;
|
|
|
|
|
|
|
|
if (a < 2)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
b0 = a >> 1;
|
|
|
|
|
|
|
|
if (a < 0x40000000)
|
|
|
|
{
|
|
|
|
if (b0 >= 0x8000)
|
|
|
|
b0 = 0x7fff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (b0 >= 0x10000)
|
|
|
|
b0 = 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
b1 = b0 + a / b0 >> 1;
|
2020-03-27 21:47:29 +01:00
|
|
|
|
2020-12-08 20:50:28 +01:00
|
|
|
if (ABS(b0 - b1) <= 100)
|
|
|
|
break;
|
|
|
|
|
|
|
|
b0 = b1;
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
return b1;
|
|
|
|
}
|
2020-03-27 21:47:29 +01:00
|
|
|
|
|
|
|
|