| @@ -21,13 +21,18 @@ final class SpeechSynthesizer: NSObject, AVSpeechSynthesizerDelegate, AVAudioPla |
| 21 | |
21 | |
| 22 | /// Clips to play from; nil means always use the synthesizer. |
22 | /// Clips to play from; nil means always use the synthesizer. |
| 23 | var library: ClipLibrary? = ClipLibrary.bundled |
23 | var library: ClipLibrary? = ClipLibrary.bundled |
| 24 | /// Gap between the sentences of a clip line. Each clip already starts with |
24 | /// Skipped at the start of every clip: the AAC encoder's 2112 priming |
| 25 | /// ~90 ms of encoder lead-in and ends with ~40 ms of quiet, so the pause |
25 | /// frames, which the ADTS files carry as silence. |
| 26 | /// a child hears is about 0.2 s. |
26 | static let leadIn = 2112.0 / 24_000 |
| 27 | static let sentenceGap = 0.05 |
27 | /// Gap between the sentences of a clip line. Each clip, after `leadIn`, |
| |
28 | /// starts and ends with ~40 ms of quiet, so the pause a child hears is |
| |
29 | /// about 0.2 s. |
| |
30 | static let sentenceGap = 0.14 |
| 28 | /// Extra time past a clip's known length before a stalled clip is skipped. |
31 | /// Extra time past a clip's known length before a stalled clip is skipped. |
| 29 | static let clipGrace = 1.5 |
32 | static let clipGrace = 1.5 |
| 30 | private var player: AVAudioPlayer? |
33 | private var player: AVAudioPlayer? |
| |
34 | /// Plays a clip silently at launch so the first real clip starts on time. |
| |
35 | private var warmupPlayer: AVAudioPlayer? |
| 31 | /// Counts players, so a watchdog only acts on the clip it was set for. |
36 | /// Counts players, so a watchdog only acts on the clip it was set for. |
| 32 | private var playerID = 0 |
37 | private var playerID = 0 |
| 33 | private var queue: [ClipLibrary.Clip] = [] |
38 | private var queue: [ClipLibrary.Clip] = [] |
| @@ -63,6 +68,14 @@ final class SpeechSynthesizer: NSObject, AVSpeechSynthesizerDelegate, AVAudioPla |
| 63 | synthesizer.speak(utterance) |
68 | synthesizer.speak(utterance) |
| 64 | } |
69 | } |
| 65 | |
70 | |
| |
71 | /// Loads the audio decoder and output by playing a clip at zero volume. |
| |
72 | func warmUpClips() { |
| |
73 | guard let url = library?.anyClip?.url, let player = try? AVAudioPlayer(contentsOf: url) else { return } |
| |
74 | player.volume = 0 |
| |
75 | player.play() |
| |
76 | warmupPlayer = player |
| |
77 | } |
| |
78 | |
| 66 | func speak(_ text: String, completion: (() -> Void)? = nil) { |
79 | func speak(_ text: String, completion: (() -> Void)? = nil) { |
| 67 | // A gentle tactile bump with every spoken line. Prepared just before |
80 | // A gentle tactile bump with every spoken line. Prepared just before |
| 68 | // firing for lowest latency. |
81 | // firing for lowest latency. |
| @@ -133,6 +146,7 @@ final class SpeechSynthesizer: NSObject, AVSpeechSynthesizerDelegate, AVAudioPla |
| 133 | return |
146 | return |
| 134 | } |
147 | } |
| 135 | next.delegate = self |
148 | next.delegate = self |
| |
149 | next.currentTime = Self.leadIn |
| 136 | player = next |
150 | player = next |
| 137 | playerID += 1 |
151 | playerID += 1 |
| 138 | let id = playerID |
152 | let id = playerID |