-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeakCounter.mm
68 lines (56 loc) · 1.31 KB
/
PeakCounter.mm
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "PeakCounter.h"
#import "LowPassFilter.h"
@implementation PeakCounter
@synthesize level, lowPassFilter, currentEdge, counter;
+ (PeakCounter*)createWithLevel:(Float32)level
{
return [[[PeakCounter alloc] initWithLevel:level] autorelease];
}
- (id)initWithLevel:(Float32)theLevel
{
if (self = [super init]) {
level = theLevel;
lowPassFilter = nil;
[self reset];
}
return self;
}
- (void)reset
{
counter = 0;
currentEdge = kEdgeKindUnknown;
if (lowPassFilter) [lowPassFilter reset];
}
- (UInt32)counterAndReset
{
UInt32 value = counter;
counter = 0;
return value;
}
#pragma mark -
#pragma mark SampleProcessorProtocol
- (void)addSamples:(Float32*)ptr count:(UInt32)count
{
while (count-- > 0) {
Float32 sample = *ptr++;
if (lowPassFilter != nil) {
sample = [lowPassFilter filter:sample];
}
if (sample >= level) {
if (currentEdge != kEdgeKindRising) {
currentEdge = kEdgeKindRising;
++counter;
}
}
else {
if (currentEdge != kEdgeKindFalling) {
currentEdge = kEdgeKindFalling;
}
}
}
}
@end