Langsame Hörhilfen für Ausspracheübungen ergänzen

This commit is contained in:
Eugenius Naumenko
2026-10-14 02:13:07 +00:00
parent 16da8cad1d
commit 83944f791e
7 changed files with 261 additions and 32 deletions
+33 -4
View File
@@ -22,6 +22,7 @@ import (
type SpeechService interface {
AssessPronunciation(ctx context.Context, input PronunciationInput) (domain.PronunciationResult, error)
Synthesize(ctx context.Context, locale, voice, text string) ([]byte, string, error)
SynthesizeExercise(ctx context.Context, locale, voice, text, mode, focusWord string) ([]byte, string, error)
}
type PronunciationInput struct {
@@ -98,13 +99,41 @@ func (s *AzureSpeechService) Synthesize(ctx context.Context, locale, voice, text
if strings.TrimSpace(text) == "" {
return nil, "", errors.New("tts text is required")
}
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
return nil, "", nil
}
locale = valueOr(locale, "en-US")
voice = valueOr(voice, "en-US-JennyNeural")
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, xmlEscape(text))
return s.synthesizeSSML(ctx, ssml)
}
func (s *AzureSpeechService) SynthesizeExercise(ctx context.Context, locale, voice, text, mode, focusWord string) ([]byte, string, error) {
if strings.TrimSpace(text) == "" {
return nil, "", errors.New("tts text is required")
}
if mode == "normal" {
return s.Synthesize(ctx, locale, voice, text)
}
locale = valueOr(locale, "en-US")
voice = valueOr(voice, "en-US-JennyNeural")
var content string
switch mode {
case "slow":
content = fmt.Sprintf(`<prosody rate="0.8">%s</prosody>`, xmlEscape(text))
case "focus":
if strings.TrimSpace(focusWord) == "" {
return nil, "", errors.New("focus word is required")
}
content = fmt.Sprintf(`<prosody rate="0.7">%s</prosody><break time="400ms"/><prosody rate="0.8">%s</prosody>`, xmlEscape(focusWord), xmlEscape(text))
default:
return nil, "", errors.New("unsupported audio mode")
}
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, content)
return s.synthesizeSSML(ctx, ssml)
}
func (s *AzureSpeechService) synthesizeSSML(ctx context.Context, ssml string) ([]byte, string, error) {
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
return nil, "", nil
}
endpoint := fmt.Sprintf("https://%s.tts.speech.microsoft.com/cognitiveservices/v1", s.cfg.AzureRegion)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(ssml))