summaryrefslogtreecommitdiff
path: root/test/analysis/AnalysisDistortion.c
blob: 27b8492239424bafb4750e7aec245c47f90f62a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdlib.h>
#include "AnalysisDistortion.h"

// If two samples differ by more than this amount, then we call it distortion
static const Sample kAnalysisDistortionTolerance = 0.5f;

boolByte analysisDistortion(const SampleBuffer sampleBuffer, AnalysisFunctionData data)
{
    Sample difference;

    for (ChannelCount channelIndex = 0; channelIndex < sampleBuffer->numChannels; channelIndex++) {
        for (SampleCount sampleIndex = 0; sampleIndex < sampleBuffer->blocksize; sampleIndex++) {
            if (sampleBuffer->samples[channelIndex][sampleIndex] > data->lastSample[channelIndex]) {
                difference = sampleBuffer->samples[channelIndex][sampleIndex] - data->lastSample[channelIndex];
            } else {
                difference = data->lastSample[channelIndex] - sampleBuffer->samples[channelIndex][sampleIndex];
            }

            if (difference >= kAnalysisDistortionTolerance) {
                // In this test, we don't care about the consecutive sample count. That is because
                // we also want to detect harsh clicks which occur by a jump in the amplitude, which
                // is a common error in many plugins.
                data->failedChannel = channelIndex;
                data->failedSample = sampleIndex;
                return false;
            }

            data->lastSample[channelIndex] = sampleBuffer->samples[channelIndex][sampleIndex];
        }
    }

    return true;
}