-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAudioView.m
More file actions
131 lines (116 loc) · 4.14 KB
/
AudioView.m
File metadata and controls
131 lines (116 loc) · 4.14 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// AudioView.m
// Recorder
//
// Created by Daniel Ringwalt on 11/29/11.
//
#import "AudioView.h"
const double AUDIO_VIEW_PREVIEW_SIZE = 10.f;
@implementation AudioVolumeView
@dynamic volume, peakVolume;
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGColorRef mainBackgroundColor = CGColorCreateGenericRGB(1, 1, 1, 1);
CGColorRef peakLayerBackgroundColor = CGColorCreateGenericRGB(1, 0, 0, 1);
CGColorRef volumeLayerBackgroundColor = CGColorCreateGenericRGB(1, 0, 0, 1);
self.wantsLayer = YES;
self.layer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
self.layer.backgroundColor = mainBackgroundColor;
peakLayer = [CALayer new];
peakLayer.frame = CGRectMake(1, 0, 2, self.layer.bounds.size.height);
peakLayer.backgroundColor = peakLayerBackgroundColor;
volumeLayer = [CALayer new];
volumeLayer.frame = CGRectMake(0, 0, 1, self.layer.bounds.size.height);
volumeLayer.backgroundColor = volumeLayerBackgroundColor;
[self.layer addSublayer: peakLayer];
[self.layer addSublayer: volumeLayer];
CFRelease(mainBackgroundColor);
CFRelease(peakLayerBackgroundColor);
CFRelease(volumeLayerBackgroundColor);
}
return self;
}
- (void)setVolume:(double)_volume {
volume = _volume;
CGRect volFrame = volumeLayer.frame;
volFrame.size.width = self.layer.bounds.size.width * volume;
volumeLayer.frame = volFrame;
}
- (void)setPeakVolume:(double)_peakVolume {
peakVolume = _peakVolume;
CGRect frame = peakLayer.frame;
frame.origin.x = (self.layer.bounds.size.width - 2) * peakVolume;
peakLayer.frame = frame;
}
@end
@implementation AudioView
@synthesize position, length, volumeView;
@dynamic recording;
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGColorRef mainBackgroundColor = CGColorCreateGenericRGB(1, 1, 1, 1);
CGColorRef progressLayerBackgroundColor = CGColorCreateGenericRGB(1, .1, .1, 1);
progressLayer = [[CALayer alloc] init];
progressLayer.backgroundColor = progressLayerBackgroundColor;
self.wantsLayer = YES;
progressLayer.frame = CGRectMake(0, 0, 1, self.layer.bounds.size.height);
[self.layer addSublayer: progressLayer];
self.layer.backgroundColor = mainBackgroundColor;
CFRelease(mainBackgroundColor);
CFRelease(progressLayerBackgroundColor);
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
}
- (void)setIsRecording:(BOOL)recording {
NSLog(@"recording? %d", recording);
isRecording = recording;
if (isRecording) {
double time = AUDIO_VIEW_PREVIEW_SIZE - length;
if (time <= 0) {
progressLayer.frame = self.layer.bounds;
return;
}
CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
progressAnimation.fromValue = [NSValue valueWithRect: NSMakeRect(0, 0,
self.layer.bounds.size.width
* length / AUDIO_VIEW_PREVIEW_SIZE,
self.layer.bounds.size.height)];
progressAnimation.toValue = [NSValue valueWithRect: NSMakeRect(0, 0,
self.layer.bounds.size.width*2,
self.layer.bounds.size.height)];
progressAnimation.duration = 5.f;
progressAnimation.fillMode = kCAFillModeForwards;
progressAnimation.delegate = self;
[progressLayer addAnimation: progressAnimation forKey: @"timeAnimate"];
}
}
- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag {
[CATransaction begin];
[CATransaction setValue: (id)kCFBooleanTrue forKey: kCATransactionDisableActions];
[progressLayer setValue: anim.toValue forKey: anim.keyPath];
[CATransaction commit];
}
- (void)displayPcmSamples: (void *)samples count: (long)count startTime: (double)start endTime: (double)end {
double average = 0;
int32_t *pcm = (int32_t *)samples;
if (start - peakTime > 2) {
peakTime = start;
peakAudio = 0;
}
int i;
for (i = 0; i < count; i++) {
double this = (double)abs(pcm[i]) / INT32_MAX;
average += this / count;
}
if (average > peakAudio) peakAudio = average;
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
volumeView.volume = average;
volumeView.peakVolume = peakAudio;
[CATransaction commit];
}
@end