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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
package extra
import (
"bytes"
"context"
"errors"
"fmt"
"gf-lt/config"
"io"
"log/slog"
"os"
"os/exec"
"sync"
"time"
"github.com/gordonklaus/portaudio"
)
type WhisperBinary struct {
logger *slog.Logger
whisperPath string
modelPath string
lang string
ctx context.Context
cancel context.CancelFunc
mu sync.Mutex
recording bool
audioBuffer []int16
}
func NewWhisperBinary(logger *slog.Logger, cfg *config.Config) *WhisperBinary {
ctx, cancel := context.WithCancel(context.Background())
return &WhisperBinary{
logger: logger,
whisperPath: cfg.WhisperBinaryPath,
modelPath: cfg.WhisperModelPath,
lang: cfg.STT_LANG,
ctx: ctx,
cancel: cancel,
}
}
func (w *WhisperBinary) StartRecording() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.recording {
return errors.New("recording is already in progress")
}
// Temporarily redirect stderr to suppress ALSA warnings during PortAudio init
origStderr := os.Stderr
origStdout := os.Stdout
nullFile, err := os.OpenFile("/dev/null", os.O_WRONLY, 0)
if err != nil {
return fmt.Errorf("failed to open /dev/null: %w", err)
}
defer nullFile.Close()
// Redirect stderr temporarily
os.Stderr = nullFile
os.Stdout = nullFile
// Initialize PortAudio (this is where ALSA warnings occur)
portaudioErr := portaudio.Initialize()
defer func() {
// Restore stderr
os.Stderr = origStderr
os.Stdout = origStdout
}()
if portaudioErr != nil {
return fmt.Errorf("portaudio init failed: %w", portaudioErr)
}
// Initialize audio buffer
w.audioBuffer = make([]int16, 0)
in := make([]int16, 1024) // buffer size
stream, err := portaudio.OpenDefaultStream(1, 0, 16000.0, len(in), in)
if err != nil {
if paErr := portaudio.Terminate(); paErr != nil {
return fmt.Errorf("failed to open microphone: %w; terminate error: %w", err, paErr)
}
return fmt.Errorf("failed to open microphone: %w", err)
}
go w.recordAudio(stream, in)
w.recording = true
w.logger.Debug("Recording started")
return nil
}
func (w *WhisperBinary) recordAudio(stream *portaudio.Stream, in []int16) {
// Temporarily redirect stderr to suppress ALSA warnings during recording operations
origStderr := os.Stderr
origStdout := os.Stdout
nullFile, err := os.OpenFile("/dev/null", os.O_WRONLY, 0)
if err != nil {
// If we can't open /dev/null, log the error but continue anyway
w.logger.Error("Failed to open /dev/null for stderr redirection", "error", err)
// Continue without stderr redirection
} else {
// Redirect stderr temporarily for the duration of this function
os.Stderr = nullFile
os.Stdout = nullFile
defer func() {
// Restore stderr when function exits
os.Stderr = origStderr
os.Stdout = origStdout
nullFile.Close()
}()
}
defer func() {
w.logger.Debug("recordAudio defer function called")
_ = stream.Stop() // Stop the stream
_ = portaudio.Terminate() // ignoring error as we're shutting down
w.logger.Debug("recordAudio terminated")
}()
w.logger.Debug("Starting audio stream")
if err := stream.Start(); err != nil {
w.logger.Error("Failed to start audio stream", "error", err)
return
}
w.logger.Debug("Audio stream started, entering recording loop")
for {
select {
case <-w.ctx.Done():
w.logger.Debug("Context done, exiting recording loop")
return
default:
// Check recording status with minimal lock time
w.mu.Lock()
recording := w.recording
w.mu.Unlock()
if !recording {
w.logger.Debug("Recording flag is false, exiting recording loop")
return
}
if err := stream.Read(); err != nil {
w.logger.Error("Error reading from stream", "error", err)
return
}
// Append samples to buffer - only acquire lock when necessary
w.mu.Lock()
if w.audioBuffer == nil {
w.audioBuffer = make([]int16, 0)
}
// Make a copy of the input buffer to avoid overwriting
tempBuffer := make([]int16, len(in))
copy(tempBuffer, in)
w.audioBuffer = append(w.audioBuffer, tempBuffer...)
w.mu.Unlock()
}
}
}
func (w *WhisperBinary) StopRecording() (string, error) {
w.logger.Debug("StopRecording called")
w.mu.Lock()
if !w.recording {
w.mu.Unlock()
return "", errors.New("not currently recording")
}
w.logger.Debug("Setting recording to false and cancelling context")
w.recording = false
w.cancel() // This will stop the recording goroutine
w.mu.Unlock()
// Small delay to allow the recording goroutine to react to context cancellation
time.Sleep(100 * time.Millisecond)
// Save the recorded audio to a temporary file
tempFile, err := w.saveAudioToTempFile()
if err != nil {
w.logger.Error("Error saving audio to temp file", "error", err)
return "", fmt.Errorf("failed to save audio to temp file: %w", err)
}
w.logger.Debug("Saved audio to temp file", "file", tempFile)
// Run the whisper binary with a separate context to avoid cancellation during transcription
cmd := exec.Command(w.whisperPath, "-m", w.modelPath, "-l", w.lang, tempFile, "2>/dev/null")
var outBuf bytes.Buffer
cmd.Stdout = &outBuf
// Redirect stderr to suppress ALSA warnings and other stderr output
cmd.Stderr = io.Discard // Suppress stderr output from whisper binary
w.logger.Debug("Running whisper binary command")
if err := cmd.Run(); err != nil {
// Clean up audio buffer
w.mu.Lock()
w.audioBuffer = nil
w.mu.Unlock()
// Since we're suppressing stderr, we'll just log that the command failed
w.logger.Error("Error running whisper binary", "error", err)
return "", fmt.Errorf("whisper binary failed: %w", err)
}
result := outBuf.String()
w.logger.Debug("Whisper binary completed", "result", result)
// Clean up audio buffer
w.mu.Lock()
w.audioBuffer = nil
w.mu.Unlock()
// Clean up the temporary file after transcription
w.logger.Debug("StopRecording completed")
os.Remove(tempFile)
return result, nil
}
// saveAudioToTempFile saves the recorded audio data to a temporary WAV file
func (w *WhisperBinary) saveAudioToTempFile() (string, error) {
w.logger.Debug("saveAudioToTempFile called")
// Create temporary WAV file
tempFile, err := os.CreateTemp("", "recording_*.wav")
if err != nil {
w.logger.Error("Failed to create temp file", "error", err)
return "", fmt.Errorf("failed to create temp file: %w", err)
}
w.logger.Debug("Created temp file", "file", tempFile.Name())
defer tempFile.Close()
// Write WAV header and data
w.logger.Debug("About to write WAV file", "file", tempFile.Name())
err = w.writeWAVFile(tempFile.Name())
if err != nil {
w.logger.Error("Error writing WAV file", "error", err)
return "", fmt.Errorf("failed to write WAV file: %w", err)
}
w.logger.Debug("WAV file written successfully", "file", tempFile.Name())
return tempFile.Name(), nil
}
// writeWAVFile creates a WAV file from the recorded audio data
func (w *WhisperBinary) writeWAVFile(filename string) error {
w.logger.Debug("writeWAVFile called", "filename", filename)
// Open file for writing
file, err := os.Create(filename)
if err != nil {
w.logger.Error("Error creating file", "error", err)
return err
}
defer file.Close()
w.logger.Debug("About to acquire mutex in writeWAVFile")
w.mu.Lock()
w.logger.Debug("Locked mutex, copying audio buffer")
audioData := make([]int16, len(w.audioBuffer))
copy(audioData, w.audioBuffer)
w.mu.Unlock()
w.logger.Debug("Unlocked mutex", "audio_data_length", len(audioData))
if len(audioData) == 0 {
w.logger.Warn("No audio data to write")
return errors.New("no audio data to write")
}
// Calculate data size (number of samples * size of int16)
dataSize := len(audioData) * 2 // 2 bytes per int16 sample
w.logger.Debug("Calculated data size", "size", dataSize)
// Write WAV header with the correct data size
header := w.createWAVHeader(16000, 1, 16, dataSize)
_, err = file.Write(header)
if err != nil {
w.logger.Error("Error writing WAV header", "error", err)
return err
}
w.logger.Debug("WAV header written successfully")
// Write audio data
w.logger.Debug("About to write audio data samples")
for i, sample := range audioData {
// Write little-endian 16-bit sample
_, err := file.Write([]byte{byte(sample), byte(sample >> 8)})
if err != nil {
w.logger.Error("Error writing sample", "index", i, "error", err)
return err
}
// Log progress every 10000 samples to avoid too much output
if i%10000 == 0 {
w.logger.Debug("Written samples", "count", i)
}
}
w.logger.Debug("All audio data written successfully")
return nil
}
// createWAVHeader creates a WAV file header
func (w *WhisperBinary) createWAVHeader(sampleRate, channels, bitsPerSample int, dataSize int) []byte {
header := make([]byte, 44)
copy(header[0:4], "RIFF")
// Total file size will be updated later
copy(header[8:12], "WAVE")
copy(header[12:16], "fmt ")
// fmt chunk size (16 for PCM)
header[16] = 16
header[17] = 0
header[18] = 0
header[19] = 0
// Audio format (1 = PCM)
header[20] = 1
header[21] = 0
// Number of channels
header[22] = byte(channels)
header[23] = 0
// Sample rate
header[24] = byte(sampleRate)
header[25] = byte(sampleRate >> 8)
header[26] = byte(sampleRate >> 16)
header[27] = byte(sampleRate >> 24)
// Byte rate
byteRate := sampleRate * channels * bitsPerSample / 8
header[28] = byte(byteRate)
header[29] = byte(byteRate >> 8)
header[30] = byte(byteRate >> 16)
header[31] = byte(byteRate >> 24)
// Block align
blockAlign := channels * bitsPerSample / 8
header[32] = byte(blockAlign)
header[33] = 0
// Bits per sample
header[34] = byte(bitsPerSample)
header[35] = 0
// "data" subchunk
copy(header[36:40], "data")
// Data size
header[40] = byte(dataSize)
header[41] = byte(dataSize >> 8)
header[42] = byte(dataSize >> 16)
header[43] = byte(dataSize >> 24)
return header
}
func (w *WhisperBinary) IsRecording() bool {
w.mu.Lock()
defer w.mu.Unlock()
return w.recording
}
|