Skip to content

Instantly share code, notes, and snippets.

@LemonHaze420
Created April 20, 2024 06:22
Show Gist options
  • Save LemonHaze420/51c4d5e2647b6460f0336492ed4155f3 to your computer and use it in GitHub Desktop.
Save LemonHaze420/51c4d5e2647b6460f0336492ed4155f3 to your computer and use it in GitHub Desktop.
Shenmue 3D audio, sample decompilation
unsigned int Shenmue::CalculateAngleBetweenAngles(float firstFloat, float secondFloat)
{
if ( secondFloat < firstFloat )
{
if ( secondFloat < -firstFloat )
{
if ( secondFloat == 0.0 )
return 32768; // -180 degrees
else
return ((((atanf(firstFloat / secondFloat) * 65536.0) / 6.2831855) + 0.5) + 32768);
}
else if ( firstFloat == 0.0 )
{
return 16384; // 90 degrees
}
else
{
return (16384 - (((atanf(secondFloat / firstFloat) * 65536.0) / 6.2831855) + 0.5));
}
}
else if ( secondFloat < -firstFloat )
{
if ( firstFloat == 0.0 )
return 49152; // 270 degrees
else
return (49152 - (((atanf(secondFloat / firstFloat) * 65536.0) / 6.2831855) + 0.5));
}
else if ( secondFloat == 0.0 )
{
return 0;
}
else
{
return (((atanf(firstFloat / secondFloat) * 65536.0) / 6.2831855) + 0.5);
}
}
void Shenmue::Execute3DSoundCommand(
uint32_t base_command,
uint32_t bank,
Vector3 *position,
float min_distance,
float max_distance)
{
z_1 = position->z;
x_1 = position->x;
mag = sqrtf(((x - x_1) * (x - x_1)) + ((z - z_1) * (z - z_1)));
if ( min_distance <= mag )
{
if ( mag <= (min_distance + max_distance) )
{
first_cmd_vol = ((max_distance - (mag - min_distance)) * (127.0 / max_distance));
}
else if ( base_command != -1 )
{
return;
}
}
else
{
first_cmd_vol = 127;
}
angle_degrees = Shenmue::CalculateAngleBetweenAngles(-(x_1 - x), -(z_1 - z));
direction_flag = (16384 - v6 + angle_degrees) < 0;
angle = 16384 - v6 + angle_degrees;
dir = ~angle;
if ( !direction_flag )
dir = angle;
second_cmd_vol = -((dir - 16384) << 6 >> 14);
if ( second_cmd_vol <= 40 )
{
if ( second_cmd_vol < -40 )
second_cmd_vol = -40;
}
else
{
second_cmd_vol = 40;
}
if ( base_command == -1 )
{
Shenmue::ExecuteAICACommand(0x10A5i64, bank, first_cmd_vol);
if ( !first_cmd_vol )
return;
Shenmue::ExecuteAICACommand(0x20A5i64, bank, second_cmd_vol);
volume = last_cmd_vol;
command = 0xA6i64;
}
else
{
Shenmue::ExecuteAICACommand(0x27B0i64, bank, first_cmd_vol);
Shenmue::ExecuteAICACommand(0x2AB0i64, bank, second_cmd_vol);
Shenmue::ExecuteAICACommand(base_command, 0, 0i64);
Shenmue::ExecuteAICACommand(0x2AB0i64, bank, 0i64);
volume = 127i64;
command = 0x27B0i64;
}
Shenmue::ExecuteAICACommand(command, bank, volume);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment