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

Jzecm wal local sync 958 #992

Merged
merged 4 commits into from
Oct 7, 2024
Merged

Jzecm wal local sync 958 #992

merged 4 commits into from
Oct 7, 2024

Conversation

beastoin
Copy link
Collaborator

@beastoin beastoin commented Oct 7, 2024

Summary by Entelligence.AI


  • Refactor: Improved frame synchronization logic in WalService class for better performance.
  • Bug Fix: Added conditional checks to prevent potential crashes when calling wal.onByteStream and wal.onBytesSync functions based on the codec type.
  • Chore: Removed unused imports and constants related to FFI and math operations for cleaner codebase.
  • Style: Renamed constants ChunkSizeInSeconds and FlushIntervalInSeconds to chunkSizeInSeconds and flushIntervalInSeconds for consistency with Dart naming conventions.

Copy link

github-actions bot commented Oct 7, 2024

Image description Entelligence.AI

Walkthrough

This update introduces conditional checks for codec types and modifies the logic for frame synchronization in the WalService class. It also adjusts timer intervals and loss threshold calculations, renames constants for better readability, and removes unused imports and constants.

Changes

File(s) Summary
app/lib/providers/capture_provider.dart, app/lib/services/wals.dart Introduced conditional checks based on codec type before calling functions. Adjusted frame synchronization logic within WalService. Modified timer intervals and loss threshold calculations. Renamed constants ChunkSizeInSeconds and FlushIntervalInSeconds to chunkSizeInSeconds and flushIntervalInSeconds. Removed unused imports and constants related to FFI and math operations.

🎉 Code refined, logic aligned, 🚀

Constants renamed, unused imports tamed, 🧹

Syncing frames with a new game, 🕹️

Celebrate these changes, they're not the same! 🎊


Uplevel your code reviews with Entelligence.AI Pro

Entelligence.AI Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, Entelligence.AI Pro is free for open source projects.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description Entelligence.AI

Commits Files that changed from the base of the PR and between 61ec210 and fa90712 commits.
Files selected (2)
  • app/lib/providers/capture_provider.dart (1)
  • app/lib/services/wals.dart (7)
Review comments generated (9)
  • Review: 8
  • LGTM: 1

Tips

Chat with Image description Entelligence.AI Bot (@Entelligence.AI)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @Entelligence.AI in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @Entelligence.AI: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines +156 to +160
// support: opus
var isWalSupported = codec == BleAudioCodec.opus;
if (isWalSupported) {
wal.onByteStream(value);
}
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The check for isWalSupported is a good addition to ensure that the codec type is supported before invoking wal.onByteStream(value). However, it would be better to return early if the codec is not supported. This way, we avoid unnecessary operations and improve code readability.

- var isWalSupported = codec == BleAudioCodec.opus;
- if (isWalSupported) {
-   wal.onByteStream(value);
- }
+ if (codec != BleAudioCodec.opus) {
+   return;
+ }
+ wal.onByteStream(value);

Comment on lines +166 to +169
// synced
if (isWalSupported) {
wal.onBytesSync(value);
}
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

Similar to the previous comment, it's good to see a check for isWalSupported before invoking wal.onBytesSync(value). But again, consider returning early if the codec is not supported.

- if (isWalSupported) {
-   wal.onBytesSync(value);
- }
+ if (codec != BleAudioCodec.opus) {
+   return;
+ }
+ wal.onBytesSync(value);

Please note that these changes assume that there are no other operations after these checks that need to be executed regardless of the codec type. If there are, you might want to adjust the logic accordingly.

@@ -74,7 +71,7 @@ class Wal {
this.filePath,
this.data = const []});

get seconds => ChunkSizeInSeconds;
get seconds => chunkSizeInSeconds;
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The seconds getter has been updated to use the new constant chunkSizeInSeconds. This change is consistent with the update in the constants section. However, it's important to ensure that this new value doesn't negatively impact the system's performance or functionality.

-  get seconds => ChunkSizeInSeconds;
+  get seconds => chunkSizeInSeconds;

@@ -166,6 +163,7 @@ class WalService implements IWalService, IWalSocketServiceListener {
}

var framesPerSeconds = 100;
var lossesThreshold = 1 * framesPerSeconds; // 1s
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

A new variable lossesThreshold has been introduced. It seems to be used for determining a threshold for frame losses. Please ensure that this new logic does not introduce any unexpected behavior in the frame processing logic.

+    var lossesThreshold = 1 * framesPerSeconds; // 1s

@beastoin beastoin marked this pull request as ready for review October 7, 2024 11:00
@beastoin beastoin merged commit df05175 into main Oct 7, 2024
1 check passed
@beastoin beastoin deleted the jzecm_wal_local_sync_958 branch October 7, 2024 11:00
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description Entelligence.AI

