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

Conversation

tphakala
Copy link
Owner

Allow starting application without any working capture device

- Enhance audio device selection with comprehensive testing and validation
- Add device initialization and start tests before selecting a capture source
- Improve error handling and logging for audio device detection
- Update capture function to pass selected device source directly
- Add platform-specific backend selection for malgo context
…nitor lifecycle

- Modify buffer initialization to collect and report multiple errors
- Update control monitor to use a dedicated done channel for cleaner synchronization
- Enhance error logging for buffer initialization with more informative messages
- Prevent premature function exit when buffer initialization partially fails
Copy link
Contributor

coderabbitai bot commented Feb 12, 2025

Walkthrough

The changes enhance the functionality of audio analysis and capture. In internal/analysis/realtime.go, the RealtimeAnalysis function now conditionally adds the "malgo" audio source based on successful device initialization, and error handling during buffer initialization has been improved to log warnings and accumulate errors. In internal/myaudio/capture.go, the selectCaptureSource function has been refactored to only require settings, and the CaptureAudio function now passes a validated capture source to captureAudioMalgo. These updates improve the robustness and clarity of real-time audio processing.

Changes

Files Summary of Changes
internal/analysis/realtime.go Modified RealtimeAnalysis to conditionally add "malgo" source; improved error handling in buffer initialization to log warnings and accumulate errors; refactored startControlMonitor to adjust wait group management.
internal/myaudio/capture.go Added initializeBuffersForSource for buffer initialization; updated selectCaptureSource to require only settings; refactored CaptureAudio to pass an initialized capture source to captureAudioMalgo; enhanced error handling and logging for device initialization.

Sequence Diagram(s)

sequenceDiagram
    participant Main as Application
    participant Analysis as RealtimeAnalysis
    participant Buffer as BufferManager
    participant Monitor as ControlMonitor

    Main->>Analysis: Initialize real-time analysis
    Analysis->>Buffer: Call initializeBuffers(sources)
    Buffer-->>Analysis: Return aggregated errors (if any)
    Analysis->>Monitor: Start control monitor
    Monitor-->>Analysis: Signal monitor completion
Loading
sequenceDiagram
    participant App as Application
    participant Capture as CaptureAudio
    participant Selector as selectCaptureSource
    participant Device as malgo Device

    App->>Capture: Invoke CaptureAudio with settings
    Capture->>Selector: Request capture source
    Selector->>Device: Initialize and test audio device
    alt Device initialization succeeds
        Device-->>Selector: Return captureSource
        Selector-->>Capture: Return captureSource
        Capture->>Device: Call captureAudioMalgo(captureSource)
        Device-->>Capture: Audio capture started
    else Device initialization fails
        Device-->>Selector: Log error and fallback
        Selector-->>Capture: Return error, try alternate source
    end
Loading

Possibly related PRs

Poem

I'm a little rabbit with a code delight,
Hopping through streams from morning to night.
Buffers now sing a resilient tune,
Channels dance under a debugging moon.
Bugs hop away as new flows unite—
Cheers to smooth code and audio insight!
🐰 Happy coding!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

- Remove unnecessary monitorDone channel in startControlMonitor
- Streamline goroutine exit mechanism
- Remove redundant channel closing and waiting logic
- Introduce initializeBuffersForSource to centralize buffer setup logic
- Improve error handling and cleanup for RTSP and local audio sources
- Reduce code duplication in CaptureAudio function
- Enhance error logging for buffer initialization failures
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
internal/myaudio/capture.go (2)

364-364: Consider handling errors from deferred cleanup.

While the comment suggests errors are handled in the caller, the error from malgoCtx.Uninit() is actually not propagated. Consider wrapping the cleanup in a named return error:

-defer malgoCtx.Uninit() //nolint:errcheck // We handle errors in the caller
+var retErr error
+defer func() {
+    if cleanupErr := malgoCtx.Uninit(); cleanupErr != nil && retErr == nil {
+        retErr = fmt.Errorf("cleanup error: %w", cleanupErr)
+    }
+}()

491-491: Handle errors in deferred cleanup calls.

Similar to the previous comment, consider handling errors from deferred cleanup calls:

  • malgoCtx.Uninit() on line 491
  • captureDevice.Stop() on line 594
-defer malgoCtx.Uninit() //nolint:errcheck // We handle errors in the caller
+defer func() {
+    if err := malgoCtx.Uninit(); err != nil {
+        log.Printf("❌ Error during context cleanup: %v", err)
+    }
+}()

