Contents

william-weng/wwmicrophoneinput

English | 正體中文

✨ 功能特色

  • 即時麥克風輸入:透過 AVAudioEngine 捕捉高品質的 PCM 音訊緩衝區
  • 靈活的音訊會話配置:支援自訂 AVAudioSession 的 category、mode 與 options
  • 非同步回呼機制:透過 @Sendable 回呼函數即時接收音訊數據
  • 型別安全的錯誤處理:使用 WWMicrophoneInputError 提供明確的錯誤資訊
  • 易於整合:設計簡潔,可輕鬆整合到語音識別、音訊處理等應用場景

📦 安裝方式

使用 Swift Package Manager

在 Xcode 中選擇:

File → Add Package Dependencies...

接著輸入套件 repository URL,選擇要使用的版本,並將套件加入 App target。

如果這是本地套件,也可以在 Xcode 中使用:

File → Add Package Dependencies... → Add Local...

🏗️ 公開 API

| API | 說明 | | --- | --- | | requestAuthorization() | 請求麥克風錄音權限。 | | init(bufferHandler:) | 建立 WWMicrophoneInput 實體。 | | configure(category:active:) | 設定音訊工作階段(AVAudioSession)的類別、模式與選項,並設定是否啟用。 | | start(level:) | 啟動麥克風輸入。 | | stop() | 停止麥克風輸入。 |

📌 公開屬性

| 名稱 | 說明 | | --- | --- | | isRunning | 是否正在錄音中。 |

🧪 [簡單範例](https://github.com/William-Weng/RealTimeSpeechToText)

private extension SpeechViewModel {
    
    func createAudioPipeline() throws {
        
        let transcription = try WWAudioStreamTranscription(locale: locale)
        
        transcription.onResult = { [weak self] text, isFinal in
            
            Task { @MainActor in
                self?.text = text
                if isFinal { self?.isRecording = false }
            }
        }
        
        transcription.onError = { [weak self] error in
            
            Task { @MainActor in
                self?.errorMessage = error.localizedDescription
                self?.stop()
            }
        }
        
        let microphone = WWMicrophoneInput { [weak transcription] buffer in
            transcription?.append(buffer: buffer.value)
        }
        
        try microphone.configure()
        try transcription.start()
        try microphone.start()
        
        self.transcription = transcription
        self.microphoneInput = microphone
    }
}

Package Metadata

Repository: william-weng/wwmicrophoneinput

Default branch: main

README: README.md