Decouple voice progress from amplitude stream
All checks were successful
Build and deploy Flutter Web / build (push) Successful in 2m30s
All checks were successful
Build and deploy Flutter Web / build (push) Successful in 2m30s
This commit is contained in:
@@ -727,12 +727,14 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
|
|||||||
|
|
||||||
Future<List<PlaceRecommendation>>? _nearbyPlacesFuture;
|
Future<List<PlaceRecommendation>>? _nearbyPlacesFuture;
|
||||||
StreamSubscription<record.Amplitude>? _amplitudeSub;
|
StreamSubscription<record.Amplitude>? _amplitudeSub;
|
||||||
|
Timer? _progressTimer;
|
||||||
XFile? _recordedFile;
|
XFile? _recordedFile;
|
||||||
var _step = 0;
|
var _step = 0;
|
||||||
var _informationUnits = 0.0;
|
var _informationUnits = 0.0;
|
||||||
var _recording = false;
|
var _recording = false;
|
||||||
var _submitting = false;
|
var _submitting = false;
|
||||||
var _micAllowed = true;
|
var _micAllowed = true;
|
||||||
|
var _heardVoice = false;
|
||||||
var _noiseDb = -72.0;
|
var _noiseDb = -72.0;
|
||||||
var _voicePeakDb = -34.0;
|
var _voicePeakDb = -34.0;
|
||||||
var _liveLevel = 0.0;
|
var _liveLevel = 0.0;
|
||||||
@@ -746,6 +748,7 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_amplitudeSub?.cancel();
|
_amplitudeSub?.cancel();
|
||||||
|
_progressTimer?.cancel();
|
||||||
_audioRecorder.dispose();
|
_audioRecorder.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@@ -783,10 +786,16 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
|
|||||||
_amplitudeSub = _audioRecorder
|
_amplitudeSub = _audioRecorder
|
||||||
.onAmplitudeChanged(_amplitudeInterval)
|
.onAmplitudeChanged(_amplitudeInterval)
|
||||||
.listen(_handleAmplitude);
|
.listen(_handleAmplitude);
|
||||||
|
_progressTimer?.cancel();
|
||||||
|
_progressTimer = Timer.periodic(
|
||||||
|
_amplitudeInterval,
|
||||||
|
(_) => _tickRecordingProgress(),
|
||||||
|
);
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_micAllowed = true;
|
_micAllowed = true;
|
||||||
_recording = true;
|
_recording = true;
|
||||||
|
_heardVoice = false;
|
||||||
_liveLevel = 0;
|
_liveLevel = 0;
|
||||||
_informationUnits = 0;
|
_informationUnits = 0;
|
||||||
});
|
});
|
||||||
@@ -795,6 +804,8 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
|
|||||||
Future<void> _stopRecording() async {
|
Future<void> _stopRecording() async {
|
||||||
await _amplitudeSub?.cancel();
|
await _amplitudeSub?.cancel();
|
||||||
_amplitudeSub = null;
|
_amplitudeSub = null;
|
||||||
|
_progressTimer?.cancel();
|
||||||
|
_progressTimer = null;
|
||||||
final path = await _audioRecorder.stop();
|
final path = await _audioRecorder.stop();
|
||||||
_recordStopwatch.stop();
|
_recordStopwatch.stop();
|
||||||
if (path != null && path.isNotEmpty) {
|
if (path != null && path.isNotEmpty) {
|
||||||
@@ -812,12 +823,26 @@ class _AddExperienceFlowState extends ConsumerState<AddExperienceFlow> {
|
|||||||
|
|
||||||
void _handleAmplitude(record.Amplitude amplitude) {
|
void _handleAmplitude(record.Amplitude amplitude) {
|
||||||
final currentDb = amplitude.current;
|
final currentDb = amplitude.current;
|
||||||
final now = DateTime.now();
|
|
||||||
final level = _normalizeDbLevel(currentDb);
|
final level = _normalizeDbLevel(currentDb);
|
||||||
final informationDelta = _consumeInformationDelta(level, now);
|
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_liveLevel = _smoothLevel(_liveLevel, level);
|
_liveLevel = _smoothLevel(_liveLevel, level);
|
||||||
|
_heardVoice = _heardVoice || level > 0.08;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _tickRecordingProgress() {
|
||||||
|
if (!_recording) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final effectiveLevel = _heardVoice ? math.max(_liveLevel, 0.34) : _liveLevel;
|
||||||
|
final informationDelta = _consumeInformationDelta(
|
||||||
|
effectiveLevel,
|
||||||
|
DateTime.now(),
|
||||||
|
);
|
||||||
|
|
||||||
|
setState(() {
|
||||||
_informationUnits = math.min(
|
_informationUnits = math.min(
|
||||||
_minimumInformationUnits,
|
_minimumInformationUnits,
|
||||||
_informationUnits + informationDelta,
|
_informationUnits + informationDelta,
|
||||||
|
|||||||
Reference in New Issue
Block a user