mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Fix voice playback: stale service worker code, button visibility, autoplay priming
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,7 @@ export const DEFAULT_SETTINGS = Object.freeze({
|
||||
theme: 'auto', // 'auto' | 'light' | 'dark'
|
||||
soundEnabled: true,
|
||||
volume: 0.8,
|
||||
voiceEnabled: false,
|
||||
voiceEnabled: true, // le bouton « Écouter » ne lit jamais automatiquement : un clic à chaque fois
|
||||
voiceRate: 1,
|
||||
});
|
||||
|
||||
|
||||
+46
-11
@@ -4,6 +4,8 @@
|
||||
// Aucun service tiers.
|
||||
|
||||
const LANG_TAGS = { fr: 'fr-FR', en: 'en-US' };
|
||||
// WAV silencieux minimal : sert à « amorcer » l'élément audio pendant le clic (voir #prime).
|
||||
const SILENT_WAV = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQAAAAA=';
|
||||
|
||||
export class VoiceEngine {
|
||||
#synth;
|
||||
@@ -12,9 +14,10 @@ export class VoiceEngine {
|
||||
#fetch;
|
||||
#enabled;
|
||||
#rate;
|
||||
#audio = null;
|
||||
#player = null;
|
||||
#objectUrl = null;
|
||||
#session = 0;
|
||||
#lastFailure = null;
|
||||
|
||||
/**
|
||||
* @param {object} [options]
|
||||
@@ -54,6 +57,11 @@ export class VoiceEngine {
|
||||
return this.#rate;
|
||||
}
|
||||
|
||||
/** null, ou 'unavailable' si la dernière lecture demandée n'a rien pu jouer (à signaler à l'apprenant). */
|
||||
get lastFailure() {
|
||||
return this.#lastFailure;
|
||||
}
|
||||
|
||||
setEnabled(enabled) {
|
||||
this.#enabled = enabled;
|
||||
if (!enabled) {
|
||||
@@ -63,8 +71,8 @@ export class VoiceEngine {
|
||||
|
||||
setRate(rate) {
|
||||
this.#rate = Math.min(2, Math.max(0.5, rate));
|
||||
if (this.#audio) {
|
||||
this.#audio.playbackRate = this.#rate;
|
||||
if (this.#player) {
|
||||
this.#player.playbackRate = this.#rate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,21 +85,45 @@ export class VoiceEngine {
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async speak(text, lang = 'fr', { audioUrl = null } = {}) {
|
||||
this.#lastFailure = null;
|
||||
if (!this.supported || !this.#enabled) {
|
||||
return false;
|
||||
}
|
||||
this.cancel();
|
||||
// Doit s'exécuter de façon synchrone dans le gestionnaire de clic, avant tout await.
|
||||
this.#prime();
|
||||
const session = this.#session;
|
||||
// Une lecture interrompue (nouvelle demande, annulation) n'est pas une panne.
|
||||
const finish = (ok) => {
|
||||
if (!ok && session === this.#session) {
|
||||
this.#lastFailure = 'unavailable';
|
||||
}
|
||||
return ok;
|
||||
};
|
||||
if (audioUrl && this.#AudioClass) {
|
||||
const played = await this.#playFile(audioUrl, session);
|
||||
if (played !== null) {
|
||||
return played;
|
||||
return finish(played);
|
||||
}
|
||||
}
|
||||
if (session !== this.#session) {
|
||||
return false;
|
||||
}
|
||||
return this.#speakWithSystemVoice(text, lang);
|
||||
return finish(await this.#speakWithSystemVoice(text, lang));
|
||||
}
|
||||
|
||||
/**
|
||||
* Débloque l'élément audio pendant le geste utilisateur. Safari/iOS n'autorise la lecture
|
||||
* que si l'élément a été démarré par un geste ; or le fichier n'arrive qu'après un fetch
|
||||
* asynchrone. On joue donc un silence tout de suite, puis on change la source plus tard.
|
||||
*/
|
||||
#prime() {
|
||||
if (!this.#AudioClass) {
|
||||
return;
|
||||
}
|
||||
this.#player ??= new this.#AudioClass();
|
||||
this.#player.src = SILENT_WAV;
|
||||
Promise.resolve(this.#player.play()).catch(() => {});
|
||||
}
|
||||
|
||||
/** Interrompt toute lecture en cours (fichier ou synthèse). */
|
||||
@@ -121,9 +153,9 @@ export class VoiceEngine {
|
||||
}
|
||||
// Lecture depuis un blob : pas de requêtes Range (exigées par iOS pour un flux direct).
|
||||
this.#objectUrl = URL.createObjectURL(blob);
|
||||
const audio = new this.#AudioClass(this.#objectUrl);
|
||||
const audio = this.#player;
|
||||
audio.src = this.#objectUrl;
|
||||
audio.playbackRate = this.#rate;
|
||||
this.#audio = audio;
|
||||
return new Promise((resolve) => {
|
||||
let started = false;
|
||||
audio.onplaying = () => {
|
||||
@@ -146,10 +178,13 @@ export class VoiceEngine {
|
||||
}
|
||||
|
||||
#releaseAudio() {
|
||||
if (this.#audio) {
|
||||
this.#audio.onpause = null;
|
||||
this.#audio.pause?.();
|
||||
this.#audio = null;
|
||||
if (this.#player) {
|
||||
// Handlers retirés d'abord : pause() ne doit pas être pris pour une interruption à signaler.
|
||||
this.#player.onpause = null;
|
||||
this.#player.onended = null;
|
||||
this.#player.onerror = null;
|
||||
this.#player.onplaying = null;
|
||||
this.#player.pause?.();
|
||||
}
|
||||
if (this.#objectUrl) {
|
||||
URL.revokeObjectURL(this.#objectUrl);
|
||||
|
||||
@@ -59,6 +59,7 @@
|
||||
"settings.sound": "Sound effects",
|
||||
"settings.volume": "Volume",
|
||||
"settings.voice": "Voice",
|
||||
"voice.failed": "The voice cannot be played on this device. Check the volume and the browser's sound permission.",
|
||||
"voice.sample": "Hello, I am the voice of PyQuest. This is how I will read the explanations.",
|
||||
"settings.testSound": "Test the sound",
|
||||
"settings.testVoice": "Test the voice",
|
||||
|
||||
@@ -59,6 +59,7 @@
|
||||
"settings.sound": "Effets sonores",
|
||||
"settings.volume": "Volume",
|
||||
"settings.voice": "Voix",
|
||||
"voice.failed": "Impossible de lire la voix sur cet appareil. Vérifie le volume et l'autorisation du son dans le navigateur.",
|
||||
"voice.sample": "Bonjour, je suis la voix de PyQuest. Voici comment je lirai les explications.",
|
||||
"settings.testSound": "Tester le son",
|
||||
"settings.testVoice": "Tester la voix",
|
||||
|
||||
+13
-2
@@ -58,6 +58,7 @@ class App {
|
||||
#voice;
|
||||
#manager;
|
||||
#toast;
|
||||
#notebook;
|
||||
#map;
|
||||
#panel;
|
||||
#levelIds = [];
|
||||
@@ -83,9 +84,10 @@ class App {
|
||||
|
||||
const client = new PyodideWorkerClient({ indexURL: new URL('/vendor/pyodide/', location.href).href });
|
||||
const notebook = new NotebookView(this.#i18n, {
|
||||
onSpeak: (text, step) => this.#voice.speak(text, this.#i18n.lang, { audioUrl: step.audioUrl }),
|
||||
onSpeak: (text, step) => this.#speak(text, step.audioUrl),
|
||||
canSpeak: () => this.#voice.supported && this.#voice.enabled,
|
||||
});
|
||||
this.#notebook = notebook;
|
||||
const toast = new BadgeToast(this.#i18n);
|
||||
this.#toast = toast;
|
||||
this.#manager = new CellManager({
|
||||
@@ -163,6 +165,7 @@ class App {
|
||||
this.#audio.setEnabled(settings.soundEnabled);
|
||||
this.#audio.setVolume(settings.volume);
|
||||
this.#voice.setEnabled(settings.voiceEnabled);
|
||||
this.#notebook.refreshVoiceButtons();
|
||||
this.#voice.setRate(settings.voiceRate);
|
||||
if (key === 'lang') {
|
||||
this.#i18n.setLang(value);
|
||||
@@ -186,7 +189,15 @@ class App {
|
||||
const lang = this.#i18n.lang;
|
||||
const sample = this.#i18n.t('voice.sample');
|
||||
const audioUrl = new URL(`assets/voice/${lang}/${speechKey(lang, sample)}.mp3`, document.baseURI).href;
|
||||
await this.#voice.speak(sample, lang, { audioUrl });
|
||||
await this.#speak(sample, audioUrl);
|
||||
}
|
||||
|
||||
/** Lit un texte ; si rien ne peut être joué, prévient l'apprenant au lieu de rester silencieux. */
|
||||
async #speak(text, audioUrl) {
|
||||
await this.#voice.speak(text, this.#i18n.lang, { audioUrl });
|
||||
if (this.#voice.lastFailure) {
|
||||
this.#toast.show(this.#i18n.t('voice.failed'));
|
||||
}
|
||||
}
|
||||
|
||||
#registerServiceWorker() {
|
||||
|
||||
@@ -54,12 +54,29 @@ export class NotebookView {
|
||||
if (image) {
|
||||
children.push(el('img', { src: image, alt: '' }));
|
||||
}
|
||||
if (voice && this.#onSpeak && this.#canSpeak()) {
|
||||
children.push(el('button', { type: 'button', class: 'btn', text: this.#i18n.t('voice.listen'), onclick: () => this.#onSpeak(text, step) }));
|
||||
if (voice && this.#onSpeak) {
|
||||
// Toujours rendu, affiché ou non selon le réglage (voir refreshVoiceButtons).
|
||||
children.push(
|
||||
el('button', {
|
||||
type: 'button',
|
||||
class: 'btn voice-button',
|
||||
hidden: !this.#canSpeak(),
|
||||
text: this.#i18n.t('voice.listen'),
|
||||
onclick: () => this.#onSpeak(text, step),
|
||||
}),
|
||||
);
|
||||
}
|
||||
return this.add(el('section', { class: 'explain', tabindex: '-1' }, ...children));
|
||||
}
|
||||
|
||||
/** Réaffiche ou masque les boutons « Écouter » existants (réglage de la voix modifié). */
|
||||
refreshVoiceButtons() {
|
||||
const visible = this.#canSpeak();
|
||||
for (const button of this.#root.querySelectorAll('.voice-button')) {
|
||||
button.hidden = !visible;
|
||||
}
|
||||
}
|
||||
|
||||
/** Bouton « Continuer » ou équivalent ; retiré du DOM une fois cliqué. */
|
||||
addAction(label, onClick) {
|
||||
const button = el('button', {
|
||||
|
||||
Reference in New Issue
Block a user