From 26aed2c6acd43826b72fe745840407765c3daa81 Mon Sep 17 00:00:00 2001 From: Jason Pepas Date: Thu, 14 Jan 2021 20:23:00 -0600 Subject: [PATCH] Code cleanup --- .../GridKeyboardViewController.swift | 163 ++++++++++-------- GridNotes/GridNotes/KeyRowView.swift | 36 ++-- GridNotes/GridNotes/Midi.swift | 10 +- .../GridNotes/SettingsViewController.swift | 4 +- 4 files changed, 123 insertions(+), 90 deletions(-) diff --git a/GridNotes/GridNotes/GridKeyboardViewController.swift b/GridNotes/GridNotes/GridKeyboardViewController.swift index 2dd30e1..4a7dfa3 100644 --- a/GridNotes/GridNotes/GridKeyboardViewController.swift +++ b/GridNotes/GridNotes/GridKeyboardViewController.swift @@ -8,11 +8,9 @@ import UIKit +/// The main piano view controller. class GridKeyboardViewController: UIViewController { - var rows: [KeyRowView] = [] - var toolbar: UIToolbar! - enum KeysPerOctave: String, CaseIterable { case chromaticKeys case diatonicKeys @@ -20,7 +18,7 @@ class GridKeyboardViewController: UIViewController { var name: String { switch self { case .chromaticKeys: - return "Chromatic (all keys)" + return "Chromatic (all 12 keys)" case .diatonicKeys: return "Diatonic (only in-scale keys)" } @@ -82,54 +80,68 @@ class GridKeyboardViewController: UIViewController { func set(model: Model) { self.model = model - _configureToolbar() - _configureKeyRows() + if isViewLoaded { + _reconfigureToolbar() + _reconfigureKeyRows() + } } // MARK: - Internals + private var _rows: [KeyRowView] = [] + private var _toolbar: UIToolbar! + override func viewDidLoad() { super.viewDidLoad() func assembleViewHierarchy() { - toolbar.translatesAutoresizingMaskIntoConstraints = false - view.addSubview(toolbar) + _toolbar.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(_toolbar) - for row in rows { + for row in _rows { row.translatesAutoresizingMaskIntoConstraints = false view.addSubview(row) } - view.topAnchor.constraint(equalTo: toolbar.topAnchor).isActive = true - view.leadingAnchor.constraint(equalTo: toolbar.leadingAnchor).isActive = true - view.trailingAnchor.constraint(equalTo: toolbar.trailingAnchor).isActive = true - toolbar.bottomAnchor.constraint(equalTo: rows.first!.topAnchor).isActive = true - toolbar.heightAnchor.constraint(equalToConstant: 44).isActive = true + // toolbar height. + _toolbar.heightAnchor.constraint(equalToConstant: 44).isActive = true + // pin the toolbar to the top and sides. + view.topAnchor.constraint(equalTo: _toolbar.topAnchor).isActive = true + view.leadingAnchor.constraint(equalTo: _toolbar.leadingAnchor).isActive = true + view.trailingAnchor.constraint(equalTo: _toolbar.trailingAnchor).isActive = true + + // pin the toolbar bottom the the first row of keys. + _toolbar.bottomAnchor.constraint(equalTo: _rows.first!.topAnchor).isActive = true + + // stack the key rows vertically. for i in 0..<(model.octaves.count-1) { - rows[i].bottomAnchor.constraint(equalTo: rows[i+1].topAnchor).isActive = true + _rows[i].bottomAnchor.constraint(equalTo: _rows[i+1].topAnchor).isActive = true } - for i in 1.. String { @@ -108,7 +116,7 @@ class KeyRowView: UIView { for subview in subviews { subview.removeFromSuperview() } - keys.removeAll() + _keys.removeAll() for (i, styledNote) in model.styledNotes.enumerated() { let key = UIButton(type: .system) @@ -152,7 +160,7 @@ class KeyRowView: UIView { key.backgroundColor = KeyRowView._shadedGray } - keys.append(key) + _keys.append(key) } _hasSetUpConstraints = false diff --git a/GridNotes/GridNotes/Midi.swift b/GridNotes/GridNotes/Midi.swift index bedada6..d6edf08 100644 --- a/GridNotes/GridNotes/Midi.swift +++ b/GridNotes/GridNotes/Midi.swift @@ -16,6 +16,7 @@ enum MIDICommand { static let patchChange: UInt32 = 0xC0 } +/// SoundFont instruments enum Instrument: String, CaseIterable { case yamahaGrandPiano = "Yamaha Grand Piano" case harpsichord = "Harpsichord" @@ -57,15 +58,16 @@ enum Instrument: String, CaseIterable { } } -var g_instrument: Instrument = .rhodesEP - +// Globals (don't @ me). +var g_instrument: Instrument = .rhodesEP var g_graph: AUGraph? var g_synthNode: AUNode = AUNode() var g_outputNode: AUNode = AUNode() var g_synthUnit: AudioUnit? +/// Start MIDI audio using the g_instrument SoundFont instrument. func initAudio() { // Allow audio output even when silent switch engaged. // Allow the piano to play mixed with the audio of other apps (e.g. youtube backing tracks). @@ -168,6 +170,7 @@ func initAudio() { } +/// Shutdown MIDI audio. func deinitAudio() { var ret: OSStatus @@ -182,6 +185,7 @@ func deinitAudio() { } +/// Play a MIDI note. func startPlaying(absoluteNote: AbsoluteNote) { var ret: OSStatus let channel: UInt32 = 0 @@ -192,6 +196,7 @@ func startPlaying(absoluteNote: AbsoluteNote) { } +/// Stop a MIDI note. func stopPlaying(absoluteNote: AbsoluteNote) { var ret: OSStatus let channel: UInt32 = 0 @@ -201,6 +206,7 @@ func stopPlaying(absoluteNote: AbsoluteNote) { } +/// Stop all MIDI notes. func stopPlayingAllNotes() { var ret: OSStatus let channel: UInt32 = 0 diff --git a/GridNotes/GridNotes/SettingsViewController.swift b/GridNotes/GridNotes/SettingsViewController.swift index 6e2ad10..cd94450 100644 --- a/GridNotes/GridNotes/SettingsViewController.swift +++ b/GridNotes/GridNotes/SettingsViewController.swift @@ -10,7 +10,7 @@ import UIKit class SettingsViewController: UITableViewController { - var model: GridKeyboardViewController.Model = GridKeyboardViewController.Model.defaultModel + private(set) var model: GridKeyboardViewController.Model = GridKeyboardViewController.Model.defaultModel func set(model: GridKeyboardViewController.Model) { self.model = model @@ -50,7 +50,7 @@ class SettingsViewController: UITableViewController { case _scaleSection: return "Scale" case _nonDiatonicSection: - return "Non-Diatonic Note Treatment" + return "Non-Diatonic (Out-of-Scale) Note Treatment" case _octaveKeysSection: return "Keys per Octave" case _stickySection: