Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixUI/hesabix_ui/lib/services/voice/voice_dictation_web.dart
2026-08-18 17:20:26 +00:00

44 lines
958 B
Dart

import 'dart:js_interop';
import 'dart:typed_data';
import 'package:web/web.dart' as web;
import 'voice_web_capture.dart';
class VoiceDictationController {
VoiceWebCapture? _capture;
web.AudioContext? _ctx;
final BytesBuilder _buffer = BytesBuilder(copy: false);
Future<void> start({required void Function(List<int> pcmChunk) onPcm}) async {
_buffer.clear();
_ctx = web.AudioContext();
try {
await _ctx!.resume().toDart;
} catch (_) {}
_capture = VoiceWebCapture(
onPcmFrame: (pcm) {
if (pcm.isEmpty) return;
_buffer.add(pcm);
onPcm(pcm);
},
);
await _capture!.start(_ctx!, 16000);
}
Future<List<int>> stop() async {
try {
await _capture?.stop();
} catch (_) {}
_capture = null;
try {
await _ctx?.close().toDart;
} catch (_) {}
_ctx = null;
return _buffer.takeBytes();
}
Future<void> dispose() async {
await stop();
}
}