~/wiki

avaudiosengine

---
title: AVAudioEngine
category: concepts
created: 2026-12-22
updated: 2026-12-22
tags: [avaudiosengine, audio-engine, real-time-audio, audio-tap, macos-audio, ios-audio, core-audio, audio-processing, microphone-capture, 16khz-audio, audio-buffer-management]
sources: [raw/conversations/2026-05-06-cursor-gosim-hack-efcdcc83.md]
confidence: high
---

# AVAudioEngine

Apple's high-level audio processing framework that provides a node-based architecture for real-time audio capture, processing, and playback on iOS and macOS. Essential for voice AI applications requiring continuous microphone access and low-latency audio processing.

## Core Architecture

### Audio Node Graph
- **Input nodes**: Microphone, file, buffer sources
- **Processing nodes**: Effects, mixers, converters
- **Output nodes**: Speakers, file writers, buffers
- **Tap nodes**: Non-destructive audio monitoring

### Real-time Processing
```swift
// Basic microphone capture setup
let audioEngine = AVAudioEngine()
let inputNode = audioEngine.inputNode
let recordingFormat = inputNode.outputFormat(forBus: 0)

// Install tap for continuous monitoring
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, time in
    // Process audio buffer in real-time
    processAudioBuffer(buffer)
}

Wake-Word Integration

Configuration for Voice AI

  • Sample rate: 16 kHz mono for speech recognition compatibility
  • Buffer size: 1024-2048 samples for low latency
  • Format conversion: PCM16 for ONNX model input
  • Continuous operation: Background processing without interruption

Audio Tap Implementation

// Configure for wake-word detection
let targetFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, 
                                sampleRate: 16000, 
                                channels: 1, 
                                interleaved: false)

inputNode.installTap(onBus: 0, bufferSize: 1280, format: targetFormat) { buffer, time in
    // Forward to wake-word pipeline
    wakeWordDetector.processAudio(buffer)
}

Performance Considerations

Memory Management

  • Audio buffers are reused by the system
  • Copy data immediately if processing asynchronously
  • Use rolling-buffers to manage streaming data

Thread Safety

  • Tap callbacks execute on audio thread
  • Dispatch to appropriate queues for heavy processing
  • Avoid blocking operations in tap callbacks

Resource Management

// Proper lifecycle management
func startAudioEngine() throws {
    try audioEngine.start()
}

func stopAudioEngine() {
    audioEngine.stop()
    audioEngine.inputNode.removeTap(onBus: 0)
}

Integration Patterns

With Swift Concurrency

  • Tap callbacks are not actor-isolated
  • Use @Sendable closures for cross-actor communication
  • Coordinate with serial-dispatch-queue for processing

With Core Audio

  • Lower-level alternative for specialized requirements
  • AVAudioEngine built on top of Core Audio
  • Direct Audio Unit access when needed

See also