Prof-Hunter commited on
Commit
7c18902
·
verified ·
1 Parent(s): e4d7b4f

Update piper_engine.py

Browse files
Files changed (1) hide show
  1. piper_engine.py +42 -39
piper_engine.py CHANGED
@@ -1,39 +1,42 @@
1
- import subprocess
2
- import tempfile
3
-
4
-
5
- def piper_tts(
6
- text,
7
- model_path,
8
- config_path,
9
- speed=1.0,
10
- pitch=1.0,
11
- ):
12
-
13
- # Output wav file
14
- with tempfile.NamedTemporaryFile(
15
- delete=False,
16
- suffix=".wav"
17
- ) as f:
18
- out_path = f.name
19
-
20
- cmd = [
21
- "piper",
22
- "--model", model_path,
23
- "--config", config_path,
24
- "--output_file", out_path,
25
- "--length_scale", str(1 / speed),
26
- "--pitch_scale", str(pitch),
27
- ]
28
-
29
- process = subprocess.Popen(
30
- cmd,
31
- stdin=subprocess.PIPE,
32
- stdout=subprocess.PIPE,
33
- stderr=subprocess.PIPE,
34
- text=True,
35
- )
36
-
37
- process.communicate(text)
38
-
39
- return out_path
 
 
 
 
1
+ # piper_engine.py
2
+
3
+ import io
4
+ import soundfile as sf
5
+
6
+ from piper.voice import PiperVoice
7
+
8
+
9
+ class PiperEngine:
10
+
11
+ def __init__(self, model_path):
12
+
13
+ self.voice = PiperVoice.load(model_path)
14
+
15
+ def synthesize(
16
+ self,
17
+ text,
18
+ speed=1.0,
19
+ pitch=1.0,
20
+ sample_rate=22050,
21
+ ):
22
+
23
+ # Generate raw audio
24
+ audio = self.voice.synthesize(
25
+ text,
26
+ length_scale=1 / speed,
27
+ pitch_scale=pitch,
28
+ )
29
+
30
+ # Convert to WAV buffer
31
+ buffer = io.BytesIO()
32
+
33
+ sf.write(
34
+ buffer,
35
+ audio,
36
+ sample_rate,
37
+ format="WAV",
38
+ )
39
+
40
+ buffer.seek(0)
41
+
42
+ return buffer