Cog/Audio/Utils/BadSampleCleaner.m
Christopher Snowhill 25077277b3 Add bad sample cleaner for debugging
A bad sample scanner and cleaner will point out in the log whenever a
bad sample, such as infinity, or Not a Number, or even huge values over
±2.0, in case some piece of code, or a decoder, or even a bad file, has
taken over the output.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-02-15 22:48:47 -08:00

40 lines
938 B
Objective-C

//
// BadSampleCleaner.m
// CogAudio Framework
//
// Created by Christopher Snowhill on 2/15/22.
//
#ifdef _DEBUG
#import "BadSampleCleaner.h"
#import "Logging.h"
@implementation BadSampleCleaner
+ (void)cleanSamples:(float *)buffer amount:(NSUInteger)amount location:(NSString *)location {
BOOL hadNaN = NO;
BOOL hadINF = NO;
BOOL hadHUGE = NO;
for(NSUInteger i = 0; i < amount; ++i) {
float sample = buffer[i];
BOOL isNaN = isnan(sample);
BOOL isINF = isinf(sample);
BOOL isHUGE = (fabs(sample) > 2.0);
hadNaN = hadNaN || isNaN;
hadINF = hadINF || isINF;
hadHUGE = hadHUGE || isHUGE;
if(isNaN || isINF || isHUGE) {
memset(&buffer[i], 0, sizeof(buffer[i]));
memset(&sample, 0, sizeof(sample));
}
}
if(hadNaN || hadINF || hadHUGE) {
DLog(@"Sample block at %@ had NaN: %@, INF: %@, HUGE: %@", location, hadNaN ? @"yes" : @"no", hadINF ? @"yes" : @"no", hadHUGE ? @"yes" : @"no");
}
}
@end
#endif