-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvarco_client.py
More file actions
105 lines (83 loc) · 3.68 KB
/
varco_client.py
File metadata and controls
105 lines (83 loc) · 3.68 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""VARCO Sound API client module."""
import base64
import requests
from pathlib import Path
BASE_URL = "https://openapi.ai.nc.com/sound/varco/v1/api"
TIMEOUT = 120
def _headers(api_key: str) -> dict:
return {"OPENAPI_KEY": api_key.strip(), "Content-Type": "application/json"}
def _audio_to_base64(file_bytes: bytes) -> str:
return base64.b64encode(file_bytes).decode("utf-8")
def _decode_audio(b64: str) -> bytes:
return base64.b64decode(b64)
def _post(api_key: str, endpoint: str, payload: dict) -> requests.Response:
url = f"{BASE_URL}/{endpoint}"
resp = requests.post(url, json=payload, headers=_headers(api_key), timeout=TIMEOUT)
resp.raise_for_status()
return resp
# ── Text to Sound ──────────────────────────────────────────────
def text_to_sound(
api_key: str,
prompt: str,
num_sample: int = 1,
version: str = "v2",
) -> list[bytes]:
payload = {"prompt": prompt, "num_sample": num_sample, "version": version}
data = _post(api_key, "text2sound", payload).json()
return [_decode_audio(item["audio"]) for item in data]
# ── Variation ──────────────────────────────────────────────────
def variation(
api_key: str,
source: bytes,
num_sample: int = 1,
strength: float = 1.0,
begin: float | None = None,
end: float | None = None,
) -> list[bytes]:
payload: dict = {
"source": _audio_to_base64(source),
"num_sample": num_sample,
"strength": strength,
}
if begin is not None and end is not None:
payload["include"] = {"begin": begin, "end": end}
data = _post(api_key, "variation", payload).json()
return [_decode_audio(item["audio"]) for item in data]
# ── Mono to Stereo ─────────────────────────────────────────────
def mono_to_stereo(api_key: str, source: bytes) -> bytes:
payload = {"source": _audio_to_base64(source)}
data = _post(api_key, "mono2stereo", payload).json()
return _decode_audio(data["audio"])
# ── Looping ────────────────────────────────────────────────────
def looping(
api_key: str,
source: bytes,
begin: float | None = None,
end: float | None = None,
) -> bytes:
payload: dict = {"source": _audio_to_base64(source)}
if begin is not None and end is not None:
payload["preserve"] = {"begin": begin, "end": end}
data = _post(api_key, "looping", payload).json()
return _decode_audio(data["audio"])
# ── Conversion ─────────────────────────────────────────────────
def conversion(
api_key: str,
source: bytes,
reference: bytes,
ratio: float = 1.0,
enhance: bool = False,
) -> bytes:
payload = {
"source": _audio_to_base64(source),
"reference": _audio_to_base64(reference),
"ratio": ratio,
"enhance": enhance,
}
data = _post(api_key, "conversion", payload).json()
return _decode_audio(data["audio"])
# ── Enhance ────────────────────────────────────────────────────
def enhance(api_key: str, source: bytes) -> bytes:
payload = {"source": _audio_to_base64(source)}
data = _post(api_key, "enhance", payload).json()
return _decode_audio(data["audio"])