Add 3d sound effects

This commit is contained in:
ABelliqueux 2021-09-14 20:35:48 +02:00
parent d578148edf
commit 4bc75f43a5
8 changed files with 52 additions and 67 deletions

View File

@ -16,11 +16,12 @@
// Sound // Sound
// Sound engine // Sound engine
#define SND_DIST_MIN 200 #define SND_DIST_MIN 200
#define SND_DIST_MAX 1200 #define SND_DIST_MAX 800
#define SND_ATTENUATION 6144 #define SND_ATTENUATION 6144
#define SND_MAX_VOL 16383 #define SND_MAX_VOL 16383
#define SND_RANGE (SND_DIST_MAX-SND_DIST_MIN) #define SND_RANGE (SND_DIST_MAX - SND_DIST_MIN)
#define SND_NMALIZED (SND_RANGE*SND_ATTENUATION/SND_MAX_VOL) #define SND_NMALIZED (SND_RANGE * SND_ATTENUATION / SND_MAX_VOL)
#define SND_DZ 32
// SPU channels // SPU channels
#define SPU_00CH (0x1L<< 0) #define SPU_00CH (0x1L<< 0)
#define SPU_01CH (0x1L<< 1) #define SPU_01CH (0x1L<< 1)

View File

@ -31,7 +31,7 @@ u_long setSPUtransfer(SpuVoiceAttr * voiceAttributes, VAGsound * sound);
void setVAGvolume(SpuVoiceAttr * voiceAttributes, VAGsound * sound, int volumeL,int volumeR); void setVAGvolume(SpuVoiceAttr * voiceAttributes, VAGsound * sound, int volumeL,int volumeR);
void setLvlVAG(LEVEL * level, SpuCommonAttr * spuSettings, SpuVoiceAttr * voiceAttributes, char spu_malloc_rec[]); void setLvlVAG(LEVEL * level, SpuCommonAttr * spuSettings, SpuVoiceAttr * voiceAttributes, char spu_malloc_rec[]);
void playSFX(SpuVoiceAttr * voiceAttributes, VAGsound * sound, int volumeL, int volumeR ); void playSFX(SpuVoiceAttr * voiceAttributes, VAGsound * sound, int volumeL, int volumeR );
VECTOR setSFXdist(LEVEL * level, CAMERA * camera, int camMode); void setSFXdist(LEVEL * level, CAMERA * camera, int camMode);
// XA playback // XA playback
void XAsetup(void); void XAsetup(void);
void getXAoffset(LEVEL * level); void getXAoffset(LEVEL * level);

View File

@ -5,8 +5,8 @@ CVECTOR level0_BGc = { 134, 231, 229, 0 };
VECTOR level0_BKc = { 173, 173, 173, 0 }; VECTOR level0_BKc = { 173, 173, 173, 0 };
CAMPOS level0_camPos_Camera = { CAMPOS level0_camPos_Camera = {
{ -770,459,623 }, { -370,359,323 },
{ 301,-4096,0 } { 301,531,0 }
}; };
CAMPATH level0_camPath = { CAMPATH level0_camPath = {
@ -992,21 +992,19 @@ NODE * level0_curNode = &level0_nodePlane;
extern u_char _binary_VAG_1_cuek_vag_start; extern u_char _binary_VAG_1_cuek_vag_start;
extern u_char _binary_VAG_3_hehe_vag_start; extern u_char _binary_VAG_3_hehe_vag_start;
extern u_char _binary_VAG_7_wron_vag_start;
VAGbank level0_VAGBank = { VAGbank level0_VAGBank = {
10, 9,
{ {
{ &_binary_VAG_1_cuek_vag_start, SPU_00CH, 0 }, { &_binary_VAG_1_cuek_vag_start, SPU_00CH, 0 },
{ &_binary_VAG_3_hehe_vag_start, SPU_01CH, 0 }, { &_binary_VAG_3_hehe_vag_start, SPU_01CH, 0 },
{ &_binary_VAG_7_wron_vag_start, SPU_02CH, 0 }, { &_binary_VAG_3_hehe_vag_start, SPU_02CH, 0 },
{ &_binary_VAG_3_hehe_vag_start, SPU_03CH, 0 }, { &_binary_VAG_1_cuek_vag_start, SPU_03CH, 0 },
{ &_binary_VAG_1_cuek_vag_start, SPU_04CH, 0 }, { &_binary_VAG_1_cuek_vag_start, SPU_04CH, 0 },
{ &_binary_VAG_1_cuek_vag_start, SPU_05CH, 0 }, { &_binary_VAG_3_hehe_vag_start, SPU_05CH, 0 },
{ &_binary_VAG_3_hehe_vag_start, SPU_06CH, 0 }, { &_binary_VAG_3_hehe_vag_start, SPU_06CH, 0 },
{ &_binary_VAG_3_hehe_vag_start, SPU_07CH, 0 }, { &_binary_VAG_1_cuek_vag_start, SPU_07CH, 0 },
{ &_binary_VAG_1_cuek_vag_start, SPU_08CH, 0 }, { &_binary_VAG_3_hehe_vag_start, SPU_08CH, 0 }
{ &_binary_VAG_3_hehe_vag_start, SPU_09CH, 0 }
} }
}; };
@ -1040,18 +1038,10 @@ SOUND_OBJECT level0_Speaker_001 = {
&level0_meshCube &level0_meshCube
}; };
SOUND_OBJECT level0_Speaker_002 = {
{-267,432,-146},
16383, 16383, 0, 16383,
&level0_VAGBank.samples[2],
0,
0
};
SOUND_OBJECT level0_Speaker_003 = { SOUND_OBJECT level0_Speaker_003 = {
{-101,156,253}, {-101,156,253},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[3], &level0_VAGBank.samples[2],
0, 0,
&level0_meshCube &level0_meshCube
}; };
@ -1059,7 +1049,7 @@ SOUND_OBJECT level0_Speaker_003 = {
SOUND_OBJECT level0_Speaker_004 = { SOUND_OBJECT level0_Speaker_004 = {
{83,161,253}, {83,161,253},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[4], &level0_VAGBank.samples[3],
0, 0,
&level0_meshCube &level0_meshCube
}; };
@ -1067,7 +1057,7 @@ SOUND_OBJECT level0_Speaker_004 = {
SOUND_OBJECT level0_Speaker_005 = { SOUND_OBJECT level0_Speaker_005 = {
{76,-39,188}, {76,-39,188},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[5], &level0_VAGBank.samples[4],
0, 0,
&level0_meshCube &level0_meshCube
}; };
@ -1075,7 +1065,7 @@ SOUND_OBJECT level0_Speaker_005 = {
SOUND_OBJECT level0_Speaker_006 = { SOUND_OBJECT level0_Speaker_006 = {
{-108,-43,188}, {-108,-43,188},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[6], &level0_VAGBank.samples[5],
0, 0,
&level0_meshCube &level0_meshCube
}; };
@ -1083,7 +1073,7 @@ SOUND_OBJECT level0_Speaker_006 = {
SOUND_OBJECT level0_Speaker_007 = { SOUND_OBJECT level0_Speaker_007 = {
{-352,55,234}, {-352,55,234},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[7], &level0_VAGBank.samples[6],
0, 0,
&level0_meshCube &level0_meshCube
}; };
@ -1091,7 +1081,7 @@ SOUND_OBJECT level0_Speaker_007 = {
SOUND_OBJECT level0_Speaker_008 = { SOUND_OBJECT level0_Speaker_008 = {
{-168,60,234}, {-168,60,234},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[8], &level0_VAGBank.samples[7],
0, 0,
&level0_meshCube &level0_meshCube
}; };
@ -1107,17 +1097,16 @@ SOUND_OBJECT level0_Speaker_009 = {
SOUND_OBJECT level0_Speaker_010 = { SOUND_OBJECT level0_Speaker_010 = {
{4,-160,143}, {4,-160,143},
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level0_VAGBank.samples[9], &level0_VAGBank.samples[8],
0, 0,
&level0_meshCube &level0_meshCube
}; };
LEVEL_SOUNDS level0_sounds = { LEVEL_SOUNDS level0_sounds = {
11, 10,
{ {
&level0_Speaker, &level0_Speaker,
&level0_Speaker_001, &level0_Speaker_001,
&level0_Speaker_002,
&level0_Speaker_003, &level0_Speaker_003,
&level0_Speaker_004, &level0_Speaker_004,
&level0_Speaker_005, &level0_Speaker_005,

View File

@ -41,7 +41,6 @@ extern XAbank level0_XABank_0;
extern XAfiles level0_XAFiles; extern XAfiles level0_XAFiles;
extern SOUND_OBJECT level0_Speaker; extern SOUND_OBJECT level0_Speaker;
extern SOUND_OBJECT level0_Speaker_001; extern SOUND_OBJECT level0_Speaker_001;
extern SOUND_OBJECT level0_Speaker_002;
extern SOUND_OBJECT level0_Speaker_003; extern SOUND_OBJECT level0_Speaker_003;
extern SOUND_OBJECT level0_Speaker_004; extern SOUND_OBJECT level0_Speaker_004;
extern SOUND_OBJECT level0_Speaker_005; extern SOUND_OBJECT level0_Speaker_005;

View File

@ -11086,7 +11086,7 @@ SOUND_OBJECT level1_Speaker = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[0], &level1_VAGBank.samples[0],
0, 0,
&level1_meshCube &level1_meshSphere
}; };
SOUND_OBJECT level1_Speaker_001 = { SOUND_OBJECT level1_Speaker_001 = {
@ -11094,7 +11094,7 @@ SOUND_OBJECT level1_Speaker_001 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[1], &level1_VAGBank.samples[1],
0, 0,
&level1_meshCube &level1_meshSphere
}; };
SOUND_OBJECT level1_Speaker_002 = { SOUND_OBJECT level1_Speaker_002 = {
@ -11110,7 +11110,7 @@ SOUND_OBJECT level1_Speaker_003 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[3], &level1_VAGBank.samples[3],
0, 0,
&level1_meshCube &level1_meshSphere_001
}; };
SOUND_OBJECT level1_Speaker_004 = { SOUND_OBJECT level1_Speaker_004 = {
@ -11126,7 +11126,7 @@ SOUND_OBJECT level1_Speaker_005 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[5], &level1_VAGBank.samples[5],
0, 0,
&level1_meshCube &level1_meshSphere
}; };
SOUND_OBJECT level1_Speaker_006 = { SOUND_OBJECT level1_Speaker_006 = {
@ -11134,7 +11134,7 @@ SOUND_OBJECT level1_Speaker_006 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[6], &level1_VAGBank.samples[6],
0, 0,
&level1_meshCube &level1_meshSphere
}; };
SOUND_OBJECT level1_Speaker_007 = { SOUND_OBJECT level1_Speaker_007 = {
@ -11142,7 +11142,7 @@ SOUND_OBJECT level1_Speaker_007 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[7], &level1_VAGBank.samples[7],
0, 0,
&level1_meshCube &level1_meshSphere
}; };
SOUND_OBJECT level1_Speaker_008 = { SOUND_OBJECT level1_Speaker_008 = {
@ -11150,7 +11150,7 @@ SOUND_OBJECT level1_Speaker_008 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
&level1_VAGBank.samples[8], &level1_VAGBank.samples[8],
0, 0,
&level1_meshCube &level1_meshSphere
}; };
SOUND_OBJECT level1_Speaker_009 = { SOUND_OBJECT level1_Speaker_009 = {
@ -11158,7 +11158,7 @@ SOUND_OBJECT level1_Speaker_009 = {
16383, 16383, 0, 16383, 16383, 16383, 0, 16383,
0, 0,
&level1_XABank_0.samples[0], &level1_XABank_0.samples[0],
&level1_meshCube &level1_meshSphere
}; };
LEVEL_SOUNDS level1_sounds = { LEVEL_SOUNDS level1_sounds = {

View File

@ -37,7 +37,7 @@ u_long overlaySize = 0;
#include "../levels/level1.h" #include "../levels/level1.h"
// Levels // Levels
u_char level = 0; u_char level = 1;
u_short levelWas = 0; u_short levelWas = 0;
u_short levelHasChanged = 0; u_short levelHasChanged = 0;
// Overlay // Overlay
@ -261,9 +261,7 @@ int main() {
respawnMesh(&curLvl, curLvl.propPtr, &propStartRot, &propStartPos, propStartNode ); respawnMesh(&curLvl, curLvl.propPtr, &propStartRot, &propStartPos, propStartNode );
} }
// Spatialize Sound // Spatialize Sound
// TODO : Use L/R to spatialize further setSFXdist(&curLvl, &camera, camMode);
VECTOR screenPos;
screenPos = setSFXdist(&curLvl, &camera, camMode);
// Spatial partitioning // Spatial partitioning
if (curLvl.curNode){ if (curLvl.curNode){
for ( int msh = 0; msh < curLvl.curNode->siblings->index; msh ++ ) { for ( int msh = 0; msh < curLvl.curNode->siblings->index; msh ++ ) {
@ -362,7 +360,7 @@ int main() {
// Angle between camera and actor // Angle between camera and actor
applyVector( dc_actorRot, applyVector( dc_actorRot,
(patan(dist, posToActor.vy) >> 4 ) - 256, (patan(dist, posToActor.vy) >> 4 ) - 256,
(patan(-posToActor.vx, -posToActor.vz) / 16) - 3076, (patan(-posToActor.vx, -posToActor.vz) / 16) + 1024,
0, 0,
= =
); );
@ -389,8 +387,7 @@ int main() {
FntPrint("CamAngle : %d\n", curCamAngle); FntPrint("CamAngle : %d\n", curCamAngle);
FntPrint("XA: %x\n", curLvl.XA); FntPrint("XA: %x\n", curLvl.XA);
FntPrint("Ofst: %d\n", curLvl.XA->banks[0]->offset); FntPrint("Ofst: %d\n", curLvl.XA->banks[0]->offset);
FntPrint("Vol: %d %d", curLvl.levelSounds->sounds[0]->volumeL, curLvl.levelSounds->sounds[0]->volumeR ); FntPrint("Vol: %d %d\n", curLvl.levelSounds->sounds[0]->volumeL, curLvl.levelSounds->sounds[0]->volumeR );
FntPrint("Screenpos: %d %d", screenPos.vx, screenPos.vy );
FntFlush(-1); FntFlush(-1);
display( &disp[db], &draw[db], otdisc[db], primbuff[db], &nextpri, &db); display( &disp[db], &draw[db], otdisc[db], primbuff[db], &nextpri, &db);

View File

@ -98,37 +98,36 @@ void playSFX(SpuVoiceAttr * voiceAttributes, VAGsound * sound, int volumeL, int
// Play voice // Play voice
SpuSetKey(SpuOn, sound->spu_channel); SpuSetKey(SpuOn, sound->spu_channel);
}; };
VECTOR setSFXdist(LEVEL * level, CAMERA * camera, int camMode ){ void setSFXdist(LEVEL * level, CAMERA * camera, int camMode ){
VECTOR output; VECTOR sndPos2D = {0};
//long long phi = 0;
if (level->levelSounds != 0){ if (level->levelSounds != 0){
//~ for(int snd = 0; snd < level0_sounds.index; snd++){
for(int snd = 0; snd < level->levelSounds->index; snd++){ for(int snd = 0; snd < level->levelSounds->index; snd++){
// TODO : move in playSFX()
// update sound location if sound has a parent
u_int r; u_int r;
// If parent is actor, // If parent is actor,
if (level->levelSounds->sounds[snd]->parent == level->actorPtr && camMode <= 1){ if (level->levelSounds->sounds[snd]->parent == level->actorPtr && camMode <= 1){
r = CAM_DIST_TO_ACT; r = CAM_DIST_TO_ACT;
// update sound location if sound has a parent and it's not actor
} else if ( level->levelSounds->sounds[snd]->parent != 0){ } else if ( level->levelSounds->sounds[snd]->parent != 0){
VECTOR dist; VECTOR dist;
copyVector(&level->levelSounds->sounds[snd]->location, &level->levelSounds->sounds[snd]->parent->pos); copyVector(&level->levelSounds->sounds[snd]->location, &level->levelSounds->sounds[snd]->parent->pos);
// Get distance between sound source and camera // Get distance between sound source and camera
addVector2(camera->pos, &level->levelSounds->sounds[snd]->location, &dist); dist.vx = -camera->pos->vx - level->levelSounds->sounds[snd]->location.vx;
dist.vz = -camera->pos->vz - level->levelSounds->sounds[snd]->location.vz;
r = psqrt((dist.vx * dist.vx) + (dist.vz * dist.vz)); r = psqrt((dist.vx * dist.vx) + (dist.vz * dist.vz));
// Get angle between sound source and camera // Get snd screen coordinates
//phi = patan( level->levelSounds->sounds[snd]->location.vx, level->levelSounds->sounds[snd]->location.vz ); // Range -1024 0 == screen left, 0 +1024 == screen right
//phi = ( phi >> 4 ); worldToScreen(&level->levelSounds->sounds[snd]->location, &sndPos2D);
} }
level->levelSounds->sounds[snd]->volumeL = level->levelSounds->sounds[snd]->volumeR = (level->levelSounds->sounds[snd]->volume_max/r) * SND_NMALIZED > SND_MAX_VOL ? SND_MAX_VOL : // Find volume base on dist
(level->levelSounds->sounds[snd]->volume_max/r) * SND_NMALIZED < 0 ? 0 : u_int volumeBase = (level->levelSounds->sounds[snd]->volume_max/r) * SND_NMALIZED > SND_MAX_VOL ? SND_MAX_VOL :
(level->levelSounds->sounds[snd]->volume_max/r) * SND_NMALIZED; (level->levelSounds->sounds[snd]->volume_max/r) * SND_NMALIZED < 0 ? 0 :
//TODO : use screen coordinates (level->levelSounds->sounds[snd]->volume_max/r) * SND_NMALIZED;
worldToScreen(&level->levelSounds->sounds[snd]->location, &output); // Avoid value of 0
sndPos2D.vx = sndPos2D.vx == 0 || sndPos2D.vx == -0 ? 1 : sndPos2D.vx;
level->levelSounds->sounds[snd]->volumeL = volumeBase / ( (sndPos2D.vx > SND_DZ ? ( sndPos2D.vx - SND_DZ >> 7) + 1 : 1) );
level->levelSounds->sounds[snd]->volumeR = volumeBase / ( (sndPos2D.vx < -SND_DZ ? ( ( -sndPos2D.vx - SND_DZ ) >> 7) + 1 : 1) );
} }
} }
return output;
}; };
void XAsetup(void){ void XAsetup(void){
u_char param[4]; u_char param[4];

View File

@ -13,9 +13,9 @@ void worldToScreen( VECTOR * worldPos, VECTOR * screenPos ) {
// Get world translation vectors from rot and add to screenPos vx, vy, vz // Get world translation vectors from rot and add to screenPos vx, vy, vz
applyVector(screenPos, curRot.t[0], curRot.t[1], curRot.t[2], +=); applyVector(screenPos, curRot.t[0], curRot.t[1], curRot.t[2], +=);
// Correct perspective // Correct perspective
screenPos -> vx = screenPos -> vx * distToScreen / ( screenPos -> vz + 1 ) ; // Add 1 to avoid division by 0 //~ screenPos -> vx = screenPos -> vx * distToScreen / ( screenPos -> vz + 1 ) ; // Add 1 to avoid division by 0
screenPos -> vy = screenPos -> vy * distToScreen / ( screenPos -> vz + 1 ) ; //~ screenPos -> vy = screenPos -> vy * distToScreen / ( screenPos -> vz + 1 ) ;
screenPos -> vz = distToScreen ; //~ screenPos -> vz = distToScreen ;
}; };
void screenToWorld( VECTOR * screenPos, VECTOR * worldPos ) { void screenToWorld( VECTOR * screenPos, VECTOR * worldPos ) {
int distToScreen; // corresponds to FOV int distToScreen; // corresponds to FOV