MCPcopy Create free account
hub / github.com/FunAudioLLM/FunMusic / reconstruct

Function reconstruct

tools/extract_semantic_token.py:75–88  ·  view source on GitHub ↗
(semantic_token_file, config_path, ckpt_path, outdir, sample_rate=24000)

Source from the content-addressed store, hash-verified

73
74
75def reconstruct(semantic_token_file, config_path, ckpt_path, outdir, sample_rate=24000):
76 if not os.path.isdir(outdir):
77 os.makedirs(outdir, exist_ok=True)
78 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
79 bandwidth_id = torch.tensor([0]).to(device)
80 wavtokenizer = WavTokenizer.from_pretrained_feat(config_path, ckpt_path).to(device)
81 utt2semantic_token = torch.load(semantic_token_file)
82 for utt in tqdm(utt2semantic_token.keys()):
83 token = utt2semantic_token[utt]
84 new_tensor = torch.tensor(token).to(device).unsqueeze(0)
85 features = wavtokenizer.codes_to_features(new_tensor)
86 wav = wavtokenizer.decode(features, bandwidth_id=bandwidth_id)
87 wav = wav.cpu().detach()
88 torchaudio.save(outdir + "/" + utt + ".wav", wav, sample_rate=sample_rate, encoding='PCM_S', bits_per_sample=16)
89
90if __name__ == "__main__":
91 parser = argparse.ArgumentParser()

Callers

nothing calls this directly

Calls 4

from_pretrained_featMethod · 0.80
loadMethod · 0.80
codes_to_featuresMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected