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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
LevelMeterView.m:
Copyright (C) 2014 Thomas Hass, Aurelius Prochazka
This file is part of Csound iOS Examples.
The Csound for iOS Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
#import "LevelMeterView.h"
@interface LevelMeterView () {
float channelValue;
float *channelPtr;
CGFloat lastY;
NSInteger ksmps, sr;
}
@property (nonatomic, strong) NSString *channelName;
@end
@implementation LevelMeterView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
channelValue = 0.0f;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
channelValue = 0.0f;
lastY = -100;
}
return self;
}
- (void)addToCsoundObj:(CsoundObj *)csoundObj forChannelName:(NSString *)channelName_
{
[csoundObj addBinding:self];
self.channelName = channelName_;
}
- (void)drawRect:(CGRect)rect
{
// Round the corners
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(10.0, 10.0)];
[clipPath addClip];
// Get the context
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Flip coordinate system
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Draw background
CGFloat grayComponents[] = {0.7f, 0.7f, 0.7f, 1.0f};
CGColorRef grayColor = CGColorCreate(colorSpace, grayComponents);
CGContextSetFillColorWithColor(context, grayColor);
CGContextFillRect(context, self.bounds);
CGColorRelease(grayColor);
// Draw rects
CGFloat width = CGRectGetMaxX(rect);
CGFloat height = CGRectGetMaxY(rect);
CGFloat squareWidth = width - 10;
CGFloat squareHeight = height/30.0f;;
CGFloat greenComponents[] = {0.0f, 1.0f, 0.0f, 1.0f};
CGFloat yellowComponents[] = {221.0f/250.0f, 223.0f/250.0f, 14.0f/250.0f, 1.0f};
CGFloat redComponents[] = {1.0f, 0.0f, 0.0f, 1.0f};
CGColorRef greenColor = CGColorCreate(colorSpace, greenComponents);
CGColorRef yellowColor = CGColorCreate(colorSpace, yellowComponents);
CGColorRef redColor = CGColorCreate(colorSpace, redComponents);
CGFloat x = 12.0f;
// Draw the peak rect.
if (lastY < (height * 0.7f)) {
CGContextSetFillColorWithColor(context, greenColor);
} else if (lastY < (height * 0.9f)) {
CGContextSetFillColorWithColor(context, yellowColor);
} else {
CGContextSetFillColorWithColor(context, redColor);
}
CGContextMoveToPoint(context, x, lastY);
CGContextAddLineToPoint(context, x, lastY + squareHeight);
CGContextAddLineToPoint(context, x + squareWidth - 12.0f, lastY + squareHeight);
CGContextAddLineToPoint(context, x + squareWidth - 12.0f, lastY);
CGContextFillPath(context);
// Draw the rest of the rects.
for (int y = 12.0f; y < height * channelValue - 12.0f; y += squareHeight + 5.0f) {
if (y < (height * 0.7f)) {
CGContextSetFillColorWithColor(context, greenColor);
} else if ((y < height * 0.9f)) {
CGContextSetFillColorWithColor(context, yellowColor);
} else {
CGContextSetFillColorWithColor(context, redColor);
}
CGContextMoveToPoint(context, x, y);
CGContextAddLineToPoint(context, x, y + squareHeight);
CGContextAddLineToPoint(context, x + squareWidth - 12.0f, y + squareHeight);
CGContextAddLineToPoint(context, x + squareWidth - 12.0f, y);
CGContextFillPath(context);
if (y > lastY) {
lastY = y;
}
}
CGColorRelease(greenColor);
CGColorRelease(yellowColor);
CGColorRelease(redColor);
CGColorSpaceRelease(colorSpace);
// Reset lastY periodically.
static NSInteger count = 0;
if (count % 100 == 0) {
lastY = -squareHeight;
}
count++;
}
#pragma mark - Csound Binding
-(void)setup:(CsoundObj *)csoundObj
{
channelPtr = [csoundObj getOutputChannelPtr:self.channelName channelType:CSOUND_AUDIO_CHANNEL];
CSOUND *cs = [csoundObj getCsound];
sr = csoundGetSr(cs);
ksmps = csoundGetKsmps(cs);
}
-(void)updateValuesFromCsound
{
channelValue = fabs(*channelPtr);
static NSInteger count = 0;
if (count % ((sr/ksmps)/20) == 0) {
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
count++;
if (count > INT_MAX) {
count -= INT_MAX;
}
}
- (void)cleanup
{
channelValue = 0;
lastY = -100;
[self setNeedsDisplay];
}
@end
|