feat: 增加在线语音 SDK 和音色管理后台,完善引擎适配
This commit is contained in:
1 parent
1856de97bc
commit
a532ba4e0f
71 files changed
+3189
-472
No files matched your search
+25
-6
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -32,26 +33,44 @@ func (e *engine) preload(pack *tts.VoicePack) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *engine) synthesize(pack *tts.VoicePack, text string, speed, pitch float32) ([]float32, int, error) {
|
||||
func (e *engine) synthesize(ctx context.Context, pack *tts.VoicePack, text string, speed, pitch float32) ([]float32, int, error) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
model, err := e.obtain(pack)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Same generation config as Synthesizer.java: silenceScale 0, sid, speed,
|
||||
// Same generation config as Synthesizer.java: silenceScale 0.2, sid, speed,
|
||||
// numSteps 5.
|
||||
cfg := &sherpa.GenerationConfig{SilenceScale: 0, Speed: speed, Sid: pack.SpeakerID, NumSteps: 5}
|
||||
audio := model.GenerateWithConfig(text, cfg, nil)
|
||||
cfg := &sherpa.GenerationConfig{SilenceScale: 0.2, Speed: speed, Sid: pack.SpeakerID, NumSteps: 5}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var shifted []float32
|
||||
audio := model.GenerateWithConfig(text, cfg, func(samples []float32, progress float32) bool {
|
||||
if ctx.Err() != nil {
|
||||
return false
|
||||
}
|
||||
if tts.PitchShiftNeeded(pitch) {
|
||||
shifted = append(shifted, tts.ShiftPitch(samples, pitch)...)
|
||||
}
|
||||
return ctx.Err() == nil
|
||||
})
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if audio == nil || len(audio.Samples) == 0 {
|
||||
return nil, 0, errors.New("synthesis produced no audio")
|
||||
}
|
||||
|
||||
samples := audio.Samples
|
||||
if tts.PitchShiftNeeded(pitch) {
|
||||
samples = tts.ShiftPitch(samples, pitch)
|
||||
samples = shifted
|
||||
}
|
||||
return samples, audio.SampleRate, nil
|
||||
}
|
||||
@@ -104,4 +123,4 @@ func (e *engine) release() {
|
||||
e.tts = nil
|
||||
e.loaded = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user