-defer captureDevice.Stop() //nolint:errcheck // We handle errors in the caller
+defer func() {
+    if err := captureDevice.Stop(); err != nil {
+        log.Printf("❌ Error stopping capture device: %v", err)
+    }
+}()

Also applies to: 594-594

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 60bbeb4 and f3f9071.

📒 Files selected for processing (1)
  • internal/myaudio/capture.go (9 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test-docker-image
  • GitHub Check: Analyze (go)
🔇 Additional comments (3)
internal/myaudio/capture.go (3)

263-298: LGTM! Well-structured buffer initialization.

The function is thread-safe, handles errors properly, and includes cleanup on failure. The implementation follows good practices.


300-340: LGTM! Improved error handling and user experience.

The changes successfully implement the PR objective by allowing the application to continue without a functioning capture device, while providing clear feedback to users.


559-564: LGTM! Well-implemented restart logic.

The restart logic properly handles both the restart signal and application shutdown with appropriate channel operations and timeouts.

@tphakala tphakala merged commit 50838b1 into main Feb 14, 2025
11 checks passed
@xconverge
Copy link
Contributor

@tphakala the container seems to start now with this change, but my RTSP source doesn't seem to be actually working, (I dont see the indicator showing sound level on the UI)

🐦 BirdNET-Go vunknown (built: 2025-02-14T16:45:55Z), using config file: /root/.config/birdnet-go/config.yaml

BirdNET GLOBAL 6K V2.4 FP32 model initialized, using 4 threads of available 4 CPUs

⚠️ Using legacy range filter modelSystem details: linux debian 12.9 on unknown hardware

Starting analyzer in realtime mode. Threshold: 0.8, overlap: 1.5, sensitivity: 1, interval: 15

Logging disabled

HTTP server started on port 8080 (AutoTLS: false)

2025/02/14 11:49:09 ⬆️  Starting ffmpeg with command: /usr/bin/ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.110:8554/mic -loglevel error -vn -f s16le -ar 48000 -ac 1 -hide_banner pipe:1

⇨ http server started on [::]:8080

2025/02/14 11:49:09 Clip retention policy: age

Available Capture Sources:

2025/02/14 11:49:09 Starting audio processing for RTSP source: rtsp://192.168.1.110:8554/mic (display name: Camera mic)

  0: Discard all samples (playback) or generate zero samples (capture), null

2025/02/14 11:49:09 ❌ Audio device selection failed: no working capture device found matching 'sysdefault'

2025/02/14 11:49:09 ⚠️  Continuing without audio device capture. You can configure a working audio device through the web interface.

2025/02/14 11:49:10 [security/cloudflare] Cloudflare Access is disabled

2025/02/14 11:49:10 [security/cloudflare] Cloudflare Access is disabled

2025/02/14 11:49:10 [security/cloudflare] Cloudflare Access is disabled

2025/02/14 11:49:10 [security/cloudflare] Cloudflare Access is disabled

2025/02/14 11:49:10 Retrieved 5 detections in 9.659422ms

@tphakala
Copy link
Owner Author

tphakala commented Feb 15, 2025

Are you sure your RTSP source isn't working? According log you provided ffmpeg was started

Starting ffmpeg with command: /usr/bin/ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.110:8554/mic -loglevel error -vn -f s16le -ar 48000 -ac 1 -hide_banner pipe:1

Audio level indicator is now a menu item, you can choose which audio source level is displayed. I just realised that it seems to default to sysdefault (sound card source) even though there isn't sound card source available, if you click level indicator and choose RTSP url you should see some activity.

@xconverge
Copy link
Contributor

Are you sure your RTSP source isn't working? According log you provided ffmpeg was started

Starting ffmpeg with command: /usr/bin/ffmpeg -rtsp_transport tcp -i rtsp://192.168.1.110:8554/mic -loglevel error -vn -f s16le -ar 48000 -ac 1 -hide_banner pipe:1

Audio level indicator is now a menu item, you can choose which audio source level is displayed. I just realised that it seems to default to sysdefault (sound card source) even though there isn't sound card source available, if you click level indicator and choose RTSP url you should see some activity.

You are certainly right, I didn't know that, I see it working now on the latest dev! Do you want me to open an issue to cache the last selected source or some other logic to not-default-to-an-undesired-default-one for a setup like mine? I know I was jumping around a bit from place to place but I just wanted to help a bit with an alternate setup as you started to gear up for the next stable release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants