Skip to content

refactor: used existing AudioJack in soundmeter #2748

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

Open
wants to merge 2 commits into
base: flutter
Choose a base branch
from

Conversation

Yugesh-Kumar-S
Copy link
Collaborator

@Yugesh-Kumar-S Yugesh-Kumar-S commented Jun 19, 2025

Part of #2738

Changes

  • Just replaced direct FlutterAudioCapture usage with existing AudioJack class.

Screenshots / Recordings

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Refactor SoundMeterStateProvider to leverage the existing AudioJack service for audio capture, replacing direct FlutterAudioCapture usage and streamlining decibel measurement via a polling mechanism.

Enhancements:

  • Replace FlutterAudioCapture with the centralized AudioJack class for audio capture in SoundMeterStateProvider
  • Start and poll AudioJack on a periodic timer to fetch audio data and update decibel readings
  • Change _calculateDecibels to accept a List instead of Float32List
  • Introduce a dedicated audio Timer and ensure AudioJack and timers are properly closed in disposeSensors

Copy link

sourcery-ai bot commented Jun 19, 2025

Reviewer's Guide

Refactors SoundMeterStateProvider to replace FlutterAudioCapture with the existing AudioJack class, switching to a timer-based polling model for audio sampling, updating decibel calculation to use List, and enhancing resource cleanup logic.

Class diagram for refactored SoundMeterStateProvider audio capture

classDiagram
    class SoundMeterStateProvider {
        - double _currentDb
        - Timer? _timeTimer
        - Timer? _audioTimer
        - List<double> _dbData
        - List<double> _timeData
        - List<FlSpot> dbChartData
        - AudioJack? _audioJack
        - double _startTime
        - double _currentTime
        - int _maxLength
        + void initializeSensors()
        + double _calculateDecibels(List<double> audioData)
        + void disposeSensors()
    }
    class AudioJack {
        + Future<void> initialize()
        + Future<void> start()
        + bool isListening()
        + List<double> read()
        + Future<void> close()
    }
    SoundMeterStateProvider --> AudioJack : uses
Loading

File-Level Changes

Change Details Files
Swap out FlutterAudioCapture for AudioJack and switch to polling
  • Import and instantiate AudioJack instead of FlutterAudioCapture
  • Call initialize() and start() on AudioJack rather than init() on FlutterAudioCapture
  • Remove callback-based start() and related imports
lib/providers/soundmeter_state_provider.dart
Introduce periodic audio polling alongside existing time timer
  • Add _audioTimer to poll AudioJack every 100ms
  • Check isListening() before reading audio data
  • Update _currentDb and notifyListeners() within polling loop
lib/providers/soundmeter_state_provider.dart
Adjust decibel computation signature
  • Change _calculateDecibels parameter type from Float32List to List
  • Keep calculation logic but adapt to the new data type
lib/providers/soundmeter_state_provider.dart
Enhance dispose logic for proper cleanup
  • Call close() on AudioJack in disposeSensors
  • Cancel both _timeTimer and _audioTimer
lib/providers/soundmeter_state_provider.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Yugesh-Kumar-S - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `lib/providers/soundmeter_state_provider.dart:40` </location>
<code_context>
-        sampleRate: 44100,
-        bufferSize: 4096,
-      );
+      _audioTimer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
+        if (_audioJack!.isListening()) {
+          final audioData = _audioJack!.read();
+          if (audioData.isNotEmpty) {
+            _currentDb = _calculateDecibels(audioData);
+            notifyListeners();
</code_context>

<issue_to_address>
Potential for race condition or null dereference with _audioJack usage.

Add null checks or guards to ensure _audioJack is valid and listening before accessing it, especially if initializeSensors or disposeSensors can be called while the timer is active.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

github-actions bot commented Jun 19, 2025

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

Successfully merging this pull request may close these issues.

1 participant