diff --git a/iggy-decompiled/src/reconstructed/CMakeLists.txt b/iggy-decompiled/src/reconstructed/CMakeLists.txt index 9007504..0ae23fc 100644 --- a/iggy-decompiled/src/reconstructed/CMakeLists.txt +++ b/iggy-decompiled/src/reconstructed/CMakeLists.txt @@ -126,6 +126,17 @@ foreach(platform IN ITEMS durango orbis ps3 vita) configure_iggy_reconstructed_target(${target} c_std_11) endforeach() +foreach(platform IN ITEMS durango orbis ps3 vita) + string(TOUPPER ${platform} platform_upper) + set(target iggy_reconstructed_mixer_${platform}) + add_library(${target} STATIC iggy_mixer.c) + target_compile_definitions( + ${target} + PRIVATE IGGY_RECONSTRUCT_MIXER_${platform_upper} + ) + configure_iggy_reconstructed_target(${target} c_std_11) +endforeach() + add_library( iggy_reconstructed_as3vm_debugger_interface STATIC diff --git a/iggy-decompiled/src/reconstructed/README.md b/iggy-decompiled/src/reconstructed/README.md index 75cf6a6..d27faba 100644 --- a/iggy-decompiled/src/reconstructed/README.md +++ b/iggy-decompiled/src/reconstructed/README.md @@ -73,6 +73,12 @@ layouts, signed pass behavior, exact GDraw render-state fields, premultiplied colors, compound bounds and filter modes, texture ownership, failure cleanup, and per-platform telemetry and math imports. +`iggy_mixer.c` restores the seven native software-mixer exports used by Iggy's +audio path. It preserves the 40-byte resampler state, fixed-point phase and +gain formats, mono/stereo routing, nearest and interpolated kernels, commit and +overshoot behavior, byte-count clamp contracts, and the PS3/Vita vector +saturation differences while keeping the existing SWF/AVM2 runtime intact. + `as3vm_debugger_interface.c` and `as3vm_op_strings.c` preserve the three intentional zero-return stubs in the shipping archives. Every platform object contains the same compiled-out behavior with no debugger metadata, opcode diff --git a/iggy-decompiled/src/reconstructed/iggy_mixer.c b/iggy-decompiled/src/reconstructed/iggy_mixer.c new file mode 100644 index 0000000..ac5fbe1 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/iggy_mixer.c @@ -0,0 +1,353 @@ +#include "iggy_mixer.h" + +#include + +#if (defined(IGGY_RECONSTRUCT_MIXER_DURANGO) + \ + defined(IGGY_RECONSTRUCT_MIXER_ORBIS) + \ + defined(IGGY_RECONSTRUCT_MIXER_PS3) + \ + defined(IGGY_RECONSTRUCT_MIXER_VITA)) != 1 +#error "select exactly one mixer target" +#endif + +_Static_assert(sizeof(IggySimpleMixInfo) == 40, "mixer info size"); +_Static_assert(offsetof(IggySimpleMixInfo, phase) == 8, "mixer phase offset"); +_Static_assert(offsetof(IggySimpleMixInfo, gain_ll) == 16, "mixer gain offset"); +_Static_assert(offsetof(IggySimpleMixInfo, flags) == 32, "mixer flags offset"); +_Static_assert(offsetof(IggySimpleMixInfo, sample_shift) == 36, + "mixer shift offset"); + +static int32_t wrap_add(int32_t left, int32_t right) { + return (int32_t)((uint32_t)left + (uint32_t)right); +} + +static int32_t wrap_mul(int32_t left, int32_t right) { + return (int32_t)((uint32_t)left * (uint32_t)right); +} + +static int32_t arithmetic_shift(int32_t value, uint32_t amount) { + if (value >= 0) return (int32_t)((uint32_t)value >> amount); + return (int32_t)~((~(uint32_t)value) >> amount); +} + +static int32_t target_float_to_s32(float value) { +#if defined(IGGY_RECONSTRUCT_MIXER_DURANGO) || \ + defined(IGGY_RECONSTRUCT_MIXER_ORBIS) + if (!(value >= -2147483648.0f && value < 2147483648.0f)) { + return INT32_MIN; + } +#elif defined(IGGY_RECONSTRUCT_MIXER_PS3) + if (value != value) return INT32_MIN; + if (value >= 2147483648.0f) return INT32_MAX; + if (value <= -2147483648.0f) return INT32_MIN; +#else + if (value != value) return 0; + if (value >= 2147483648.0f) return INT32_MAX; + if (value <= -2147483648.0f) return INT32_MIN; +#endif + return (int32_t)value; +} + +static int16_t scalar_s16(int32_t value) { + if (value >= INT32_C(0x03fff800)) return INT16_MAX; + if (value <= -INT32_C(0x03fff800)) return -INT16_C(32767); + return (int16_t)arithmetic_shift(value, 11); +} + +#if defined(IGGY_RECONSTRUCT_MIXER_PS3) || \ + defined(IGGY_RECONSTRUCT_MIXER_VITA) +static int16_t vector_s16(int32_t value) { + const int32_t shifted = arithmetic_shift(value, 11); + if (shifted > INT16_MAX) return INT16_MAX; + if (shifted < INT16_MIN) return INT16_MIN; + return (int16_t)shifted; +} +#endif + +int16_t *iggy_clamp_to_s16( + int16_t *destination, const int32_t *source, uint32_t source_bytes) { + uint32_t offset = 0; +#if defined(IGGY_RECONSTRUCT_MIXER_PS3) + while (offset < source_bytes && + (((uintptr_t)((const uint8_t *)source + offset) & 15U) != 0U)) { + *destination++ = scalar_s16(*(const int32_t *)((const uint8_t *)source + offset)); + offset += 4; + } + while (offset <= source_bytes && source_bytes - offset >= 64U) { + for (uint32_t index = 0; index < 16; ++index) { + destination[index] = vector_s16( + *(const int32_t *)((const uint8_t *)source + offset + index * 4U)); + } + destination += 16; + offset += 64; + } +#elif defined(IGGY_RECONSTRUCT_MIXER_VITA) + while (offset <= source_bytes && source_bytes - offset >= 64U) { + for (uint32_t index = 0; index < 16; ++index) { + destination[index] = vector_s16( + *(const int32_t *)((const uint8_t *)source + offset + index * 4U)); + } + destination += 16; + offset += 64; + } +#endif + while (offset < source_bytes) { + *destination++ = scalar_s16( + *(const int32_t *)((const uint8_t *)source + offset)); + offset += 4; + } + return destination; +} + +int16_t *iggy_clamp_to_s16_interleave_2_4( + int16_t *destination, const int32_t *front, const int32_t *rear, + uint32_t front_bytes) { + uint32_t offset = 0; + while (offset < front_bytes) { + destination[0] = scalar_s16(front[0]); + destination[1] = scalar_s16(front[1]); + destination[2] = scalar_s16(rear[0]); + destination[3] = scalar_s16(rear[1]); + destination += 4; + front += 2; + rear += 2; + offset += 8; + } + return destination; +} + +uint8_t *iggy_clamp_to_u8( + uint8_t *destination, const int32_t *source, uint32_t source_bytes) { + uint32_t offset = 0; + while (offset < source_bytes) { + const int32_t value = *(const int32_t *)((const uint8_t *)source + offset); + if (value >= INT32_C(0x03fff800)) { + *destination++ = UINT8_MAX; + } else if (value <= -INT32_C(0x03fff800)) { + *destination++ = 0; + } else { + const int32_t shifted = arithmetic_shift(value, 19); + *destination++ = (uint8_t)(shifted + 128); + } + offset += 4; + } + return destination; +} + +void iggy_simpmix_init_info( + IggySimpleMixInfo *info, uint32_t output_rate, uint32_t source_rate, + uint32_t output_channels, uint32_t source_channels, uint32_t source_bits) { + const uint32_t frame_bytes = (source_channels > 1U ? 2U : 1U) * + (source_bits > 8U ? 2U : 1U); + if (output_rate < source_rate) { + const uint32_t quotient = source_rate / output_rate; + info->advance_regular = quotient * frame_bytes; + info->advance_carry = info->advance_regular + frame_bytes; + info->phase_step = (uint32_t)( + ((uint64_t)(source_rate - quotient * output_rate) << 31) / output_rate); + } else { + info->advance_carry = frame_bytes; + info->advance_regular = 0; + info->phase_step = (uint32_t)(((uint64_t)source_rate << 31) / output_rate); + } + info->phase = info->phase_step & ~UINT32_C(1); + info->flags = (source_bits > 8U ? 1U : 0U) | + (output_channels > 1U ? 2U : 0U) | + (source_channels > 1U ? 4U : 0U); + if (source_rate * 3U < output_rate) { + info->flags |= 0x10U; + info->phase = 0; + } + info->sample_shift = (uint8_t)( + (source_channels == 2U ? 1U : 0U) + (source_bits == 16U ? 1U : 0U)); +} + +static uint32_t coefficient_bits(uint32_t flags) { + if ((flags & 0x10U) != 0U) return 0; + return (flags & 1U) != 0U ? 11U : 19U; +} + +static int matrix_has_cross_terms(float left_to_right, float right_to_left) { + const float threshold = 0.000030517578125f; +#if defined(IGGY_RECONSTRUCT_MIXER_DURANGO) || \ + defined(IGGY_RECONSTRUCT_MIXER_VITA) + return left_to_right > threshold || right_to_left > threshold || + left_to_right != left_to_right || right_to_left != right_to_left; +#elif defined(IGGY_RECONSTRUCT_MIXER_ORBIS) + return left_to_right > threshold || left_to_right != left_to_right || + right_to_left > threshold; +#else + return left_to_right > threshold || right_to_left > threshold; +#endif +} + +static int matrix_diagonal_is_unity(float left, float right) { + const float lower = 0.999969482421875f; + const float upper = 1.000030517578125f; +#if defined(IGGY_RECONSTRUCT_MIXER_PS3) + const int left_matches = left != left || (left >= lower && left <= upper); + const int right_matches = right != right || (right >= lower && right <= upper); + return left_matches && right_matches; +#else + return left >= lower && left <= upper && right >= lower && right <= upper; +#endif +} + +void iggy_simpmix_set_volume( + IggySimpleMixInfo *info, uint32_t volume, uint32_t pan) { + if (volume - 0x7fffU < 3U && pan - 0x7fffU < 3U) { + info->flags &= ~UINT32_C(8); + return; + } + const uint32_t shift = 31U - coefficient_bits(info->flags); + if (pan < 0x8000U) { + info->gain_ll = (int32_t)((volume << 16) >> shift); + info->gain_rr = (int32_t)((volume * pan * 2U) >> shift); + } else { + info->gain_ll = (int32_t)(((0x10000U - pan) * volume * 2U) >> shift); + info->gain_rr = (int32_t)((volume << 16) >> shift); + } + info->flags |= 8U; +} + +void iggy_simpmix_set_matrix( + IggySimpleMixInfo *info, float volume, const float matrix[4]) { + const float left = volume * matrix[0]; + const float right = volume * matrix[3]; + if (matrix_has_cross_terms(matrix[1], matrix[2])) { + const float scale = (float)(UINT32_C(1) << coefficient_bits(info->flags)); + info->gain_ll = target_float_to_s32(left * scale); + info->gain_rr = target_float_to_s32(right * scale); + info->gain_rl = target_float_to_s32(volume * matrix[2] * scale); + info->gain_lr = target_float_to_s32(volume * matrix[1] * scale); + if ((info->flags & 6U) == 6U) { + info->flags = (info->flags & ~UINT32_C(0x28)) | 0x20U; + } else { + info->gain_ll = wrap_add(info->gain_ll, info->gain_lr); + info->gain_rr = wrap_add(info->gain_rr, info->gain_rl); + info->flags = (info->flags & ~UINT32_C(0x28)) | 8U; + } + return; + } + if (matrix_diagonal_is_unity(left, right)) { + info->flags &= ~UINT32_C(8); + return; + } + const uint32_t shift = 31U - coefficient_bits(info->flags); + info->gain_ll = arithmetic_shift( + target_float_to_s32(left * 2147483648.0f), shift); + info->gain_rr = arithmetic_shift( + target_float_to_s32(right * 2147483648.0f), shift); + info->flags |= 8U; +} + +static int32_t read_source(const uint8_t *source, uint32_t channel, int is_s16) { + if (!is_s16) return (int32_t)source[channel] - 128; + int16_t value; + __builtin_memcpy(&value, source + channel * 2U, 2); + return value; +} + +static int32_t scaled_sample( + const uint8_t *source, uint32_t channel, uint32_t frame_bytes, + int is_s16, int interpolate, int32_t fraction) { + const int32_t current = read_source(source, channel, is_s16); + if (!interpolate) { + return wrap_mul(current, is_s16 ? 0x800 : 0x80000); + } + const int32_t next = read_source(source + frame_bytes, channel, is_s16); + const int32_t base = wrap_mul(current, 256); + return wrap_add(base, wrap_mul((int32_t)fraction, next - current)); +} + +uint32_t iggy_simple_mix( + IggySimpleMixInfo *info, int32_t *destination, int32_t destination_first, + int32_t destination_end, const void *source_base, int32_t *source_byte_offset, + int32_t source_byte_end, int32_t *source_frames_consumed, int32_t commit) { + const uint32_t mode = info->flags; + uint32_t phase = info->phase; + const int32_t original_offset = *source_byte_offset; + const uint8_t *const base = (const uint8_t *)source_base; + const uint8_t *source = base + original_offset; + const uint8_t *const source_end = base + source_byte_end; + if ((phase & 1U) != 0U) { + source += info->advance_regular; + phase &= ~UINT32_C(1); + } + int32_t *output = destination + destination_first; + int32_t *const output_begin = output; + int32_t *const output_end = destination + destination_end; + const int is_s16 = (mode & 1U) != 0U; + const int output_stereo = (mode & 2U) != 0U; + const int source_stereo = (mode & 4U) != 0U; + const int use_gain = (mode & 8U) != 0U; + const int interpolate = (mode & 0x10U) != 0U; + const int full_matrix = (mode & 0x20U) != 0U; + const int valid = mode < 0x20U || mode == 0x26U || mode == 0x27U || + mode == 0x36U || mode == 0x37U; + const uint32_t frame_bytes = (source_stereo ? 2U : 1U) * (is_s16 ? 2U : 1U); + while (valid && source < source_end && output < output_end) { + const int32_t fraction = arithmetic_shift((int32_t)phase, 23); + int32_t left = scaled_sample( + source, 0, frame_bytes, is_s16, interpolate, fraction); + int32_t right = source_stereo + ? scaled_sample(source, 1, frame_bytes, is_s16, interpolate, fraction) + : left; + if (interpolate && !use_gain && !full_matrix) { + left = wrap_mul(left, is_s16 ? 8 : 0x800); + right = wrap_mul(right, is_s16 ? 8 : 0x800); + } + if (use_gain || full_matrix) { + if (!interpolate) { + left = arithmetic_shift(left, is_s16 ? 11U : 19U); + right = arithmetic_shift(right, is_s16 ? 11U : 19U); + } + if (full_matrix) { + const int32_t out_left = wrap_add( + wrap_mul(left, info->gain_ll), wrap_mul(right, info->gain_lr)); + const int32_t out_right = wrap_add( + wrap_mul(left, info->gain_rl), wrap_mul(right, info->gain_rr)); + output[0] = wrap_add(output[0], out_left); + output[1] = wrap_add(output[1], out_right); + } else if (source_stereo && !output_stereo) { + output[0] = wrap_add( + wrap_mul(left, info->gain_ll), wrap_mul(right, info->gain_rr)); + } else if (!output_stereo) { + output[0] = wrap_add(output[0], wrap_mul(left, info->gain_ll)); + } else { + output[0] = wrap_add(output[0], wrap_mul(left, info->gain_ll)); + output[1] = wrap_add( + output[1], wrap_mul(source_stereo ? right : left, info->gain_rr)); + } + } else if (source_stereo && !output_stereo) { + output[0] = wrap_add(left, right); + } else if (!output_stereo) { + output[0] = wrap_add(output[0], left); + } else { + output[0] = wrap_add(output[0], left); + output[1] = wrap_add(output[1], source_stereo ? right : left); + } + output += output_stereo ? 2 : 1; + if (interpolate) { + const uint32_t next_phase = phase + info->phase_step; + source += (int32_t)next_phase < 0 + ? info->advance_carry : info->advance_regular; + phase = next_phase & 0x7fffffffU; + } else { + source += (int32_t)phase < 0 + ? info->advance_carry : info->advance_regular; + phase = (phase & 0x7fffffffU) + info->phase_step; + } + } + if (commit != 0) { + uint32_t saved_phase = phase & ~UINT32_C(1); + if (source > source_end) { + source = source_end; + saved_phase |= 1U; + } + *source_frames_consumed = (int32_t)( + (source - (base + original_offset)) >> info->sample_shift); + *source_byte_offset = (int32_t)(source - base); + info->phase = saved_phase; + } + return (uint32_t)(output - output_begin); +} diff --git a/iggy-decompiled/src/reconstructed/iggy_mixer.h b/iggy-decompiled/src/reconstructed/iggy_mixer.h new file mode 100644 index 0000000..558d616 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/iggy_mixer.h @@ -0,0 +1,47 @@ +#ifndef IGGY_RECONSTRUCTED_MIXER_H +#define IGGY_RECONSTRUCTED_MIXER_H + +#include + +typedef struct IggySimpleMixInfo { + uint32_t advance_carry; + uint32_t advance_regular; + uint32_t phase; + uint32_t phase_step; + int32_t gain_ll; + int32_t gain_rr; + int32_t gain_rl; + int32_t gain_lr; + uint32_t flags; + uint8_t sample_shift; + uint8_t reserved[3]; +} IggySimpleMixInfo; + +#ifdef __cplusplus +extern "C" { +#endif + +uint32_t iggy_simple_mix( + IggySimpleMixInfo *info, int32_t *destination, int32_t destination_first, + int32_t destination_end, const void *source, int32_t *source_byte_offset, + int32_t source_byte_end, int32_t *source_frames_consumed, int32_t commit); +void iggy_simpmix_init_info( + IggySimpleMixInfo *info, uint32_t output_rate, uint32_t source_rate, + uint32_t output_channels, uint32_t source_channels, uint32_t source_bits); +void iggy_simpmix_set_volume( + IggySimpleMixInfo *info, uint32_t volume, uint32_t pan); +void iggy_simpmix_set_matrix( + IggySimpleMixInfo *info, float volume, const float matrix[4]); +int16_t *iggy_clamp_to_s16( + int16_t *destination, const int32_t *source, uint32_t source_bytes); +int16_t *iggy_clamp_to_s16_interleave_2_4( + int16_t *destination, const int32_t *front, const int32_t *rear, + uint32_t front_bytes); +uint8_t *iggy_clamp_to_u8( + uint8_t *destination, const int32_t *source, uint32_t source_bytes); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/iggy-decompiled/src/reconstructed/modules.tsv b/iggy-decompiled/src/reconstructed/modules.tsv index eb16f3e..c612964 100644 --- a/iggy-decompiled/src/reconstructed/modules.tsv +++ b/iggy-decompiled/src/reconstructed/modules.tsv @@ -11,6 +11,7 @@ iggy_file_reader 3 1 durango complete iggy_file_reader 2 1 orbis,ps3,vita complete iggy_platform_deps 8 8 durango,orbis,ps3,vita complete swf_draw_filter 5 5 durango,orbis,ps3,vita complete +iggy_mixer 7 7 durango,orbis,ps3,vita complete as3vm_debugger_interface 2 2 durango,orbis,ps3,vita complete as3vm_op_strings 1 1 durango,orbis,ps3,vita complete iggy_platform_audio 2 2 durango complete diff --git a/tests/test_reconstructed_iggy_mixer.py b/tests/test_reconstructed_iggy_mixer.py new file mode 100644 index 0000000..4e31093 --- /dev/null +++ b/tests/test_reconstructed_iggy_mixer.py @@ -0,0 +1,499 @@ +import ctypes +import os +from pathlib import Path +import shlex +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed" +SOURCE = RECONSTRUCTED / "iggy_mixer.c" +VARIANTS = { + name: f"IGGY_RECONSTRUCT_MIXER_{name.upper()}" + for name in ("durango", "orbis", "ps3", "vita") +} + + +class Info(ctypes.Structure): + _fields_ = [ + ("hi", ctypes.c_uint32), ("lo", ctypes.c_uint32), + ("phase", ctypes.c_uint32), ("step", ctypes.c_uint32), + ("ll", ctypes.c_int32), ("rr", ctypes.c_int32), + ("rl", ctypes.c_int32), ("lr", ctypes.c_int32), + ("flags", ctypes.c_uint32), ("shift", ctypes.c_uint8), + ("reserved", ctypes.c_uint8 * 3), + ] + + +class ReconstructedIggyMixerTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + requested_directory = os.environ.get("IGGY_TEST_BUILD_DIR") + if requested_directory: + cls.temp = None + build_directory = Path(requested_directory) + build_directory.mkdir(parents=True, exist_ok=True) + else: + cls.temp = tempfile.TemporaryDirectory() + build_directory = Path(cls.temp.name) + extra_flags = shlex.split(os.environ.get("IGGY_TEST_CFLAGS", "")) + cls.libraries = {} + for platform, define in VARIANTS.items(): + output = build_directory / f"mixer_{platform}.dylib" + result = subprocess.run([ + "clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra", + "-Werror", f"-D{define}=1", "-I", str(RECONSTRUCTED), + str(SOURCE), *extra_flags, "-o", str(output), + ], capture_output=True, text=True) + if result.returncode: + raise AssertionError((platform, result.stdout + result.stderr)) + library = ctypes.CDLL(str(output)) + library.iggy_simpmix_init_info.argtypes = [ + ctypes.POINTER(Info), *([ctypes.c_uint32] * 5), + ] + library.iggy_simpmix_set_volume.argtypes = [ + ctypes.POINTER(Info), ctypes.c_uint32, ctypes.c_uint32, + ] + library.iggy_simpmix_set_matrix.argtypes = [ + ctypes.POINTER(Info), ctypes.c_float, + ctypes.POINTER(ctypes.c_float), + ] + library.iggy_simple_mix.argtypes = [ + ctypes.POINTER(Info), ctypes.POINTER(ctypes.c_int32), + ctypes.c_int32, ctypes.c_int32, ctypes.c_void_p, + ctypes.POINTER(ctypes.c_int32), ctypes.c_int32, + ctypes.POINTER(ctypes.c_int32), ctypes.c_int32, + ] + library.iggy_simple_mix.restype = ctypes.c_uint32 + library.iggy_clamp_to_s16.argtypes = [ + ctypes.POINTER(ctypes.c_int16), ctypes.POINTER(ctypes.c_int32), + ctypes.c_uint32, + ] + library.iggy_clamp_to_s16.restype = ctypes.POINTER(ctypes.c_int16) + library.iggy_clamp_to_s16_interleave_2_4.argtypes = [ + ctypes.POINTER(ctypes.c_int16), ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), ctypes.c_uint32, + ] + library.iggy_clamp_to_s16_interleave_2_4.restype = \ + ctypes.POINTER(ctypes.c_int16) + library.iggy_clamp_to_u8.argtypes = [ + ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_int32), + ctypes.c_uint32, + ] + library.iggy_clamp_to_u8.restype = ctypes.POINTER(ctypes.c_uint8) + cls.libraries[platform] = library + + @classmethod + def tearDownClass(cls): + if cls.temp is not None: + cls.temp.cleanup() + + def test_layout_and_rate_initialization(self): + self.assertEqual(ctypes.sizeof(Info), 40) + for library in self.libraries.values(): + info = Info() + library.iggy_simpmix_init_info(ctypes.byref(info), 44100, 48000, 2, 2, 16) + self.assertEqual((info.hi, info.lo, info.phase, info.step, info.flags, info.shift), + (8, 4, 0x0B51D9AE, 0x0B51D9AF, 7, 2)) + + def test_initialization_preserves_gains_padding_and_native_quirks(self): + for library in self.libraries.values(): + info = Info(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + (ctypes.c_uint8 * 3)(21, 22, 23)) + library.iggy_simpmix_init_info( + ctypes.byref(info), 192000, 48000, 1, 1, 8, + ) + self.assertEqual( + (info.hi, info.lo, info.phase, info.step, info.flags, info.shift), + (1, 0, 0, 0x20000000, 0x10, 0), + ) + self.assertEqual( + (info.ll, info.rr, info.rl, info.lr, list(info.reserved)), + (15, 16, 17, 18, [21, 22, 23]), + ) + + library.iggy_simpmix_init_info( + ctypes.byref(info), 96000, 48000, 0, 3, 24, + ) + self.assertEqual( + (info.hi, info.lo, info.phase, info.step, info.flags, info.shift), + (4, 0, 0x40000000, 0x40000000, 5, 0), + ) + + def test_scalar_clamps_and_advanced_return(self): + values = [-0x4000000, -0x3FFF800, -2048, 0, 2048, 0x3FFF800, 0x4000000] + source = (ctypes.c_int32 * len(values))(*values) + for library in self.libraries.values(): + s16 = (ctypes.c_int16 * len(values))() + end = library.iggy_clamp_to_s16(s16, source, ctypes.sizeof(source)) + self.assertEqual(list(s16), [-32767, -32767, -1, 0, 1, 32767, 32767]) + self.assertEqual(ctypes.addressof(end.contents), ctypes.addressof(s16) + 2 * len(values)) + u8 = (ctypes.c_uint8 * len(values))() + library.iggy_clamp_to_u8(u8, source, ctypes.sizeof(source)) + self.assertEqual(list(u8), [0, 0, 127, 128, 128, 255, 255]) + + def test_interleave_and_platform_vector_saturation(self): + for platform, library in self.libraries.items(): + front = (ctypes.c_int32 * 2)(-2048, 2048) + rear = (ctypes.c_int32 * 2)(0x3FFF800, -0x3FFF800) + output = (ctypes.c_int16 * 4)() + library.iggy_clamp_to_s16_interleave_2_4( + output, front, rear, ctypes.sizeof(front), + ) + self.assertEqual(list(output), [-1, 1, 32767, -32767]) + + storage = (ctypes.c_ubyte * 96)() + aligned = (ctypes.addressof(storage) + 15) & ~15 + source = (ctypes.c_int32 * 17).from_address(aligned) + for index in range(17): + source[index] = -0x4000000 + saturated = (ctypes.c_int16 * 17)() + library.iggy_clamp_to_s16(saturated, source, 68) + expected_bulk = -32768 if platform in ("ps3", "vita") else -32767 + self.assertEqual(list(saturated[:16]), [expected_bulk] * 16) + self.assertEqual(saturated[16], -32767) + + storage = (ctypes.c_ubyte * 112)() + unaligned = ((ctypes.addressof(storage) + 15) & ~15) + 4 + source = (ctypes.c_int32 * 20).from_address(unaligned) + for index in range(20): + source[index] = -0x4000000 + saturated = (ctypes.c_int16 * 20)() + library.iggy_clamp_to_s16(saturated, source, 80) + if platform == "ps3": + self.assertEqual(list(saturated[:3]), [-32767] * 3) + self.assertEqual(list(saturated[3:19]), [-32768] * 16) + self.assertEqual(saturated[19], -32767) + elif platform == "vita": + self.assertEqual(list(saturated[:16]), [-32768] * 16) + self.assertEqual(list(saturated[16:]), [-32767] * 4) + else: + self.assertEqual(list(saturated), [-32767] * 20) + + def test_clamps_round_partial_source_lengths_up(self): + values = (ctypes.c_int32 * 2)(2048, -2048) + front = (ctypes.c_int32 * 2)(2048, -2048) + rear = (ctypes.c_int32 * 2)(0, 0x3FFF800) + for library in self.libraries.values(): + s16 = (ctypes.c_int16 * 2)() + end = library.iggy_clamp_to_s16(s16, values, 5) + self.assertEqual(list(s16), [1, -1]) + self.assertEqual( + ctypes.addressof(end.contents), ctypes.addressof(s16) + 4, + ) + interleaved = (ctypes.c_int16 * 4)() + end = library.iggy_clamp_to_s16_interleave_2_4( + interleaved, front, rear, 1, + ) + self.assertEqual(list(interleaved), [1, -1, 0, 32767]) + self.assertEqual( + ctypes.addressof(end.contents), + ctypes.addressof(interleaved) + 8, + ) + + def test_volume_and_matrix_configuration_quirks(self): + matrix = (ctypes.c_float * 4)(1, 0.25, 0.5, 1) + identity = (ctypes.c_float * 4)(1, 0, 0, 1) + for library in self.libraries.values(): + info = Info() + library.iggy_simpmix_set_volume(ctypes.byref(info), 0x4000, 0x4000) + self.assertEqual((info.ll, info.rr, info.flags), (0x40000, 0x20000, 8)) + library.iggy_simpmix_set_volume(ctypes.byref(info), 0x8000, 0x8000) + self.assertEqual(info.flags, 0) + + info.flags = 6 + library.iggy_simpmix_set_matrix(ctypes.byref(info), 0.5, matrix) + self.assertEqual( + (info.ll, info.rr, info.rl, info.lr, info.flags), + (262144, 262144, 131072, 65536, 0x26), + ) + library.iggy_simpmix_set_matrix(ctypes.byref(info), 1, identity) + self.assertEqual(info.flags, 0x26) + + info.flags = 0x28 + library.iggy_simpmix_set_volume( + ctypes.byref(info), 0x8001, 0x7FFF, + ) + self.assertEqual(info.flags, 0x20) + + info.flags = 4 + library.iggy_simpmix_set_matrix(ctypes.byref(info), 0.5, matrix) + self.assertEqual( + (info.ll, info.rr, info.rl, info.lr, info.flags), + (327680, 393216, 131072, 65536, 12), + ) + + negative_cross = (ctypes.c_float * 4)(1, -1, -1, 1) + info.flags = 0x28 + library.iggy_simpmix_set_matrix( + ctypes.byref(info), 1, negative_cross, + ) + self.assertEqual(info.flags, 0x20) + + info.flags = 1 + library.iggy_simpmix_set_matrix(ctypes.byref(info), 0.5, matrix) + self.assertEqual((info.ll, info.rr, info.rl, info.lr), + (1280, 1536, 512, 256)) + + info.flags = 0x10 + library.iggy_simpmix_set_matrix(ctypes.byref(info), 1, matrix) + self.assertEqual((info.ll, info.rr, info.rl, info.lr), + (1, 1, 0, 0)) + + info.flags = 1 + library.iggy_simpmix_set_volume( + ctypes.byref(info), 0x4000, 0x4000, + ) + self.assertEqual((info.ll, info.rr), (0x400, 0x200)) + + info.flags = 0x10 + library.iggy_simpmix_set_volume( + ctypes.byref(info), 0x8000, 0x4000, + ) + self.assertEqual((info.ll, info.rr), (1, 0)) + + def test_matrix_conversion_matches_each_target_instruction(self): + identity = (ctypes.c_float * 4)(1, 0, 0, 1) + nan_diagonal = (ctypes.c_float * 4)(float("nan"), 0, 0, float("nan")) + huge_matrix = (ctypes.c_float * 4)(8192, 1, 1, 8192) + for platform, library in self.libraries.items(): + info = Info() + library.iggy_simpmix_set_matrix( + ctypes.byref(info), ctypes.c_float(1.1), identity, + ) + overflow = -524288 if platform in ("durango", "orbis") else 524287 + self.assertEqual((info.ll, info.rr), (overflow, overflow)) + + info = Info(0, 0, 0, 0, 11, 22, 33, 44, 8, 0) + library.iggy_simpmix_set_matrix( + ctypes.byref(info), 1, nan_diagonal, + ) + if platform == "ps3": + self.assertEqual((info.ll, info.rr, info.flags), (11, 22, 0)) + else: + nan_result = 0 if platform == "vita" else -524288 + self.assertEqual((info.ll, info.rr, info.flags), + (nan_result, nan_result, 8)) + + info = Info() + info.flags = 6 + library.iggy_simpmix_set_matrix( + ctypes.byref(info), 1, huge_matrix, + ) + full_overflow = -0x80000000 if platform in ("durango", "orbis") \ + else 0x7FFFFFFF + self.assertEqual((info.ll, info.rr), + (full_overflow, full_overflow)) + + for cross_index in (1, 2): + values = [1, 0, 0, 1] + values[cross_index] = float("nan") + cross_nan = (ctypes.c_float * 4)(*values) + info = Info(0, 0, 0, 0, 11, 22, 33, 44, 14, 0) + library.iggy_simpmix_set_matrix( + ctypes.byref(info), 1, cross_nan, + ) + full = platform in ("durango", "vita") or \ + (platform == "orbis" and cross_index == 1) + if full: + nan_coefficient = 0 if platform == "vita" else -0x80000000 + self.assertEqual(info.flags, 0x26) + self.assertEqual(info.ll, 0x80000) + self.assertEqual(info.rr, 0x80000) + self.assertEqual( + info.lr if cross_index == 1 else info.rl, + nan_coefficient, + ) + else: + self.assertEqual( + (info.ll, info.rr, info.rl, info.lr, info.flags), + (11, 22, 33, 44, 6), + ) + + forced_nan = (ctypes.c_float * 4)(1, 1, float("nan"), 1) + info = Info() + info.flags = 6 + library.iggy_simpmix_set_matrix( + ctypes.byref(info), 1, forced_nan, + ) + nan_coefficient = 0 if platform == "vita" else -0x80000000 + self.assertEqual( + (info.ll, info.rr, info.rl, info.lr, info.flags), + (0x80000, 0x80000, nan_coefficient, 0x80000, 0x26), + ) + + def test_unity_kernels_routing_commit_and_interpolation(self): + for library in self.libraries.values(): + info = Info() + library.iggy_simpmix_init_info(ctypes.byref(info), 48000, 48000, 1, 1, 8) + source = (ctypes.c_uint8 * 3)(0, 128, 255) + destination = (ctypes.c_int32 * 3)() + position = ctypes.c_int32(0) + consumed = ctypes.c_int32(-1) + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 3, source, + ctypes.byref(position), 3, ctypes.byref(consumed), 1, + ) + self.assertEqual(written, 3) + self.assertEqual(list(destination), [-0x4000000, 0, 0x3F80000]) + self.assertEqual((position.value, consumed.value), (3, 3)) + + library.iggy_simpmix_init_info(ctypes.byref(info), 48000, 48000, 1, 2, 8) + stereo = (ctypes.c_uint8 * 4)(0, 255, 128, 128) + destination = (ctypes.c_int32 * 2)(123, 123) + position.value = 0 + consumed.value = -1 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 2, stereo, + ctypes.byref(position), 4, ctypes.byref(consumed), 1, + ) + self.assertEqual((written, list(destination)), (2, [-0x80000, 0])) + + info = Info(1, 0, 0x40000000, 0, 0, 0, 0, 0, 0x10, 0) + pair = (ctypes.c_uint8 * 2)(0, 255) + destination = (ctypes.c_int32 * 1)() + position.value = 0 + snapshot = bytes(info) + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, pair, + ctypes.byref(position), 1, ctypes.byref(consumed), 0, + ) + self.assertEqual((written, destination[0]), (1, -262144)) + self.assertEqual((bytes(info), position.value), (snapshot, 0)) + + def test_stereo_output_gain_and_full_matrix_kernels(self): + for library in self.libraries.values(): + position = ctypes.c_int32(0) + consumed = ctypes.c_int32(-1) + + info = Info(1, 1, 0, 0, 0, 0, 0, 0, 2, 0) + source = (ctypes.c_uint8 * 2)(129, 127) + destination = (ctypes.c_int32 * 6)(10, 20, 30, 40, 50, 60) + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 1, 5, source, + ctypes.byref(position), 2, ctypes.byref(consumed), 1, + ) + self.assertEqual(written, 4) + self.assertEqual( + list(destination), + [10, 0x80014, 0x8001E, -0x7FFD8, -0x7FFCE, 60], + ) + + info = Info(1, 1, 0, 0, 3, -4, 0, 0, 10, 0) + source = (ctypes.c_uint8 * 1)(130) + destination = (ctypes.c_int32 * 2)(100, 200) + position.value = 0 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 2, source, + ctypes.byref(position), 1, ctypes.byref(consumed), 1, + ) + self.assertEqual((written, list(destination)), (2, [106, 192])) + + info = Info(2, 2, 0, 0, 10, 20, 0, 0, 12, 1) + source = (ctypes.c_uint8 * 2)(130, 125) + destination = (ctypes.c_int32 * 1)(999) + position.value = 0 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, source, + ctypes.byref(position), 2, ctypes.byref(consumed), 1, + ) + self.assertEqual((written, destination[0]), (1, -40)) + + info = Info(2, 2, 0, 0, 10, 40, 30, 20, 0x26, 1) + source = (ctypes.c_uint8 * 2)(130, 125) + destination = (ctypes.c_int32 * 2)(1000, 2000) + position.value = 0 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 2, source, + ctypes.byref(position), 2, ctypes.byref(consumed), 1, + ) + self.assertEqual((written, list(destination)), (2, [960, 1940])) + + info = Info(1, 0, 0x40000000, 0, 3, 0, 0, 0, 0x18, 0) + source = (ctypes.c_uint8 * 2)(128, 130) + destination = (ctypes.c_int32 * 1)() + position.value = 0 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, source, + ctypes.byref(position), 1, ctypes.byref(consumed), 0, + ) + self.assertEqual((written, destination[0]), (1, 768)) + + def test_s16_nearest_and_interpolating_kernels(self): + for library in self.libraries.values(): + info = Info() + library.iggy_simpmix_init_info( + ctypes.byref(info), 48000, 48000, 1, 1, 16, + ) + source = (ctypes.c_int16 * 2)(-32768, 32767) + destination = (ctypes.c_int32 * 2)() + position = ctypes.c_int32(0) + consumed = ctypes.c_int32(-1) + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 2, source, + ctypes.byref(position), 4, ctypes.byref(consumed), 1, + ) + self.assertEqual( + (written, list(destination), position.value, consumed.value), + (2, [-0x4000000, 0x3FFF800], 4, 2), + ) + + info = Info(2, 0, 0x40000000, 0, 0, 0, 0, 0, 0x11, 1) + source = (ctypes.c_int16 * 2)(100, 200) + destination = (ctypes.c_int32 * 1)() + position.value = 0 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, source, + ctypes.byref(position), 2, ctypes.byref(consumed), 0, + ) + self.assertEqual((written, destination[0]), (1, 307200)) + + info = Info(1, 0, 0x80000000, 0, 0, 0, 0, 0, 0x10, 0) + source_u8 = (ctypes.c_uint8 * 2)(128, 129) + destination = (ctypes.c_int32 * 1)() + position.value = 0 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, source_u8, + ctypes.byref(position), 1, ctypes.byref(consumed), 0, + ) + self.assertEqual((written, destination[0]), (1, -0x80000)) + + def test_commit_overshoot_marker_and_invalid_dispatch(self): + for library in self.libraries.values(): + info = Info(4, 2, 0x80000000, 0, 0, 0, 0, 0, 4, 1) + source = (ctypes.c_uint8 * 6)(129, 128, 128, 128, 128, 128) + destination = (ctypes.c_int32 * 2)() + position = ctypes.c_int32(0) + consumed = ctypes.c_int32(-1) + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, source, + ctypes.byref(position), 3, ctypes.byref(consumed), 1, + ) + self.assertEqual(written, 1) + self.assertEqual((position.value, consumed.value, info.phase), + (3, 1, 1)) + + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 1, 2, source, + ctypes.byref(position), 5, ctypes.byref(consumed), 1, + ) + self.assertEqual(written, 0) + self.assertEqual((position.value, consumed.value, info.phase), + (5, 1, 0)) + + info = Info(0, 0, 0, 0, 0, 0, 0, 0, 0x20, 0) + destination = (ctypes.c_int32 * 1)(1234) + position.value = 0 + consumed.value = -1 + written = library.iggy_simple_mix( + ctypes.byref(info), destination, 0, 1, source, + ctypes.byref(position), 6, ctypes.byref(consumed), 1, + ) + self.assertEqual((written, destination[0]), (0, 1234)) + self.assertEqual((position.value, consumed.value), (0, 0)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_reconstructed_iggy_mixer_cross.py b/tests/test_reconstructed_iggy_mixer_cross.py new file mode 100644 index 0000000..cbbadaf --- /dev/null +++ b/tests/test_reconstructed_iggy_mixer_cross.py @@ -0,0 +1,95 @@ +from pathlib import Path +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed" +SOURCE = RECONSTRUCTED / "iggy_mixer.c" +LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm") +VARIANTS = { + "durango": ("clang", "x86_64-pc-windows-msvc"), + "orbis": ("clang", "x86_64-unknown-freebsd"), + "vita": ("clang", "armv7a-unknown-linux-gnueabihf"), +} +PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang") +if PS3_CLANG.exists(): + VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu") + +EXPECTED_EXPORTS = { + "iggy_clamp_to_s16", + "iggy_clamp_to_s16_interleave_2_4", + "iggy_clamp_to_u8", + "iggy_simple_mix", + "iggy_simpmix_init_info", + "iggy_simpmix_set_matrix", + "iggy_simpmix_set_volume", +} +COMPILER_IMPORTS = { + "durango": {"_fltused"}, + "orbis": set(), + "ps3": {"__udivdi3"}, + "vita": {"__aeabi_uidiv", "__aeabi_uldivmod"}, +} + + +class ReconstructedIggyMixerCrossTests(unittest.TestCase): + def test_target_exports_dependencies_and_cpp_header(self): + with tempfile.TemporaryDirectory() as directory: + temporary = Path(directory) + for platform, (compiler, target) in VARIANTS.items(): + output = temporary / f"mixer_{platform}.o" + result = subprocess.run([ + compiler, "-target", target, "-ffreestanding", "-c", + "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", + f"-DIGGY_RECONSTRUCT_MIXER_{platform.upper()}=1", + "-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output), + ], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, (platform, result.stderr)) + if not LLVM_NM.exists(): + continue + defined = subprocess.run( + [str(LLVM_NM), "--defined-only", str(output)], + capture_output=True, text=True, + ) + undefined = subprocess.run( + [str(LLVM_NM), "--undefined-only", str(output)], + capture_output=True, text=True, + ) + self.assertEqual(defined.returncode, 0, defined.stderr) + self.assertEqual(undefined.returncode, 0, undefined.stderr) + names = { + line.split()[-1].removeprefix(".") + for line in defined.stdout.splitlines() if line.split() + } + imports = { + line.split()[-1].removeprefix(".") + for line in undefined.stdout.splitlines() if line.split() + } + self.assertEqual( + {name for name in names if name.startswith("iggy_")}, + EXPECTED_EXPORTS, + (platform, names), + ) + self.assertEqual(imports, COMPILER_IMPORTS[platform], + (platform, imports)) + + cpp = temporary / "mixer.cpp" + cpp.write_text( + '#include "iggy_mixer.h"\n' + "static_assert(sizeof(IggySimpleMixInfo) == 40);\n" + "void use_mixer(IggySimpleMixInfo *info) {\n" + " iggy_simpmix_set_volume(info, 0x8000, 0x8000);\n" + "}\n", + encoding="utf-8", + ) + syntax = subprocess.run([ + "clang++", "-fsyntax-only", "-std=c++17", "-Wall", "-Wextra", + "-Werror", "-I", str(RECONSTRUCTED), str(cpp), + ], capture_output=True, text=True) + self.assertEqual(syntax.returncode, 0, syntax.stderr) + + +if __name__ == "__main__": + unittest.main()