mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Add AudioEngine (synthesized sounds) and VoiceEngine
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -49,7 +49,7 @@ Le brief original est [`prompt-pyquest.md`](prompt-pyquest.md) (résumé fidèle
|
|||||||
6. ✅ FAIT — `app/js/core/ErrorTranslator.js` + `app/js/i18n/errors.{fr,en}.json` — traduit les tracebacks Python en messages simples fr/en
|
6. ✅ FAIT — `app/js/core/ErrorTranslator.js` + `app/js/i18n/errors.{fr,en}.json` — traduit les tracebacks Python en messages simples fr/en
|
||||||
(cf. ton de l'exemple 2 du brief).
|
(cf. ton de l'exemple 2 du brief).
|
||||||
7. ✅ FAIT (IndexedDB non testé en navigateur) — `app/js/core/ProgressStore.js` + `KeyValueStores.js` — IndexedDB (XP, badges, streaks, niveaux complétés) + export/import.
|
7. ✅ FAIT (IndexedDB non testé en navigateur) — `app/js/core/ProgressStore.js` + `KeyValueStores.js` — IndexedDB (XP, badges, streaks, niveaux complétés) + export/import.
|
||||||
8. `app/js/core/AudioEngine.js` + `app/js/core/VoiceEngine.js` — effets sonores et
|
8. ✅ FAIT (testé avec des doubles ; sons synthétisés, pas de fichiers mp3) — `app/js/core/AudioEngine.js` + `app/js/core/VoiceEngine.js` — effets sonores et
|
||||||
`speechSynthesis`, déblocage uniquement sur geste utilisateur (contrainte iOS autoplay).
|
`speechSynthesis`, déblocage uniquement sur geste utilisateur (contrainte iOS autoplay).
|
||||||
9. `app/js/ui/*` — `NotebookView`, `CellManager`, `CellView`, `HintPanel`, `ProgressMap`,
|
9. `app/js/ui/*` — `NotebookView`, `CellManager`, `CellView`, `HintPanel`, `ProgressMap`,
|
||||||
`BadgeToast`, `SettingsPanel`.
|
`BadgeToast`, `SettingsPanel`.
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
// Effets sonores courts via Web Audio API, synthétisés (aucun fichier audio à servir ni à licencier).
|
||||||
|
// Toujours désactivable ; jamais déclenché avant un geste utilisateur (contrainte autoplay iOS).
|
||||||
|
|
||||||
|
// Chaque son est une suite de notes : fréquence (Hz), début et durée (s), forme d'onde.
|
||||||
|
const SOUNDS = {
|
||||||
|
success_small: [
|
||||||
|
{ frequency: 659.25, start: 0, duration: 0.12, type: 'sine' },
|
||||||
|
{ frequency: 880, start: 0.1, duration: 0.2, type: 'sine' },
|
||||||
|
],
|
||||||
|
success_big: [
|
||||||
|
{ frequency: 523.25, start: 0, duration: 0.12, type: 'triangle' },
|
||||||
|
{ frequency: 659.25, start: 0.12, duration: 0.12, type: 'triangle' },
|
||||||
|
{ frequency: 783.99, start: 0.24, duration: 0.12, type: 'triangle' },
|
||||||
|
{ frequency: 1046.5, start: 0.36, duration: 0.35, type: 'triangle' },
|
||||||
|
],
|
||||||
|
level_unlocked: [
|
||||||
|
{ frequency: 392, start: 0, duration: 0.15, type: 'square' },
|
||||||
|
{ frequency: 523.25, start: 0.15, duration: 0.15, type: 'square' },
|
||||||
|
{ frequency: 659.25, start: 0.3, duration: 0.15, type: 'square' },
|
||||||
|
{ frequency: 783.99, start: 0.45, duration: 0.15, type: 'square' },
|
||||||
|
{ frequency: 1046.5, start: 0.6, duration: 0.5, type: 'square' },
|
||||||
|
],
|
||||||
|
error: [
|
||||||
|
{ frequency: 220, start: 0, duration: 0.18, type: 'sawtooth' },
|
||||||
|
{ frequency: 174.61, start: 0.15, duration: 0.25, type: 'sawtooth' },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
// Les sons « square » et « sawtooth » sont agressifs : gain de base réduit.
|
||||||
|
const BASE_GAIN = 0.15;
|
||||||
|
|
||||||
|
export class AudioEngine {
|
||||||
|
#createContext;
|
||||||
|
#context = null;
|
||||||
|
#enabled;
|
||||||
|
#volume;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} [options]
|
||||||
|
* @param {boolean} [options.enabled]
|
||||||
|
* @param {number} [options.volume] De 0 à 1.
|
||||||
|
* @param {() => AudioContext|null} [options.contextFactory] Remplaçable pour les tests.
|
||||||
|
*/
|
||||||
|
constructor({ enabled = true, volume = 0.8, contextFactory = AudioEngine.#defaultFactory } = {}) {
|
||||||
|
this.#enabled = enabled;
|
||||||
|
this.#volume = volume;
|
||||||
|
this.#createContext = contextFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
static #defaultFactory() {
|
||||||
|
const Context = globalThis.AudioContext ?? globalThis.webkitAudioContext;
|
||||||
|
return Context ? new Context() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static get soundNames() {
|
||||||
|
return Object.keys(SOUNDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
get enabled() {
|
||||||
|
return this.#enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
get volume() {
|
||||||
|
return this.#volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Vrai si le navigateur sait produire du son. */
|
||||||
|
get supported() {
|
||||||
|
return Boolean(globalThis.AudioContext ?? globalThis.webkitAudioContext) || this.#context !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
setEnabled(enabled) {
|
||||||
|
this.#enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
setVolume(volume) {
|
||||||
|
this.#volume = Math.min(1, Math.max(0, volume));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* À appeler depuis un geste utilisateur (clic sur « Exécuter », etc.) : crée ou reprend le
|
||||||
|
* contexte audio, bloqué tant qu'aucun geste n'a eu lieu.
|
||||||
|
*/
|
||||||
|
async unlock() {
|
||||||
|
if (!this.#context) {
|
||||||
|
this.#context = this.#createContext();
|
||||||
|
}
|
||||||
|
if (this.#context?.state === 'suspended') {
|
||||||
|
await this.#context.resume();
|
||||||
|
}
|
||||||
|
return this.#context !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joue un son prédéfini. Sans effet si désactivé, non débloqué ou nom inconnu.
|
||||||
|
* @returns {boolean} vrai si le son a été lancé
|
||||||
|
*/
|
||||||
|
play(name) {
|
||||||
|
const notes = SOUNDS[name];
|
||||||
|
if (!this.#enabled || !this.#context || this.#context.state !== 'running' || !notes) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const context = this.#context;
|
||||||
|
const now = context.currentTime;
|
||||||
|
for (const note of notes) {
|
||||||
|
const oscillator = context.createOscillator();
|
||||||
|
const gain = context.createGain();
|
||||||
|
const peak = BASE_GAIN * this.#volume;
|
||||||
|
oscillator.type = note.type;
|
||||||
|
oscillator.frequency.value = note.frequency;
|
||||||
|
// Enveloppe courte pour éviter les clics de début et de fin.
|
||||||
|
gain.gain.setValueAtTime(0, now + note.start);
|
||||||
|
gain.gain.linearRampToValueAtTime(peak, now + note.start + 0.01);
|
||||||
|
gain.gain.linearRampToValueAtTime(0, now + note.start + note.duration);
|
||||||
|
oscillator.connect(gain).connect(context.destination);
|
||||||
|
oscillator.start(now + note.start);
|
||||||
|
oscillator.stop(now + note.start + note.duration + 0.02);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
// Narration vocale optionnelle via speechSynthesis (voix du système, aucun service tiers).
|
||||||
|
// À appeler uniquement depuis un geste utilisateur (bouton « Écouter »), jamais automatiquement.
|
||||||
|
|
||||||
|
const LANG_TAGS = { fr: 'fr-FR', en: 'en-US' };
|
||||||
|
|
||||||
|
export class VoiceEngine {
|
||||||
|
#synth;
|
||||||
|
#UtteranceClass;
|
||||||
|
#enabled;
|
||||||
|
#rate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} [options]
|
||||||
|
* @param {boolean} [options.enabled]
|
||||||
|
* @param {number} [options.rate] Vitesse de 0.5 à 2 (1 = normale).
|
||||||
|
* @param {SpeechSynthesis|null} [options.synth] Remplaçable pour les tests.
|
||||||
|
* @param {typeof SpeechSynthesisUtterance} [options.Utterance]
|
||||||
|
*/
|
||||||
|
constructor({
|
||||||
|
enabled = true,
|
||||||
|
rate = 1,
|
||||||
|
synth = globalThis.speechSynthesis ?? null,
|
||||||
|
Utterance = globalThis.SpeechSynthesisUtterance,
|
||||||
|
} = {}) {
|
||||||
|
this.#synth = synth;
|
||||||
|
this.#UtteranceClass = Utterance;
|
||||||
|
this.#enabled = enabled;
|
||||||
|
this.#rate = rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Faux sur les navigateurs sans speechSynthesis : l'UI masque alors le bouton. */
|
||||||
|
get supported() {
|
||||||
|
return this.#synth !== null && this.#UtteranceClass !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
get enabled() {
|
||||||
|
return this.#enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
get rate() {
|
||||||
|
return this.#rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
setEnabled(enabled) {
|
||||||
|
this.#enabled = enabled;
|
||||||
|
if (!enabled) {
|
||||||
|
this.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setRate(rate) {
|
||||||
|
this.#rate = Math.min(2, Math.max(0.5, rate));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lit un texte. Résout à la fin de la lecture ; false si rien n'a été lu.
|
||||||
|
* @param {string} text Markdown léger toléré (les `` ` `` sont retirés).
|
||||||
|
* @param {string} [lang] 'fr' ou 'en'.
|
||||||
|
* @returns {Promise<boolean>}
|
||||||
|
*/
|
||||||
|
speak(text, lang = 'fr') {
|
||||||
|
if (!this.supported || !this.#enabled) {
|
||||||
|
return Promise.resolve(false);
|
||||||
|
}
|
||||||
|
this.cancel();
|
||||||
|
const utterance = new this.#UtteranceClass(this.#clean(text));
|
||||||
|
utterance.lang = LANG_TAGS[lang] ?? lang;
|
||||||
|
utterance.rate = this.#rate;
|
||||||
|
const voice = this.#pickVoice(utterance.lang);
|
||||||
|
if (voice) {
|
||||||
|
utterance.voice = voice;
|
||||||
|
}
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
utterance.onend = () => resolve(true);
|
||||||
|
// Un cancel() déclenche aussi onerror : ce n'est pas une panne.
|
||||||
|
utterance.onerror = () => resolve(false);
|
||||||
|
this.#synth.speak(utterance);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel() {
|
||||||
|
this.#synth?.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Voix du système correspondant à la langue, en privilégiant l'étiquette exacte. */
|
||||||
|
#pickVoice(tag) {
|
||||||
|
const voices = this.#synth.getVoices?.() ?? [];
|
||||||
|
const base = tag.split('-')[0];
|
||||||
|
return (
|
||||||
|
voices.find((voice) => voice.lang === tag) ?? voices.find((voice) => voice.lang.split(/[-_]/)[0] === base) ?? null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#clean(text) {
|
||||||
|
return text.replace(/`/g, '').replace(/\s+/g, ' ').trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ PyQuest/
|
|||||||
│ │ ├── fr.json
|
│ │ ├── fr.json
|
||||||
│ │ └── en.json
|
│ │ └── en.json
|
||||||
│ └── assets/
|
│ └── assets/
|
||||||
│ ├── sounds/ # success_small.mp3, success_big.mp3, level_unlocked.mp3
|
│ ├── sounds/ # (inutilisé : AudioEngine synthétise les sons via Web Audio, pas de mp3)
|
||||||
│ └── icons/ # icônes PWA (plusieurs résolutions)
|
│ └── icons/ # icônes PWA (plusieurs résolutions)
|
||||||
│
|
│
|
||||||
├── vendor/
|
├── vendor/
|
||||||
|
|||||||
Reference in New Issue
Block a user