Merge pull request #1151 from omnivore-app/feature/prioritize-synthesis

Prioritize synthesis on playing audio over pre-fetching
This commit is contained in:
Hongbo Wu
2022-08-29 16:16:20 +08:00
committed by GitHub
2 changed files with 11 additions and 12 deletions

View File

@ -25,6 +25,11 @@ public enum PlayerScrubState {
case scrubEnded(TimeInterval)
}
enum DownloadPriority: String {
case low = "low"
case high = "high"
}
// Our observable object class
public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
@Published public var state: AudioSessionState = .stopped
@ -77,7 +82,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
}
// Attempt to fetch the file if not downloaded already
let result = try? await downloadAudioFile(pageId: pageId)
let result = try? await downloadAudioFile(pageId: pageId, priority: .low)
if result == nil {
print("audio file had error downloading: ", pageId)
pendingList.append(pageId)
@ -168,7 +173,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
let pageId = item!.unwrappedID
downloadTask = Task {
let result = try? await downloadAudioFile(pageId: pageId)
let result = try? await downloadAudioFile(pageId: pageId, priority: .high)
if Task.isCancelled { return }
if result == nil {
@ -347,14 +352,14 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
}
}
func downloadAudioFile(pageId: String) async throws -> (pending: Bool, url: URL?) {
func downloadAudioFile(pageId: String, priority: DownloadPriority) async throws -> (pending: Bool, url: URL?) {
let audioUrl = pathForAudioFile(pageId: pageId)
if FileManager.default.fileExists(atPath: audioUrl.path) {
return (pending: false, url: audioUrl)
}
guard let url = URL(string: "/api/article/\(pageId)/mp3/\(currentVoice)", relativeTo: appEnvironment.serverBaseURL) else {
guard let url = URL(string: "/api/article/\(pageId)/mp3/\(priority)/\(currentVoice)", relativeTo: appEnvironment.serverBaseURL) else {
throw BasicError.message(messageText: "Invalid audio URL")
}

View File

@ -20,7 +20,6 @@ import { Speech, SpeechState } from '../entity/speech'
import { getPageById, updatePage } from '../elastic/pages'
import { generateDownloadSignedUrl } from '../utils/uploads'
import { enqueueTextToSpeech } from '../utils/createTask'
import { UserPersonalization } from '../entity/user_personalization'
import { createPubSubClient } from '../datalayer/pubsub'
const logger = buildLogger('app.dispatch')
@ -78,7 +77,7 @@ export function articleRouter() {
async (req, res) => {
const articleId = req.params.id
const outputFormat = req.params.outputFormat
const voice = req.params.voice
const voice = req.params.voice || 'en-US-JennyNeural'
const priority = req.params.priority
if (
!articleId ||
@ -145,17 +144,12 @@ export function articleRouter() {
if (!page) {
return res.status(404).send('Page not found')
}
const userPersonalization = await getRepository(
UserPersonalization
).findOneBy({
user: { id: uid },
})
// initialize state
const speech = await getRepository(Speech).save({
user: { id: uid },
elasticPageId: articleId,
state: SpeechState.INITIALIZED,
voice: voice || userPersonalization?.speechVoice || 'en-US-JennyNeural',
voice,
})
// enqueue a task to convert text to speech
const taskName = await enqueueTextToSpeech({