Spaces:
Sleeping
Sleeping
Download soprano/utils/streaming.py from sampleacc-3003/Soprano-TTS: direct link, hf CLI and curl.
- Browser
- Download file 918 Bytes
-
https://huggingface.co/spaces/sampleacc-3003/Soprano-TTS/resolve/main/soprano/utils/streaming.py
- Command line
-
hf download hf://spaces/sampleacc-3003/Soprano-TTS/soprano/utils/streaming.py
-
curl -L -o streaming.py https://huggingface.co/spaces/sampleacc-3003/Soprano-TTS/resolve/main/soprano/utils/streaming.py
918 Bytes
| import sounddevice as sd | |
| import torch | |
| import time | |
| def play_stream(stream, sample_rate=32000): | |
| """ | |
| Play streamed audio chunks to speakers in real time. | |
| """ | |
| with sd.OutputStream( | |
| samplerate=sample_rate, | |
| channels=1, | |
| dtype='float32', | |
| blocksize=0 | |
| ) as out_stream: | |
| start = time.time() | |
| latency = None | |
| first = True | |
| for chunk in stream: | |
| if first: | |
| latency = time.time()-start | |
| first = False | |
| if isinstance(chunk, torch.Tensor): | |
| chunk = chunk.detach().cpu() | |
| # Ensure shape (N, 1) | |
| if chunk.dim() == 1: | |
| chunk = chunk.unsqueeze(1) | |
| elif chunk.dim() == 2 and chunk.shape[0] == 1: | |
| chunk = chunk.transpose(0, 1) | |
| out_stream.write(chunk.numpy()) | |
| return latency | |