Repository navigation
Expand file tree
/
Copy pathcodec.sh
More file actions
executable file
·86 lines (71 loc) · 3.2 KB
/
Copy pathcodec.sh
File metadata and controls
executable file
·86 lines (71 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# codec.sh — extract speaker embeddings (.spk) and RVQ codes (.rvq) from a WAV, or encode/decode .rvq.
#
# Usage: codec.sh [input.wav] [output_dir] [speaker_name]
#
# Input WAV : reference voice sample
# Output Dir : directory for extracted files (default: embeddings/)
# Speaker Name : name prefix for output files (default: speaker)
# Mode : controlled by QWENTTS_CODEC_MODE in .env (extract, encode, decode)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ── load defaults ────────────────────────────────────────────────────
if [[ ! -f "$SCRIPT_DIR/.env" ]]; then
echo "Error: .env file not found." >&2
echo "Copy .env.example to .env and configure it:" >&2
echo " cp .env.example .env" >&2
echo " # Then edit .env to set QWENTTS_PATH and other settings" >&2
exit 1
fi
set -a; source "$SCRIPT_DIR/.env"; set +a
# ── defaults ─────────────────────────────────────────────────────────
INPUT_FILE="${1:-}"
OUTPUT_DIR="${2:-$SCRIPT_DIR/embeddings}"
SPEAKER_NAME="${3:-speaker}"
# ── advanced CLI flags (env overrides) ───────────────────────────────
# Codec mode: encode | decode | extract (default: extract)
CODEC_MODE="${QWENTTS_CODEC_MODE:-extract}"
# ── model paths ──────────────────────────────────────────────────────
MODEL_PATH="$QWENTTS_PATH/models/$QWENTTS_CODEC_MODEL"
TALKER_PATH="$QWENTTS_PATH/models/$QWENTTS_TALKER_MODEL"
if [[ -z "$INPUT_FILE" ]]; then
echo "Usage: $0 <input.wav|input.rvq> [output_dir] [speaker_name]" >&2
exit 1
fi
mkdir -p "$OUTPUT_DIR"
# ── build argument array ─────────────────────────────────────────────
ARGS=()
ARGS+=(--model "$MODEL_PATH")
case "$CODEC_MODE" in
extract)
# Extract speaker embedding + RVQ codes from WAV
ARGS+=(--talker "$TALKER_PATH")
;;
encode)
# Encode WAV → .rvq (no speaker embedding)
;;
decode)
# Decode .rvq → WAV
;;
*)
echo "Unknown CODEC_MODE: $CODEC_MODE (use extract, encode, or decode)" >&2
exit 1
;;
esac
ARGS+=(-i "$INPUT_FILE")
"$QWENTTS_PATH/build/qwen-codec" "${ARGS[@]}"
# ── move output to embeddings directory ──────────────────────────────
INPUT_DIR="$(dirname "$INPUT_FILE")"
INPUT_BASE="$(basename "$INPUT_FILE" .wav)"
INPUT_BASE="${INPUT_BASE%.rvq}" # strip .rvq if that was the extension
for ext in spk rvq wav; do
SRC="$INPUT_DIR/${INPUT_BASE}.${ext}"
if [[ -f "$SRC" ]]; then
cp "$SRC" "$OUTPUT_DIR/${SPEAKER_NAME}.${ext}"
echo "Saved $OUTPUT_DIR/${SPEAKER_NAME}.${ext}"
fi
done
echo "Done."
if [[ "$CODEC_MODE" == "extract" ]]; then
echo "Use with clone.sh via QWENTTS_REF_SPK and QWENTTS_REF_RVQ."
fi