Skip to content

Commit

Permalink
rf: minor code cleanup changePauseFor
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed Jun 26, 2023
1 parent fe943c5 commit 49e48bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
19 changes: 14 additions & 5 deletions speech_to_text/lib/speech_to_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -436,22 +436,25 @@ class SpeechToText {
throw ListenFailedException(e.message, e.details, e.stacktrace);
}
}

void changePauseFor(Duration pauseFor) {
//Setup new pauseFor only if listen is active and pauseFor is different
if(isNotListening) {
if (isNotListening) {
throw ListenNotStartedException();
}

if (_pauseFor == null || _pauseFor!.compareTo(pauseFor) != 0) {
if (_pauseFor != pauseFor) {
_listenTimer?.cancel();
_listenTimer = null;
//Ignore elapsed pause for prevent immediately stop listen
// ignoreElapsePause ensures that the timer waits for the full pauseFor
// duration before stopping the listen
_setupListenAndPause(pauseFor, _listenFor, ignoreElapsedPause: true);
}
}

void _setupListenAndPause(
Duration? initialPauseFor, Duration? initialListenFor, {bool ignoreElapsedPause = false}) {
Duration? initialPauseFor, Duration? initialListenFor,
{bool ignoreElapsedPause = false}) {
_pauseFor = null;
_listenFor = null;
if (null == initialPauseFor && null == initialListenFor) {
Expand All @@ -460,7 +463,8 @@ class SpeechToText {
var pauseFor = initialPauseFor;
var listenFor = initialListenFor;
if (null != pauseFor) {
var remainingMillis = pauseFor.inMilliseconds - (ignoreElapsedPause ? 0 : _elapsedSinceSpeechEvent);
var remainingMillis = pauseFor.inMilliseconds -
(ignoreElapsedPause ? 0 : _elapsedSinceSpeechEvent);
pauseFor = Duration(milliseconds: max(remainingMillis, 0));
}
if (null != listenFor) {
Expand All @@ -485,8 +489,13 @@ class SpeechToText {
_listenTimer = Timer(minDuration, _stopOnPauseOrListen);
}

/// milliseconds since the last listen was started, this is used for
/// the listen for calculations
int get _elapsedListenMillis =>
clock.now().millisecondsSinceEpoch - _listenStartedAt;

/// milliseconds since the last speech event was detected, this
/// is used for the pause calculations
int get _elapsedSinceSpeechEvent =>
clock.now().millisecondsSinceEpoch - _lastSpeechEventAt;

Expand Down
8 changes: 5 additions & 3 deletions speech_to_text/test/speech_to_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void main() {
expect(speech.isListening, isTrue);
});
});
test('trows on changePauseFor when not listening', () async {
test('throws on changePauseFor when not listening', () async {
fakeAsync((fa) {
speech.initialize();
fa.flushMicrotasks();
Expand Down Expand Up @@ -221,7 +221,8 @@ void main() {
expect(speech.isListening, isFalse);
});
});
test('keeps listening after late changePauseFor with speech event', () async {
test('keeps listening after late changePauseFor with speech event',
() async {
fakeAsync((fa) {
speech.initialize();
fa.flushMicrotasks();
Expand All @@ -241,7 +242,8 @@ void main() {
expect(speech.isListening, isTrue);
});
});
test('Stop listen after late changePauseFor without initial pauseFor', () async {
test('Stop listen after late changePauseFor without initial pauseFor',
() async {
fakeAsync((fa) {
speech.initialize();
fa.flushMicrotasks();
Expand Down

0 comments on commit 49e48bc

Please sign in to comment.