Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Cookbook MIDI Track Demo #82

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions Cookbook/Cookbook/Recipes/MIDITrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,48 @@ import AudioKitUI
import SwiftUI

struct MIDITrackDemo: View {
@StateObject var viewModel = MIDITrackViewModel()
@State var fileURL: URL? = Bundle.main.url(forResource: "Demo", withExtension: "mid")!
@State var isPlaying = false
public var body: some View {
ScrollView {
VStack {
GeometryReader { geometry in
MIDITrackView(trackWidth: geometry.size.width - 20,
trackHeight: 200.0,
fileURL: Bundle.main.url(forResource: "Demo", withExtension: "mid")!,
noteZoom: 100_000)
ScrollView {
if let fileURL = fileURL {
ForEach(
MIDIFile(url: fileURL).tracks.indices, id: \.self) { number in
MIDITrackView(fileURL: $fileURL,
trackNumber: number,
trackWidth: geometry.size.width,
trackHeight: 200.0
)
.background(Color.primary)
.cornerRadius(10.0)
}
}
}
}
}
.padding()
.navigationBarTitle(Text("MIDI Track View"))
.onTapGesture {
isPlaying.toggle()
}
.onChange(of: isPlaying, perform: { newValue in
if newValue == true {
viewModel.play()
} else {
viewModel.stop()
}
})
.onAppear(perform: {
viewModel.startEngine()
viewModel.loadSequencerFile(fileURL: Bundle.main.url(forResource: "Demo", withExtension: "mid")!)
})
.onDisappear(perform: {
viewModel.stop()
viewModel.stopEngine()
})
.environmentObject(viewModel)
}
}