-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (110 loc) · 3.6 KB
/
Copy pathmain.go
File metadata and controls
131 lines (110 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/boyuanchen/asr-audio-streamer/audio"
"github.com/boyuanchen/asr-audio-streamer/config"
)
var (
sampleRate = flag.Int("rate", 16000, "Sample rate in Hz (e.g., 16000, 44100)")
chunkSize = flag.Int("chunk", 0, "Chunk size in frames (0 for auto-calculated ~200ms)")
deviceIndex = flag.Int("device", -1, "Input device index (-1 for default)")
languageCode = flag.String("lang", "en-US", "Language code for ASR (e.g., en-US, zh-CN)")
asrEndpoint = flag.String("endpoint", "ws://localhost:8080/asr", "ASR WebSocket endpoint URL (ws:// or wss://)")
listDevices = flag.Bool("list", false, "List available audio input devices and exit")
enableInterrupt = flag.Bool("enable-interrupt", false, "Enable TTS interrupt handling")
statsInterval = flag.Int("stats", 10, "Statistics print interval in seconds (0 to disable)")
bufferSize = flag.Int("buffer", 50, "Audio buffer size (number of chunks to buffer)")
)
func main() {
flag.Parse()
// List devices if requested
if *listDevices {
if err := audio.ListDevices(); err != nil {
log.Fatalf("Error listing devices: %v", err)
}
return
}
// Create configuration
cfg := config.DefaultConfig()
cfg.SampleRate = *sampleRate
cfg.ChunkSize = *chunkSize
cfg.DeviceIndex = *deviceIndex
cfg.LanguageCode = *languageCode
cfg.ASREndpoint = *asrEndpoint
cfg.EnableTTSInterrupt = *enableInterrupt
cfg.BufferSize = *bufferSize
// Print configuration
printConfiguration(cfg)
// Create audio input stream
log.Println("Initializing audio input stream...")
inputStream, err := audio.NewInputStream(cfg, nil)
if err != nil {
log.Fatalf("Failed to create input stream: %v", err)
}
// Create audio sender
sender := audio.NewSender(cfg)
// Start the audio input stream
if err := inputStream.Start(); err != nil {
log.Fatalf("Failed to start input stream: %v", err)
}
// Start the sender
if err := sender.Start(inputStream); err != nil {
log.Fatalf("Failed to start sender: %v", err)
}
log.Println("Audio streaming started. Press Ctrl+C to stop.")
// Start statistics printer if enabled
var statsTicker *time.Ticker
if *statsInterval > 0 {
statsTicker = time.NewTicker(time.Duration(*statsInterval) * time.Second)
defer statsTicker.Stop()
go func() {
for range statsTicker.C {
sender.PrintStatistics()
}
}()
}
// Wait for interrupt signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Println("\nShutting down...")
// Stop sender first
sender.Stop()
// Then stop input stream
inputStream.Stop()
// Print final statistics
if *statsInterval > 0 {
fmt.Println("\nFinal Statistics:")
sender.PrintStatistics()
}
log.Println("Shutdown complete")
}
func printConfiguration(cfg *config.AudioConfig) {
fmt.Println("\n=== Audio Streamer Configuration ===")
fmt.Printf("Sample Rate: %d Hz\n", cfg.SampleRate)
if cfg.ChunkSize > 0 {
fmt.Printf("Chunk Size: %d frames\n", cfg.ChunkSize)
} else {
calculatedSize := cfg.CalculateChunkSize()
fmt.Printf("Chunk Size: %d frames (auto-calculated ~200ms)\n", calculatedSize)
}
if cfg.DeviceIndex >= 0 {
fmt.Printf("Device Index: %d\n", cfg.DeviceIndex)
} else {
fmt.Println("Device Index: Default device")
}
fmt.Printf("Language Code: %s\n", cfg.LanguageCode)
fmt.Printf("ASR Endpoint: %s\n", cfg.ASREndpoint)
fmt.Printf("TTS Interrupt: %v\n", cfg.EnableTTSInterrupt)
fmt.Printf("Buffer Size: %d\n", cfg.BufferSize)
fmt.Printf("Timeout: %v\n", cfg.Timeout)
fmt.Println(strings.Repeat("=", 37))
fmt.Println()
}