Skip to content

Commit

Permalink
FFTView animation warning and preview mock data (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt54 committed Oct 9, 2022
1 parent ea175bd commit cd6964e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Sources/AudioKitUI/Visualizations/FFTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class FFTModel: ObservableObject {
var maxAmplitude: Float = 0.0
var minAmplitude: Float = -70.0
var referenceValueForFFT: Float = 12.0
@Environment(\.isPreview) var isPreview

init() {
if isPreview {
mockAudioInput()
}
}

func updateNode(_ node: Node, fftValidBinCount: FFTValidBinCount? = nil) {
if node !== self.node {
Expand Down Expand Up @@ -55,6 +62,18 @@ class FFTModel: ObservableObject {
self.amplitudes = decibels
}
}

func mockAudioInput() {
var mockFloats = [Float]()
for _ in 0...65 {
mockFloats.append(Float.random(in: 0...0.1))
}
updateAmplitudes(mockFloats)
let waitTime: TimeInterval = 0.1
DispatchQueue.main.asyncAfter(deadline: .now() + waitTime) {
self.mockAudioInput()
}
}
}

public struct FFTView: View {
Expand Down Expand Up @@ -166,7 +185,7 @@ struct AmplitudeBar: View {
Rectangle()
.fill(backgroundColor)
.mask(Rectangle().padding(.bottom, geometry.size.height * CGFloat(amplitude)))
.animation(.easeOut(duration: 0.15))
.animation(.easeOut(duration: 0.15), value: amplitude)

// White bar with slower animation for floating effect
if includeCaps {
Expand All @@ -190,6 +209,6 @@ struct AmplitudeBar: View {
.fill(Color.white)
.frame(height: capHeight)
.offset(x: 0.0, y: -height > capOffset - capHeight ? capMaxOffset : capOffset) // prevents offset from pushing cap outside of its frame
.animation(.easeOut(duration: 0.6))
.animation(.easeOut(duration: 0.6), value: amplitude)
}
}

0 comments on commit cd6964e

Please sign in to comment.