@@ -24,13 +24,13 @@ import org.readium.r2.shared.extensions.tryOrNull
24
24
import org.readium.r2.shared.publication.Locator
25
25
26
26
/* *
27
- * Plays the content from a [TtsContentIterator ] with a [TtsEngine].
27
+ * Plays the content from a [TtsUtteranceIterator ] with a [TtsEngine].
28
28
*/
29
29
@ExperimentalReadiumApi
30
30
internal class TtsPlayer <S : TtsEngine .Settings , P : TtsEngine .Preferences <P >,
31
31
E : TtsEngine .Error , V : TtsEngine .Voice > private constructor (
32
32
private val engineFacade: TtsEngineFacade <S , P , E , V >,
33
- private val contentIterator: TtsContentIterator ,
33
+ private val contentIterator: TtsUtteranceIterator ,
34
34
initialWindow: UtteranceWindow ,
35
35
initialPreferences: P
36
36
) : Configurable <S , P > {
@@ -40,7 +40,7 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
40
40
suspend operator fun <S : TtsEngine .Settings , P : TtsEngine .Preferences <P >,
41
41
E : TtsEngine.Error , V : TtsEngine.Voice > invoke (
42
42
engine : TtsEngine <S , P , E , V >,
43
- contentIterator : TtsContentIterator ,
43
+ contentIterator : TtsUtteranceIterator ,
44
44
initialPreferences : P
45
45
): TtsPlayer <S , P , E , V >? {
46
46
val initialContext = tryOrNull { contentIterator.startContext() }
@@ -59,23 +59,23 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
59
59
)
60
60
}
61
61
62
- private suspend fun TtsContentIterator .startContext (): UtteranceWindow ? {
63
- val previousUtterance = previousUtterance ()
64
- val currentUtterance = nextUtterance ()
62
+ private suspend fun TtsUtteranceIterator .startContext (): UtteranceWindow ? {
63
+ val previousUtterance = previous ()
64
+ val currentUtterance = next ()
65
65
66
66
val startWindow = if (currentUtterance != null ) {
67
67
UtteranceWindow (
68
68
previousUtterance = previousUtterance,
69
69
currentUtterance = currentUtterance,
70
- nextUtterance = nextUtterance (),
70
+ nextUtterance = next (),
71
71
ended = false
72
72
)
73
73
} else {
74
74
val actualCurrentUtterance = previousUtterance ? : return null
75
- val actualPreviousUtterance = previousUtterance ()
75
+ val actualPreviousUtterance = previous ()
76
76
77
77
// Go back to the end of the iterator.
78
- nextUtterance ()
78
+ next ()
79
79
80
80
UtteranceWindow (
81
81
previousUtterance = actualPreviousUtterance,
@@ -129,15 +129,14 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
129
129
data class Position (
130
130
val resourceIndex : Int ,
131
131
val locations : Locator .Locations ,
132
- val textBefore : String? ,
133
- val textAfter : String?
132
+ val text : Locator .Text
134
133
)
135
134
}
136
135
137
136
private data class UtteranceWindow (
138
- val previousUtterance : TtsContentIterator .Utterance ? ,
139
- val currentUtterance : TtsContentIterator .Utterance ,
140
- val nextUtterance : TtsContentIterator .Utterance ? ,
137
+ val previousUtterance : TtsUtteranceIterator .Utterance ? ,
138
+ val currentUtterance : TtsUtteranceIterator .Utterance ,
139
+ val nextUtterance : TtsUtteranceIterator .Utterance ? ,
141
140
val ended : Boolean = false
142
141
)
143
142
@@ -411,19 +410,19 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
411
410
val previousUtterance =
412
411
try {
413
412
// Get previously currentUtterance once more
414
- contentIterator.previousUtterance ()
413
+ contentIterator.previous ()
415
414
416
415
// Get previously previousUtterance once more
417
- contentIterator.previousUtterance ()
416
+ contentIterator.previous ()
418
417
419
418
// Get new previous utterance
420
- val previousUtterance = contentIterator.previousUtterance ()
419
+ val previousUtterance = contentIterator.previous ()
421
420
422
421
// Go to currentUtterance position
423
- contentIterator.nextUtterance ()
422
+ contentIterator.next ()
424
423
425
424
// Go to nextUtterance position
426
- contentIterator.nextUtterance ()
425
+ contentIterator.next ()
427
426
428
427
previousUtterance
429
428
} catch (e: Exception ) {
@@ -448,7 +447,7 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
448
447
}
449
448
450
449
val nextUtterance = try {
451
- contentIterator.nextUtterance ()
450
+ contentIterator.next ()
452
451
} catch (e: Exception ) {
453
452
onContentError(e)
454
453
return
@@ -498,8 +497,8 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
498
497
playContinuous()
499
498
}
500
499
501
- private suspend fun speakUtterance (utterance : TtsContentIterator .Utterance ): E ? =
502
- engineFacade.speak(utterance.text , utterance.language, ::onRangeChanged)
500
+ private suspend fun speakUtterance (utterance : TtsUtteranceIterator .Utterance ): E ? =
501
+ engineFacade.speak(utterance.utterance , utterance.language, ::onRangeChanged)
503
502
504
503
private fun onEngineError (error : E ) {
505
504
playbackMutable.value = playbackMutable.value.copy(
@@ -535,15 +534,14 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
535
534
private fun isPlaying () =
536
535
playbackMutable.value.playWhenReady && playback.value.state == State .Ready
537
536
538
- private fun TtsContentIterator .Utterance.ttsPlayerUtterance (): Utterance =
537
+ private fun TtsUtteranceIterator .Utterance.ttsPlayerUtterance (): Utterance =
539
538
Utterance (
540
- text = text ,
539
+ text = utterance ,
541
540
range = null ,
542
541
position = Utterance .Position (
543
542
resourceIndex = resourceIndex,
544
543
locations = locations,
545
- textAfter = textAfter,
546
- textBefore = textBefore
544
+ text = text
547
545
)
548
546
)
549
547
}
0 commit comments