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

Add analysisMode to AmplitudeView #77

Merged
merged 3 commits into from
Jan 17, 2024
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
53 changes: 44 additions & 9 deletions Sources/AudioKitUI/Visualizations/AmplitudeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AmplitudeModel: ObservableObject {
var nodeTap: AmplitudeTap!
var node: Node?
var stereoMode: StereoMode = .center
var analysisMode: AnalysisMode = .peak
@Environment(\.isPreview) var isPreview

init() {
Expand All @@ -19,7 +20,12 @@ class AmplitudeModel: ObservableObject {
func updateNode(_ node: Node) {
if node !== self.node {
self.node = node
nodeTap = AmplitudeTap(node, stereoMode: stereoMode, analysisMode: .peak, callbackQueue: .main) { amp in
nodeTap = AmplitudeTap(
node,
stereoMode: stereoMode,
analysisMode: analysisMode,
callbackQueue: .main
) { amp in
self.pushData(amp)
}
nodeTap.start()
Expand All @@ -43,26 +49,54 @@ public struct AmplitudeView: View {
@StateObject var amplitudeModel = AmplitudeModel()
let node: Node
let stereoMode: StereoMode
let analysisMode: AnalysisMode
let numberOfSegments: Int
let fillType: FillType

public init(_ node: Node, stereoMode: StereoMode = .center, numberOfSegments: Int = 20) {
let backgroundColor: Color

public init(
_ node: Node,
stereoMode: StereoMode = .center,
analysisMode: AnalysisMode = .peak,
backgroundColor: Color = .black,
numberOfSegments: Int = 20
) {
self.node = node
self.stereoMode = stereoMode
self.analysisMode = analysisMode
self.backgroundColor = backgroundColor
self.fillType = .gradient(gradient: Gradient(colors: [.red, .yellow, .green]))
self.numberOfSegments = numberOfSegments
}

public init(_ node: Node, color: Color, stereoMode: StereoMode = .center, numberOfSegments: Int = 20) {
public init(
_ node: Node,
color: Color,
stereoMode: StereoMode = .center,
analysisMode: AnalysisMode = .peak,
backgroundColor: Color = .black,
numberOfSegments: Int = 20
) {
self.node = node
self.stereoMode = stereoMode
self.analysisMode = analysisMode
self.backgroundColor = backgroundColor
self.fillType = .solid(color: color)
self.numberOfSegments = numberOfSegments
}

public init(_ node: Node, colors: Gradient, stereoMode: StereoMode = .center, numberOfSegments: Int = 20) {
public init(
_ node: Node,
colors: Gradient,
stereoMode: StereoMode = .center,
analysisMode: AnalysisMode = .peak,
backgroundColor: Color = .black,
numberOfSegments: Int = 20
) {
self.node = node
self.stereoMode = stereoMode
self.analysisMode = analysisMode
self.backgroundColor = backgroundColor
self.fillType = .gradient(gradient: colors)
self.numberOfSegments = numberOfSegments
}
Expand All @@ -88,15 +122,16 @@ public struct AmplitudeView: View {
// some are "on" or "off" - based on their opacity to create the colored regions
addSegments(width: geometry.size.width, height: geometry.size.height, numberOfBlackSegments: numberOfBlackSegments)
} else {
// simply cover a certain amount of the colored rectangle with black from the top
// simply cover a certain amount of the colored rectangle with the background color from the top
Rectangle()
.fill(Color.black)
.fill(self.backgroundColor)
.mask(Rectangle().padding(.bottom, geometry.size.height * CGFloat(amplitudeModel.amplitude)))
.animation(.linear(duration: 0.05), value: amplitudeModel.amplitude)
}
}
.onAppear {
amplitudeModel.stereoMode = stereoMode
amplitudeModel.analysisMode = analysisMode
amplitudeModel.updateNode(node)
}
}
Expand All @@ -113,7 +148,7 @@ public struct AmplitudeView: View {

if index != numberOfBlackSegments + 1 {
Rectangle()
.fill(Color.black)
.fill(self.backgroundColor)
.frame(height: spaceHeight)
}
addOpacityRectangle(height: solidHeight, index: index, n: numberOfBlackSegments)
Expand All @@ -127,7 +162,7 @@ public struct AmplitudeView: View {
let opacity = amplitudeModel.amplitude > Double(index - 1) / Double(n + 1) ? 0.0 : 1.0

return Rectangle()
.fill(Color.black)
.fill(self.backgroundColor)
.frame(height: height)
.opacity(opacity)
.animation(.linear(duration: 0.05), value: amplitudeModel.amplitude)
Expand Down
Loading