| 1 | package com.rox.apu; | |
| 2 | ||
| 3 | /** | |
| 4 | * Combines the five NES APU channel outputs into a single sample (~0.0-1.0). | |
| 5 | * | |
| 6 | * <p>Real NES hardware doesn't mix its 5 channels with a simple average, and it doesn't mix them | |
| 7 | * all together in one place either. Two separate analog resistor networks on the chip each sum a | |
| 8 | * subset of the channels, and each network's output voltage is a <em>non-linear</em> function of | |
| 9 | * its inputs (a consequence of the physical circuit, not a deliberate audio design choice): | |
| 10 | * <ul> | |
| 11 | * <li>one network sums {@code pulse1} + {@code pulse2} ({@link #pulseOut});</li> | |
| 12 | * <li>the other sums {@code triangle} + {@code noise} + {@code dmc} - commonly abbreviated | |
| 13 | * "TND" in NES documentation and emulator source - ({@link #tndOut}).</li> | |
| 14 | * </ul> | |
| 15 | * The two formulas below are curve-fits (reverse-engineered from real hardware measurements) for | |
| 16 | * those two circuits' voltage output, not something derived from first principles - which is why | |
| 17 | * their constants (95.88, 8128, 8227, 12241, 22638, ...) don't individually mean anything; they're | |
| 18 | * just the coefficients that reproduce the measured curves. {@code output} is simply the sum of | |
| 19 | * both circuits' voltages, since on real hardware that's the final combined analog signal. | |
| 20 | * | |
| 21 | * <p>{@code pulse1}/{@code pulse2}/{@code triangle}/{@code noise} are 0-15, {@code dmc} is 0-127. | |
| 22 | * | |
| 23 | * <p>Deliberately not precomputing a single lookup table for {@link #tndOut} keyed by a combined | |
| 24 | * index like {@code 3*triangle + 2*noise + dmc} (a common optimisation in other NES emulators): | |
| 25 | * that collapses three independently-weighted terms (divisors 8227/12241/22638 - not proportional | |
| 26 | * to 3:2:1) into one dimension, which is only an approximation of the real circuit's curve, not an | |
| 27 | * exact optimisation of it. | |
| 28 | * | |
| 29 | * <p>{@link #mix} is called on every CPU cycle, so both circuits are precomputed instead - but exactly: one | |
| 30 | * entry per possible input (31 pulse sums; every triangle/noise/dmc combination, 16 x 16 x 128), each | |
| 31 | * computed by the same formula as before, so a lookup returns the identical double. | |
| 32 | */ | |
| 33 | public final class Mixer { | |
| 34 | /** Curve-fit coefficients for the pulse1+pulse2 summing circuit's output voltage. */ | |
| 35 | private static final double PULSE_CIRCUIT_NUMERATOR = 95.88; | |
| 36 | private static final double PULSE_CIRCUIT_DIVISOR_CONSTANT = 8128.0; | |
| 37 | private static final double PULSE_CIRCUIT_OFFSET = 100.0; | |
| 38 | ||
| 39 | /** Curve-fit coefficients for the triangle+noise+dmc ("TND") summing circuit's output voltage. */ | |
| 40 | private static final double TND_CIRCUIT_NUMERATOR = 159.79; | |
| 41 | private static final double TRIANGLE_CONTRIBUTION_DIVISOR = 8227.0; | |
| 42 | private static final double NOISE_CONTRIBUTION_DIVISOR = 12241.0; | |
| 43 | private static final double DMC_CONTRIBUTION_DIVISOR = 22638.0; | |
| 44 | private static final double TND_CIRCUIT_OFFSET = 100.0; | |
| 45 | ||
| 46 | private static final int FOUR_BIT_VALUES = 16; | |
| 47 | private static final int SEVEN_BIT_VALUES = 128; | |
| 48 | private static final double[] PULSE_OUT = new double[2 * FOUR_BIT_VALUES - 1]; | |
| 49 | private static final double[] TND_OUT = new double[FOUR_BIT_VALUES * FOUR_BIT_VALUES * SEVEN_BIT_VALUES]; | |
| 50 | ||
| 51 | static { | |
| 52 | for (int sum = 0; sum < PULSE_OUT.length; sum++){ | |
| 53 | PULSE_OUT[sum] = pulseOut(sum); | |
| 54 | } | |
| 55 | for (int triangle = 0; triangle < FOUR_BIT_VALUES; triangle++){ | |
| 56 | for (int noise = 0; noise < FOUR_BIT_VALUES; noise++){ | |
| 57 | for (int dmc = 0; dmc < SEVEN_BIT_VALUES; dmc++){ | |
| 58 | TND_OUT[tndIndex(triangle, noise, dmc)] = tndOut(triangle, noise, dmc); | |
| 59 | } | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | private Mixer(){ | |
| 65 | /** | |
| 66 | * XXX Code coverage fails us here. This constructors only job is to make `new Mixer()` a compile error, | |
| 67 | * since a utility class with only static methods being instantiable is meaningless. If I remove it, | |
| 68 | * Java auto-generates a public no-arg constructor, so `new Mixer()` becomes legal (harmless, but there's | |
| 69 | * no longer anything stopping it). | |
| 70 | */ | |
| 71 | } | |
| 72 | ||
| 73 | /** The combined analog output voltage of both summing circuits, as they'd appear on real hardware. */ | |
| 74 | public static double mix(final int pulse1, | |
| 75 | final int pulse2, | |
| 76 | final int triangle, | |
| 77 | final int noise, | |
| 78 | final int dmc){ | |
| 79 |
3
1. mix : Replaced integer addition with subtraction → KILLED 2. mix : replaced double return with 0.0d for com/rox/apu/Mixer::mix → KILLED 3. mix : Replaced double addition with subtraction → KILLED |
return PULSE_OUT[pulse1 + pulse2] + TND_OUT[tndIndex(triangle, noise, dmc)]; |
| 80 | } | |
| 81 | ||
| 82 | private static int tndIndex(final int triangle, final int noise, final int dmc){ | |
| 83 | return (triangle * FOUR_BIT_VALUES + noise) * SEVEN_BIT_VALUES + dmc; | |
| 84 | } | |
| 85 | ||
| 86 | /** The pulse1+pulse2 summing circuit's output voltage for {@code pulse1 + pulse2} (0 when both are silent). */ | |
| 87 | private static double pulseOut(final int sum){ | |
| 88 | if (sum == 0){ | |
| 89 | return 0; | |
| 90 | } | |
| 91 | return PULSE_CIRCUIT_NUMERATOR / (PULSE_CIRCUIT_DIVISOR_CONSTANT / sum + PULSE_CIRCUIT_OFFSET); | |
| 92 | } | |
| 93 | ||
| 94 | /** The triangle+noise+dmc ("TND") summing circuit's output voltage (0 when all three are silent). */ | |
| 95 | private static double tndOut(final int triangle, final int noise, final int dmc){ | |
| 96 | if (triangle == 0 && noise == 0 && dmc == 0){ | |
| 97 | return 0; | |
| 98 | } | |
| 99 | final double combinedContribution = triangle / TRIANGLE_CONTRIBUTION_DIVISOR | |
| 100 | + noise / NOISE_CONTRIBUTION_DIVISOR | |
| 101 | + dmc / DMC_CONTRIBUTION_DIVISOR; | |
| 102 | return TND_CIRCUIT_NUMERATOR / (1.0 / combinedContribution + TND_CIRCUIT_OFFSET); | |
| 103 | } | |
| 104 | } | |
Mutations | ||
| 79 |
1.1 2.2 3.3 |