| 1 |
/* |
| 2 |
|
| 3 |
Waveview.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 |
|
| 28 |
import UIKit |
| 29 |
|
| 30 |
class Waveview: UIView { |
| 31 |
|
| 32 |
private var lastY: CGFloat = 0 |
| 33 |
private var displayData = [Float]() |
| 34 |
fileprivate var table: UnsafeMutablePointer<Float>? |
| 35 |
fileprivate var tableLength = 0 |
| 36 |
fileprivate var fTableNumber = 0 |
| 37 |
fileprivate var csObj = CsoundObj() |
| 38 |
fileprivate var tableLoaded = false |
| 39 |
|
| 40 |
override init(frame: CGRect) { |
| 41 |
super.init(frame: frame) |
| 42 |
displayData = [Float](repeatElement(Float(0), count: Int(frame.width))) // Init with 0s |
| 43 |
} |
| 44 |
|
| 45 |
required init?(coder aDecoder: NSCoder) { |
| 46 |
super.init(coder: aDecoder) |
| 47 |
displayData = [Float](repeatElement(Float32(0), count: Int(frame.width))) |
| 48 |
} |
| 49 |
|
| 50 |
func displayFTable(_ fTableNum: Int) { |
| 51 |
fTableNumber = fTableNum |
| 52 |
tableLoaded = false |
| 53 |
self.updateValuesFromCsound() |
| 54 |
} |
| 55 |
|
| 56 |
// Take in a pointer to a float and return a Swift array of values stored beginning at that memory location |
| 57 |
func ptoa(_ ptr: UnsafeMutablePointer<Float>, length: Int) -> [Float] { |
| 58 |
let bfr = UnsafeBufferPointer(start: ptr, count: length) |
| 59 |
return [Float](bfr) |
| 60 |
} |
| 61 |
|
| 62 |
// MARK: Drawing Code |
| 63 |
override func draw(_ rect: CGRect) { |
| 64 |
let context = UIGraphicsGetCurrentContext() |
| 65 |
context?.setFillColor(UIColor.black.cgColor) |
| 66 |
context?.fill(rect) |
| 67 |
|
| 68 |
if tableLoaded { |
| 69 |
context?.setStrokeColor(UIColor.white.cgColor) |
| 70 |
context?.setFillColor(UIColor.white.cgColor) |
| 71 |
let fill_path = CGMutablePath() |
| 72 |
let x: CGFloat = 0 |
| 73 |
let y: CGFloat = CGFloat(displayData[0]) |
| 74 |
|
| 75 |
fill_path.move(to: CGPoint(x: x, y: y), transform: .identity) |
| 76 |
|
| 77 |
for i in 0 ..< displayData.count { |
| 78 |
fill_path.addLine(to: CGPoint(x: CGFloat(i), y: CGFloat(displayData[i])), transform: .identity) |
| 79 |
} |
| 80 |
|
| 81 |
context?.addPath(fill_path) |
| 82 |
context?.setAllowsAntialiasing(true) |
| 83 |
context?.drawPath(using: .stroke) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Update values from F-table in displayData array |
| 88 |
func updateDisplayData() { |
| 89 |
let scalingFactor: Float32 = 0.9 |
| 90 |
let width = self.frame.size.width |
| 91 |
let height = self.frame.size.height |
| 92 |
let middle: Float32 = Float32(height / 2.0) |
| 93 |
|
| 94 |
if table != nil { |
| 95 |
let valTable = ptoa(table!, length: tableLength) |
| 96 |
displayData = [Float](repeatElement(Float(0), count: Int(width))) |
| 97 |
|
| 98 |
for i in 0 ..< displayData.count { |
| 99 |
let percent: Float = Float(i)/Float(width) |
| 100 |
let index: Int = Int(percent * Float(tableLength)) |
| 101 |
if table != nil { |
| 102 |
displayData[i] = (-(valTable[index] * middle * scalingFactor) + middle) |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
DispatchQueue.main.async { [unowned self] in |
| 108 |
self.setNeedsDisplay() |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
extension Waveview: CsoundBinding { |
| 114 |
func setup(_ csoundObj: CsoundObj!) { |
| 115 |
tableLoaded = false |
| 116 |
csObj = csoundObj |
| 117 |
fTableNumber = 1 |
| 118 |
} |
| 119 |
|
| 120 |
// Update F-table values from Csound |
| 121 |
func updateValuesFromCsound() { |
| 122 |
if !tableLoaded { |
| 123 |
let cs = csObj.getCsound() |
| 124 |
tableLength = Int(csoundTableLength(cs, Int32(fTableNumber))) |
| 125 |
if tableLength > 0 { |
| 126 |
table = UnsafeMutablePointer<Float>.allocate(capacity: tableLength) |
| 127 |
csoundGetTable(cs, &table, Int32(fTableNumber)) |
| 128 |
tableLoaded = true |
| 129 |
} |
| 130 |
|
| 131 |
DispatchQueue.global(qos: .background).async { [unowned self] in |
| 132 |
self.updateDisplayData() |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |