luke3000 commited on
Commit
6f15c76
·
verified ·
1 Parent(s): af20af8

Upload hf_job_build.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. hf_job_build.py +70 -7
hf_job_build.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import json
2
  import os
3
  import shutil
@@ -72,18 +74,79 @@ def write_wav16(path, x):
72
  w.writeframes(pcm.tobytes())
73
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def synth(args):
76
  word, path = args
77
  p = Path(path)
78
  if p.exists() and p.stat().st_size > 44:
79
  return word, True
80
  p.parent.mkdir(parents=True, exist_ok=True)
81
- with tempfile.TemporaryDirectory() as td:
82
- raw = Path(td) / "t.wav"
83
- subprocess.run(["espeak-ng", "-v", VOICE, "-s", SPEED, "-w", str(raw), word],
84
- check=True, capture_output=True)
85
- x, sr = read_wav_mono(raw)
86
- write_wav16(p, resample(x, sr))
 
 
 
 
 
 
87
  return word, False
88
 
89
 
@@ -110,7 +173,7 @@ def save(done):
110
  (idx / "manifest.json").write_text(json.dumps(
111
  {"model": MODEL, "sample_rate": SR, "dim": int(mat.shape[1]), "count": len(words),
112
  "pooling": "mean", "feature": FEATURE, "centered": True, "normalized": True,
113
- "tts": "espeak-ng", "voice": VOICE}, indent=2))
114
  return words
115
 
116
 
 
1
+ import ctypes
2
+ import ctypes.util
3
  import json
4
  import os
5
  import shutil
 
74
  w.writeframes(pcm.tobytes())
75
 
76
 
77
+ CARRIER = os.environ.get("CARRIER", "i say {word} now")
78
+ EVENT_WORD = 1
79
+ AUDIO_OUTPUT_SYNCHRONOUS = 2
80
+
81
+
82
+ class _Event(ctypes.Structure):
83
+ _fields_ = [("type", ctypes.c_int), ("uid", ctypes.c_uint), ("tpos", ctypes.c_int),
84
+ ("length", ctypes.c_int), ("apos", ctypes.c_int), ("sample", ctypes.c_int),
85
+ ("ud", ctypes.c_void_p)]
86
+
87
+
88
+ _CB = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.POINTER(ctypes.c_short), ctypes.c_int,
89
+ ctypes.POINTER(_Event))
90
+ _lib = None
91
+
92
+
93
+ def _espeak_init():
94
+ global _lib
95
+ if _lib is not None:
96
+ return
97
+ name = ctypes.util.find_library("espeak-ng") or "libespeak-ng.so.1"
98
+ _lib = ctypes.CDLL(name)
99
+ _lib.espeak_Initialize.restype = ctypes.c_int
100
+ _lib.espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, None, 0)
101
+ _lib.espeak_SetVoiceByName.argtypes = [ctypes.c_char_p]
102
+ _lib.espeak_SetVoiceByName(VOICE.encode())
103
+ _lib.espeak_SetParameter(1, int(SPEED), 0)
104
+ _lib.espeak_Synth.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_uint,
105
+ ctypes.c_int, ctypes.c_uint, ctypes.c_uint,
106
+ ctypes.c_void_p, ctypes.c_void_p]
107
+
108
+
109
+ def _synth_text(text):
110
+ _espeak_init()
111
+ chunks, events = [], []
112
+
113
+ def cb(wav, n, evp):
114
+ if n > 0 and wav:
115
+ chunks.append(np.ctypeslib.as_array(wav, shape=(n,)).copy())
116
+ i = 0
117
+ while evp[i].type != 0:
118
+ if evp[i].type == EVENT_WORD:
119
+ events.append((evp[i].sample, evp[i].length, evp[i].tpos))
120
+ i += 1
121
+ return 0
122
+
123
+ c = _CB(cb)
124
+ _lib.espeak_SetSynthCallback.argtypes = [_CB]
125
+ _lib.espeak_SetSynthCallback(c)
126
+ data = text.encode()
127
+ _lib.espeak_Synth(data, len(data) + 1, 0, 0, 0, 0, None, None)
128
+ audio = np.concatenate(chunks).astype(np.float32) / 32768.0 if chunks else np.zeros(0, np.float32)
129
+ return audio, events
130
+
131
+
132
  def synth(args):
133
  word, path = args
134
  p = Path(path)
135
  if p.exists() and p.stat().st_size > 44:
136
  return word, True
137
  p.parent.mkdir(parents=True, exist_ok=True)
138
+ text = CARRIER.format(word=word)
139
+ audio, events = _synth_text(text)
140
+ ev = sorted(e for e in events if e[1] > 0)
141
+ seg = None
142
+ for i, (start, length, tpos) in enumerate(ev):
143
+ if text[tpos - 1:tpos - 1 + length].strip() == word:
144
+ end = ev[i + 1][0] if i + 1 < len(ev) else len(audio)
145
+ seg = audio[start:end]
146
+ break
147
+ if seg is None or len(seg) < 400:
148
+ seg, _ = _synth_text(word)
149
+ write_wav16(p, resample(seg, 22050))
150
  return word, False
151
 
152
 
 
173
  (idx / "manifest.json").write_text(json.dumps(
174
  {"model": MODEL, "sample_rate": SR, "dim": int(mat.shape[1]), "count": len(words),
175
  "pooling": "mean", "feature": FEATURE, "centered": True, "normalized": True,
176
+ "tts": "espeak-ng", "voice": VOICE, "carrier": CARRIER}, indent=2))
177
  return words
178
 
179