Skip to content

Commit

Permalink
Merge pull request #77 from hoopes/add-analysis-mode-to-amplitude-view
Browse files Browse the repository at this point in the history
Add analysisMode to AmplitudeView
  • Loading branch information
emurray2 committed Jan 17, 2024
2 parents a5b6c27 + 9459fc0 commit 16a012e
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions Sources/AudioKitUI/Visualizations/AmplitudeView.swift
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

0 comments on commit 16a012e

Please sign in to comment.