Skip to content

Commit

Permalink
Made it compatible with AudioKitUI Namespace and OS availability
Browse files Browse the repository at this point in the history
  • Loading branch information
mahal committed Mar 5, 2024
1 parent 887d552 commit ae39564
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ import SwiftUI
/// a central feature in your app. Furthermore it's not scientificicly correct, when displaying
/// white noise, it will not show a uniform distribution.

// might be easy to make available in earlier versions, primarly because of .onChange(of:
@available(iOS 17.0, *)
public struct SpectrogramFlatView: View {
// this static var is a shortcut: better to have this in SpectrogramModel or SpectrogramFFTMetaData
public static var gradientUIColors: [UIColor] = [(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)), (#colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 0.6275583187)), (#colorLiteral(red: 0.4217140079, green: 0.6851614118, blue: 0.9599093795, alpha: 0.8245213468)), (#colorLiteral(red: 0.8122602105, green: 0.6033009887, blue: 0.8759307861, alpha: 1)), (#colorLiteral(red: 0.9826132655, green: 0.5594901443, blue: 0.4263145328, alpha: 1)), (#colorLiteral(red: 1, green: 0.2607713342, blue: 0.4242972136, alpha: 1))]
@StateObject var spectrogram = SpectrogramModel()
@StateObject var spectrogram = SpectrogramFlatModel()
let node: Node
let backgroundColor: Color

Expand Down Expand Up @@ -93,7 +95,8 @@ public struct SpectrogramFlatView: View {

// MARK: Preview

struct SpectrogramView_Previews: PreviewProvider {
@available(iOS 17.0, *)
struct SpectrogramFlatView_Previews: PreviewProvider {
static var previews: some View {
return SpectrogramFlatView(node: Mixer())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct SpectrogramFFTMetaData {
var sampleRate: double_t = 44100
}

@available(iOS 17.0, *)
struct SliceQueue {
var maxItems: Int = 120
var items: [SpectrogramSlice] = []
Expand All @@ -43,7 +44,8 @@ struct SliceQueue {
}

/// Model for the SpectrogramFlatView. Makes connection to the audio node and receives FFT data
class SpectrogramModel: ObservableObject {
@available(iOS 17.0, *)
class SpectrogramFlatModel: ObservableObject {
@Published var slices = SliceQueue()
var sliceSize = CGSize(width: 10, height: 250) {
didSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SwiftUI
/// Resulting image has an integral size (dimensions in Int), so they are most of
/// the time a bit smaller than requested. This is because they are drawn in
/// a CGContext that doesn't have fractions of pixels to draw.
@available(iOS 17.0, *)
struct SpectrogramSlice: View, Identifiable {
static var counterSinceStart = 0
let id: Int
Expand Down Expand Up @@ -227,21 +228,25 @@ struct SpectrogramSlice: View, Identifiable {
}
}

#Preview {
// This shows the wrong behaviour of the slice: the lowest frequency isn't shown, the
// lowest amplitude below -200 should be black but is white.
SpectrogramSlice(gradientUIColors:
[(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)), (#colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)), (#colorLiteral(red: 0.4217140079, green: 0.6851614118, blue: 0.9599093795, alpha: 1)), (#colorLiteral(red: 0.8122602105, green: 0.6033009887, blue: 0.8759307861, alpha: 1)), (#colorLiteral(red: 0.9826132655, green: 0.5594901443, blue: 0.4263145328, alpha: 1)), (#colorLiteral(red: 1, green: 0.2607713342, blue: 0.4242972136, alpha: 1))],
sliceWidth: 40, sliceHeight: 150,
fftReadingsAsTupels:[
CGPoint(x: 150, y: -80),
CGPoint(x: 350, y: -50),
CGPoint(x: 500, y: -10),
CGPoint(x: 1000, y: -160),
CGPoint(x: 1500, y: -260),
CGPoint(x: 2000, y: -120),
CGPoint(x: 3000, y: -80)],
spectrogramMinFreq: 140, spectrogramMaxFreq: 4000,
fftMetaData: SpectrogramFFTMetaData()
).scaleEffect(x: 1, y: -1)
// MARK: Preview
@available(iOS 17.0, *)
struct SpectrogramSlice_Previews: PreviewProvider {
static var previews: some View {
// This shows the wrong behaviour of the slice: the lowest frequency isn't shown, the
// lowest amplitude below -200 should be black but is white.
return SpectrogramSlice(gradientUIColors:
[(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)), (#colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)), (#colorLiteral(red: 0.4217140079, green: 0.6851614118, blue: 0.9599093795, alpha: 1)), (#colorLiteral(red: 0.8122602105, green: 0.6033009887, blue: 0.8759307861, alpha: 1)), (#colorLiteral(red: 0.9826132655, green: 0.5594901443, blue: 0.4263145328, alpha: 1)), (#colorLiteral(red: 1, green: 0.2607713342, blue: 0.4242972136, alpha: 1))],
sliceWidth: 40, sliceHeight: 150,
fftReadingsAsTupels:[
CGPoint(x: 150, y: -80),
CGPoint(x: 350, y: -50),
CGPoint(x: 500, y: -10),
CGPoint(x: 1000, y: -160),
CGPoint(x: 1500, y: -260),
CGPoint(x: 2000, y: -120),
CGPoint(x: 3000, y: -80)],
spectrogramMinFreq: 140, spectrogramMaxFreq: 4000,
fftMetaData: SpectrogramFFTMetaData()
).scaleEffect(x: 1, y: -1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UIKit
/// let color = [.green, .yellow, .red].intermediate(0.7)
/// inspired by https://stackoverflow.com/questions/15032562/ios-find-color-at-point-between-two-colors/59996029#59996029
extension Array where Element: UIColor {
func intermediate(_ percentage: CGFloat) -> UIColor {
public func intermediate(_ percentage: CGFloat) -> UIColor {
let percentage = Swift.max(Swift.min(percentage, 1), 0)
switch percentage {
case 0: return first ?? .clear
Expand Down

0 comments on commit ae39564

Please sign in to comment.