Update live waveform using rtaudio

This commit is contained in:
Hariharan Mudaliar 2026-04-04 23:39:48 +05:30
commit f7f29adea9
No known key found for this signature in database
GPG key ID: 025583EB0BE5640E
19 changed files with 893 additions and 5 deletions

View file

@ -0,0 +1,27 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* Objective-C bridge to expose C++ AudioProcessor to Swift.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#import <Foundation/Foundation.h>
#import <simd/simd.h>
NS_ASSUME_NONNULL_BEGIN
@interface AudioBridge : NSObject
- (void)processBuffer:(const float *)buffer count:(int)count;
// Reads atomically from the processor — safe to call from any thread
- (simd_float4)getSmoothedMagnitudes;
@end
NS_ASSUME_NONNULL_END

View file

@ -0,0 +1,47 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* Objective-C++ implementation bridging AudioProcessor to Swift.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#import "AudioBridge.h"
#import "AudioProcessor.hpp"
@implementation AudioBridge {
AudioProcessor *processor;
}
- (instancetype)init {
self = [super init];
if (self) {
processor = new AudioProcessor();
}
return self;
}
- (void)processBuffer:(const float *)buffer count:(int)count {
processor->process(buffer, count);
}
- (simd_float4)getSmoothedMagnitudes {
// Calls getBand() which does memory_order_relaxed atomic loads —
// no heap allocation, safe to call from the render thread
return simd_make_float4(
processor->getBand(0),
processor->getBand(1),
processor->getBand(2),
processor->getBand(3)
);
}
- (void)dealloc {
delete processor;
}
@end

View file

@ -0,0 +1,11 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include "AudioProcessor.hpp"

View file

@ -0,0 +1,134 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* Real-time audio processor using Accelerate framework for efficient FFT
* and band filtering. Adapted from rtaudio project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#ifndef AudioProcessor_hpp
#define AudioProcessor_hpp
#include <Accelerate/Accelerate.h>
#include <atomic>
#include <cmath>
class AudioProcessor {
private:
static constexpr int kBlockSize = 512; // lower latency than 1024
static constexpr int kBands = 4;
static constexpr float kAttack = 0.85f; // fast rise
static constexpr float kRelease = 0.40f; // slower fall — more musical
static constexpr float kGains[kBands] = {3.5f, 6.0f, 9.0f, 20.0f};
float sampleRate;
int writePos = 0;
float mono[kBlockSize] = {};
float filtered[kBlockSize] = {};
vDSP_biquad_Setup setups[kBands];
alignas(16) float delays[kBands][4] = {};
float envelopes[kBands] = {};
// Lock-free: audio thread writes, render thread reads
// One atomic per band — fits in a cache line
alignas(64) std::atomic<float> bandParams[kBands] = {};
public:
explicit AudioProcessor(float sr = 48000.0f) : sampleRate(sr) {
// Single biquad per band — just enough separation, minimal CPU
setupBiquad(0, FilterType::LowPass, 250.0f, 0.707f);
setupBiquad(1, FilterType::BandPass, 707.0f, 0.40f);
setupBiquad(2, FilterType::BandPass, 3464.0f, 0.85f);
setupBiquad(3, FilterType::HighPass, 6000.0f, 0.707f);
}
~AudioProcessor() {
for (int i = 0; i < kBands; ++i)
vDSP_biquad_DestroySetup(setups[i]);
}
// Called by CoreAudio — must be real-time safe (no alloc, no locks)
void process(const float* __restrict__ buffer, int totalSamples) {
if (__builtin_expect(totalSamples <= 0, 0)) return;
const float* src = buffer;
int remaining = totalSamples;
while (remaining > 0) {
int toCopy = std::min(remaining, kBlockSize - writePos);
memcpy(mono + writePos, src, toCopy * sizeof(float));
writePos += toCopy;
src += toCopy;
remaining -= toCopy;
if (writePos >= kBlockSize) {
processBlock();
writePos = 0;
}
}
}
// Called by render thread — lock-free read
float getBand(int i) const {
return bandParams[i].load(std::memory_order_relaxed);
}
private:
enum class FilterType { LowPass, BandPass, HighPass };
void processBlock() {
for (int i = 0; i < kBands; ++i) {
vDSP_biquad(setups[i], delays[i], mono, 1, filtered, 1, kBlockSize);
// RMS over the block — more stable than peak for driving animation
float rms = 0.0f;
vDSP_rmsqv(filtered, 1, &rms, kBlockSize);
if (__builtin_expect(!std::isfinite(rms), 0)) rms = 0.0f;
float boostedRms = rms * kGains[i];
boostedRms = std::min(boostedRms, 1.0f);
// Asymmetric envelope: attack fast, release slow
float prev = envelopes[i];
envelopes[i] = (boostedRms > prev)
? prev * (1.0f - kAttack) + boostedRms * kAttack
: prev * (1.0f - kRelease) + boostedRms * kRelease;
bandParams[i].store(envelopes[i], std::memory_order_relaxed);
}
}
void setupBiquad(int idx, FilterType type, float freq, float q) {
const double w0 = 2.0 * M_PI * freq / sampleRate;
const double sinw = std::sin(w0);
const double cosw = std::cos(w0);
const double alpha = sinw / (2.0 * q);
double b0, b1, b2;
const double a0 = 1.0 + alpha;
const double a1 = -2.0 * cosw;
const double a2 = 1.0 - alpha;
switch (type) {
case FilterType::LowPass:
b0 = (1.0 - cosw) * 0.5; b1 = 1.0 - cosw; b2 = b0; break;
case FilterType::HighPass:
b0 = (1.0 + cosw) * 0.5; b1 = -(1.0 + cosw); b2 = b0; break;
case FilterType::BandPass:
b0 = alpha; b1 = 0.0; b2 = -alpha; break;
}
const double coeffs[5] = { b0/a0, b1/a0, b2/a0, a1/a0, a2/a0 };
setups[idx] = vDSP_biquad_CreateSetup(coeffs, 1);
}
};
#endif // !AudioProcessor_hpp

