zero-alloc audio pipeline: pointer-based IO from ring buffer to stdout
Replace Data/AudioPacket allocations with raw pointer callbacks through the entire audio pipeline. Ring buffer hands out direct pointers (or linearizes into a pre-allocated scratch buffer on wrap-around), converter accepts/emits pointers via its cached buffers, and output handler writes to stdout via write(2) with EINTR handling. Remove AudioPacket (dead code), --flush flag (no-op with raw write(2)). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,17 +4,19 @@ import Foundation
|
||||
/// CLI-specific output handler that writes raw PCM audio to stdout
|
||||
/// and lifecycle messages to stderr via the logger.
|
||||
class BinaryAudioOutputHandler: AudioOutputHandler {
|
||||
private let flushAfterWrite: Bool
|
||||
private let fd = STDOUT_FILENO
|
||||
|
||||
init(flushAfterWrite: Bool = false) {
|
||||
self.flushAfterWrite = flushAfterWrite
|
||||
}
|
||||
|
||||
func handleAudioPacket(_ packet: AudioPacket) {
|
||||
// Write raw binary audio data directly to stdout
|
||||
FileHandle.standardOutput.write(packet.data)
|
||||
if flushAfterWrite {
|
||||
fflush(stdout)
|
||||
func handleAudioData(_ pointer: UnsafeRawPointer, count: Int) {
|
||||
var written = 0
|
||||
while written < count {
|
||||
let result = write(fd, pointer.advanced(by: written), count - written)
|
||||
if result >= 0 {
|
||||
written += result
|
||||
} else if errno == EINTR {
|
||||
continue
|
||||
} else {
|
||||
break // EPIPE, EIO, etc — consumer gone or real error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user