Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve audio device initialization process #450

Merged
merged 4 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions internal/analysis/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ func RealtimeAnalysis(settings *conf.Settings, notificationChan chan handlers.No
sources = settings.Realtime.RTSP.URLs
}
if settings.Realtime.Audio.Source != "" {
// We'll add malgo to sources only if device initialization succeeds
// This will be handled in CaptureAudio
sources = append(sources, "malgo")
}

// Initialize buffers for all audio sources
if err := initializeBuffers(sources); err != nil {
log.Printf("❌ %v", err)
return err
// If buffer initialization fails, log the error but continue
// Some sources might still work
log.Printf("⚠️ Error initializing buffers: %v", err)
log.Println("⚠️ Some audio sources might not be available.")
}
} else {
log.Println("⚠️ Starting without active audio sources. You can configure audio devices or RTSP streams through the web interface.")
Expand Down Expand Up @@ -353,9 +357,7 @@ func initBirdImageCache(ds datastore.Interface, metrics *telemetry.Metrics) *ima

// startControlMonitor handles various control signals for realtime analysis mode
func startControlMonitor(wg *sync.WaitGroup, controlChan chan string, quitChan, restartChan chan struct{}, notificationChan chan handlers.Notification, bufferManager *BufferManager) {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case signal := <-controlChan:
Expand Down Expand Up @@ -438,16 +440,20 @@ func startControlMonitor(wg *sync.WaitGroup, controlChan chan string, quitChan,

// initializeBuffers handles initialization of all audio-related buffers
func initializeBuffers(sources []string) error {
var initErrors []string

// Initialize analysis buffers
if err := myaudio.InitAnalysisBuffers(conf.BufferSize*3, sources); err != nil { // 3x buffer size to avoid underruns
return fmt.Errorf("failed to initialize analysis buffers: %w", err)
initErrors = append(initErrors, fmt.Sprintf("failed to initialize analysis buffers: %v", err))
}

// Initialize capture buffers
if err := myaudio.InitCaptureBuffers(60, conf.SampleRate, conf.BitDepth/8, sources); err != nil {
// Cleanup analysis buffers on failure
myaudio.CleanupAnalysisBuffers()
return fmt.Errorf("failed to initialize capture buffers: %w", err)
initErrors = append(initErrors, fmt.Sprintf("failed to initialize capture buffers: %v", err))
}

if len(initErrors) > 0 {
return fmt.Errorf("buffer initialization errors: %s", strings.Join(initErrors, "; "))
}

return nil
Expand Down
Loading
Loading