Debian Codegraph @

iOS/Csound iOS Obj-C Examples/Csound iOS Examples/ViewControllers/RecordTest/LevelMeterView.m

raw
1 /*
2
3 LevelMeterView.m:
4
5 Copyright (C) 2014 Thomas Hass, Aurelius Prochazka
6
7 This file is part of Csound iOS Examples.
8
9 The Csound for iOS Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 Csound is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with Csound; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA
23
24 */
25
26 #import "LevelMeterView.h"
27
28 @interface LevelMeterView () {
29 float channelValue;
30 float *channelPtr;
31 CGFloat lastY;
32 NSInteger ksmps, sr;
33 }
34 @property (nonatomic, strong) NSString *channelName;
35
36 @end
37
38 @implementation LevelMeterView
39
40 - (id)initWithFrame:(CGRect)frame
41 {
42 self = [super initWithFrame:frame];
43 if (self) {
44 channelValue = 0.0f;
45 }
46 return self;
47 }
48
49 - (id)initWithCoder:(NSCoder *)aDecoder
50 {
51 self = [super initWithCoder:aDecoder];
52 if (self) {
53 channelValue = 0.0f;
54 lastY = -100;
55 }
56 return self;
57 }
58
59 - (void)addToCsoundObj:(CsoundObj *)csoundObj forChannelName:(NSString *)channelName_
60 {
61 [csoundObj addBinding:self];
62 self.channelName = channelName_;
63 }
64
65 - (void)drawRect:(CGRect)rect
66 {
67 // Round the corners
68 UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
69 byRoundingCorners:UIRectCornerAllCorners
70 cornerRadii:CGSizeMake(10.0, 10.0)];
71 [clipPath addClip];
72
73 // Get the context
74 CGContextRef context = UIGraphicsGetCurrentContext();
75 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
76
77 // Flip coordinate system
78 CGContextTranslateCTM(context, 0, rect.size.height);
79 CGContextScaleCTM(context, 1.0, -1.0);
80
81 // Draw background
82 CGFloat grayComponents[] = {0.7f, 0.7f, 0.7f, 1.0f};
83 CGColorRef grayColor = CGColorCreate(colorSpace, grayComponents);
84 CGContextSetFillColorWithColor(context, grayColor);
85 CGContextFillRect(context, self.bounds);
86 CGColorRelease(grayColor);
87
88 // Draw rects
89 CGFloat width = CGRectGetMaxX(rect);
90 CGFloat height = CGRectGetMaxY(rect);
91 CGFloat squareWidth = width - 10;
92 CGFloat squareHeight = height/30.0f;;
93
94 CGFloat greenComponents[] = {0.0f, 1.0f, 0.0f, 1.0f};
95 CGFloat yellowComponents[] = {221.0f/250.0f, 223.0f/250.0f, 14.0f/250.0f, 1.0f};
96 CGFloat redComponents[] = {1.0f, 0.0f, 0.0f, 1.0f};
97 CGColorRef greenColor = CGColorCreate(colorSpace, greenComponents);
98 CGColorRef yellowColor = CGColorCreate(colorSpace, yellowComponents);
99 CGColorRef redColor = CGColorCreate(colorSpace, redComponents);
100
101 CGFloat x = 12.0f;
102
103 // Draw the peak rect.
104 if (lastY < (height * 0.7f)) {
105 CGContextSetFillColorWithColor(context, greenColor);
106 } else if (lastY < (height * 0.9f)) {
107 CGContextSetFillColorWithColor(context, yellowColor);
108 } else {
109 CGContextSetFillColorWithColor(context, redColor);
110 }
111 CGContextMoveToPoint(context, x, lastY);
112 CGContextAddLineToPoint(context, x, lastY + squareHeight);
113 CGContextAddLineToPoint(context, x + squareWidth - 12.0f, lastY + squareHeight);
114 CGContextAddLineToPoint(context, x + squareWidth - 12.0f, lastY);
115 CGContextFillPath(context);
116
117 // Draw the rest of the rects.
118 for (int y = 12.0f; y < height * channelValue - 12.0f; y += squareHeight + 5.0f) {
119 if (y < (height * 0.7f)) {
120 CGContextSetFillColorWithColor(context, greenColor);
121 } else if ((y < height * 0.9f)) {
122 CGContextSetFillColorWithColor(context, yellowColor);
123 } else {
124 CGContextSetFillColorWithColor(context, redColor);
125 }
126 CGContextMoveToPoint(context, x, y);
127 CGContextAddLineToPoint(context, x, y + squareHeight);
128 CGContextAddLineToPoint(context, x + squareWidth - 12.0f, y + squareHeight);
129 CGContextAddLineToPoint(context, x + squareWidth - 12.0f, y);
130 CGContextFillPath(context);
131 if (y > lastY) {
132 lastY = y;
133 }
134 }
135 CGColorRelease(greenColor);
136 CGColorRelease(yellowColor);
137 CGColorRelease(redColor);
138 CGColorSpaceRelease(colorSpace);
139
140 // Reset lastY periodically.
141 static NSInteger count = 0;
142 if (count % 100 == 0) {
143 lastY = -squareHeight;
144 }
145 count++;
146 }
147
148 #pragma mark - Csound Binding
149
150 -(void)setup:(CsoundObj *)csoundObj
151 {
152 channelPtr = [csoundObj getOutputChannelPtr:self.channelName channelType:CSOUND_AUDIO_CHANNEL];
153 CSOUND *cs = [csoundObj getCsound];
154 sr = csoundGetSr(cs);
155 ksmps = csoundGetKsmps(cs);
156 }
157
158 -(void)updateValuesFromCsound
159 {
160 channelValue = fabs(*channelPtr);
161
162 static NSInteger count = 0;
163 if (count % ((sr/ksmps)/20) == 0) {
164 [self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
165 }
166 count++;
167 if (count > INT_MAX) {
168 count -= INT_MAX;
169 }
170 }
171
172 - (void)cleanup
173 {
174 channelValue = 0;
175 lastY = -100;
176 [self setNeedsDisplay];
177 }
178
179 @end