Files
spark-pocket-tts/pocket_tts/data/audio_utils.py
T
Saleem DjimaandVáclav Volhejn 4ec89acf95 Feat/add chunk size control arg (#56)
This pull request addresses issue #38 

**Changes included:**

- Added a `max_tokens` parameter to the typer cli for the `generate`
command, allowing user to set a default number of tokens per chunk.
- Introduced a default variable `MAX_TOKEN_PER_CHUNK` to centralize the
default value.
- Fixed a tokenization issue by inserting a comma, allowing the model to
correctly read each chunk as intended rather than treating one chunk as
a single sentence.

**Future improvement suggestion :**

- Consider enhancing the `split_into_best_sentences` function using
regex to split text more accurately without modifying it, which would
improve clarity and maintainability.

---------

Co-authored-by: Václav Volhejn <8401624+vvolhejn@users.noreply.github.com>
2026-01-21 11:54:53 +01:00

29 lines
947 B
Python

"""Various utilities for audio conversion (pcm format, sample rate and channels),
and volume normalization."""
import torch
from scipy.signal import resample_poly
def convert_audio(
wav: torch.Tensor, from_rate: int | float, to_rate: int | float, to_channels: int
) -> torch.Tensor:
"""Convert audio to new sample rate and number of audio channels."""
if from_rate != to_rate:
# Convert to numpy for scipy resampling
wav_np = wav.detach().cpu().numpy()
# Calculate resampling parameters
gcd = int(torch.gcd(torch.tensor(from_rate), torch.tensor(to_rate)).item())
up = int(to_rate // gcd)
down = int(from_rate // gcd)
# Resample using scipy
resampled_np = resample_poly(wav_np, up, down, axis=-1)
# Convert back to torch tensor
wav = torch.from_numpy(resampled_np).to(wav.device).to(wav.dtype)
assert wav.shape[-2] == to_channels
return wav