From 8c4c88a51a75ce77b126e3b30531bcbf41bdd082 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 12 Sep 2022 13:00:22 +0800 Subject: [PATCH] Recalculate durations when speed is changed --- .../Sources/Services/AudioSession/AudioController.swift | 9 ++++----- .../Services/AudioSession/SpeechSynthesizer.swift | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index 9c14b31a8..57fefa5aa 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -160,7 +160,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate func updateDuration(forItem item: SpeechItem, newDuration: TimeInterval) { if let durations = self.durations, item.audioIdx < durations.count { - self.durations?[item.audioIdx] = newDuration + self.durations?[item.audioIdx] = (newDuration / playbackRate) } } @@ -280,7 +280,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate player = AVQueuePlayer(items: []) let synthesizer = SpeechSynthesizer(appEnvironment: appEnvironment, networker: networker, document: document!) - durations = synthesizer.estimatedDurations(forSpeed: 1.0) + durations = synthesizer.estimatedDurations(forSpeed: playbackRate) self.synthesizer = synthesizer synthesizeFrom(start: 0, playWhenReady: true) @@ -297,8 +297,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate // TODO: in here we need to supply an offset into the playeritem when seeking if playWhenReady, self.player?.items().count == 1 { self.startTimer() - self.player?.play() - self.state = .playing + self.unpause() self.setupRemoteControl() self.fireTimer() } @@ -317,7 +316,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate public func unpause() { if let player = player { - player.play() + player.rate = Float(playbackRate) state = .playing } } diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift index d30e15305..984202cfb 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift @@ -43,7 +43,7 @@ struct SpeechDocument: Decodable { public let utterances: [Utterance] public func estimatedDuration(utterance: Utterance, speed: Double) -> Double { - utterance.wordCount / averageWPM * speed * 60.0 + utterance.wordCount / averageWPM / speed * 60.0 } var audioDirectory: URL {