Attempt: redirect to /dev/null alsa warnings
This commit is contained in:
@@ -47,51 +47,30 @@ func (w *WhisperBinary) StartRecording() error {
|
|||||||
return errors.New("recording is already in progress")
|
return errors.New("recording is already in progress")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Suppress ALSA warnings by setting environment variables
|
// Temporarily redirect stderr to suppress ALSA warnings during PortAudio init
|
||||||
origCard := os.Getenv("ALSA_PCM_CARD")
|
origStderr := os.Stderr
|
||||||
origDevice := os.Getenv("ALSA_PCM_DEVICE")
|
origStdout := os.Stdout
|
||||||
origSubdevice := os.Getenv("ALSA_PCM_SUBDEVICE")
|
nullFile, err := os.OpenFile("/dev/null", os.O_WRONLY, 0)
|
||||||
|
if err != nil {
|
||||||
// Set specific ALSA device to prevent "Unknown PCM card.pcm.rear" warnings
|
return fmt.Errorf("failed to open /dev/null: %w", err)
|
||||||
os.Setenv("ALSA_PCM_CARD", "0")
|
|
||||||
os.Setenv("ALSA_PCM_DEVICE", "0")
|
|
||||||
os.Setenv("ALSA_PCM_SUBDEVICE", "0")
|
|
||||||
|
|
||||||
if err := portaudio.Initialize(); err != nil {
|
|
||||||
// Restore original environment variables on error
|
|
||||||
if origCard != "" {
|
|
||||||
os.Setenv("ALSA_PCM_CARD", origCard)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv("ALSA_PCM_CARD")
|
|
||||||
}
|
|
||||||
if origDevice != "" {
|
|
||||||
os.Setenv("ALSA_PCM_DEVICE", origDevice)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv("ALSA_PCM_DEVICE")
|
|
||||||
}
|
|
||||||
if origSubdevice != "" {
|
|
||||||
os.Setenv("ALSA_PCM_SUBDEVICE", origSubdevice)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv("ALSA_PCM_SUBDEVICE")
|
|
||||||
}
|
|
||||||
return fmt.Errorf("portaudio init failed: %w", err)
|
|
||||||
}
|
}
|
||||||
|
defer nullFile.Close()
|
||||||
|
|
||||||
// Restore original environment variables after initialization
|
// Redirect stderr temporarily
|
||||||
if origCard != "" {
|
os.Stderr = nullFile
|
||||||
os.Setenv("ALSA_PCM_CARD", origCard)
|
os.Stdout = nullFile
|
||||||
} else {
|
|
||||||
os.Unsetenv("ALSA_PCM_CARD")
|
// Initialize PortAudio (this is where ALSA warnings occur)
|
||||||
}
|
portaudioErr := portaudio.Initialize()
|
||||||
if origDevice != "" {
|
|
||||||
os.Setenv("ALSA_PCM_DEVICE", origDevice)
|
defer func() {
|
||||||
} else {
|
// Restore stderr
|
||||||
os.Unsetenv("ALSA_PCM_DEVICE")
|
os.Stderr = origStderr
|
||||||
}
|
os.Stdout = origStdout
|
||||||
if origSubdevice != "" {
|
}()
|
||||||
os.Setenv("ALSA_PCM_SUBDEVICE", origSubdevice)
|
|
||||||
} else {
|
if portaudioErr != nil {
|
||||||
os.Unsetenv("ALSA_PCM_SUBDEVICE")
|
return fmt.Errorf("portaudio init failed: %w", portaudioErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize audio buffer
|
// Initialize audio buffer
|
||||||
@@ -112,6 +91,26 @@ func (w *WhisperBinary) StartRecording() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *WhisperBinary) recordAudio(stream *portaudio.Stream, in []int16) {
|
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() {
|
defer func() {
|
||||||
w.logger.Debug("recordAudio defer function called")
|
w.logger.Debug("recordAudio defer function called")
|
||||||
_ = stream.Stop() // Stop the stream
|
_ = stream.Stop() // Stop the stream
|
||||||
|
|||||||
Reference in New Issue
Block a user