Resampler.java

1
package com.rox.audio;
2
3
import java.util.OptionalDouble;
4
5
/**
6
 * Downsamples a per-input-cycle stream of audio samples (e.g. the APU's per-CPU-cycle mixed
7
 * output, ~1.79MHz) to a lower output rate (e.g. 44100Hz) by averaging each window of input
8
 * samples into one output sample (a box filter).
9
 *
10
 * The input:output ratio is rarely a whole number (1,789,773 / 44,100 ≈ 40.61), so a fixed
11
 * window size would drift the output rate over time. Instead this mirrors
12
 * {@link com.rox.clock.FPSClock#ticksThisFrame()}'s fractional-remainder accumulation: window
13
 * sizes are mostly {@code inputRate / outputRate} input samples, with an extra sample folded in
14
 * periodically so the long-run average window size is exactly {@code inputRate / outputRate}
15
 * (kept exact, not just close, the same way {@code FPSClock} keeps its tick rate exact).
16
 */
17
public class Resampler {
18
    private final long outputRate;
19
    private long baseWindowSize;
20
    private long windowSizeRemainderPerOutputSample;
21
22
    private long windowRemainderBuffer;
23
    private long samplesNeededForCurrentWindow;
24
25
    private double accumulatedSum;
26
    private long accumulatedSampleCount;
27
    private double output;
28
29
    public Resampler(final long inputRate, final long outputRate){
30 1 1. <init> : removed call to com/rox/audio/Resampler::checkRates → KILLED
        checkRates(inputRate, outputRate);
31
        this.outputRate = outputRate;
32 1 1. <init> : Replaced long division with multiplication → KILLED
        this.baseWindowSize = inputRate / outputRate;
33 1 1. <init> : Replaced long modulus with multiplication → KILLED
        this.windowSizeRemainderPerOutputSample = inputRate % outputRate;
34
        this.samplesNeededForCurrentWindow = nextWindowSize();
35
    }
36
37
    /**
38
     * Retargets to a new input rate - e.g. the CPU running at double speed, so twice as many input samples
39
     * make up each output sample (and the sound plays an octave up). The window in progress finishes at its
40
     * old size; later ones use the new rate.
41
     *
42
     * @throws IllegalArgumentException as the constructor
43
     */
44
    public void setInputRate(final long inputRate){
45 1 1. setInputRate : removed call to com/rox/audio/Resampler::checkRates → KILLED
        checkRates(inputRate, outputRate);
46 1 1. setInputRate : Replaced long division with multiplication → KILLED
        baseWindowSize = inputRate / outputRate;
47 1 1. setInputRate : Replaced long modulus with multiplication → KILLED
        windowSizeRemainderPerOutputSample = inputRate % outputRate;
48
    }
49
50
    private static void checkRates(final long inputRate, final long outputRate){
51 4 1. checkRates : changed conditional boundary → SURVIVED
2. checkRates : negated conditional → KILLED
3. checkRates : negated conditional → KILLED
4. checkRates : changed conditional boundary → KILLED
        if (inputRate <= 0 || outputRate <= 0){
52
            throw new IllegalArgumentException(
53
                    "inputRate and outputRate must both be positive, got inputRate=" + inputRate + ", outputRate=" + outputRate);
54
        }
55 2 1. checkRates : changed conditional boundary → KILLED
2. checkRates : negated conditional → KILLED
        if (inputRate < outputRate){
56
            throw new IllegalArgumentException(
57
                    "inputRate must be >= outputRate (this is a downsampler only), got inputRate=" + inputRate + ", outputRate=" + outputRate);
58
        }
59
    }
60
61
    /**
62
     * Accepts one input-rate sample. Returns the averaged output sample once a full window has
63
     * been accumulated, otherwise empty.
64
     */
65
    public OptionalDouble accept(final double sample){
66 2 1. accept : replaced return value with null for com/rox/audio/Resampler::accept → KILLED
2. accept : negated conditional → KILLED
        return add(sample) ? OptionalDouble.of(output) : OptionalDouble.empty();
67
    }
68
69
    /**
70
     * {@link #accept} without the wrapper, for a caller on the per-cycle hot path: returns true when this sample
71
     * completed a window, whose average is then {@link #output()}.
72
     */
73
    public boolean add(final double sample){
74 1 1. add : Replaced double addition with subtraction → KILLED
        accumulatedSum += sample;
75 1 1. add : Replaced long addition with subtraction → KILLED
        accumulatedSampleCount++;
76
77 2 1. add : negated conditional → KILLED
2. add : changed conditional boundary → KILLED
        if (accumulatedSampleCount < samplesNeededForCurrentWindow){
78 1 1. add : replaced boolean return with true for com/rox/audio/Resampler::add → KILLED
            return false;
79
        }
80
81 1 1. add : Replaced double division with multiplication → KILLED
        output = accumulatedSum / accumulatedSampleCount;
82
        accumulatedSum = 0;
83
        accumulatedSampleCount = 0;
84
        samplesNeededForCurrentWindow = nextWindowSize();
85 1 1. add : replaced boolean return with false for com/rox/audio/Resampler::add → KILLED
        return true;
86
    }
87
88
    /** The output sample from the last {@link #add} that returned true. */
89
    public double output(){
90 1 1. output : replaced double return with 0.0d for com/rox/audio/Resampler::output → KILLED
        return output;
91
    }
92
93
    private long nextWindowSize(){
94
        long windowSize = baseWindowSize;
95 1 1. nextWindowSize : Replaced long addition with subtraction → KILLED
        windowRemainderBuffer += windowSizeRemainderPerOutputSample;
96 2 1. nextWindowSize : negated conditional → KILLED
2. nextWindowSize : changed conditional boundary → KILLED
        if (windowRemainderBuffer >= outputRate){
97 1 1. nextWindowSize : Replaced long addition with subtraction → KILLED
            windowSize++;
98 1 1. nextWindowSize : Replaced long subtraction with addition → KILLED
            windowRemainderBuffer -= outputRate;
99
        }
100 1 1. nextWindowSize : replaced long return with 0 for com/rox/audio/Resampler::nextWindowSize → KILLED
        return windowSize;
101
    }
102
}

