Langsame Hörhilfen für Ausspracheübungen ergänzen
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -1,6 +1,72 @@
|
||||
package services
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"speech-learning-api/internal/config"
|
||||
)
|
||||
|
||||
type speechRoundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (fn speechRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return fn(req)
|
||||
}
|
||||
|
||||
func TestSynthesizeExerciseSendsValidSSML(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
mode string
|
||||
word string
|
||||
rates []string
|
||||
}{
|
||||
{"slow", "", []string{"0.8"}},
|
||||
{"focus", "chips", []string{"0.7", "0.8"}},
|
||||
} {
|
||||
t.Run(test.mode, func(t *testing.T) {
|
||||
var body string
|
||||
service := &AzureSpeechService{
|
||||
cfg: config.Config{AzureSpeechKey: "test-key", AzureRegion: "test-region"},
|
||||
client: &http.Client{Transport: speechRoundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
payload, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body = string(payload)
|
||||
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("mp3")), Header: make(http.Header)}, nil
|
||||
})},
|
||||
}
|
||||
_, _, err := service.SynthesizeExercise(context.Background(), "en-US", "en-US-JennyNeural", "Fish & chips are tasty.", test.mode, test.word)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoder := xml.NewDecoder(strings.NewReader(body))
|
||||
for {
|
||||
_, err := decoder.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("invalid SSML: %v; body %q", err, body)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(body, "Fish & chips are tasty.") {
|
||||
t.Fatalf("sentence was not XML-escaped: %q", body)
|
||||
}
|
||||
for _, rate := range test.rates {
|
||||
if !strings.Contains(body, `rate="`+rate+`"`) {
|
||||
t.Fatalf("missing rate %s: %q", rate, body)
|
||||
}
|
||||
}
|
||||
if test.mode == "focus" && (!strings.Contains(body, `<break time="400ms"/>`) || !strings.Contains(body, `>chips</prosody>`)) {
|
||||
t.Fatalf("focus clip lacks the word or pause: %q", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockPronunciationReturnsWordScores(t *testing.T) {
|
||||
result := mockPronunciation("Hello, my name is Anna.")
|
||||
|
||||
Reference in New Issue
Block a user