View file

@ -0,0 +1,346 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* CoreAudio tap for capturing real-time audio from music applications.
* Uses macOS 14.2+ Process Tap API for efficient audio capture.
* Adapted from rtaudio project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
import AppKit
import AudioToolbox
import CoreAudio
import simd
import os.log
private let audioTapLog = OSLog(subsystem: "com.atoll.dynamicisland", category: "AudioTap")
// Debug: track callback invocations
private var callbackCount: Int = 0
// CoreAudio fires this on a high-priority background real-time thread.
let audioIOProc: AudioDeviceIOProc = {
inDevice, inNow, inInputData, inInputTime, outOutputData, inOutputTime, clientData in
guard let clientData = clientData else { return noErr }
let scanner = Unmanaged<AudioTap>.fromOpaque(clientData).takeUnretainedValue()
if scanner.isPaused { return noErr }
let mutableInputData = UnsafeMutablePointer(mutating: inInputData)
let bufferList = UnsafeMutableAudioBufferListPointer(mutableInputData)
if let firstBuffer = bufferList.first, let data = firstBuffer.mData {
// CoreAudio gives us byte size, divide by 4 (Float size) to get array length
let floatCount = Int32(firstBuffer.mDataByteSize) / Int32(MemoryLayout<Float>.size)
let floatData = data.assumingMemoryBound(to: Float.self)
// Pass the mono array directly to C++
scanner.bridge.processBuffer(floatData, count: floatCount)
// Debug: log periodically with audio level info
callbackCount += 1
if callbackCount % 1000 == 0 {
// Calculate max absolute value in buffer to check if audio is present
var maxVal: Float = 0.0
for i in 0..<Int(floatCount) {
let absVal = abs(floatData[i])
if absVal > maxVal { maxVal = absVal }
}
os_log(.debug, log: audioTapLog, "🔊 Audio callback fired %d times, buffer size: %d, max amplitude: %f", callbackCount, floatCount, maxVal)
// Also log the current band values from the bridge
let mags = scanner.bridge.getSmoothedMagnitudes()
os_log(.debug, log: audioTapLog, "🎚️ Bridge magnitudes: [%f, %f, %f, %f]", mags.x, mags.y, mags.z, mags.w)
}
}
return noErr
}
private func getAudioObjectID(for pid: pid_t) -> AudioObjectID? {
var audioObjectID: AudioObjectID = kAudioObjectUnknown
var pidValue = pid
var address = AudioObjectPropertyAddress(
mSelector: kAudioHardwarePropertyTranslatePIDToProcessObject,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain
)
var size = UInt32(MemoryLayout<AudioObjectID>.size)
let qualifierSize = UInt32(MemoryLayout<pid_t>.size)
// We query the global system object (kAudioObjectSystemObject)
// We pass the PID as the "qualifier", and it returns the AudioObjectID
let status = AudioObjectGetPropertyData(
AudioObjectID(kAudioObjectSystemObject),
&address,
qualifierSize,
&pidValue,
&size,
&audioObjectID
)
if status == noErr && audioObjectID != kAudioObjectUnknown {
return audioObjectID
}
return nil
}
/// Singleton class for real-time audio capture from music apps
class AudioTap: NSObject {
static let shared = AudioTap()
let bridge = AudioBridge()
var isPaused: Bool = false
private var displayMagnitudes = simd_float4(0, 0, 0, 0)
// CoreAudio stuff
private var tapID: AudioObjectID = kAudioObjectUnknown
private var aggregateDeviceID: AudioObjectID = kAudioObjectUnknown
private var ioProcID: AudioDeviceIOProcID? = nil
private var captureIsRunning = false
// Serial queue to prevent race conditions
private let audioQueue = DispatchQueue(label: "com.atoll.audiotap", qos: .userInitiated)
// Debounce restart requests
private var pendingRestartWorkItem: DispatchWorkItem?
private let targetBundleIDs = [
"com.apple.Music",
"com.spotify.client",
"com.apple.Safari",
"com.tidal.desktop",
"tv.plex.plexamp",
"com.roon.Roon",
"com.audirvana.Audirvana-Studio",
"com.vox.vox",
"com.coppertino.Vox",
]
private override init() {
super.init()
}
// Helper function to smooth out the magnitudes for prettifying purposes
func getSmoothedMagnitudes() -> simd_float4 {
// Zero bridging overhead. Just passing 16 bytes of memory.
let targetLevels = bridge.getSmoothedMagnitudes()
let smoothingFactor: Float = 0.4
// Vector math! This does all 4 calculations simultaneously.
let difference = targetLevels - displayMagnitudes
displayMagnitudes += difference * smoothingFactor
return displayMagnitudes
}
func startCapture() async {
await withCheckedContinuation { continuation in
audioQueue.async { [weak self] in
self?.startCaptureSync()
continuation.resume()
}
}
}
private func startCaptureSync() {
guard !captureIsRunning else {
print("⚠️ [AudioTap] Capture already running, skipping start")
return
}
let runningApps = NSWorkspace.shared.runningApplications
var targetPIDs: [AudioDeviceID] = []
for app in runningApps {
if let bundleID = app.bundleIdentifier, targetBundleIDs.contains(bundleID) {
if let deviceID = getAudioObjectID(for: app.processIdentifier) {
targetPIDs.append(deviceID)
print("🎯 [AudioTap] Found \(app.localizedName ?? "App") with PID: \(app.processIdentifier), AudioObjectID: \(deviceID)")
}
}
}
if targetPIDs.isEmpty {
print("⚠️ [AudioTap] None of our target apps are running right now.")
return
}
let description = CATapDescription()
description.processes = targetPIDs
description.isMixdown = true
description.isMono = true
print("📋 [AudioTap] Creating tap for \(targetPIDs.count) processes: \(targetPIDs)")
tapID = AudioObjectID(kAudioObjectUnknown)
var status = AudioHardwareCreateProcessTap(description, &tapID)
guard status == noErr else {
print("🛑 [AudioTap] Tap Error: \(status) (\(fourCharCodeToString(status)))")
return
}
print("✅ [AudioTap] Created process tap with ID: \(tapID)")
// Get the tap's unique hardware UID
var tapUID: CFString = "" as CFString
var propertySize = UInt32(MemoryLayout<CFString>.stride)
var propertyAddress = AudioObjectPropertyAddress(
mSelector: kAudioTapPropertyUID,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain
)
status = withUnsafeMutablePointer(to: &tapUID) { uidPtr in
AudioObjectGetPropertyData(tapID, &propertyAddress, 0, nil, &propertySize, uidPtr)
}
guard status == noErr else {
print("🛑 [AudioTap] UID Error: \(status) (\(fourCharCodeToString(status)))")
cleanupPartialSetup()
return
}
print("✅ [AudioTap] Got tap UID: \(tapUID)")
// Create the Aggregate Device (a "virtual microphone" that we can route the tap into)
let tapList = [[kAudioSubTapUIDKey: tapUID]]
let aggregateDict: [String: Any] = [
kAudioAggregateDeviceNameKey: "Atoll_Virtual_Tap",
kAudioAggregateDeviceUIDKey: UUID().uuidString,
kAudioAggregateDeviceIsPrivateKey: true, // Hides it from the user's sound settings
kAudioAggregateDeviceTapListKey: tapList,
]
aggregateDeviceID = AudioObjectID(kAudioObjectUnknown)
status = AudioHardwareCreateAggregateDevice(
aggregateDict as CFDictionary, &aggregateDeviceID)
guard status == noErr else {
print("🛑 [AudioTap] Aggregate Error: \(status) (\(fourCharCodeToString(status)))")
cleanupPartialSetup()
return
}
print("✅ [AudioTap] Created aggregate device with ID: \(aggregateDeviceID)")
// Bind the Callback to the device
let selfPointer = Unmanaged.passUnretained(self).toOpaque()
status = AudioDeviceCreateIOProcID(aggregateDeviceID, audioIOProc, selfPointer, &ioProcID)
guard status == noErr, let validIOProcID = ioProcID else {
print("🛑 [AudioTap] IOProc Error: \(status) (\(fourCharCodeToString(status)))")
cleanupPartialSetup()
return
}
print("✅ [AudioTap] Created IO proc")
// Start listening
status = AudioDeviceStart(aggregateDeviceID, validIOProcID)
guard status == noErr else {
print("🛑 [AudioTap] Start Error: \(status) (\(fourCharCodeToString(status)))")
cleanupPartialSetup()
return
}
captureIsRunning = true
callbackCount = 0
print("🟢 [AudioTap] CoreAudio CATap flowing through Aggregate Device!")
}
private func cleanupPartialSetup() {
if let validIOProcID = ioProcID, aggregateDeviceID != kAudioObjectUnknown {
AudioDeviceDestroyIOProcID(aggregateDeviceID, validIOProcID)
}
if aggregateDeviceID != kAudioObjectUnknown {
AudioHardwareDestroyAggregateDevice(aggregateDeviceID)
}
if tapID != kAudioObjectUnknown {
AudioHardwareDestroyProcessTap(tapID)
}
tapID = kAudioObjectUnknown
aggregateDeviceID = kAudioObjectUnknown
ioProcID = nil
}
func restartCapture() {
// Cancel any pending restart
pendingRestartWorkItem?.cancel()
// Debounce: wait 500ms before actually restarting
let workItem = DispatchWorkItem { [weak self] in
self?.audioQueue.async {
print("🔄 [AudioTap] Restarting capture...")
self?.stopCaptureSync()
// Small delay to let CoreAudio fully release resources
Thread.sleep(forTimeInterval: 0.1)
self?.startCaptureSync()
}
}
pendingRestartWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: workItem)
}
func stopCapture() {
audioQueue.sync { [weak self] in
self?.stopCaptureSync()
}
}
private func stopCaptureSync() {
guard captureIsRunning else { return }
// Stop listening
if let validIOProcID = ioProcID, aggregateDeviceID != kAudioObjectUnknown {
AudioDeviceStop(aggregateDeviceID, validIOProcID)
AudioDeviceDestroyIOProcID(aggregateDeviceID, validIOProcID)
}
// Destroy resources
if aggregateDeviceID != kAudioObjectUnknown {
AudioHardwareDestroyAggregateDevice(aggregateDeviceID)
}
if tapID != kAudioObjectUnknown {
AudioHardwareDestroyProcessTap(tapID)
}
tapID = kAudioObjectUnknown
aggregateDeviceID = kAudioObjectUnknown
ioProcID = nil
captureIsRunning = false
// Reset display magnitudes
displayMagnitudes = simd_float4(0, 0, 0, 0)
print("🔴 [AudioTap] CoreAudio CATap capture stopped")
}
var isCapturing: Bool {
captureIsRunning
}
deinit {
stopCaptureSync()
}
}
// Helper to convert OSStatus to readable string
private func fourCharCodeToString(_ code: OSStatus) -> String {
let bytes = [
UInt8((code >> 24) & 0xFF),
UInt8((code >> 16) & 0xFF),
UInt8((code >> 8) & 0xFF),
UInt8(code & 0xFF)
]
if bytes.allSatisfy({ $0 >= 32 && $0 < 127 }) {
return String(bytes: bytes, encoding: .ascii) ?? "????"
}
return String(code)
}

