mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
Add live mesh traffic view
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -80,10 +80,11 @@ create_feature_graphic.py
|
||||
# Tool caches and local state
|
||||
.cachebro/
|
||||
.osgrep/
|
||||
third_party/lpcnet_flutter/
|
||||
|
||||
# Claude Code local settings (permissions, personal config)
|
||||
.claude/settings.local.json
|
||||
|
||||
# Dart code coverage
|
||||
coverage/
|
||||
lcov.info
|
||||
lcov.info
|
||||
|
||||
@@ -57,8 +57,6 @@ PODS:
|
||||
- FlutterMacOS
|
||||
- image_picker_ios (0.0.1):
|
||||
- Flutter
|
||||
- lpcnet_flutter (0.1.0):
|
||||
- Flutter
|
||||
- nsd_ios (0.0.1):
|
||||
- Flutter
|
||||
- package_info_plus (0.4.5):
|
||||
@@ -100,7 +98,6 @@ DEPENDENCIES:
|
||||
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
|
||||
- geolocator_apple (from `.symlinks/plugins/geolocator_apple/darwin`)
|
||||
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
|
||||
- lpcnet_flutter (from `.symlinks/plugins/lpcnet_flutter/ios`)
|
||||
- nsd_ios (from `.symlinks/plugins/nsd_ios/ios`)
|
||||
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
|
||||
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
|
||||
@@ -144,8 +141,6 @@ EXTERNAL SOURCES:
|
||||
:path: ".symlinks/plugins/geolocator_apple/darwin"
|
||||
image_picker_ios:
|
||||
:path: ".symlinks/plugins/image_picker_ios/ios"
|
||||
lpcnet_flutter:
|
||||
:path: ".symlinks/plugins/lpcnet_flutter/ios"
|
||||
nsd_ios:
|
||||
:path: ".symlinks/plugins/nsd_ios/ios"
|
||||
package_info_plus:
|
||||
@@ -182,7 +177,6 @@ SPEC CHECKSUMS:
|
||||
flutter_local_notifications: 643a3eda1ce1c0599413ca31672536d423dee214
|
||||
geolocator_apple: ab36aa0e8b7d7a2d7639b3b4e48308394e8cef5e
|
||||
image_picker_ios: e0ece4aa2a75771a7de3fa735d26d90817041326
|
||||
lpcnet_flutter: 67a3c72ea4caa511373ee9b028223e33f0e85b97
|
||||
nsd_ios: 596ad79109ddd3e52d665f650f36428049e3653e
|
||||
package_info_plus: af8e2ca6888548050f16fa2f1938db7b5a5df499
|
||||
path_provider_foundation: bb55f6dbba17d0dccd6737fe6f7f34fbd0376880
|
||||
|
||||
@@ -325,25 +325,7 @@ class VoiceProvider with ChangeNotifier {
|
||||
}
|
||||
|
||||
Int16List _preparePlaybackPcm(Int16List pcm, VoicePacketMode mode) {
|
||||
if (pcm.isEmpty) {
|
||||
return pcm;
|
||||
}
|
||||
|
||||
if (mode.codec != VoiceCodecKind.lpcnet) {
|
||||
return pcm;
|
||||
}
|
||||
|
||||
final output = Int16List(pcm.length);
|
||||
var dc = 0.0;
|
||||
const dcAlpha = 0.995;
|
||||
|
||||
for (var i = 0; i < pcm.length; i++) {
|
||||
final sample = pcm[i].toDouble();
|
||||
dc = (dcAlpha * dc) + ((1.0 - dcAlpha) * sample);
|
||||
final filtered = sample - dc;
|
||||
output[i] = filtered.clamp(-32768.0, 32767.0).round();
|
||||
}
|
||||
return output;
|
||||
return pcm;
|
||||
}
|
||||
|
||||
Future<void> clearStoredVoiceData() async {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,6 @@ import '../widgets/messages/messages_content.dart';
|
||||
import '../widgets/common/contact_avatar.dart';
|
||||
import '../services/message_destination_preferences.dart';
|
||||
import '../services/voice_bitrate_preferences.dart';
|
||||
import '../services/voice_codec_preferences.dart';
|
||||
import '../services/voice_recorder_service.dart';
|
||||
import '../services/voice_codec_service.dart';
|
||||
import '../utils/toast_logger.dart';
|
||||
@@ -72,7 +71,7 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
final VoiceRecorderService _voiceRecorder = VoiceRecorderService();
|
||||
bool _isRecording = false;
|
||||
bool _isSendingVoice = false;
|
||||
static const Duration _maxVoiceRecordingDuration = Duration(seconds: 4);
|
||||
static const Duration _maxVoiceRecordingDuration = Duration(seconds: 30);
|
||||
static const double _silenceRmsThreshold = 500.0;
|
||||
static const double _silencePeakThreshold = 1400.0;
|
||||
static const int _maxInteriorSilentChunks = 2;
|
||||
@@ -82,7 +81,6 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
final List<Int16List> _recordedChunks = [];
|
||||
VoicePacketMode? _activeVoiceMode;
|
||||
int _selectedVoiceBitrate = VoiceBitratePreferences.defaultBitrate;
|
||||
VoiceCodecKind _selectedVoiceCodec = VoiceCodecPreferences.defaultCodec;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -98,11 +96,9 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
|
||||
Future<void> _loadVoiceSettings() async {
|
||||
final bitrate = await VoiceBitratePreferences.getBitrate();
|
||||
final codec = await VoiceCodecPreferences.getCodec();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_selectedVoiceBitrate = bitrate;
|
||||
_selectedVoiceCodec = codec;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -823,15 +819,12 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
debugPrint('🎙️ [Voice] _startVoiceRecording called');
|
||||
// Read fresh voice preferences so settings changes apply immediately.
|
||||
final selectedBitrate = await VoiceBitratePreferences.getBitrate();
|
||||
final selectedCodec = await VoiceCodecPreferences.getCodec();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_selectedVoiceBitrate = selectedBitrate;
|
||||
_selectedVoiceCodec = selectedCodec;
|
||||
});
|
||||
} else {
|
||||
_selectedVoiceBitrate = selectedBitrate;
|
||||
_selectedVoiceCodec = selectedCodec;
|
||||
}
|
||||
final hasPermission = await _voiceRecorder.requestPermission();
|
||||
debugPrint('🎙️ [Voice] hasPermission=$hasPermission');
|
||||
@@ -859,9 +852,9 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
(_) => rng.nextInt(256),
|
||||
).map((b) => b.toRadixString(16).padLeft(2, '0')).join();
|
||||
|
||||
_activeVoiceMode = _selectedVoiceCodec == VoiceCodecKind.lpcnet
|
||||
? VoicePacketMode.lpcnet1600
|
||||
: VoiceBitratePreferences.toVoiceMode(_selectedVoiceBitrate);
|
||||
_activeVoiceMode = VoiceBitratePreferences.toVoiceMode(
|
||||
_selectedVoiceBitrate,
|
||||
);
|
||||
final packetDuration = Duration(
|
||||
milliseconds: _activeVoiceMode!.packetDurationMs,
|
||||
);
|
||||
@@ -878,7 +871,6 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
final stream = _voiceRecorder.startCapture(
|
||||
chunkDuration: packetDuration,
|
||||
sampleRateHz: _activeVoiceMode!.sampleRateHz,
|
||||
codecKind: _activeVoiceMode!.codec,
|
||||
enableBandPassFilter: appProvider.isVoiceBandPassFilterEnabled,
|
||||
enableCompressor: appProvider.isVoiceCompressorEnabled,
|
||||
enableLimiter: appProvider.isVoiceLimiterEnabled,
|
||||
@@ -1021,15 +1013,10 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
debugPrint(
|
||||
'🎙️ [Voice] encoding $total packets for deferred voice fetch, mode=${mode.label}, session=$sessionId',
|
||||
);
|
||||
final encodedChunks = mode.codec == VoiceCodecKind.lpcnet
|
||||
? await _encodeLpcNetChunks(codec, chunks, mode)
|
||||
: <Uint8List>[];
|
||||
for (var i = 0; i < total; i++) {
|
||||
if (!mounted) return;
|
||||
try {
|
||||
final codec2Data = mode.codec == VoiceCodecKind.lpcnet
|
||||
? encodedChunks[i]
|
||||
: await codec.encode(chunks[i], mode);
|
||||
final codec2Data = await codec.encode(chunks[i], mode);
|
||||
debugPrint(
|
||||
'🎙️ [Voice] packet $i/$total encoded: ${codec2Data.length} bytes',
|
||||
);
|
||||
@@ -1133,36 +1120,6 @@ class _MessagesTabState extends State<MessagesTab> {
|
||||
messagesProvider.markMessageSent(msgId, 0, 0);
|
||||
}
|
||||
|
||||
Future<List<Uint8List>> _encodeLpcNetChunks(
|
||||
VoiceCodecService codec,
|
||||
List<Int16List> chunks,
|
||||
VoicePacketMode mode,
|
||||
) async {
|
||||
final totalSamples = chunks.fold<int>(
|
||||
0,
|
||||
(sum, chunk) => sum + chunk.length,
|
||||
);
|
||||
final merged = Int16List(totalSamples);
|
||||
final encodedByteLengths = <int>[];
|
||||
var sampleOffset = 0;
|
||||
for (final chunk in chunks) {
|
||||
merged.setRange(sampleOffset, sampleOffset + chunk.length, chunk);
|
||||
sampleOffset += chunk.length;
|
||||
encodedByteLengths.add((chunk.length ~/ 640) * 8);
|
||||
}
|
||||
|
||||
final encoded = await codec.encode(merged, mode);
|
||||
final encodedChunks = <Uint8List>[];
|
||||
var byteOffset = 0;
|
||||
for (final length in encodedByteLengths) {
|
||||
encodedChunks.add(
|
||||
Uint8List.sublistView(encoded, byteOffset, byteOffset + length),
|
||||
);
|
||||
byteOffset += length;
|
||||
}
|
||||
return encodedChunks;
|
||||
}
|
||||
|
||||
List<Int16List> _prepareChunksForSending(
|
||||
List<Int16List> chunks,
|
||||
VoicePacketMode mode,
|
||||
|
||||
@@ -19,7 +19,6 @@ import '../services/locale_preferences.dart';
|
||||
import '../services/mesh_map_nodes_service.dart';
|
||||
import '../services/update_checker_service.dart';
|
||||
import '../services/voice_bitrate_preferences.dart';
|
||||
import '../services/voice_codec_preferences.dart';
|
||||
import '../services/image_preferences.dart';
|
||||
import '../services/route_hash_preferences.dart';
|
||||
import '../services/image_codec_service.dart';
|
||||
@@ -60,7 +59,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
bool _showRxTxIndicators = true;
|
||||
bool _isCheckingForUpdates = false;
|
||||
int _voiceBitrate = VoiceBitratePreferences.defaultBitrate;
|
||||
VoiceCodecKind _voiceCodec = VoiceCodecPreferences.defaultCodec;
|
||||
int _routeHashSize = RouteHashPreferences.defaultHashSize;
|
||||
int _imageMaxSize = ImagePreferences.defaultMaxSize;
|
||||
int _imageCompression = ImagePreferences.defaultQuality;
|
||||
@@ -181,11 +179,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
|
||||
Future<void> _loadVoicePreferences() async {
|
||||
final value = await VoiceBitratePreferences.getBitrate();
|
||||
final codec = await VoiceCodecPreferences.getCodec();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_voiceBitrate = value;
|
||||
_voiceCodec = codec;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -197,27 +193,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _saveVoiceCodecPreference(VoiceCodecKind value) async {
|
||||
await VoiceCodecPreferences.setCodec(value);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_voiceCodec = value;
|
||||
});
|
||||
}
|
||||
|
||||
String _voiceBitrateSubtitle(int bitrate) {
|
||||
if (_voiceCodec == VoiceCodecKind.lpcnet) {
|
||||
return 'Fixed 1600 bps';
|
||||
}
|
||||
return '$bitrate bps';
|
||||
}
|
||||
|
||||
String _voiceCodecSubtitle() {
|
||||
return _voiceCodec == VoiceCodecKind.codec2
|
||||
? 'Selectable low-bitrate modes'
|
||||
: 'Fixed 16 kHz / 1.6 kbps mode';
|
||||
}
|
||||
|
||||
Future<void> _loadRouteHashSizePreference() async {
|
||||
final value = await RouteHashPreferences.getHashSize();
|
||||
if (!mounted) return;
|
||||
@@ -1179,7 +1158,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
Consumer2<AppProvider, ConnectionProvider>(
|
||||
builder: (context, appProvider, connectionProvider, child) =>
|
||||
_buildVoiceStatsCard(
|
||||
codec: _voiceCodec,
|
||||
bitrate: _voiceBitrate,
|
||||
connectionProvider: connectionProvider,
|
||||
bandPassEnabled: appProvider.isVoiceBandPassFilterEnabled,
|
||||
@@ -1194,22 +1172,12 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
),
|
||||
),
|
||||
_buildSettingsCard([
|
||||
ListTile(
|
||||
leading: const Icon(Icons.record_voice_over),
|
||||
title: const Text('Voice codec'),
|
||||
subtitle: Text(_voiceCodecSubtitle()),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: _showVoiceCodecDialog,
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.graphic_eq),
|
||||
title: const Text('Voice bitrate'),
|
||||
subtitle: Text(_voiceBitrateSubtitle(_voiceBitrate)),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
enabled: _voiceCodec == VoiceCodecKind.codec2,
|
||||
onTap: _voiceCodec == VoiceCodecKind.codec2
|
||||
? _showVoiceBitrateDialog
|
||||
: null,
|
||||
onTap: _showVoiceBitrateDialog,
|
||||
),
|
||||
Consumer<AppProvider>(
|
||||
builder: (context, appProvider, child) => SwitchListTile(
|
||||
@@ -1797,7 +1765,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
}
|
||||
|
||||
Widget _buildVoiceStatsCard({
|
||||
required VoiceCodecKind codec,
|
||||
required int bitrate,
|
||||
required ConnectionProvider connectionProvider,
|
||||
required bool bandPassEnabled,
|
||||
@@ -1826,9 +1793,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
final radioSf = connectionProvider.deviceInfo.radioSf;
|
||||
final radioCr = connectionProvider.deviceInfo.radioCr;
|
||||
final bwHz = _resolveBandwidthHz(radioBw);
|
||||
final voiceMode = codec == VoiceCodecKind.lpcnet
|
||||
? VoicePacketMode.lpcnet1600
|
||||
: VoiceBitratePreferences.toVoiceMode(bitrate);
|
||||
final voiceMode = VoiceBitratePreferences.toVoiceMode(bitrate);
|
||||
const voicePreviewMs = 10000; // 10-second reference clip
|
||||
final packetDurationMs = voiceMode.packetDurationMs;
|
||||
final voicePacketCount =
|
||||
@@ -1865,7 +1830,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Codec: ${voiceMode.label}${codec == VoiceCodecKind.codec2 ? ' · $bitrate bps' : ' · 1600 bps'}',
|
||||
'Codec: ${voiceMode.label} · $bitrate bps',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
@@ -2219,47 +2184,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
void _showVoiceCodecDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Voice codec'),
|
||||
content: SingleChildScrollView(
|
||||
child: RadioGroup<VoiceCodecKind>(
|
||||
groupValue: _voiceCodec,
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
_saveVoiceCodecPreference(value);
|
||||
}
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
RadioListTile<VoiceCodecKind>(
|
||||
value: VoiceCodecKind.codec2,
|
||||
title: const Text('Codec2'),
|
||||
subtitle: const Text('Selectable ultra-low bitrate modes'),
|
||||
),
|
||||
RadioListTile<VoiceCodecKind>(
|
||||
value: VoiceCodecKind.lpcnet,
|
||||
title: const Text('LPCNet'),
|
||||
subtitle: const Text('Fixed 16 kHz / 1.6 kbps neural mode'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(AppLocalizations.of(context)!.cancel),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showAboutDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:codec2_flutter/codec2_flutter.dart';
|
||||
import 'package:lpcnet_flutter/lpcnet_flutter.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../utils/voice_message_parser.dart';
|
||||
|
||||
@@ -20,8 +19,6 @@ Codec2Mode codec2ModeFor(VoicePacketMode pktMode) {
|
||||
return Codec2Mode.mode1300;
|
||||
case VoicePacketMode.mode2400:
|
||||
return Codec2Mode.mode2400;
|
||||
case VoicePacketMode.lpcnet1600:
|
||||
throw ArgumentError('LPCNet mode does not map to Codec2');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,22 +45,12 @@ class VoiceCodecService {
|
||||
|
||||
Future<Uint8List> encode(Int16List pcm, VoicePacketMode mode) {
|
||||
_ensureCodec2Supported();
|
||||
switch (mode.codec) {
|
||||
case VoiceCodecKind.codec2:
|
||||
return Codec2.encodeInIsolate(pcm, codec2ModeFor(mode));
|
||||
case VoiceCodecKind.lpcnet:
|
||||
return LpcNet.encodeInIsolate(pcm);
|
||||
}
|
||||
return Codec2.encodeInIsolate(pcm, codec2ModeFor(mode));
|
||||
}
|
||||
|
||||
Future<Int16List> decode(Uint8List codec2Bytes, VoicePacketMode mode) {
|
||||
_ensureCodec2Supported();
|
||||
switch (mode.codec) {
|
||||
case VoiceCodecKind.codec2:
|
||||
return Codec2.decodeInIsolate(codec2Bytes, codec2ModeFor(mode));
|
||||
case VoiceCodecKind.lpcnet:
|
||||
return LpcNet.decodeInIsolate(codec2Bytes);
|
||||
}
|
||||
return Codec2.decodeInIsolate(codec2Bytes, codec2ModeFor(mode));
|
||||
}
|
||||
|
||||
/// Decode and concatenate multiple [packets] into a single PCM Int16List.
|
||||
@@ -73,9 +60,6 @@ class VoiceCodecService {
|
||||
VoicePacketMode mode,
|
||||
) async {
|
||||
_ensureCodec2Supported();
|
||||
if (mode.codec == VoiceCodecKind.lpcnet) {
|
||||
return _decodeLpcNetPackets(packets, mode);
|
||||
}
|
||||
final all = <Int16List>[];
|
||||
for (final pkt in packets) {
|
||||
if (pkt == null || pkt.codec2Data.isEmpty) {
|
||||
@@ -93,38 +77,4 @@ class VoiceCodecService {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Int16List> _decodeLpcNetPackets(
|
||||
List<VoicePacket?> packets,
|
||||
VoicePacketMode mode,
|
||||
) async {
|
||||
final segments = <Int16List>[];
|
||||
final run = <int>[];
|
||||
|
||||
Future<void> flushRun() async {
|
||||
if (run.isEmpty) return;
|
||||
final decoded = await LpcNet.decodeInIsolate(Uint8List.fromList(run));
|
||||
segments.add(decoded);
|
||||
run.clear();
|
||||
}
|
||||
|
||||
for (final pkt in packets) {
|
||||
if (pkt == null || pkt.codec2Data.isEmpty) {
|
||||
await flushRun();
|
||||
segments.add(Int16List(mode.samplesPerPacket));
|
||||
continue;
|
||||
}
|
||||
run.addAll(pkt.codec2Data);
|
||||
}
|
||||
await flushRun();
|
||||
|
||||
final total = segments.fold<int>(0, (sum, chunk) => sum + chunk.length);
|
||||
final result = Int16List(total);
|
||||
var offset = 0;
|
||||
for (final chunk in segments) {
|
||||
result.setRange(offset, offset + chunk.length, chunk);
|
||||
offset += chunk.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,8 +60,6 @@ Codec2Mode codec2ModeFor(VoicePacketMode pktMode) {
|
||||
return Codec2Mode.mode1300;
|
||||
case VoicePacketMode.mode2400:
|
||||
return Codec2Mode.mode2400;
|
||||
case VoicePacketMode.lpcnet1600:
|
||||
throw ArgumentError('LPCNet mode does not map to Codec2');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import 'dart:math' as math;
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:record/record.dart';
|
||||
import '../utils/voice_message_parser.dart';
|
||||
|
||||
/// Captures raw PCM audio at a codec-selected sample rate, 16-bit mono.
|
||||
///
|
||||
@@ -32,7 +31,6 @@ class VoiceRecorderService {
|
||||
Stream<Int16List> startCapture({
|
||||
Duration chunkDuration = const Duration(seconds: 1),
|
||||
int sampleRateHz = 8000,
|
||||
VoiceCodecKind codecKind = VoiceCodecKind.codec2,
|
||||
bool enableBandPassFilter = true,
|
||||
bool enableCompressor = true,
|
||||
bool enableLimiter = true,
|
||||
@@ -50,7 +48,6 @@ class VoiceRecorderService {
|
||||
_startRecording(
|
||||
chunkDuration,
|
||||
sampleRateHz: sampleRateHz,
|
||||
codecKind: codecKind,
|
||||
enableBandPassFilter: enableBandPassFilter,
|
||||
enableCompressor: enableCompressor,
|
||||
enableLimiter: enableLimiter,
|
||||
@@ -64,7 +61,6 @@ class VoiceRecorderService {
|
||||
Future<void> _startRecording(
|
||||
Duration chunkDuration, {
|
||||
required int sampleRateHz,
|
||||
required VoiceCodecKind codecKind,
|
||||
required bool enableBandPassFilter,
|
||||
required bool enableCompressor,
|
||||
required bool enableLimiter,
|
||||
@@ -72,10 +68,9 @@ class VoiceRecorderService {
|
||||
required bool enableEchoCancellation,
|
||||
required bool enableNoiseSuppression,
|
||||
}) async {
|
||||
final bypassProcessing = codecKind == VoiceCodecKind.lpcnet;
|
||||
final useBandPassFilter = !bypassProcessing && enableBandPassFilter;
|
||||
final useCompressor = !bypassProcessing && enableCompressor;
|
||||
final useLimiter = !bypassProcessing && enableLimiter;
|
||||
final useBandPassFilter = enableBandPassFilter;
|
||||
final useCompressor = enableCompressor;
|
||||
final useLimiter = enableLimiter;
|
||||
final config = RecordConfig(
|
||||
encoder: AudioEncoder.pcm16bits,
|
||||
sampleRate: sampleRateHz,
|
||||
@@ -88,25 +83,21 @@ class VoiceRecorderService {
|
||||
|
||||
try {
|
||||
final stream = await _recorder.startStream(config);
|
||||
final voiceFilter = bypassProcessing
|
||||
? null
|
||||
: _VoiceBandPassFilter(
|
||||
sampleRate: sampleRateHz,
|
||||
lowCutHz: 250.0,
|
||||
highCutHz: 3400.0,
|
||||
);
|
||||
final dynamics = bypassProcessing
|
||||
? null
|
||||
: _VoiceDynamicsProcessor(
|
||||
sampleRate: sampleRateHz,
|
||||
thresholdDb: -18.0,
|
||||
ratio: 2.5,
|
||||
attackMs: 8.0,
|
||||
releaseMs: 120.0,
|
||||
makeupGainDb: 4.0,
|
||||
enableCompressor: useCompressor,
|
||||
enableLimiter: useLimiter,
|
||||
);
|
||||
final voiceFilter = _VoiceBandPassFilter(
|
||||
sampleRate: sampleRateHz,
|
||||
lowCutHz: 250.0,
|
||||
highCutHz: 3400.0,
|
||||
);
|
||||
final dynamics = _VoiceDynamicsProcessor(
|
||||
sampleRate: sampleRateHz,
|
||||
thresholdDb: -18.0,
|
||||
ratio: 2.5,
|
||||
attackMs: 8.0,
|
||||
releaseMs: 120.0,
|
||||
makeupGainDb: 4.0,
|
||||
enableCompressor: useCompressor,
|
||||
enableLimiter: useLimiter,
|
||||
);
|
||||
final chunkBytes =
|
||||
sampleRateHz * 2 * chunkDuration.inMilliseconds ~/ 1000;
|
||||
final buffer = <int>[];
|
||||
@@ -118,28 +109,16 @@ class VoiceRecorderService {
|
||||
final chunk = buffer.sublist(0, chunkBytes);
|
||||
buffer.removeRange(0, chunkBytes);
|
||||
final pcm = _bytesToInt16(Uint8List.fromList(chunk));
|
||||
if (bypassProcessing) {
|
||||
_controller?.add(pcm);
|
||||
} else {
|
||||
final filtered = useBandPassFilter
|
||||
? voiceFilter!.process(pcm)
|
||||
: pcm;
|
||||
_controller?.add(dynamics!.process(filtered));
|
||||
}
|
||||
final filtered = useBandPassFilter ? voiceFilter.process(pcm) : pcm;
|
||||
_controller?.add(dynamics.process(filtered));
|
||||
}
|
||||
},
|
||||
onDone: () {
|
||||
if (buffer.isNotEmpty) {
|
||||
final padded = _padToEven(buffer);
|
||||
final pcm = _bytesToInt16(Uint8List.fromList(padded));
|
||||
if (bypassProcessing) {
|
||||
_controller?.add(pcm);
|
||||
} else {
|
||||
final filtered = useBandPassFilter
|
||||
? voiceFilter!.process(pcm)
|
||||
: pcm;
|
||||
_controller?.add(dynamics!.process(filtered));
|
||||
}
|
||||
final filtered = useBandPassFilter ? voiceFilter.process(pcm) : pcm;
|
||||
_controller?.add(dynamics.process(filtered));
|
||||
}
|
||||
_controller?.close();
|
||||
},
|
||||
|
||||
@@ -12,8 +12,7 @@ const int _defaultLoRaExplicitHeader = 1;
|
||||
const double _defaultAirtimeBudgetFactor = 1.0; // one half duty-cycle
|
||||
|
||||
enum VoiceCodecKind {
|
||||
codec2(0, 'Codec2'),
|
||||
lpcnet(1, 'LPCNet');
|
||||
codec2(0, 'Codec2');
|
||||
|
||||
const VoiceCodecKind(this.id, this.label);
|
||||
final int id;
|
||||
@@ -29,8 +28,7 @@ enum VoicePacketMode {
|
||||
mode1300(3, '1300', VoiceCodecKind.codec2, 8000, 175, 880),
|
||||
mode1400(4, '1400', VoiceCodecKind.codec2, 8000, 175, 880),
|
||||
mode1600(5, '1600', VoiceCodecKind.codec2, 8000, 200, 800),
|
||||
mode3200(6, '3200', VoiceCodecKind.codec2, 8000, 400, 400),
|
||||
lpcnet1600(7, 'LPCNet', VoiceCodecKind.lpcnet, 16000, 200, 40);
|
||||
mode3200(6, '3200', VoiceCodecKind.codec2, 8000, 400, 400);
|
||||
|
||||
const VoicePacketMode(
|
||||
this.id,
|
||||
@@ -176,8 +174,14 @@ class VoicePacket {
|
||||
|
||||
/// Estimated audio duration of this packet in milliseconds.
|
||||
int get durationMs {
|
||||
if (mode.bytesPerSecond == 0) return 0;
|
||||
return (codec2Data.length * 1000 ~/ mode.bytesPerSecond).clamp(0, 1500);
|
||||
try {
|
||||
final bytesPerSecond = voiceModeBytesPerSecond(mode);
|
||||
if (bytesPerSecond <= 0) return 0;
|
||||
return (codec2Data.length * 1000 ~/ bytesPerSecond).clamp(0, 1500);
|
||||
} catch (_) {
|
||||
// Be permissive with stale or malformed persisted voice metadata.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -257,7 +261,13 @@ class VoiceEnvelope {
|
||||
}
|
||||
|
||||
int voiceModeBytesPerSecond(VoicePacketMode mode) => switch (mode) {
|
||||
_ => mode.bytesPerSecond,
|
||||
VoicePacketMode.mode700c => 100,
|
||||
VoicePacketMode.mode1200 => 150,
|
||||
VoicePacketMode.mode2400 => 300,
|
||||
VoicePacketMode.mode1300 => 175,
|
||||
VoicePacketMode.mode1400 => 175,
|
||||
VoicePacketMode.mode1600 => 200,
|
||||
VoicePacketMode.mode3200 => 400,
|
||||
};
|
||||
|
||||
/// Approximate end-to-end transmit time for a voice session over MeshCore LoRa.
|
||||
|
||||
@@ -775,15 +775,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.1.0"
|
||||
lpcnet_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "."
|
||||
ref: main
|
||||
resolved-ref: "39fadf011552a785993657506aedff85186b968b"
|
||||
url: "https://github.com/dz0ny/lpcnet_flutter.git"
|
||||
source: git
|
||||
version: "0.1.0"
|
||||
matcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -51,10 +51,6 @@ dependencies:
|
||||
git:
|
||||
url: https://github.com/dz0ny/codec2_flutter.git
|
||||
ref: 03998a2
|
||||
lpcnet_flutter:
|
||||
git:
|
||||
url: https://github.com/dz0ny/lpcnet_flutter.git
|
||||
ref: main
|
||||
ffi: ^2.1.0
|
||||
|
||||
# Voice recording (raw PCM stream at 8000 Hz)
|
||||
|
||||
Reference in New Issue
Block a user