Debian Codegraph @

iOS/Csound iOS Obj-C Examples/Csound iOS Examples/ViewControllers/Waveview/Waveview.m

raw
1 /*
2
3 Waveview.m:
4
5 Copyright (C) 2015 Steven Yi, Ed Costello, 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 "Waveview.h"
27 #import "CsoundObj.h"
28
29 @implementation Waveview
30 {
31 BOOL tableLoaded;
32 CGFloat lastY;
33 CsoundObj *csObj;
34 MYFLT *table;
35 int tableLength;
36 MYFLT *displayData;
37 int fTableNumber;
38 }
39
40 - (void)displayFTable:(int)fTableNum
41 {
42 fTableNumber = fTableNum;
43 tableLoaded = NO;
44 [self updateValuesFromCsound];
45 }
46
47 - (void)drawRect:(CGRect)rect
48 {
49 CGContextRef context = UIGraphicsGetCurrentContext();
50 CGContextSetRGBFillColor(context, 0, 0, 0, 1);
51 CGContextFillRect(context, rect);
52
53 if (tableLoaded) {
54 int width = self.frame.size.width;
55
56 CGContextSetRGBStrokeColor(context, 255, 255, 255, 1);
57 CGContextSetRGBFillColor(context, 255, 255, 255, 1);
58 CGMutablePathRef fill_path = CGPathCreateMutable();
59 CGFloat x = 0;
60 CGFloat y = displayData[0];
61
62 CGPathMoveToPoint(fill_path, &CGAffineTransformIdentity, x, y);
63
64 for(int i = 1; i < width; i++) {
65 CGPathAddLineToPoint(fill_path, &CGAffineTransformIdentity, i, displayData[i]);
66 }
67 CGContextAddPath(context, fill_path);
68 CGContextSetAllowsAntialiasing(context, YES);
69 CGContextDrawPath(context, kCGPathStroke);
70 CGPathRelease(fill_path);
71 }
72 }
73
74 - (void)setup:(CsoundObj *)csoundObj
75 {
76 tableLoaded = NO;
77 csObj = csoundObj;
78 fTableNumber = 1;
79 }
80
81 - (void)updataDisplayData
82 {
83 float scalingFactor = 0.9;
84 int width = self.frame.size.width;
85 int height = self.frame.size.height;
86 int middle = (height / 2);
87
88 displayData = malloc(sizeof(MYFLT) * width);
89
90 for(int i = 0; i < width; i++) {
91 float percent = i / (float)(width);
92 int index = (int)(percent * tableLength);
93 displayData[i] = (-(table[index] * middle * scalingFactor) + middle);
94 }
95
96 [self performSelectorOnMainThread:@selector(setNeedsDisplay)
97 withObject:nil
98 waitUntilDone:NO];
99 }
100
101 - (void)updateValuesFromCsound
102 {
103 if (!tableLoaded) {
104 CSOUND *cs = [csObj getCsound];
105
106 if ((tableLength = csoundTableLength(cs, fTableNumber)) > 0) {
107 table = malloc(tableLength * sizeof(MYFLT));
108 csoundGetTable(cs, &table, fTableNumber);
109 tableLoaded = YES;
110 [self performSelectorInBackground:@selector(updataDisplayData) withObject:nil];
111 }
112 }
113 }
114
115 @end