-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBufferBlock.m
More file actions
91 lines (75 loc) · 2.52 KB
/
DataBufferBlock.m
File metadata and controls
91 lines (75 loc) · 2.52 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// DataBufferBlock.m
// OpenSpirometry
//
// Created by Eric Larson
// Copyright (c) 2015 Eric Larson. All rights reserved.
//
#import "DataBufferBlock.h"
#import<QuartzCore/QuartzCore.h>
@interface DataBufferBlock()
@property (nonatomic,readwrite) NSUInteger writePosition;
@property (nonatomic,readwrite) NSUInteger length;
@property (nonatomic,readwrite) CFTimeInterval timeCreated;
@property (nonatomic,readwrite) BOOL isFull;
@end
@implementation DataBufferBlock
-(float*)data{ // on demand in case never used
if(!_data){
_data = (float *)calloc(self.length,sizeof(float));
}
return _data;
}
-(id)initWithCapacity:(NSUInteger)numItems{
if(self = [super init]){
//set backing variables
_length = numItems;
_writePosition = 0;
_timeCreated = CACurrentMediaTime();
_isFull = NO;
return self;
}
return nil;
}
-(id)init{
return [self initWithCapacity:512]; //probably not what you want, use the designated init above
}
-(void)addFloatData:(float*)data withLength:(NSUInteger)dataLength{
if(self.writePosition+dataLength <= self.length){ // wont go off the end, just copy
memcpy(&self.data[self.writePosition], data, dataLength*sizeof(float));
self.writePosition += dataLength;
}else{ // we will go over the end, only copy some
NSUInteger floatsToCopy = self.length - self.writePosition;
memcpy(&self.data[self.writePosition], data, floatsToCopy*sizeof(float));
self.writePosition += floatsToCopy;
}
if(self.writePosition >= self.length)
self.isFull = YES;
}
-(void)addInterleavedFloatData:(float*)data fromChannel:(NSUInteger)whichChannel
withNumChannels:(NSUInteger)numChannels withLength:(NSUInteger)dataLength
{
if(self.writePosition+dataLength <= self.length){ // wont go off the end, just copy
float *p = &data[whichChannel];
for(int i=0;i<dataLength;++i,p+=numChannels){
self.data[self.writePosition+i] = *p;
}
self.writePosition += dataLength;
}else{ // we will go over the end, only copy some
NSUInteger floatsToCopy = self.length - self.writePosition;
float *p = &data[whichChannel];
for(int i=0;i<floatsToCopy;++i,p+=numChannels){
self.data[self.writePosition+i] = *p;
}
self.writePosition += floatsToCopy;
}
if(self.writePosition >= self.length)
self.isFull = YES;
}
-(void)dealloc{
if(_data){
free(_data);
_data = nil;
}
}
@end