Download recipe/scripts/copy_synth.py from gowombat/tiny-voca-da: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/scripts/copy_synth.py
- Command line
-
hf download hf://gowombat/tiny-voca-da/recipe/scripts/copy_synth.py
-
curl -L -o copy_synth.py https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/scripts/copy_synth.py
3.72 kB
| """Copy synthesis: real recordings re-voiced by a tiny_voca.vocoder from their own Vocos log-mel, | |
| pitch and voicing (the vocoder's validation path). Separates vocoder artefacts from the voice | |
| model's: if the re-voiced recording is already buzzy, the vocoder is the cause. | |
| Usage (kit or kit-v3 image): | |
| python scripts/copy_synth.py --vocoder models/vocoder-da-prior-s50000.pt \ | |
| --manifest data/full-features/validation.jsonl --ids out/val120.jsonl --output out/copy-voc50k | |
| Writes <output>/real/<id>.wav (the stored 24 kHz recording) and <output>/vocoded/<id>.wav. | |
| --high-hz/--high-gain override the prior's high-band pulse share at inference (a sweep; a vocoder | |
| trained without it is then outside its training distribution). | |
| """ | |
| import argparse | |
| import dataclasses | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import soundfile as sf | |
| import torch | |
| sys.path.insert(0, '/opt/kit') | |
| from tiny_voca.losses import VocosMel # noqa: E402 | |
| from tiny_voca.vocoder import Vocoder, load_vocoder # noqa: E402 | |
| def with_high_band(vocoder, high_hz, high_gain): | |
| """The same weights with another high-band pulse share (its buffers derive from the config).""" | |
| config = dataclasses.replace(vocoder.config, prior_high_hz=high_hz, prior_high_gain=high_gain) | |
| other = Vocoder(config) | |
| other.load_state_dict(vocoder.state_dict()) | |
| return other.eval() | |
| def main(): | |
| p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| p.add_argument('--vocoder', type=Path, required=True) | |
| p.add_argument('--manifest', type=Path, required=True, help='prepared feature manifest (wave + prosody)') | |
| p.add_argument('--ids', type=Path, help='jsonl whose ids select the clips') | |
| p.add_argument('--output', type=Path, required=True) | |
| p.add_argument('--high-hz', type=float) | |
| p.add_argument('--high-gain', type=float) | |
| args = p.parse_args() | |
| vocoder = load_vocoder(args.vocoder) | |
| if args.high_hz is not None: | |
| vocoder = with_high_band(vocoder, args.high_hz, args.high_gain if args.high_gain is not None else 0.05) | |
| keep = None | |
| if args.ids: | |
| keep = {json.loads(l)['id'] for l in args.ids.read_text(encoding='utf-8').splitlines() if l.strip()} | |
| rows = [json.loads(l) for l in args.manifest.read_text(encoding='utf-8').splitlines() if l.strip()] | |
| rows = [r for r in rows if keep is None or r['id'] in keep] | |
| for sub in ('real', 'vocoded'): | |
| (args.output / sub).mkdir(parents=True, exist_ok=True) | |
| features = VocosMel() | |
| with torch.inference_mode(): | |
| for row in rows: | |
| stored = torch.load(args.manifest.parent / row['path'], map_location='cpu', weights_only=True) | |
| wave = stored['wave'].float()[None] | |
| mel = features(wave).transpose(1, 2) | |
| f0 = (200.0 * torch.exp(stored['prosody'][:, 0].float()))[None] | |
| voiced = (stored['prosody'][:, 1] > 0.5).float()[None] | |
| torch.manual_seed(0) | |
| fake = vocoder(mel, f0, voiced)[0, :wave.shape[1]] | |
| sf.write(args.output / 'real' / f"{row['id']}.wav", wave[0].numpy(), vocoder.config.sample_rate, subtype='FLOAT') | |
| sf.write(args.output / 'vocoded' / f"{row['id']}.wav", fake.numpy(), vocoder.config.sample_rate, subtype='FLOAT') | |
| (args.output / 'copy.json').write_text(json.dumps({'vocoder': str(args.vocoder), 'manifest': str(args.manifest), | |
| 'high_hz': args.high_hz, 'high_gain': args.high_gain, | |
| 'clips': [r['id'] for r in rows]}, indent=2)) | |
| print(f'copy-synthesised {len(rows)} clips -> {args.output}/{{real,vocoded}}') | |
| if __name__ == '__main__': | |
| main() | |