Skip to content

Commit

Permalink
Merge pull request #450 from tphakala/malgo-init-fixes
Browse files Browse the repository at this point in the history
refactor: improve audio device initialization process
  • Loading branch information
tphakala authored Feb 14, 2025
2 parents 444a156 + f3f9071 commit 50838b1
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 127 deletions.
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

0 comments on commit 50838b1

Please sign in to comment.