Prof-Hunter commited on
Commit
706d6b4
·
verified ·
1 Parent(s): 8a4ca67

Update piper_engine.py

Browse files
Files changed (1) hide show
  1. piper_engine.py +6 -20
piper_engine.py CHANGED
@@ -3,14 +3,11 @@
3
  import io
4
  import numpy as np
5
  import soundfile as sf
6
-
7
  from piper.voice import PiperVoice
8
 
9
-
10
  class PiperEngine:
11
 
12
  def __init__(self, model_path):
13
- # Load the voice model
14
  self.voice = PiperVoice.load(model_path)
15
 
16
  def synthesize(
@@ -20,33 +17,22 @@ class PiperEngine:
20
  pitch=1.0,
21
  sample_rate=22050,
22
  ):
23
- """
24
- Synthesizes expressive speech from text using Piper.
25
- Returns a BytesIO WAV buffer.
26
- """
27
 
28
- # Generate raw audio (generator)
29
  audio_gen = self.voice.synthesize(
30
- text,
31
- 1 / speed, # length_scale
32
- pitch # pitch_scale
33
  )
34
 
35
- # Consume generator into a NumPy array
36
  audio = np.fromiter(audio_gen, dtype=np.float32)
37
 
38
- # Ensure audio is 2D: shape (samples, channels)
39
  if audio.ndim == 1:
40
  audio = audio[:, np.newaxis]
41
 
42
- # Write to WAV buffer
43
  buffer = io.BytesIO()
44
- sf.write(
45
- buffer,
46
- audio,
47
- sample_rate,
48
- format="WAV",
49
- )
50
  buffer.seek(0)
51
 
52
  return buffer
 
3
  import io
4
  import numpy as np
5
  import soundfile as sf
 
6
  from piper.voice import PiperVoice
7
 
 
8
  class PiperEngine:
9
 
10
  def __init__(self, model_path):
 
11
  self.voice = PiperVoice.load(model_path)
12
 
13
  def synthesize(
 
17
  pitch=1.0,
18
  sample_rate=22050,
19
  ):
 
 
 
 
20
 
21
+ # Correctly call synthesize with keyword arguments
22
  audio_gen = self.voice.synthesize(
23
+ text=text,
24
+ length_scale=1 / speed,
25
+ pitch_scale=pitch
26
  )
27
 
28
+ # Consume generator into NumPy array
29
  audio = np.fromiter(audio_gen, dtype=np.float32)
30
 
 
31
  if audio.ndim == 1:
32
  audio = audio[:, np.newaxis]
33
 
 
34
  buffer = io.BytesIO()
35
+ sf.write(buffer, audio, sample_rate, format="WAV")
 
 
 
 
 
36
  buffer.seek(0)
37
 
38
  return buffer