Skip to content

Commit

Permalink
fix: for AudioKit 5.6.0 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushairer authored Mar 5, 2023
1 parent faa1a53 commit 2a2599b
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 40 deletions.
40 changes: 19 additions & 21 deletions Sources/AudioKitUI/Visualizations/AmplitudeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@ class AmplitudeModel: ObservableObject {
var node: Node?
var stereoMode: StereoMode = .center
@Environment(\.isPreview) var isPreview

init() {
if isPreview {
mockAmplitudeChange()
}
}

func updateNode(_ node: Node) {
if node !== self.node {
self.node = node
nodeTap = AmplitudeTap(node, stereoMode: stereoMode, analysisMode: .peak) { amp in
DispatchQueue.main.async {
self.pushData(amp)
}
nodeTap = AmplitudeTap(node, stereoMode: stereoMode, analysisMode: .peak, callbackQueue: .main) { amp in
self.pushData(amp)
}
nodeTap.start()
}
}

func pushData(_ amp: Float) {
amplitude = Double(amp)
}

func mockAmplitudeChange() {
amplitude = Double.random(in: 0...1.0)
let waitTime: TimeInterval = 0.1
Expand All @@ -47,32 +45,32 @@ public struct AmplitudeView: View {
let stereoMode: StereoMode
let numberOfSegments: Int
let fillType: FillType

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

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

public init(_ node: Node, colors: Gradient, stereoMode: StereoMode = .center, numberOfSegments: Int = 20) {
self.node = node
self.stereoMode = stereoMode
self.fillType = .gradient(gradient: colors)
self.numberOfSegments = numberOfSegments
}

public var body: some View {
let isClipping = amplitudeModel.amplitude >= 1.0 ? true : false
let numberOfBlackSegments = numberOfSegments - 1

GeometryReader { geometry in
ZStack(alignment: .bottom) {
// colored rectangle in the back
Expand All @@ -83,7 +81,7 @@ public struct AmplitudeView: View {
Rectangle()
.fill(Color.red)
}

if numberOfSegments > 1 {
// draw rectangles in front of the colored rectangle
// some are constant black to create the segments
Expand All @@ -104,15 +102,15 @@ public struct AmplitudeView: View {
}
.drawingGroup()
}

func addSegments(width _: CGFloat, height: CGFloat, numberOfBlackSegments: Int) -> some View {
let splitHeight = height / CGFloat(numberOfBlackSegments + 1)
let solidHeight = splitHeight * (2.0 / 3.0)
let spaceHeight = splitHeight * (1.0 / 3.0) + splitHeight * (1.0 / 3.0) / CGFloat(numberOfBlackSegments)

return VStack(spacing: 0.0) {
ForEach((1 ... numberOfBlackSegments + 1).reversed(), id: \.self) { index in

if index != numberOfBlackSegments + 1 {
Rectangle()
.fill(Color.black)
Expand All @@ -123,11 +121,11 @@ public struct AmplitudeView: View {
}
}
}

// these sit in front of the color rectangles and are either on or off (opacity used for animating)
func addOpacityRectangle(height: CGFloat, index: Int, n: Int) -> some View {
let opacity = amplitudeModel.amplitude > Double(index - 1) / Double(n + 1) ? 0.0 : 1.0

return Rectangle()
.fill(Color.black)
.frame(height: height)
Expand All @@ -140,10 +138,10 @@ struct AmplitudeView_Previews: PreviewProvider {
static var previews: some View {
AmplitudeView(Mixer(), numberOfSegments: 1)
.previewLayout(.fixed(width: 40, height: 500))

AmplitudeView(Mixer(), numberOfSegments: 20)
.previewLayout(.fixed(width: 40, height: 500))

AmplitudeView(Mixer(), color: .blue, numberOfSegments: 20)
.previewLayout(.fixed(width: 40, height: 500))
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/AudioKitUI/Visualizations/FFTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ class FFTModel: ObservableObject {
func updateNode(_ node: Node, fftValidBinCount: FFTValidBinCount? = nil) {
if node !== self.node {
self.node = node
nodeTap = FFTTap(node, fftValidBinCount: fftValidBinCount) { fftData in
DispatchQueue.main.async {
self.updateAmplitudes(fftData)
}
nodeTap = FFTTap(node, fftValidBinCount: fftValidBinCount, callbackQueue: .main) { fftData in
self.updateAmplitudes(fftData)
}
nodeTap.isNormalized = false
nodeTap.start()
Expand Down
2 changes: 1 addition & 1 deletion Sources/AudioKitUI/Visualizations/NodeFFTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct NodeFFTView: ViewRepresentable {
let backgroundColorAddress = 1

public init(_ node: Node) {
nodeTap = FFTTap(node, bufferSize: UInt32(bufferSampleCount)) { _ in }
nodeTap = FFTTap(node, bufferSize: UInt32(bufferSampleCount), callbackQueue: .main) { _ in }
}

internal var plot: FloatPlot {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AudioKitUI/Visualizations/NodeOutputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public struct NodeOutputView: ViewRepresentable {
backgroundColor: backgroundColor.cg,
isCentered: true,
isFilled: false)
nodeTap = RawDataTap(node, bufferSize: UInt32(bufferSize))
nodeTap = RawDataTap(node, bufferSize: UInt32(bufferSize), callbackQueue: .main)
}

var plot: FloatPlot {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AudioKitUI/Visualizations/NodeRollingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct NodeRollingView: ViewRepresentable {
backgroundColor: backgroundColor.cg,
isCentered: isCentered,
isFilled: isFilled)
nodeTap = RawDataTap(node, bufferSize: bufferSize)
nodeTap = RawDataTap(node, bufferSize: bufferSize, callbackQueue: .main)
rollingData = RollingViewData(bufferSize: bufferSize)
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/AudioKitUI/Visualizations/RawOutputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ class RawOutputModel: ObservableObject {
if node !== self.node {
self.node = node
self.bufferSize = bufferSize
nodeTap = RawDataTap(node, bufferSize: UInt32(bufferSize)) { rawAudioData in
DispatchQueue.main.async {
self.updateData(rawAudioData.map { CGFloat($0) })
}
nodeTap = RawDataTap(node, bufferSize: UInt32(bufferSize), callbackQueue: .main) { rawAudioData in
self.updateData(rawAudioData.map { CGFloat($0) })
}
nodeTap.start()
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/AudioKitUI/Visualizations/SpectrogramView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ class SpectrogramModel: ObservableObject {
func updateNode(_ node: Node) {
if node !== self.node {
self.node = node
nodeTap = FFTTap(node, bufferSize: UInt32(FFT_SIZE * 2)) { fftData in
DispatchQueue.main.async {
self.pushData(fftData)
}
nodeTap = FFTTap(node, bufferSize: UInt32(FFT_SIZE * 2), callbackQueue: .main) { fftData in
self.pushData(fftData)
}
nodeTap.isNormalized = false
nodeTap.zeroPaddingFactor = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ class SpectrumModel: ObservableObject {
func updateNode(_ node: Node) {
if node !== self.node {
self.node = node
nodeTap = FFTTap(node, bufferSize: UInt32(FFT_SIZE * 2)) { fftData in
DispatchQueue.main.async {
self.pushData(fftData)
}
nodeTap = FFTTap(node, bufferSize: UInt32(FFT_SIZE * 2), callbackQueue: .main) { fftData in
self.pushData(fftData)
}
nodeTap.isNormalized = false
nodeTap.zeroPaddingFactor = 1
Expand Down

0 comments on commit 2a2599b

Please sign in to comment.