Use single microphone stream for recording
All checks were successful
Build and deploy Flutter Web / build (push) Successful in 2m50s

This commit is contained in:
Ruslan Bakiev
2026-05-14 09:16:31 +07:00
parent 21945b2335
commit 584e30624d
3 changed files with 56 additions and 61 deletions

View File

@@ -2,13 +2,13 @@ import 'dart:async';
import 'dart:convert';
import 'dart:math' as math;
import 'package:cross_file/cross_file.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:latlong2/latlong.dart' hide Path;
import 'package:waveform_flutter/waveform_flutter.dart' show Amplitude;
import 'package:waveform_recorder/waveform_recorder.dart';
import 'package:record/record.dart' as record;
import '../api/mapflow_api.dart';
import '../auth/telegram_login_button.dart';
@@ -98,29 +98,29 @@ class _MapContent extends ConsumerWidget {
SafeArea(
child: Align(
alignment: Alignment.topLeft,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
_UserAvatar(
user: state.currentUser,
onLogout: () {
telegram_session.clearMapflowSession();
ref.invalidate(placeControllerProvider);
telegram_session.reloadApp();
},
),
if (state.currentUser?.isAdmin == true)
_AdminReviewsButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => const AdminVoiceExperiencesScreen(),
),
),
),
],
child: _UserAvatar(
user: state.currentUser,
onLogout: () {
telegram_session.clearMapflowSession();
ref.invalidate(placeControllerProvider);
telegram_session.reloadApp();
},
),
),
),
if (state.currentUser?.isAdmin == true)
SafeArea(
child: Align(
alignment: Alignment.topRight,
child: _AdminReviewsButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => const AdminVoiceExperiencesScreen(),
),
),
),
),
),
if (availableTraits.isNotEmpty)
SafeArea(
child: Align(
@@ -281,7 +281,7 @@ class _AdminReviewsButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.only(top: 8, right: 12),
child: FilledButton.icon(
onPressed: onPressed,
icon: const Icon(Icons.table_rows_outlined, size: 18),
@@ -711,21 +711,22 @@ class AddExperienceFlow extends ConsumerStatefulWidget {
class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
static const _minimumInformationUnits = 16.0;
static const _nearbyPlaceRadiusMeters = 200;
final _api = MapflowApi();
final _waveController = WaveformRecorderController(
interval: const Duration(milliseconds: 45),
config: const RecordConfig(
numChannels: 1,
sampleRate: 44100,
autoGain: true,
echoCancel: true,
noiseSuppress: true,
),
static const _recordConfig = record.RecordConfig(
encoder: record.AudioEncoder.wav,
numChannels: 1,
sampleRate: 44100,
autoGain: true,
echoCancel: true,
noiseSuppress: true,
);
final _api = MapflowApi();
final _audioRecorder = record.AudioRecorder();
final _recordStopwatch = Stopwatch();
Future<List<PlaceRecommendation>>? _nearbyPlacesFuture;
StreamSubscription<Amplitude>? _amplitudeSub;
StreamSubscription<record.Amplitude>? _amplitudeSub;
XFile? _recordedFile;
var _step = 0;
var _informationUnits = 0.0;
var _recording = false;
@@ -744,7 +745,7 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
@override
void dispose() {
_amplitudeSub?.cancel();
_waveController.dispose();
_audioRecorder.dispose();
super.dispose();
}
@@ -770,10 +771,17 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
}
Future<void> _startRecording() async {
await _waveController.startRecording();
_recordedFile = null;
final path = 'mapflow-${DateTime.now().microsecondsSinceEpoch}.wav';
await _audioRecorder.start(_recordConfig, path: path);
await _amplitudeSub?.cancel();
_recordStopwatch
..reset()
..start();
_lastInformationAt = DateTime.now();
_amplitudeSub = _waveController.amplitudeStream.listen(_handleAmplitude);
_amplitudeSub = _audioRecorder
.onAmplitudeChanged(const Duration(milliseconds: 45))
.listen(_handleAmplitude);
setState(() {
_micAllowed = true;
@@ -786,7 +794,11 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
Future<void> _stopRecording() async {
await _amplitudeSub?.cancel();
_amplitudeSub = null;
await _waveController.stopRecording();
final path = await _audioRecorder.stop();
_recordStopwatch.stop();
if (path != null && path.isNotEmpty) {
_recordedFile = XFile(path, mimeType: 'audio/wav');
}
_lastInformationAt = null;
if (!mounted) {
return;
@@ -797,7 +809,7 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
});
}
void _handleAmplitude(Amplitude amplitude) {
void _handleAmplitude(record.Amplitude amplitude) {
final currentDb = amplitude.current;
final now = DateTime.now();
final level = _normalizeDbLevel(currentDb);
@@ -812,7 +824,7 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
});
ref
.read(placeControllerProvider.notifier)
.setReviewDuration(_waveController.timeElapsed);
.setReviewDuration(_recordStopwatch.elapsed);
}
double _normalizeDbLevel(double currentDb) {
@@ -880,7 +892,7 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
onSelect: (place) async {
setState(() => _submitting = true);
controller.setReviewPlace(place.name);
final file = _waveController.file;
final file = _recordedFile;
if (file == null) {
throw StateError('Voice recording file is required.');
}