From d5304686baf62f8bd2faa40884278b060e24671e Mon Sep 17 00:00:00 2001 From: Michael Panzlaff Date: Sat, 3 Jul 2021 21:30:50 +0200 Subject: arm7: decompile SND_util --- arm7/lib/src/SND_util.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 arm7/lib/src/SND_util.c (limited to 'arm7/lib/src/SND_util.c') diff --git a/arm7/lib/src/SND_util.c b/arm7/lib/src/SND_util.c new file mode 100644 index 00000000..6a939c02 --- /dev/null +++ b/arm7/lib/src/SND_util.c @@ -0,0 +1,91 @@ +#include "SND_util.h" + +#include "syscall.h" + +// TODO remove this extern once the static const definition of it is here +extern s8 sLfoSinTable[0x21]; + +u16 SND_CalcTimer(int timer, int pitch) { + int octave = 0; + int pitch_normalized = -pitch; + + while (pitch_normalized < 0) { + octave--; + pitch_normalized += 768; + } + + while (pitch_normalized >= 768) { + octave++; + pitch_normalized -= 768; + } + + u64 result = SVC_GetPitchTable(pitch_normalized); + + result += 0x10000; + result *= timer; + + int shift = octave - 16; + + if (shift <= 0) { + shift = -shift; + result >>= shift; + } else if (shift < 32) { + // what ??? + u64 tmp = result & ~0uLL << (32 - shift); + if (tmp != 0) + return 0xFFFF; + result <<= shift; + } else { + return 0xFFFF; + } + + if (result < 0x10) + result = 0x10; + else if (result > 0xFFFF) + result = 0xFFFF; + + return (u16)result; +} + +u16 SND_CalcChannelVolume(int value) { + if (value < SND_VOL_DB_MIN) + value = SND_VOL_DB_MIN; + else if (value > 0) + value = 0; + + int result = SVC_GetVolumeTable(value + (-SND_VOL_DB_MIN)); + int div; + + if (value < -240) + div = 3; + else if (value < -120) + div = 2; + else if (value < -60) + div = 1; + else + div = 0; + + return (u16)(result | (div << 8)); +} + +s8 SND_SinIdx(int x) { + // BUG: UB for out of range values + + if (x < 0x20) { + return sLfoSinTable[x]; + } else if (x < 0x40) { + return sLfoSinTable[0x40 - x]; + } else if (x < 0x60) { + return (s8)(-sLfoSinTable[x - 0x40]); + } else { + return (s8)(-sLfoSinTable[0x20 - (x - 0x60)]); + } +} + +u16 SND_CalcRandom(void) { + static u32 state = 0x12345678; + + // values from "Numerical Recipes" + state = state * 1664525u + 1013904223u; + return (u16)(state >> 16u); +} -- cgit v1.2.3 From d72270d4bd4c3160f98812de51cfb76fb6b47295 Mon Sep 17 00:00:00 2001 From: Michael Panzlaff Date: Tue, 3 Aug 2021 18:44:55 +0200 Subject: arm7: run clang-format on SND_* --- arm7/lib/src/SND_util.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'arm7/lib/src/SND_util.c') diff --git a/arm7/lib/src/SND_util.c b/arm7/lib/src/SND_util.c index 6a939c02..c9a98c3f 100644 --- a/arm7/lib/src/SND_util.c +++ b/arm7/lib/src/SND_util.c @@ -5,16 +5,19 @@ // TODO remove this extern once the static const definition of it is here extern s8 sLfoSinTable[0x21]; -u16 SND_CalcTimer(int timer, int pitch) { +u16 SND_CalcTimer(int timer, int pitch) +{ int octave = 0; int pitch_normalized = -pitch; - while (pitch_normalized < 0) { + while (pitch_normalized < 0) + { octave--; pitch_normalized += 768; } - while (pitch_normalized >= 768) { + while (pitch_normalized >= 768) + { octave++; pitch_normalized -= 768; } @@ -23,19 +26,24 @@ u16 SND_CalcTimer(int timer, int pitch) { result += 0x10000; result *= timer; - + int shift = octave - 16; - if (shift <= 0) { + if (shift <= 0) + { shift = -shift; result >>= shift; - } else if (shift < 32) { + } + else if (shift < 32) + { // what ??? u64 tmp = result & ~0uLL << (32 - shift); if (tmp != 0) return 0xFFFF; result <<= shift; - } else { + } + else + { return 0xFFFF; } @@ -47,7 +55,8 @@ u16 SND_CalcTimer(int timer, int pitch) { return (u16)result; } -u16 SND_CalcChannelVolume(int value) { +u16 SND_CalcChannelVolume(int value) +{ if (value < SND_VOL_DB_MIN) value = SND_VOL_DB_MIN; else if (value > 0) @@ -68,21 +77,30 @@ u16 SND_CalcChannelVolume(int value) { return (u16)(result | (div << 8)); } -s8 SND_SinIdx(int x) { +s8 SND_SinIdx(int x) +{ // BUG: UB for out of range values - - if (x < 0x20) { + + if (x < 0x20) + { return sLfoSinTable[x]; - } else if (x < 0x40) { + } + else if (x < 0x40) + { return sLfoSinTable[0x40 - x]; - } else if (x < 0x60) { + } + else if (x < 0x60) + { return (s8)(-sLfoSinTable[x - 0x40]); - } else { + } + else + { return (s8)(-sLfoSinTable[0x20 - (x - 0x60)]); } } -u16 SND_CalcRandom(void) { +u16 SND_CalcRandom(void) +{ static u32 state = 0x12345678; // values from "Numerical Recipes" -- cgit v1.2.3 From ab1ad9955b4f81e442b7a7114ec1ce82572c10ed Mon Sep 17 00:00:00 2001 From: Michael Panzlaff Date: Wed, 25 Aug 2021 17:57:32 +0200 Subject: PR: implement review corrections 3 --- arm7/lib/src/SND_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arm7/lib/src/SND_util.c') diff --git a/arm7/lib/src/SND_util.c b/arm7/lib/src/SND_util.c index c9a98c3f..5c0b220b 100644 --- a/arm7/lib/src/SND_util.c +++ b/arm7/lib/src/SND_util.c @@ -36,7 +36,7 @@ u16 SND_CalcTimer(int timer, int pitch) } else if (shift < 32) { - // what ??? + // clamp in case timer value overflows u64 tmp = result & ~0uLL << (32 - shift); if (tmp != 0) return 0xFFFF; -- cgit v1.2.3