Mutations

30

1.1
Location : <init>
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:rejectsNegativeInputRate()]
removed call to com/rox/audio/Resampler::checkRates → KILLED

32

1.1
Location : <init>
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
Replaced long division with multiplication → KILLED

33

1.1
Location : <init>
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
Replaced long modulus with multiplication → KILLED

45

1.1
Location : setInputRate
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:setInputRateRejectsWhatTheConstructorDoes()]
removed call to com/rox/audio/Resampler::checkRates → KILLED

46

1.1
Location : setInputRate
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:aNewInputRateAppliesFromTheNextWindow()]
Replaced long division with multiplication → KILLED

47

1.1
Location : setInputRate
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:aNewInputRateAppliesFromTheNextWindow()]
Replaced long modulus with multiplication → KILLED

51

1.1
Location : checkRates
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:rejectsZeroOutputRate()]
negated conditional → KILLED

2.2
Location : checkRates
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
negated conditional → KILLED

3.3
Location : checkRates
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

4.4
Location : checkRates
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:rejectsZeroOutputRate()]
changed conditional boundary → KILLED

55

1.1
Location : checkRates
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:allowsInputRateEqualToOutputRate()]
changed conditional boundary → KILLED

2.2
Location : checkRates
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:rejectsInputRateBelowOutputRate()]
negated conditional → KILLED

66

1.1
Location : accept
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:emptyUntilTheWindowIsFull()]
replaced return value with null for com/rox/audio/Resampler::accept → KILLED

2.2
Location : accept
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:emptyUntilTheWindowIsFull()]
negated conditional → KILLED

74

1.1
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:outputIsTheAverageOfItsWindow()]
Replaced double addition with subtraction → KILLED

75

1.1
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
Replaced long addition with subtraction → KILLED

77

1.1
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
negated conditional → KILLED

2.2
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
changed conditional boundary → KILLED

78

1.1
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
replaced boolean return with true for com/rox/audio/Resampler::add → KILLED

81

1.1
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:outputIsTheAverageOfItsWindow()]
Replaced double division with multiplication → KILLED

85

1.1
Location : add
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
replaced boolean return with false for com/rox/audio/Resampler::add → KILLED

90

1.1
Location : output
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:addReportsEachCompletedWindowAndOutputIsItsAverage()]
replaced double return with 0.0d for com/rox/audio/Resampler::output → KILLED

95

1.1
Location : nextWindowSize
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
Replaced long addition with subtraction → KILLED

96

1.1
Location : nextWindowSize
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
negated conditional → KILLED

2.2
Location : nextWindowSize
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
changed conditional boundary → KILLED

97

1.1
Location : nextWindowSize
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
Replaced long addition with subtraction → KILLED

98

1.1
Location : nextWindowSize
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
Replaced long subtraction with addition → KILLED

100

1.1
Location : nextWindowSize
Killed by : com.rox.audio.ResamplerTest.[engine:junit-jupiter]/[class:com.rox.audio.ResamplerTest]/[method:windowSizesSumExactlyToTheInputRateOverMultipleCycles()]
replaced long return with 0 for com/rox/audio/Resampler::nextWindowSize → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1