refactor: move source files to src/ directory and rename project to Captioneer

Restructure Go source code into a dedicated `src/` directory, rename the project from "audio-listen" to "Captioneer", and update README accordingly. Add `.gitignore` entry for the built binary `captioneer`.
This commit is contained in:
2026-07-16 12:01:51 +03:00
parent d042637351
commit ce2161b7e6
12 changed files with 385 additions and 327 deletions
+93
View File
@@ -0,0 +1,93 @@
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
)
func validatePrograms() error {
for _, program := range []string{"pactl", "parec"} {
if _, err := exec.LookPath(program); err != nil {
return fmt.Errorf("%s was not found in PATH: %w", program, err)
}
}
return nil
}
func defaultMonitorSource(ctx context.Context) (string, error) {
output, err := exec.CommandContext(ctx, "pactl", "get-default-sink").Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)) + ".monitor", nil
}
func capturePackets(ctx context.Context, monitorSource string) (<-chan []float32, func() error, error) {
cmd := exec.CommandContext(ctx, "parec",
"--device="+monitorSource,
"--format=s16le",
fmt.Sprintf("--rate=%d", sampleRate),
fmt.Sprintf("--channels=%d", channels),
"--raw",
"--latency-msec=50",
)
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, err
}
packets := make(chan []float32, 50) // Five seconds at 100 ms per packet.
var waitOnce sync.Once
var waitErr error
wait := func() error {
waitOnce.Do(func() { waitErr = cmd.Wait() })
return waitErr
}
go func() {
defer close(packets)
packetBytes := make([]byte, samplesPerPacket()*bytesPerSample)
for {
n, readErr := io.ReadFull(stdout, packetBytes)
if n > 0 {
enqueueLatest(packets, pcm16LEToFloat32(packetBytes[:n]))
}
if readErr != nil {
if ctx.Err() == nil && !errors.Is(readErr, io.EOF) && !errors.Is(readErr, io.ErrUnexpectedEOF) {
fmt.Fprintf(os.Stderr, "audio capture read error: %v\n", readErr)
}
return
}
}
}()
return packets, wait, nil
}
func enqueueLatest(packets chan []float32, packet []float32) {
select {
case packets <- packet:
return
default:
}
// Keep the newest audio so captions recover quickly after overload.
select {
case <-packets:
default:
}
select {
case packets <- packet:
default:
}
fmt.Fprintln(os.Stderr, "warning: caption processing overloaded; dropped 100 ms of oldest audio")
}