Skip to content

Commit

Permalink
#64 - Workaround for Spectrogram band 0
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Sep 30, 2023
1 parent c9b91e6 commit 9ff56b0
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildLocationStyle</key>
<string>UseAppPreferences</string>
<key>CustomBuildLocationType</key>
<string>RelativeToDerivedData</string>
<key>DerivedDataLocationStyle</key>
<string>Default</string>
<key>ShowSharedSchemesAutomaticallyEnabled</key>
<true/>
</dict>
</plist>
2 changes: 2 additions & 0 deletions Source/ObjectGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class ObjectGraph {

let mediaKeyHandler: MediaKeyHandler

let fft: FFT = FFT()

@available(OSX 10.12.2, *)
lazy var remoteControlManager: RemoteControlManager = RemoteControlManager(playbackInfo: playbackInfoDelegate, audioGraph: audioGraphDelegate,
sequencer: sequencerDelegate, preferences: preferences)
Expand Down
4 changes: 2 additions & 2 deletions Source/UI/Visualizer/FFT/FFT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class FFT: Destroyable {
windowSize = bufferSizePOT
windowSize_vDSPLength = vDSP_Length(windowSize)

transferBuffer = UnsafeMutablePointer<Float>.allocate(capacity: windowSize)
transferBuffer = .allocate(capacity: windowSize)
window = [Float](repeating: 0, count: windowSize)

magnitudes = [Float](repeating: 0, count: halfBufferSize)
normalizedMagnitudes = UnsafeMutablePointer<Float>.allocate(capacity: halfBufferSize)
normalizedMagnitudes = .allocate(capacity: halfBufferSize)
}

func analyze(buffer: AudioBufferList) {
Expand Down
12 changes: 10 additions & 2 deletions Source/UI/Visualizer/FFT/SpectrogramFFTData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ class SpectrogramFFTData {

func update(with fft: FFT) {

for band in bands {
let alternateLogicForBand0: Bool = fft.bufferSize < objectGraph.audioGraph.visualizationAnalysisBufferSize

for (index, band) in bands.enumerated() {

if alternateLogicForBand0, index == 0 {
maxVal = (fft.normalizedMagnitudes[0] + fft.normalizedMagnitudes[1]) / 2

} else {
vDSP_maxv(fft.normalizedMagnitudes.advanced(by: band.minIndex), 1, &maxVal, band.indexCount)
}

vDSP_maxv(fft.normalizedMagnitudes.advanced(by: band.minIndex), 1, &maxVal, band.indexCount)
band.maxVal = maxVal.clamp(to: fft.magnitudeRange)
}
}
Expand Down
4 changes: 4 additions & 0 deletions Source/UI/Visualizer/Views/DiscoBall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class DiscoBall: AuralSCNView, VisualizerViewProtocol {

let textureImage: NSImage = NSImage(named: "DiscoBall")!

func setUp(with fft: FFT) {
data.setUp(for: fft)
}

func presentView(with fft: FFT) {

data.setUp(for: fft)
Expand Down
4 changes: 4 additions & 0 deletions Source/UI/Visualizer/Views/Spectrogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class Spectrogram: SKView, VisualizerViewProtocol {
}
}

func setUp(with fft: FFT) {
data.setUp(fft: fft, numberOfBands: numberOfBands)
}

func presentView(with fft: FFT) {

data.setUp(fft: fft, numberOfBands: numberOfBands)
Expand Down
4 changes: 4 additions & 0 deletions Source/UI/Visualizer/Views/Supernova.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Supernova: SKView, VisualizerViewProtocol {

private var glowWidth: CGFloat = 50

func setUp(with fft: FFT) {
data.setUp(for: fft)
}

func presentView(with fft: FFT) {

data.setUp(for: fft)
Expand Down
2 changes: 2 additions & 0 deletions Source/UI/Visualizer/Views/VisualizerViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protocol VisualizerViewProtocol {

func dismissView()

func setUp(with fft: FFT)

func update(with fft: FFT)

func setColors(startColor: NSColor, endColor: NSColor)
Expand Down
6 changes: 5 additions & 1 deletion Source/UI/Visualizer/Visualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typealias VisualizerRenderCallback = () -> Void
class Visualizer: AudioGraphRenderObserverProtocol, Destroyable {

// Fast Fourier Transform
let fft: FFT = FFT()
let fft: FFT = objectGraph.fft

private var audioGraph: AudioGraphDelegateProtocol = objectGraph.audioGraphDelegate
private var normalDeviceBufferSize: Int = 0
Expand Down Expand Up @@ -65,7 +65,11 @@ class Visualizer: AudioGraphRenderObserverProtocol, Destroyable {
normalDeviceBufferSize = newDeviceBufferSize

if newDeviceBufferSize != audioGraph.visualizationAnalysisBufferSize {

audioGraph.outputDeviceBufferSize = audioGraph.visualizationAnalysisBufferSize

fft.setUp(sampleRate: Float(audioGraph.outputDeviceSampleRate),
bufferSize: audioGraph.outputDeviceBufferSize)
}
}

Expand Down
17 changes: 17 additions & 0 deletions Source/UI/Visualizer/VisualizerWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class VisualizerWindowController: NSWindowController, NSWindowDelegate, Destroya

private lazy var uiState: VisualizerUIState = objectGraph.visualizerUIState

private(set) lazy var messenger = Messenger(for: self)

override func windowDidLoad() {

super.windowDidLoad()
messenger.subscribeAsync(to: .audioGraph_outputDeviceChanged, handler: audioOutputDeviceChanged)
}

override func awakeFromNib() {

window?.delegate = self
Expand All @@ -58,6 +66,7 @@ class VisualizerWindowController: NSWindowController, NSWindowDelegate, Destroya

close()
visualizer.destroy()
messenger.unsubscribeFromAll()
}

override func showWindow(_ sender: Any?) {
Expand Down Expand Up @@ -150,6 +159,14 @@ class VisualizerWindowController: NSWindowController, NSWindowDelegate, Destroya
}
}

// When the audio output device changes, restart the audio engine and continue playback as before.
func audioOutputDeviceChanged() {

allViews.forEach {
$0.setUp(with: objectGraph.fft)
}
}

@IBAction func closeWindowAction(_ sender: Any) {
close()
}
Expand Down

0 comments on commit 9ff56b0

Please sign in to comment.