Skip to content

Add public on some visualizations views #61

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

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion Sources/AudioKitUI/Visualizations/FloatPlot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public class FloatPlot: MTKView, MTKViewDelegate {
texture1d<float, access::sample> waveform, device float* parameters, device float4* colorParameters) {
"""

init(frame frameRect: CGRect, fragment: String? = nil, dataCallback: @escaping () -> [Float]) {
public init(frame frameRect: CGRect,
fragment: String? = nil,
dataCallback: @escaping () -> [Float]) {
self.dataCallback = dataCallback
bufferSampleCount = Int(frameRect.width)

Expand Down
12 changes: 6 additions & 6 deletions Sources/AudioKitUI/Visualizations/FragmentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public class FragmentBuilder {
var isFilled: Bool = true
var isFFT: Bool = false

init(foregroundColor: CGColor = Color.white.cg,
backgroundColor: CGColor = Color.clear.cg,
isCentered: Bool = true,
isFilled: Bool = true,
isFFT: Bool = false)
public init(foregroundColor: CGColor = Color.white.cg,
backgroundColor: CGColor = Color.clear.cg,
isCentered: Bool = true,
isFilled: Bool = true,
isFFT: Bool = false)
{
self.foregroundColor = foregroundColor
self.backgroundColor = backgroundColor
Expand All @@ -36,7 +36,7 @@ public class FragmentBuilder {
self.isFFT = isFFT
}

var stringValue: String {
public var stringValue: String {
return """
float sample = waveform.sample(s, \(isFFT ? "(pow(10, in.t.x) - 1.0) / 9.0" : "in.t.x")).x;

Expand Down
39 changes: 28 additions & 11 deletions Sources/AudioKitUI/Visualizations/NodeRollingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ import AVFoundation
import SwiftUI

public class RollingViewData {
let bufferSampleCount = 128
var history = [Float](repeating: 0.0, count: 1024)
var framesToRMS = 128
let bufferSampleCount: UInt
let framesToRMS: UInt

func calculate(_ nodeTap: RawDataTap) -> [Float] {
private var history: [Float]

public init(bufferSampleCount: UInt = 128,
bufferSize: UInt,
framesToRMS: UInt = 128) {
self.bufferSampleCount = bufferSampleCount
self.framesToRMS = framesToRMS
history = [Float](repeating: 0.0, count: 1024)
}

public func calculate(_ nodeTap: RawDataTap) -> [Float] {
var framesToTransform = [Float]()

let signal = nodeTap.data

for j in 0 ..< bufferSampleCount / framesToRMS {
for i in 0 ..< framesToRMS {
framesToTransform.append(signal[i + j * framesToRMS])
framesToTransform.append(signal[Int(i + j * framesToRMS)])
}

var rms: Float = 0.0
Expand All @@ -32,14 +41,22 @@ public class RollingViewData {
}

public struct NodeRollingView: ViewRepresentable {
var nodeTap: RawDataTap
var metalFragment: FragmentBuilder
var rollingData = RollingViewData()

public init(_ node: Node, color: Color = .gray, bufferSize: Int = 1024) {
private let nodeTap: RawDataTap
private let metalFragment: FragmentBuilder
private let rollingData: RollingViewData

metalFragment = FragmentBuilder(foregroundColor: color.cg, isCentered: false, isFilled: false)
public init(_ node: Node,
color: Color = .gray,
backgroundColor: Color = .clear,
isCentered: Bool = false,
isFilled: Bool = false,
bufferSize: Int = 1024) {
metalFragment = FragmentBuilder(foregroundColor: color.cg,
backgroundColor: backgroundColor.cg,
isCentered: isCentered,
isFilled: isFilled)
nodeTap = RawDataTap(node, bufferSize: UInt32(bufferSize))
rollingData = RollingViewData(bufferSize: UInt(bufferSize))
}

var plot: FloatPlot {
Expand Down
30 changes: 24 additions & 6 deletions Sources/AudioKitUI/Visualizations/SpectrogramView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,30 @@ public struct SpectrogramView: View {
@StateObject var spectrogram = SpectrogramModel()
var node: Node

var linearGradient = LinearGradient(gradient: Gradient(colors: [.blue, .green, .yellow, .red]), startPoint: .bottom, endPoint: .top)
@State var strokeColor = Color.white.opacity(0.8)
@State var fillColor = Color.green.opacity(1.0)
@State var bottomColor = Color.white.opacity(0.5)
@State var sideColor = Color.white.opacity(0.2)
@State var backgroundColor = Color.black
var linearGradient: LinearGradient
@Binding var strokeColor: Color
@Binding var fillColor: Color
@Binding var bottomColor: Color
@Binding var sideColor: Color
@Binding var backgroundColor: Color

public init(node: Node,
linearGradient: LinearGradient = LinearGradient(gradient: .init(colors: [.blue, .green, .yellow, .red]),
startPoint: .bottom,
endPoint: .top),
strokeColor: Binding<Color> = .constant(Color.white.opacity(0.8)),
fillColor: Binding<Color> = .constant(Color.green.opacity(1.0)),
bottomColor: Binding<Color> = .constant(Color.white.opacity(0.5)),
sideColor: Binding<Color> = .constant(Color.white.opacity(0.2)),
backgroundColor: Binding<Color> = .constant(Color.black)) {
self.node = node
self.linearGradient = linearGradient
_strokeColor = strokeColor
_fillColor = fillColor
_bottomColor = bottomColor
_sideColor = sideColor
_backgroundColor = backgroundColor
}

public var body: some View {
let xOffset = CGFloat(0.22) / CGFloat(spectrogram.fftDataReadings.maxItems)
Expand Down