-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreaming_utils.py
73 lines (57 loc) · 1.6 KB
/
streaming_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import io
import wave
import numpy as np
import typing as t
from pydantic import BaseModel
if t.TYPE_CHECKING:
import torch
class StreamingInputs(BaseModel):
text: str
language: str
add_wav_header: bool = True
stream_chunk_size: int = 20
def postprocess(wav):
"""Post process the output waveform"""
import torch
if isinstance(wav, list):
wav = torch.cat(wav, dim=0)
wav = wav.clone().detach().cpu().numpy()
wav = wav[None, : int(wav.shape[0])]
wav = np.clip(wav, -1, 1)
wav = (wav * 32767).astype(np.int16)
return wav
def encode_audio_common(
frame_input, sample_rate=24000, sample_width=2, channels=1
):
wav_buf = io.BytesIO()
with wave.open(wav_buf, "wb") as vfout:
vfout.setnchannels(channels)
vfout.setsampwidth(sample_width)
vfout.setframerate(sample_rate)
vfout.writeframes(frame_input)
wav_buf.seek(0)
return wav_buf.read()
def predict_streaming_generator(
model,
text: str,
language: str,
speaker_embedding: "torch.Tensor",
gpt_cond_latent: "torch.Tensor",
stream_chunk_size: int,
add_wav_header: bool,
):
chunks = model.inference_stream(
text,
language,
gpt_cond_latent,
speaker_embedding,
stream_chunk_size=stream_chunk_size,
enable_text_splitting=True
)
for i, chunk in enumerate(chunks):
chunk = postprocess(chunk)
if i == 0 and add_wav_header:
yield encode_audio_common(b"")
yield chunk.tobytes()
else:
yield chunk.tobytes()