View file

@ -0,0 +1,35 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* Unified audio visualizer that conditionally uses real-time audio spectrum
* or the original animated spectrum based on user preference.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
import SwiftUI
import Defaults
/// Unified audio visualizer view that switches between real-time and animated based on user preference
struct AudioVisualizerView: View {
@Binding var isPlaying: Bool
@Default(.enableRealTimeWaveform) private var enableRealTimeWaveform
var body: some View {
if enableRealTimeWaveform {
RealTimeAudioSpectrumView(isPlaying: $isPlaying)
} else {
AudioSpectrumView(isPlaying: $isPlaying)
}
}
}
#Preview("Animated") {
AudioVisualizerView(isPlaying: .constant(true))
.frame(width: 16, height: 20)
.padding()
}

View file

@ -0,0 +1,161 @@
/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* Real-time audio spectrum visualization using CoreAudio tap data.
* Adapted from rtaudio project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
import AppKit
import Cocoa
import SwiftUI
import simd
/// NSView-based real-time audio spectrum visualizer
class RealTimeAudioSpectrum: NSView {
private var barLayers: [CAShapeLayer] = []
private var isPlaying: Bool = true
private var animationTimer: Timer?
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
wantsLayer = true
setupBars()
}
deinit {
stopAnimating()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
wantsLayer = true
setupBars()
}
private func setupBars() {
let barWidth: CGFloat = 2
let barCount = 4
let spacing: CGFloat = barWidth
let totalWidth = CGFloat(barCount) * (barWidth + spacing)
let totalHeight: CGFloat = 14
frame.size = CGSize(width: totalWidth, height: totalHeight)
for i in 0 ..< barCount {
let xPosition = CGFloat(i) * (barWidth + spacing)
let barLayer = CAShapeLayer()
barLayer.frame = CGRect(x: xPosition, y: 0, width: barWidth, height: totalHeight)
barLayer.position = CGPoint(x: xPosition + barWidth / 2, y: totalHeight / 2)
barLayer.fillColor = NSColor.white.cgColor
let path = NSBezierPath(roundedRect: CGRect(x: 0, y: 0, width: barWidth, height: totalHeight),
xRadius: barWidth / 2,
yRadius: barWidth / 2)
barLayer.path = path.cgPath
barLayers.append(barLayer)
layer?.addSublayer(barLayer)
}
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
if window == nil {
stopAnimating()
} else if isPlaying {
startAnimating()
}
}
private func startAnimating() {
guard animationTimer == nil else { return }
// Use a timer at ~30fps for smooth animation
animationTimer = Timer.scheduledTimer(withTimeInterval: 1.0/30.0, repeats: true) { [weak self] _ in
self?.updateBarsFromAudio()
}
}
private func stopAnimating() {
animationTimer?.invalidate()
animationTimer = nil
resetBars()
}
private var debugLogCounter = 0
private func updateBarsFromAudio() {
guard isPlaying else {
resetBars()
return
}
// Get real-time magnitudes from AudioTap
let magnitudes = AudioTap.shared.getSmoothedMagnitudes()
// Debug: log magnitudes periodically
debugLogCounter += 1
if debugLogCounter % 60 == 0 { // Every 2 seconds at 30fps
print("📊 [Spectrum] Magnitudes: [\(magnitudes.x), \(magnitudes.y), \(magnitudes.z), \(magnitudes.w)]")
}
// Update each bar with its corresponding band magnitude
for (index, barLayer) in barLayers.enumerated() {
let magnitude = magnitudes[index]
// Map magnitude (0-1) to scale (0.2 - 1.0) for visual appeal
let scale = max(0.2, min(1.0, CGFloat(magnitude) * 1.5 + 0.2))
CATransaction.begin()
CATransaction.setDisableActions(true)
barLayer.transform = CATransform3DMakeScale(1, scale, 1)
CATransaction.commit()
}
}
private func resetBars() {
for barLayer in barLayers {
CATransaction.begin()
CATransaction.setDisableActions(true)
barLayer.transform = CATransform3DMakeScale(1, 0.2, 1)
CATransaction.commit()
}
}
func setPlaying(_ playing: Bool) {
isPlaying = playing
if isPlaying {
startAnimating()
} else {
stopAnimating()
}
}
}
/// SwiftUI wrapper for RealTimeAudioSpectrum
struct RealTimeAudioSpectrumView: NSViewRepresentable {
@Binding var isPlaying: Bool
func makeNSView(context: Context) -> RealTimeAudioSpectrum {
let spectrum = RealTimeAudioSpectrum()
spectrum.setPlaying(isPlaying)
return spectrum
}
func updateNSView(_ nsView: RealTimeAudioSpectrum, context: Context) {
nsView.setPlaying(isPlaying)
}
static func dismantleNSView(_ nsView: RealTimeAudioSpectrum, coordinator: ()) {
nsView.setPlaying(false)
}
}
#Preview {
RealTimeAudioSpectrumView(isPlaying: .constant(true))
.frame(width: 16, height: 20)
.padding()
}