Commits Files that changed from the base of the PR and between fa90712 and 843ff07 commits.
Files selected (1)
  • app/lib/services/wals.dart (7)
Review comments generated (7)
  • Review: 6
  • LGTM: 1

Tips

Chat with Image description Entelligence.AI Bot (@Entelligence.AI)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @Entelligence.AI in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @Entelligence.AI: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines 3 to +16
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:friend_private/backend/http/api/memories.dart';
import 'package:friend_private/backend/preferences.dart';
import 'package:friend_private/backend/schema/memory.dart';
import 'package:friend_private/backend/schema/message_event.dart';
import 'package:friend_private/providers/message_provider.dart';
import 'package:friend_private/services/sockets/transcription_connection.dart';
import 'package:friend_private/services/sockets/wal_connection.dart';
import 'package:path_provider/path_provider.dart';

const ChunkSizeInSeconds = 7; // 30
const FlushIntervalInSeconds = 15; //300
const chunkSizeInSeconds = 30;
const flushIntervalInSeconds = 60;
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The removal of unused imports and constants is a good practice for code cleanliness and readability. The renaming of the constants ChunkSizeInSeconds and FlushIntervalInSeconds to chunkSizeInSeconds and flushIntervalInSeconds follows Dart's naming conventions for constants, which is also a good change.

- import 'dart:ffi';
- import 'dart:math';
- const ChunkSizeInSeconds = 7; // 30
- const FlushIntervalInSeconds = 15; //300
+ const chunkSizeInSeconds = 30;
+ const flushIntervalInSeconds = 60;

Comment on lines +150 to +153
_chunkingTimer = Timer.periodic(const Duration(seconds: chunkSizeInSeconds), (t) async {
await _chunk();
});
_flushingTimer = Timer.periodic(const Duration(seconds: FlushIntervalInSeconds), (t) async {
_flushingTimer = Timer.periodic(const Duration(seconds: flushIntervalInSeconds), (t) async {
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The timer intervals have been updated to use the renamed constants. This maintains consistency in the codebase.

- _chunkingTimer = Timer.periodic(const Duration(seconds: ChunkSizeInSeconds), (t) async {
- _flushingTimer = Timer.periodic(const Duration(seconds: FlushIntervalInSeconds), (t) async {
+ _chunkingTimer = Timer.periodic(const Duration(seconds: chunkSizeInSeconds), (t) async {
+ _flushingTimer = Timer.periodic(const Duration(seconds: flushIntervalInSeconds), (t) async {

@@ -166,6 +163,7 @@ class WalService implements IWalService, IWalSocketServiceListener {
}

var framesPerSeconds = 100;
var lossesThreshold = 3 * framesPerSeconds; // 3s
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The introduction of lossesThreshold improves the readability of the code by making it clear what this value represents. It also makes it easier to adjust this value in the future if needed.

+ var lossesThreshold = 3 * framesPerSeconds; // 3s

Comment on lines +177 to +192
var low = high - framesPerSeconds * chunkSizeInSeconds;
if (low < 0) {
low = 0;
}
var synced = true;
var losses = 0;
var chunk = _frames.sublist(low, high);
for (var f in chunk) {
var head = f.sublist(0, 3);
var seq = Uint8List.fromList(head..add(0)).buffer.asByteData().getInt32(0);
if (!_syncFrameSeq.contains(seq)) {
synced = false;
break;
losses++;
if (losses >= lossesThreshold) {
synced = false;
break;
}
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The logic for frame synchronization has been adjusted. Now, instead of marking a chunk as unsynced as soon as a single frame is not found in _syncFrameSeq, it allows for up to lossesThreshold missing frames before marking the chunk as unsynced. This could potentially improve the robustness of the synchronization process against minor data losses.

- if (!_syncFrameSeq.contains(seq)) {
- synced = false;
- break;
+ if (!_syncFrameSeq.contains(seq)) {
+ losses++;
+ if (losses >= lossesThreshold) {
+ synced = false;
+ break;

@@ -218,7 +220,7 @@ class WalService implements IWalService, IWalSocketServiceListener {
}

// next
timerEnd -= ChunkSizeInSeconds;
timerEnd -= chunkSizeInSeconds;
Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The timer end calculation has been updated to use the renamed constant, maintaining consistency in the codebase.

- timerEnd -= ChunkSizeInSeconds;
+ timerEnd -= chunkSizeInSeconds;

Comment on lines 280 to 285
Future stop() async {
_socket?.stop();

debugPrint("wal service stop");
_chunkingTimer?.cancel();
_flushingTimer?.cancel();

Copy link

Choose a reason for hiding this comment

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

Image description Entelligence.AI

The removal of the debug print statement improves the cleanliness of the code. The rest of the changes are consistent with the previous state of the code.

- debugPrint("wal service stop");

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.

1 participant