forked from Aayush9029/Miano
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All the instruments have been linked with the detail view. UI improvements. Proper engine startup and close function. Keyboard support for drum pad added.
- Loading branch information
1 parent
54ba111
commit b13dc4c
Showing
21 changed files
with
411 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Haptics+Extensions.swift | ||
// Miano | ||
// | ||
// Created by Aayush Pokharel on 2023-05-30. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension View { | ||
func performHapticFeedback(_ feedback: NSHapticFeedbackManager.FeedbackPattern = .generic) { | ||
NSHapticFeedbackManager.defaultPerformer.perform( | ||
feedback, | ||
performanceTime: .now | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// | ||
// DrumsConductor.swift | ||
// Miano | ||
// | ||
// Created by Aayush Pokharel on 2023-05-30. | ||
// | ||
|
||
import AudioKit | ||
import AudioKitEX | ||
import AudioKitUI | ||
import AVFoundation | ||
import Combine | ||
import SwiftUI | ||
|
||
struct DrumSample { | ||
var name: String | ||
var fileName: String | ||
var midiNote: Int | ||
var audioFile: AVAudioFile? | ||
var color: Color | ||
var key: KeyEquivalent | ||
|
||
init(_ prettyName: String, file: String, note: Int, _ drumColor: Color = Color.red, keyboardKey: KeyEquivalent) { | ||
name = prettyName | ||
fileName = file | ||
midiNote = note | ||
color = drumColor | ||
key = keyboardKey | ||
|
||
guard let url = Bundle.main.resourceURL?.appendingPathComponent(file) else { return } | ||
|
||
do { | ||
audioFile = try AVAudioFile(forReading: url) | ||
} catch { | ||
Log("Could not load: \(fileName)") | ||
} | ||
} | ||
} | ||
|
||
class DrumsConductor: ObservableObject, HasAudioEngine { | ||
// Mark Published so View updates label on changes | ||
@Published private(set) var lastPlayed: String = "None" | ||
|
||
let engine = AudioEngine() | ||
|
||
let drumSamples: [DrumSample] = | ||
[ | ||
DrumSample( | ||
"OPEN HI HAT", file: "open_hi_hat_A#1.wav", | ||
note: 34, .pink, | ||
keyboardKey: "t" | ||
), | ||
DrumSample( | ||
"HI TOM", file: "hi_tom_D2.wav", | ||
note: 38, .orange, | ||
keyboardKey: "y" | ||
), | ||
DrumSample( | ||
"MID TOM", file: "mid_tom_B1.wav", | ||
note: 35, .orange, | ||
keyboardKey: "u" | ||
), | ||
DrumSample( | ||
"LO TOM", file: "lo_tom_F1.wav", | ||
note: 29, .orange, | ||
keyboardKey: "i" | ||
), | ||
DrumSample( | ||
"HI HAT", file: "closed_hi_hat_F#1.wav", | ||
note: 30, .pink, | ||
keyboardKey: "g" | ||
), | ||
DrumSample( | ||
"CLAP", file: "clap_D#1.wav", | ||
note: 27, .blue, | ||
keyboardKey: "h" | ||
), | ||
DrumSample( | ||
"SNARE", file: "snare_D1.wav", | ||
note: 26, .indigo, | ||
keyboardKey: "j" | ||
), | ||
DrumSample( | ||
"KICK", file: "bass_drum_C1.wav", | ||
note: 24, .mint, | ||
keyboardKey: "k" | ||
), | ||
] | ||
|
||
let drums = AppleSampler() | ||
|
||
func playPad(padNumber: Int) { | ||
drums.play(noteNumber: MIDINoteNumber(drumSamples[padNumber].midiNote)) | ||
let fileName = drumSamples[padNumber].fileName | ||
lastPlayed = fileName.components(separatedBy: "/").last! | ||
} | ||
|
||
init() { | ||
engine.output = drums | ||
do { | ||
let files = drumSamples.map { | ||
$0.audioFile! | ||
} | ||
try drums.loadAudioFiles(files) | ||
|
||
} catch { | ||
Log("Files Didn't Load") | ||
} | ||
} | ||
|
||
// Refreshing the visualizer UI | ||
@Published var running: Int = 0 | ||
|
||
func start() { | ||
print("RUNNING INSTANCES \(running)") | ||
running += 1 | ||
try? engine.start() | ||
} | ||
|
||
func stop() { | ||
running -= 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.