Debian Codegraph @

iOS/Csound iOS Swift Examples/Csound iOS SwiftExamples/LevelMeterView.swift

raw
1 /*
2
3 LevelMeterView.swift
4
5 Nikhil Singh, Dr. Richard Boulanger
6 Adapted from the Csound iOS Examples by Steven Yi and Victor Lazzarini
7
8 This file is part of Csound iOS SwiftExamples.
9
10 The Csound for iOS Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
14
15 Csound is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with Csound; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA
24
25 */
26
27 import UIKit
28
29 class LevelMeterView: UIView {
30
31 private var lastY: CGFloat = -100
32 private var count = 0
33 fileprivate var count2 = 0
34 fileprivate var channelValue: Float = 0
35 fileprivate var channelPtr: UnsafeMutablePointer<Float>?
36 fileprivate var ksmps = 0
37 fileprivate var sr = 0
38
39 var channelName = ""
40
41 override func draw(_ rect: CGRect) {
42 let clipPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 10, height: 10))
43 clipPath.addClip()
44
45 let context = UIGraphicsGetCurrentContext()
46 let colorSpace = CGColorSpaceCreateDeviceRGB()
47
48 context?.translateBy(x: 0, y: rect.size.height)
49 context?.scaleBy(x: 1, y: -1) // Invert Y Axis
50
51 let grayComponents: [CGFloat] = [0.7, 0.7, 0.7, 1.0]
52 let grayColor = CGColor(colorSpace: colorSpace, components: grayComponents)
53 context?.setFillColor(grayColor!)
54 context?.fill(self.bounds)
55
56 let width = rect.maxX
57 let height = rect.maxY
58 let squareWidth = width - 10
59 let squareHeight = height/30.0
60
61 // Define colors
62 let greenComponents: [CGFloat] = [0, 1, 0, 1]
63 let yellowComponents: [CGFloat] = [221.0/250.0, 223.0/250.0, 14.0/250.0, 1.0]
64 let redComponents: [CGFloat] = [1, 0, 0, 1]
65
66 let greenColor = CGColor(colorSpace: colorSpace, components: greenComponents)
67 let yellowColor = CGColor(colorSpace: colorSpace, components: yellowComponents)
68 let redColor = CGColor(colorSpace: colorSpace, components: redComponents)
69
70 let x: CGFloat = 12
71
72 if lastY < (height * 0.7) {
73 context?.setFillColor(greenColor!)
74 } else if lastY < (height * 0.9) {
75 context?.setFillColor(yellowColor!)
76 } else {
77 context?.setFillColor(redColor!)
78 }
79
80 context?.move(to: CGPoint(x: x, y: lastY))
81 context?.addLine(to: CGPoint(x: x, y: lastY + squareHeight))
82 context?.addLine(to: CGPoint(x: x + squareWidth - 12.0, y: lastY + squareHeight))
83 context?.addLine(to: CGPoint(x: x + squareWidth - 12.0, y: lastY))
84 context?.fillPath()
85
86 var y: CGFloat = 12
87
88 while y < (height * CGFloat(channelValue)) {
89
90 if (y < (height * 0.7)) {
91 context?.setFillColor(greenColor!)
92 } else if ((y < height * 0.9)) {
93 context?.setFillColor(yellowColor!)
94 } else {
95 context?.setFillColor(redColor!)
96 }
97
98 y += squareHeight + 5
99
100 context?.move(to: CGPoint(x: x, y: y))
101 context?.addLine(to: CGPoint(x: x, y: y + squareHeight))
102 context?.addLine(to: CGPoint(x: x + squareWidth - 12.0, y: y + squareHeight))
103 context?.addLine(to: CGPoint(x: x + squareWidth - 12.0, y: y))
104 context?.fillPath()
105 }
106
107 if y > lastY {
108 lastY = y
109 }
110
111 if count % 100 == 0 {
112 lastY = -squareHeight
113 }
114 count += 1
115 }
116
117 override init(frame: CGRect) {
118 super.init(frame: frame)
119 }
120
121 required init?(coder aDecoder: NSCoder) {
122 super.init(coder: aDecoder)
123 }
124
125 func add(to csoundObj: CsoundObj, for channel: String) {
126 csoundObj.addBinding(self)
127 channelName = channel
128 }
129
130 func cleanup() {
131 channelValue = 0
132 lastY = -100
133 setNeedsDisplay()
134 }
135 }
136
137 extension LevelMeterView: CsoundBinding {
138 func setup(_ csoundObj: CsoundObj!) {
139 channelPtr = csoundObj.getOutputChannelPtr(channelName, channelType: CSOUND_AUDIO_CHANNEL)
140 let cs = csoundObj.getCsound()
141 sr = Int(csoundGetSr(cs))
142 ksmps = Int(csoundGetKsmps(cs))
143 }
144
145 func updateValuesFromCsound() {
146 if channelPtr != nil {
147 channelValue = fabsf((channelPtr?.pointee)!)
148 }
149
150 if count2 % ((sr/ksmps)/20) == 0 {
151 DispatchQueue.main.async { [unowned self] in
152 self.setNeedsDisplay()
153 }
154 }
155 count2 += 1
156
157 if count2 > Int.max {
158 count2 -= Int.max
159 }
160 }
161 }