@@ -24,13 +24,13 @@ import org.readium.r2.shared.extensions.tryOrNull
2424import org.readium.r2.shared.publication.Locator
2525
2626/* *
27- * Plays the content from a [TtsContentIterator ] with a [TtsEngine].
27+ * Plays the content from a [TtsUtteranceIterator ] with a [TtsEngine].
2828 */
2929@ExperimentalReadiumApi
3030internal class TtsPlayer <S : TtsEngine .Settings , P : TtsEngine .Preferences <P >,
3131 E : TtsEngine .Error , V : TtsEngine .Voice > private constructor (
3232 private val engineFacade: TtsEngineFacade <S , P , E , V >,
33- private val contentIterator: TtsContentIterator ,
33+ private val contentIterator: TtsUtteranceIterator ,
3434 initialWindow: UtteranceWindow ,
3535 initialPreferences: P
3636) : Configurable <S , P > {
@@ -40,7 +40,7 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
4040 suspend operator fun <S : TtsEngine .Settings , P : TtsEngine .Preferences <P >,
4141 E : TtsEngine.Error , V : TtsEngine.Voice > invoke (
4242 engine : TtsEngine <S , P , E , V >,
43- contentIterator : TtsContentIterator ,
43+ contentIterator : TtsUtteranceIterator ,
4444 initialPreferences : P
4545 ): TtsPlayer <S , P , E , V >? {
4646 val initialContext = tryOrNull { contentIterator.startContext() }
@@ -59,23 +59,23 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
5959 )
6060 }
6161
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 ()
6565
6666 val startWindow = if (currentUtterance != null ) {
6767 UtteranceWindow (
6868 previousUtterance = previousUtterance,
6969 currentUtterance = currentUtterance,
70- nextUtterance = nextUtterance (),
70+ nextUtterance = next (),
7171 ended = false
7272 )
7373 } else {
7474 val actualCurrentUtterance = previousUtterance ? : return null
75- val actualPreviousUtterance = previousUtterance ()
75+ val actualPreviousUtterance = previous ()
7676
7777 // Go back to the end of the iterator.
78- nextUtterance ()
78+ next ()
7979
8080 UtteranceWindow (
8181 previousUtterance = actualPreviousUtterance,
@@ -129,15 +129,14 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
129129 data class Position (
130130 val resourceIndex : Int ,
131131 val locations : Locator .Locations ,
132- val textBefore : String? ,
133- val textAfter : String?
132+ val text : Locator .Text
134133 )
135134 }
136135
137136 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 ? ,
141140 val ended : Boolean = false
142141 )
143142
@@ -411,19 +410,19 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
411410 val previousUtterance =
412411 try {
413412 // Get previously currentUtterance once more
414- contentIterator.previousUtterance ()
413+ contentIterator.previous ()
415414
416415 // Get previously previousUtterance once more
417- contentIterator.previousUtterance ()
416+ contentIterator.previous ()
418417
419418 // Get new previous utterance
420- val previousUtterance = contentIterator.previousUtterance ()
419+ val previousUtterance = contentIterator.previous ()
421420
422421 // Go to currentUtterance position
423- contentIterator.nextUtterance ()
422+ contentIterator.next ()
424423
425424 // Go to nextUtterance position
426- contentIterator.nextUtterance ()
425+ contentIterator.next ()
427426
428427 previousUtterance
429428 } catch (e: Exception ) {
@@ -448,7 +447,7 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
448447 }
449448
450449 val nextUtterance = try {
451- contentIterator.nextUtterance ()
450+ contentIterator.next ()
452451 } catch (e: Exception ) {
453452 onContentError(e)
454453 return
@@ -498,8 +497,8 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
498497 playContinuous()
499498 }
500499
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)
503502
504503 private fun onEngineError (error : E ) {
505504 playbackMutable.value = playbackMutable.value.copy(
@@ -535,15 +534,14 @@ internal class TtsPlayer<S : TtsEngine.Settings, P : TtsEngine.Preferences<P>,
535534 private fun isPlaying () =
536535 playbackMutable.value.playWhenReady && playback.value.state == State .Ready
537536
538- private fun TtsContentIterator .Utterance.ttsPlayerUtterance (): Utterance =
537+ private fun TtsUtteranceIterator .Utterance.ttsPlayerUtterance (): Utterance =
539538 Utterance (
540- text = text ,
539+ text = utterance ,
541540 range = null ,
542541 position = Utterance .Position (
543542 resourceIndex = resourceIndex,
544543 locations = locations,
545- textAfter = textAfter,
546- textBefore = textBefore
544+ text = text
547545 )
548546 )
549547}
0 commit comments