codec: add pre-encoded voice reference (--ref-spk / --ref-rvq)
qwen-codec --talker extracts the speaker embedding (.spk, raw f32) and the ICL codes (.rvq) in one pass, encode truncated to the hop boundary conforming to the --ref-wav path. qwen-tts loads them via --ref-spk / --ref-rvq and skips the speaker encoder and codec encode on every synthesis: TTFA 205 ms -> 89 ms. Extends qt_tts_params with ABI v2 latent fields, adds qt_num_codebooks(), ships freeman.spk + freeman.rvq and switches clone scripts to the latent path. Output is bit-identical to the raw path at fixed seed.
This commit is contained in:
+99
-116
@@ -5,6 +5,13 @@
|
||||
// file extension: .wav in -> encode, .rvq in -> decode. Output is
|
||||
// auto-named next to the input file by swapping the extension.
|
||||
//
|
||||
// Encode truncates the input to the hop boundary, strictly conforming
|
||||
// to the qwen-tts --ref-wav ICL path, so a .rvq produced here feeds
|
||||
// qwen-tts --ref-rvq directly. Passing --talker additionally runs the
|
||||
// speaker encoder from the talker GGUF on the full input and writes
|
||||
// the x-vector embedding next to the .rvq as a .spk file (raw f32,
|
||||
// enc_dim values), feeding qwen-tts --ref-spk.
|
||||
//
|
||||
// File format (.rvq): flat code stream packed at 11 bits per code,
|
||||
// LSB-first, no header. Layout is [K, T] row-major. K is fixed by the
|
||||
// codec config in the GGUF (16 codebooks for the 12Hz tokenizer,
|
||||
@@ -12,7 +19,11 @@
|
||||
|
||||
#include "audio-io.h"
|
||||
#include "backend.h"
|
||||
#include "gguf-weights.h"
|
||||
#include "pipeline-codec.h"
|
||||
#include "rvq-file.h"
|
||||
#include "speaker-encoder-extract.h"
|
||||
#include "speaker-encoder-weights.h"
|
||||
#include "utf8.h"
|
||||
#include "version.h"
|
||||
|
||||
@@ -23,115 +34,24 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static const uint32_t RVQ_CODE_MASK = (1u << TOKENIZER_CODE_BITS) - 1u;
|
||||
|
||||
static void print_usage(const char * prog) {
|
||||
fprintf(stderr, "qwentts.cpp %s\n\n", QWEN_VERSION);
|
||||
fprintf(stderr,
|
||||
"Usage: %s --model <gguf> [-i <input>] [--format <fmt>]\n\n"
|
||||
"Usage: %s --model <gguf> [-i <input>] [--talker <gguf>] [--format <fmt>]\n\n"
|
||||
"Required:\n"
|
||||
" --model <gguf> Codec GGUF (qwen-tokenizer-12hz-*.gguf)\n\n"
|
||||
"Optional:\n"
|
||||
" -i <path> Input. WAV -> encode, .rvq -> decode\n"
|
||||
" --talker <gguf> Talker GGUF (Base only). Encode also extracts the speaker\n"
|
||||
" embedding and writes it next to the .rvq as a .spk file\n"
|
||||
" --format <fmt> WAV output format: wav16, wav24, wav32 (default: wav16)\n\n"
|
||||
"Output is auto-named next to input : clip.wav -> clip.rvq, clip.rvq -> clip.wav.\n"
|
||||
"Encode truncates to the hop boundary, conforming to the qwen-tts --ref-wav path:\n"
|
||||
"the .rvq feeds qwen-tts --ref-rvq, the .spk feeds qwen-tts --ref-spk.\n"
|
||||
"When -i is omitted, runs a load self-test of the codec GGUF.\n",
|
||||
prog);
|
||||
}
|
||||
|
||||
// Symmetric unpack: reads N codes from packed bytes (11 bits LSB-first).
|
||||
static std::vector<int32_t> unpack_codes(const std::vector<uint8_t> & in, size_t n_codes) {
|
||||
std::vector<int32_t> out(n_codes);
|
||||
uint64_t acc = 0;
|
||||
int bits_in_acc = 0;
|
||||
size_t in_pos = 0;
|
||||
for (size_t i = 0; i < n_codes; i++) {
|
||||
while (bits_in_acc < TOKENIZER_CODE_BITS && in_pos < in.size()) {
|
||||
acc |= ((uint64_t) in[in_pos++]) << bits_in_acc;
|
||||
bits_in_acc += 8;
|
||||
}
|
||||
out[i] = (int32_t) (acc & RVQ_CODE_MASK);
|
||||
acc >>= TOKENIZER_CODE_BITS;
|
||||
bits_in_acc -= TOKENIZER_CODE_BITS;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Pack flat int32 codes into 11-bit LSB-first packed bytes. Output size is
|
||||
// ceil(N * 11 / 8) bytes.
|
||||
static std::vector<uint8_t> pack_codes(const std::vector<int32_t> & codes) {
|
||||
const size_t total_bits = codes.size() * (size_t) TOKENIZER_CODE_BITS;
|
||||
std::vector<uint8_t> out((total_bits + 7) / 8, 0);
|
||||
uint64_t acc = 0;
|
||||
int bits_in_acc = 0;
|
||||
size_t out_pos = 0;
|
||||
for (size_t i = 0; i < codes.size(); i++) {
|
||||
acc |= ((uint64_t) ((uint32_t) codes[i] & RVQ_CODE_MASK)) << bits_in_acc;
|
||||
bits_in_acc += TOKENIZER_CODE_BITS;
|
||||
while (bits_in_acc >= 8) {
|
||||
out[out_pos++] = (uint8_t) (acc & 0xFF);
|
||||
acc >>= 8;
|
||||
bits_in_acc -= 8;
|
||||
}
|
||||
}
|
||||
if (bits_in_acc > 0) {
|
||||
out[out_pos++] = (uint8_t) (acc & 0xFF);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Read a .rvq file and unpack it into K*T codes. T is inferred from the
|
||||
// file size: T = (filesize * 8) / (K * TOKENIZER_CODE_BITS).
|
||||
static bool read_rvq(const char * path, int K, std::vector<int32_t> & codes, int * n_frames) {
|
||||
FILE * f = utf8_fopen(path, "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "[Codec] FATAL: cannot open %s\n", path);
|
||||
return false;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (sz <= 0) {
|
||||
fprintf(stderr, "[Codec] FATAL: %s is empty\n", path);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
std::vector<uint8_t> buf((size_t) sz);
|
||||
if (fread(buf.data(), 1, buf.size(), f) != buf.size()) {
|
||||
fprintf(stderr, "[Codec] FATAL: short read on %s\n", path);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
const size_t total_bits = (size_t) sz * 8;
|
||||
const size_t n_codes = total_bits / (size_t) TOKENIZER_CODE_BITS;
|
||||
if (n_codes == 0 || (n_codes % (size_t) K) != 0) {
|
||||
fprintf(stderr, "[Codec] FATAL: %s yields %zu codes, not a multiple of K=%d\n", path, n_codes, K);
|
||||
return false;
|
||||
}
|
||||
codes = unpack_codes(buf, n_codes);
|
||||
*n_frames = (int) (n_codes / (size_t) K);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Pack and write a .rvq file.
|
||||
static bool write_rvq(const char * path, const std::vector<int32_t> & codes) {
|
||||
std::vector<uint8_t> packed = pack_codes(codes);
|
||||
FILE * f = utf8_fopen(path, "wb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "[Codec] FATAL: cannot open %s for write\n", path);
|
||||
return false;
|
||||
}
|
||||
if (fwrite(packed.data(), 1, packed.size(), f) != packed.size()) {
|
||||
fprintf(stderr, "[Codec] FATAL: short write on %s\n", path);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Replace or append extension on a path string.
|
||||
static std::string swap_ext(const std::string & path, const char * ext) {
|
||||
size_t dot = path.find_last_of('.');
|
||||
@@ -154,6 +74,60 @@ static int infer_mode(const char * path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Load the speaker encoder from the talker GGUF, run it on the full input
|
||||
// buffer and write the embedding as a raw f32 .spk file (enc_dim values,
|
||||
// validated by filesize on the qwen-tts side). Returns 0 on success.
|
||||
static int extract_spk(const char * talker_path,
|
||||
BackendPair bp,
|
||||
const float * audio,
|
||||
int n_samples,
|
||||
const char * out_path) {
|
||||
GGUFModel gf = {};
|
||||
if (!gf_load(&gf, talker_path)) {
|
||||
fprintf(stderr, "[Codec] FATAL: cannot open talker GGUF %s\n", talker_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SpeakerEncoderWeights sw = {};
|
||||
if (!speaker_encoder_weights_load(&sw, gf, bp.backend)) {
|
||||
fprintf(stderr, "[Codec] FATAL: speaker encoder load failed from %s\n", talker_path);
|
||||
gf_close(&gf);
|
||||
return 1;
|
||||
}
|
||||
gf_close(&gf);
|
||||
if (sw.weight_buf == NULL) {
|
||||
fprintf(stderr, "[Codec] FATAL: %s has no speaker encoder (Base only)\n", talker_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ggml_backend_sched_t sched = backend_sched_new(bp, 4096);
|
||||
const int enc_dim = sw.enc_dim;
|
||||
std::vector<float> emb;
|
||||
bool ok = speaker_encoder_extract(&sw, sched, audio, n_samples, emb);
|
||||
ggml_backend_sched_free(sched);
|
||||
speaker_encoder_weights_free(&sw);
|
||||
if (!ok || (int) emb.size() != enc_dim) {
|
||||
fprintf(stderr, "[Codec] FATAL: speaker embedding extraction failed (%zu values, enc_dim %d)\n", emb.size(),
|
||||
enc_dim);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE * f = utf8_fopen(out_path, "wb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "[Codec] FATAL: cannot open %s for write\n", out_path);
|
||||
return 1;
|
||||
}
|
||||
if (fwrite(emb.data(), sizeof(float), emb.size(), f) != emb.size()) {
|
||||
fprintf(stderr, "[Codec] FATAL: short write on %s\n", out_path);
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
fprintf(stderr, "[Codec] Wrote %s: %zu f32 values (%zu bytes)\n", out_path, emb.size(), emb.size() * sizeof(float));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
utf8_init(&argc, &argv);
|
||||
if (argc <= 1) {
|
||||
@@ -161,13 +135,16 @@ int main(int argc, char ** argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char * model_path = NULL;
|
||||
const char * input_path = NULL;
|
||||
WavFormat wav_fmt = WAV_S16;
|
||||
const char * model_path = NULL;
|
||||
const char * input_path = NULL;
|
||||
const char * talker_path = NULL;
|
||||
WavFormat wav_fmt = WAV_S16;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--model") == 0 && i + 1 < argc) {
|
||||
model_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--talker") == 0 && i + 1 < argc) {
|
||||
talker_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) {
|
||||
input_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--format") == 0 && i + 1 < argc) {
|
||||
@@ -217,40 +194,46 @@ int main(int argc, char ** argv) {
|
||||
if (!input_path) {
|
||||
fprintf(stderr, "[Codec] Load self-test passed\n");
|
||||
} else if (mode == 1) {
|
||||
// Encode .wav -> .rvq
|
||||
// Encode .wav -> .rvq (+ .spk with --talker)
|
||||
const std::string out_str = swap_ext(input_path, ".rvq");
|
||||
|
||||
int T_in = 0;
|
||||
float * audio_in = audio_read_mono(input_path, TOKENIZER_SAMPLE_RATE, &T_in);
|
||||
if (!audio_in || T_in <= 0) {
|
||||
fprintf(stderr, "[Codec] FATAL: cannot read %s\n", input_path);
|
||||
if (!audio_in || T_in < TOKENIZER_HOP_LENGTH) {
|
||||
fprintf(stderr, "[Codec] FATAL: cannot read %s or input shorter than one hop (%d samples)\n", input_path,
|
||||
TOKENIZER_HOP_LENGTH);
|
||||
free(audio_in);
|
||||
rc = 1;
|
||||
} else {
|
||||
// Pad to a multiple of HOP_LENGTH so the RVQ frame count is integral.
|
||||
int hop = TOKENIZER_HOP_LENGTH;
|
||||
int T_padded = ((T_in + hop - 1) / hop) * hop;
|
||||
int T_frames = T_padded / hop;
|
||||
// Truncate to a multiple of HOP_LENGTH, strictly conforming to
|
||||
// the qwen-tts --ref-wav ICL path.
|
||||
int hop = TOKENIZER_HOP_LENGTH;
|
||||
int T_aligned = (T_in / hop) * hop;
|
||||
int T_frames = T_aligned / hop;
|
||||
|
||||
std::vector<float> audio_buf((size_t) T_padded, 0.0f);
|
||||
memcpy(audio_buf.data(), audio_in, (size_t) T_in * sizeof(float));
|
||||
free(audio_in);
|
||||
fprintf(stderr, "[Codec] Encode: %s, %d samples @ %d Hz, truncated to %d (%d frames @ 12.5 Hz, %.2f s)\n",
|
||||
input_path, T_in, TOKENIZER_SAMPLE_RATE, T_aligned, T_frames,
|
||||
(double) T_aligned / (double) TOKENIZER_SAMPLE_RATE);
|
||||
|
||||
fprintf(stderr, "[Codec] Encode: %s, %d samples @ %d Hz, padded to %d (%d frames @ 12.5 Hz, %.2f s)\n",
|
||||
input_path, T_in, TOKENIZER_SAMPLE_RATE, T_padded, T_frames,
|
||||
(double) T_padded / (double) TOKENIZER_SAMPLE_RATE);
|
||||
|
||||
std::vector<int32_t> codes = pipeline_codec_encode(&pc, audio_buf.data(), T_padded);
|
||||
std::vector<int32_t> codes = pipeline_codec_encode(&pc, audio_in, T_aligned);
|
||||
if (codes.empty()) {
|
||||
fprintf(stderr, "[Codec] FATAL: encode failed\n");
|
||||
rc = 1;
|
||||
} else if (!write_rvq(out_str.c_str(), codes)) {
|
||||
} else if (!rvq_write_file(out_str.c_str(), codes, TOKENIZER_CODE_BITS)) {
|
||||
rc = 1;
|
||||
} else {
|
||||
fprintf(stderr, "[Codec] Wrote %s: K=%d T=%d, %zu codes -> %zu packed bytes\n", out_str.c_str(),
|
||||
TOKENIZER_NUM_CODEBOOKS, T_frames, codes.size(),
|
||||
(codes.size() * (size_t) TOKENIZER_CODE_BITS + 7) / 8);
|
||||
}
|
||||
|
||||
// Speaker embedding extraction, conforming to the qwen-tts
|
||||
// --ref-wav mode A path: the encoder consumes the FULL input
|
||||
// buffer, never the hop-truncated one.
|
||||
if (rc == 0 && talker_path) {
|
||||
rc = extract_spk(talker_path, bp, audio_in, T_in, swap_ext(input_path, ".spk").c_str());
|
||||
}
|
||||
free(audio_in);
|
||||
}
|
||||
} else {
|
||||
// Decode .rvq -> .wav
|
||||
@@ -258,7 +241,7 @@ int main(int argc, char ** argv) {
|
||||
|
||||
std::vector<int32_t> codes;
|
||||
int T = 0;
|
||||
if (!read_rvq(input_path, TOKENIZER_NUM_CODEBOOKS, codes, &T)) {
|
||||
if (!rvq_read_file(input_path, TOKENIZER_NUM_CODEBOOKS, TOKENIZER_CODE_BITS, codes, &T)) {
|
||||
rc = 1;
|
||||
} else {
|
||||
fprintf(stderr, "[Codec] Decode: %s, K=%d T=%d (%.2f s)\n", input_path, TOKENIZER_NUM_CODEBOOKS, T,
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "audio-io.h"
|
||||
#include "qwen.h"
|
||||
#include "rvq-file.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -40,6 +41,10 @@ static void print_usage(const char * prog) {
|
||||
" CustomVoice, rejected for Base\n"
|
||||
" --speaker <name> Speaker name (CustomVoice only)\n"
|
||||
" --ref-wav <path> Reference WAV for voice cloning (Base only)\n"
|
||||
" --ref-spk <path> Pre-extracted speaker embedding from qwen-codec --talker\n"
|
||||
" (replaces --ref-wav, Base only)\n"
|
||||
" --ref-rvq <path> Pre-encoded reference codes from qwen-codec (requires\n"
|
||||
" --ref-spk and --ref-text, enables ICL clone mode)\n"
|
||||
" --ref-text <path> Transcript file for the reference (enables ICL clone mode)\n"
|
||||
" --max-new <n> Max new audio frames (default: 2048)\n"
|
||||
" --codec-chunk-dur <f> Codec decode chunk duration in seconds (default: 24.0)\n"
|
||||
@@ -69,6 +74,8 @@ struct Args {
|
||||
const char * instruct;
|
||||
const char * speaker;
|
||||
const char * ref_wav;
|
||||
const char * ref_spk;
|
||||
const char * ref_rvq;
|
||||
const char * ref_text_path;
|
||||
const char * dump_dir;
|
||||
const char * out_wav;
|
||||
@@ -104,6 +111,34 @@ static std::string read_stdin_text() {
|
||||
}
|
||||
|
||||
// Read a small text file into a string. Trims trailing newlines.
|
||||
// 11 bits per code (V <= 2048), matching qwen-codec.
|
||||
static const int RVQ_CODE_BITS = 11;
|
||||
|
||||
// Read a .spk file: raw f32 values, the count IS the embedding dimension.
|
||||
static bool read_spk_file(const char * path, std::vector<float> & emb) {
|
||||
FILE * f = utf8_fopen(path, "rb");
|
||||
if (!f) {
|
||||
fprintf(stderr, "[CLI] ERROR: cannot open --ref-spk '%s'\n", path);
|
||||
return false;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (sz <= 0 || (sz % (long) sizeof(float)) != 0) {
|
||||
fprintf(stderr, "[CLI] ERROR: --ref-spk '%s' size %ld is not a positive multiple of 4\n", path, sz);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
emb.resize((size_t) sz / sizeof(float));
|
||||
if (fread(emb.data(), sizeof(float), emb.size(), f) != emb.size()) {
|
||||
fprintf(stderr, "[CLI] ERROR: short read on --ref-spk '%s'\n", path);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_text_file(const char * path, std::string & out) {
|
||||
FILE * f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
@@ -168,6 +203,10 @@ static bool parse_args(int argc, char ** argv, Args & a) {
|
||||
a.speaker = argv[++i];
|
||||
} else if (std::strcmp(arg, "--ref-wav") == 0 && i + 1 < argc) {
|
||||
a.ref_wav = argv[++i];
|
||||
} else if (std::strcmp(arg, "--ref-spk") == 0 && i + 1 < argc) {
|
||||
a.ref_spk = argv[++i];
|
||||
} else if (std::strcmp(arg, "--ref-rvq") == 0 && i + 1 < argc) {
|
||||
a.ref_rvq = argv[++i];
|
||||
} else if (std::strcmp(arg, "--ref-text") == 0 && i + 1 < argc) {
|
||||
a.ref_text_path = argv[++i];
|
||||
} else if (std::strcmp(arg, "--format") == 0 && i + 1 < argc) {
|
||||
@@ -278,6 +317,29 @@ static int run(const Args & a) {
|
||||
ref_n_samples = T_in;
|
||||
}
|
||||
|
||||
// Latent reference files. The .spk holds raw f32 values whose count
|
||||
// IS the embedding dimension; the .rvq holds the packed ICL code
|
||||
// matrix. The facade validates the structural constraints (mutual
|
||||
// exclusions, dim match against the talker hidden size).
|
||||
std::vector<float> ref_spk_emb;
|
||||
std::vector<int32_t> ref_codes;
|
||||
int ref_T = 0;
|
||||
if (a.ref_spk) {
|
||||
if (!read_spk_file(a.ref_spk, ref_spk_emb)) {
|
||||
qt_free(q);
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "[CLI] Reference SPK: %s, %zu f32 values\n", a.ref_spk, ref_spk_emb.size());
|
||||
}
|
||||
if (a.ref_rvq) {
|
||||
const int K = qt_num_codebooks(q);
|
||||
if (!rvq_read_file(a.ref_rvq, K, RVQ_CODE_BITS, ref_codes, &ref_T)) {
|
||||
qt_free(q);
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "[CLI] Reference RVQ: %s, K=%d T=%d\n", a.ref_rvq, K, ref_T);
|
||||
}
|
||||
|
||||
// Resolve output WAV format string: wav16 / wav24 / wav32. Default
|
||||
// wav16 mirrors the omnivoice.cpp default.
|
||||
WavFormat wav_fmt;
|
||||
@@ -323,6 +385,10 @@ static int run(const Args & a) {
|
||||
params.ref_audio_24k = ref_audio_24k;
|
||||
params.ref_n_samples = ref_n_samples;
|
||||
params.ref_text = ref_text;
|
||||
params.ref_spk_emb = ref_spk_emb.empty() ? NULL : ref_spk_emb.data();
|
||||
params.ref_spk_dim = (int) ref_spk_emb.size();
|
||||
params.ref_codes = ref_codes.empty() ? NULL : ref_codes.data();
|
||||
params.ref_T = ref_T;
|
||||
params.seed = a.seed;
|
||||
params.max_new_tokens = a.max_new_tokens;
|
||||
params.do_sample = a.do_sample;
|
||||
|
||||
Reference in New Issue
Block a user