Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ee8968e2d6 | |||
| 4bc36019c2 | |||
| 1b537eb395 | |||
| 80d7555b60 |
@@ -200,7 +200,7 @@ struct AudioTee {
|
|||||||
// Helper for stderr output
|
// Helper for stderr output
|
||||||
var standardError = FileHandle.standardError
|
var standardError = FileHandle.standardError
|
||||||
|
|
||||||
extension FileHandle: @retroactive TextOutputStream {
|
extension FileHandle: TextOutputStream {
|
||||||
public func write(_ string: String) {
|
public func write(_ string: String) {
|
||||||
let data = Data(string.utf8)
|
let data = Data(string.utf8)
|
||||||
self.write(data)
|
self.write(data)
|
||||||
|
|||||||
@@ -2,17 +2,62 @@ import CoreAudio
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public class AudioBuffer {
|
public class AudioBuffer {
|
||||||
private var buffer = Data()
|
private var buffer: [UInt8]
|
||||||
private let targetChunkDuration: Double
|
private var writeIndex: Int = 0
|
||||||
private let streamFormat: AudioStreamBasicDescription
|
private var readIndex: Int = 0
|
||||||
|
private var availableBytes: Int = 0
|
||||||
|
private let maxBufferSize: Int
|
||||||
|
|
||||||
|
private let bytesPerChunk: Int
|
||||||
|
private let chunkDuration: Double
|
||||||
|
|
||||||
public init(format: AudioStreamBasicDescription, chunkDuration: Double = 0.2) {
|
public init(format: AudioStreamBasicDescription, chunkDuration: Double = 0.2) {
|
||||||
self.streamFormat = format
|
|
||||||
self.targetChunkDuration = chunkDuration
|
// Pre-calculate chunk parameters
|
||||||
|
let bytesPerFrame = Int(format.mBytesPerFrame)
|
||||||
|
let samplesPerChunk = Int(format.mSampleRate * chunkDuration)
|
||||||
|
self.bytesPerChunk = samplesPerChunk * bytesPerFrame
|
||||||
|
self.chunkDuration = Double(samplesPerChunk) / format.mSampleRate
|
||||||
|
|
||||||
|
// Calculate max buffer size to hold ~10 seconds of audio, way more than the maximum we allow
|
||||||
|
let bytesPerSecond = Int(format.mSampleRate) * bytesPerFrame
|
||||||
|
self.maxBufferSize = bytesPerSecond * 10
|
||||||
|
|
||||||
|
// Pre-allocated ring buffer
|
||||||
|
self.buffer = Array(repeating: 0, count: maxBufferSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func append(_ data: Data) {
|
public func append(_ data: Data) {
|
||||||
buffer.append(data)
|
guard availableBytes + data.count <= maxBufferSize else {
|
||||||
|
Logger.error("Audio buffer overflow", context: [
|
||||||
|
"requested": String(data.count),
|
||||||
|
"available": String(maxBufferSize - availableBytes)
|
||||||
|
])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data.withUnsafeBytes { bytes in
|
||||||
|
let sourceBytes = bytes.bindMemory(to: UInt8.self)
|
||||||
|
let dataSize = sourceBytes.count
|
||||||
|
|
||||||
|
// Check if we can copy in one block (no wrap-around)
|
||||||
|
if writeIndex + dataSize <= maxBufferSize {
|
||||||
|
// only one write needed
|
||||||
|
buffer.replaceSubrange(writeIndex..<writeIndex + dataSize, with: sourceBytes)
|
||||||
|
writeIndex = (writeIndex + dataSize) % maxBufferSize
|
||||||
|
} else {
|
||||||
|
// two writes needed due to wrap-around
|
||||||
|
let firstChunkSize = maxBufferSize - writeIndex
|
||||||
|
let secondChunkSize = dataSize - firstChunkSize
|
||||||
|
|
||||||
|
buffer.replaceSubrange(writeIndex..<maxBufferSize, with: sourceBytes.prefix(firstChunkSize))
|
||||||
|
buffer.replaceSubrange(0..<secondChunkSize, with: sourceBytes.suffix(secondChunkSize))
|
||||||
|
|
||||||
|
writeIndex = secondChunkSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
availableBytes += data.count
|
||||||
}
|
}
|
||||||
|
|
||||||
public func processChunks() -> [AudioPacket] {
|
public func processChunks() -> [AudioPacket] {
|
||||||
@@ -25,37 +70,37 @@ public class AudioBuffer {
|
|||||||
return packets
|
return packets
|
||||||
}
|
}
|
||||||
|
|
||||||
public func flushRemaining() -> AudioPacket? {
|
|
||||||
guard !buffer.isEmpty else { return nil }
|
|
||||||
|
|
||||||
let packet = AudioPacket(
|
|
||||||
timestamp: Date(),
|
|
||||||
duration: 0.0, // Unknown duration for final chunk
|
|
||||||
peakAmplitude: 0.0,
|
|
||||||
rawAudioData: buffer
|
|
||||||
)
|
|
||||||
|
|
||||||
buffer.removeAll()
|
|
||||||
return packet
|
|
||||||
}
|
|
||||||
|
|
||||||
private func nextChunk() -> AudioPacket? {
|
private func nextChunk() -> AudioPacket? {
|
||||||
let bytesPerFrame = Int(streamFormat.mBytesPerFrame)
|
// Check if we have enough data for a complete chunk
|
||||||
let samplesPerChunk = Int(streamFormat.mSampleRate * targetChunkDuration)
|
guard availableBytes >= bytesPerChunk else { return nil }
|
||||||
let bytesPerChunk = samplesPerChunk * bytesPerFrame
|
|
||||||
|
|
||||||
guard buffer.count >= bytesPerChunk else { return nil }
|
var chunkData = Data(capacity: bytesPerChunk)
|
||||||
|
|
||||||
let chunkData = buffer.prefix(bytesPerChunk)
|
// Check if we can copy in one block (no wrap-around)
|
||||||
|
if readIndex + bytesPerChunk <= maxBufferSize {
|
||||||
|
// one copy needed
|
||||||
|
chunkData.append(contentsOf: buffer[readIndex..<readIndex + bytesPerChunk])
|
||||||
|
readIndex = (readIndex + bytesPerChunk) % maxBufferSize
|
||||||
|
} else {
|
||||||
|
// two copies needed due to wrap-around
|
||||||
|
let firstChunkSize = maxBufferSize - readIndex
|
||||||
|
let secondChunkSize = bytesPerChunk - firstChunkSize
|
||||||
|
|
||||||
|
chunkData.append(contentsOf: buffer[readIndex..<maxBufferSize])
|
||||||
|
chunkData.append(contentsOf: buffer[0..<secondChunkSize])
|
||||||
|
|
||||||
|
readIndex = secondChunkSize
|
||||||
|
}
|
||||||
|
|
||||||
|
availableBytes -= bytesPerChunk
|
||||||
|
|
||||||
let packet = AudioPacket(
|
let packet = AudioPacket(
|
||||||
timestamp: Date(),
|
timestamp: Date(),
|
||||||
duration: Double(samplesPerChunk) / streamFormat.mSampleRate,
|
duration: chunkDuration,
|
||||||
peakAmplitude: 0.0, // No analysis in raw mode
|
peakAmplitude: 0.0,
|
||||||
rawAudioData: Data(chunkData)
|
rawAudioData: chunkData
|
||||||
)
|
)
|
||||||
|
|
||||||
buffer.removeFirst(bytesPerChunk)
|
|
||||||
return packet
|
return packet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,7 +74,8 @@ public class AudioRecorder {
|
|||||||
Logger.info("Audio device started successfully")
|
Logger.info("Audio device started successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: note to self, what about installTap? Would require audio engine and a node?
|
// Note to self, what about installTap? Would require audio engine and a node?
|
||||||
|
// No; AudioEngine.installTap() can only fire as often as 100ms. too slow for us
|
||||||
private func setupAndStartIOProc() {
|
private func setupAndStartIOProc() {
|
||||||
Logger.debug("Creating IO proc")
|
Logger.debug("Creating IO proc")
|
||||||
var status = AudioDeviceCreateIOProcID(
|
var status = AudioDeviceCreateIOProcID(
|
||||||
@@ -126,8 +127,8 @@ public class AudioRecorder {
|
|||||||
|
|
||||||
func stopRecording() {
|
func stopRecording() {
|
||||||
// Send any remaining buffered audio, applying conversion if needed
|
// Send any remaining buffered audio, applying conversion if needed
|
||||||
if let finalPacket = audioBuffer?.flushRemaining() {
|
audioBuffer?.processChunks().forEach { packet in
|
||||||
let processedPacket = converter?.transform(finalPacket) ?? finalPacket
|
let processedPacket = converter?.transform(packet) ?? packet
|
||||||
outputHandler.handleAudioPacket(processedPacket)
|
outputHandler.handleAudioPacket(processedPacket)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user