From c655fdf68e87b23de99fa139f8415f363d12a62f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Mon, 7 Oct 2024 17:19:26 +0700 Subject: [PATCH 1/4] Add loss threshold --- app/lib/services/wals.dart | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/lib/services/wals.dart b/app/lib/services/wals.dart index 135f56bbd..83bceac84 100644 --- a/app/lib/services/wals.dart +++ b/app/lib/services/wals.dart @@ -166,6 +166,7 @@ class WalService implements IWalService, IWalSocketServiceListener { } var framesPerSeconds = 100; + var lossesThreshold = 1 * framesPerSeconds; // 1s var newFrameSyncDelaySeconds = 5; // wait 5s for new frame synced var timerEnd = DateTime.now().millisecondsSinceEpoch ~/ 1000 - newFrameSyncDelaySeconds; var pivot = _frames.length - newFrameSyncDelaySeconds * framesPerSeconds; @@ -181,13 +182,17 @@ class WalService implements IWalService, IWalSocketServiceListener { 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; + } } } var timerStart = timerEnd - (high - low) ~/ framesPerSeconds; From bc9340e014499e81d7cd34244ecd23b8b25a3ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Mon, 7 Oct 2024 17:34:01 +0700 Subject: [PATCH 2/4] Validate codec opus only for wals --- app/lib/providers/capture_provider.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/lib/providers/capture_provider.dart b/app/lib/providers/capture_provider.dart index 8b343a627..1876f1859 100644 --- a/app/lib/providers/capture_provider.dart +++ b/app/lib/providers/capture_provider.dart @@ -152,11 +152,21 @@ class CaptureProvider extends ChangeNotifier _bleBytesStream?.cancel(); _bleBytesStream = await _getBleAudioBytesListener(id, onAudioBytesReceived: (List value) { if (value.isEmpty) return; - wal.onByteStream(value); + + // support: opus + var isWalSupported = codec == BleAudioCodec.opus; + if (isWalSupported) { + wal.onByteStream(value); + } + if (_socket?.state == SocketServiceState.connected) { final trimmedValue = value.sublist(3); _socket?.send(trimmedValue); - wal.onBytesSync(value); + + // synced + if (isWalSupported) { + wal.onBytesSync(value); + } } }); setAudioBytesConnected(true); From fa90712848da9e1e8302faf2354a1e31e51af9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Mon, 7 Oct 2024 17:42:41 +0700 Subject: [PATCH 3/4] Revive chunk size 30s, flush interval 60s --- app/lib/services/wals.dart | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/lib/services/wals.dart b/app/lib/services/wals.dart index 83bceac84..f895d83bb 100644 --- a/app/lib/services/wals.dart +++ b/app/lib/services/wals.dart @@ -1,8 +1,6 @@ import 'dart:async'; import 'dart:collection'; -import 'dart:ffi'; import 'dart:io'; -import 'dart:math'; import 'dart:typed_data'; import 'package:flutter/foundation.dart'; @@ -10,13 +8,12 @@ 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; abstract class IWalService { void start(); @@ -74,7 +71,7 @@ class Wal { this.filePath, this.data = const []}); - get seconds => ChunkSizeInSeconds; + get seconds => chunkSizeInSeconds; factory Wal.fromJson(Map json) { return Wal( @@ -150,10 +147,10 @@ class WalService implements IWalService, IWalSocketServiceListener { void start() { _wals = SharedPreferencesUtil().wals; debugPrint("wal service start: ${_wals.length}"); - _chunkingTimer = Timer.periodic(const Duration(seconds: ChunkSizeInSeconds), (t) async { + _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 { await _flush(); }); _status = WalServiceStatus.ready; @@ -177,7 +174,7 @@ class WalService implements IWalService, IWalSocketServiceListener { // Scan backward var high = pivot; while (high > 0) { - var low = high - framesPerSeconds * ChunkSizeInSeconds; + var low = high - framesPerSeconds * chunkSizeInSeconds; if (low < 0) { low = 0; } @@ -223,7 +220,7 @@ class WalService implements IWalService, IWalSocketServiceListener { } // next - timerEnd -= ChunkSizeInSeconds; + timerEnd -= chunkSizeInSeconds; high = low; } @@ -283,7 +280,6 @@ class WalService implements IWalService, IWalSocketServiceListener { Future stop() async { _socket?.stop(); - debugPrint("wal service stop"); _chunkingTimer?.cancel(); _flushingTimer?.cancel(); From 843ff071909a68375ca5bd0b72337ba7f989cdc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Mon, 7 Oct 2024 17:59:48 +0700 Subject: [PATCH 4/4] Change losses threshold to 3s --- app/lib/services/wals.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/services/wals.dart b/app/lib/services/wals.dart index f895d83bb..292c77119 100644 --- a/app/lib/services/wals.dart +++ b/app/lib/services/wals.dart @@ -163,7 +163,7 @@ class WalService implements IWalService, IWalSocketServiceListener { } var framesPerSeconds = 100; - var lossesThreshold = 1 * framesPerSeconds; // 1s + var lossesThreshold = 3 * framesPerSeconds; // 3s var newFrameSyncDelaySeconds = 5; // wait 5s for new frame synced var timerEnd = DateTime.now().millisecondsSinceEpoch ~/ 1000 - newFrameSyncDelaySeconds; var pivot = _frames.length - newFrameSyncDelaySeconds * framesPerSeconds;