speakd 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
speakd-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ibrahim Alfa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
speakd-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,252 @@
1
+ Metadata-Version: 2.4
2
+ Name: speakd
3
+ Version: 0.1.0
4
+ Summary: Local text-to-speech daemon and CLI for speech notifications from scripts, builds, cron jobs, and ML training runs, powered by Kokoro TTS with GPU offload and espeak fallback.
5
+ Author: ibrahim Alfa
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/I-Alpha/speakd
8
+ Project-URL: Repository, https://github.com/I-Alpha/speakd
9
+ Project-URL: Issues, https://github.com/I-Alpha/speakd/issues
10
+ Project-URL: Changelog, https://github.com/I-Alpha/speakd/blob/master/CHANGELOG.md
11
+ Keywords: tts,text-to-speech,speech-synthesis,speech-notifications,kokoro,kokoro-tts,daemon,unix-socket,cli,narration,notifications,machine-learning,gpu,cuda,developer-tools
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Environment :: No Input/Output (Daemon)
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: POSIX :: Linux
17
+ Classifier: Operating System :: MacOS
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
23
+ Classifier: Topic :: System :: Monitoring
24
+ Requires-Python: >=3.10
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Requires-Dist: kokoro>=0.9
28
+ Requires-Dist: soundfile>=0.12
29
+ Requires-Dist: numpy>=1.24
30
+ Requires-Dist: tomli>=2.0; python_version < "3.11"
31
+ Dynamic: license-file
32
+
33
+ # speakd
34
+
35
+ **A local text-to-speech daemon and CLI for speech notifications from
36
+ long-running jobs.**
37
+
38
+ `speakd` is a Python TTS daemon powered by
39
+ [Kokoro](https://github.com/hexgrad/kokoro) (a fast, high-quality local
40
+ text-to-speech model). Shell scripts, machine-learning training runs, builds,
41
+ cron jobs, CI hooks, and Python programs can send fire-and-forget speech
42
+ notifications over a Unix socket; the caller returns in about a millisecond
43
+ while the daemon queues, synthesizes, and plays each line in order. If anything
44
+ in the audio stack fails, the line degrades to espeak instead of disappearing.
45
+
46
+ It was built to narrate machine-learning training runs on a single-GPU
47
+ workstation, which shaped its defining feature: **the TTS model dynamically
48
+ offloads itself from the GPU** when narration goes quiet, so it never holds
49
+ VRAM hostage from the workload it is narrating.
50
+
51
+ ```
52
+ $ pip install .
53
+ $ speak "training started" # daemon auto-spawns on first use
54
+ $ speak --interrupt "loss is NaN" # cuts off whatever is playing, speaks NOW
55
+ $ make 2>&1 | tail -1 | speak # pipe-friendly
56
+ ```
57
+
58
+ ## Use cases
59
+
60
+ - Add voice alerts to machine-learning training runs when epochs finish,
61
+ checkpoints save, loss becomes NaN, or jobs crash.
62
+ - Turn shell scripts, Makefiles, cron jobs, and CI hooks into spoken status
63
+ updates.
64
+ - Use Kokoro TTS locally without blocking the process that asked for speech.
65
+ - Share one text-to-speech queue across multiple processes so messages do not
66
+ overlap.
67
+ - Release GPU VRAM after narration bursts with dynamic CPU/GPU offload.
68
+
69
+ ## Why a daemon?
70
+
71
+ Calling a TTS library inline is the obvious approach and the wrong one for
72
+ narration: it blocks the caller for seconds per line, loads a model per
73
+ process, and overlapping lines talk over each other. `speakd` inverts this:
74
+
75
+ - **~1 ms per call.** The client writes one line to a Unix socket and returns.
76
+ Narration can sit inside hot loops and signal handlers.
77
+ - **One model, one queue.** A single daemon owns the model and serialises
78
+ playback. Ten processes can narrate concurrently without crosstalk.
79
+ - **Failure-proof by design.** Daemon down? The client spawns it. Spawn fails?
80
+ espeak fallback. No audio at all? The caller still never raises.
81
+
82
+ ## Architecture
83
+
84
+ ```
85
+ any process, any language speakd daemon (one per socket, flock-enforced)
86
+ ┌──────────────────────┐ ┌───────────────────────────────────────────────┐
87
+ │ speak "epoch done" │──┐ │ asyncio Unix-socket server │
88
+ └──────────────────────┘ │ │ │ │
89
+ ┌──────────────────────┐ │ UTF-8 │ ├── volume msg ──▶ live volume │
90
+ │ Python: speak(...) │──┼─ line ─▶│ ├── interrupt ───▶ drain queue + │
91
+ └──────────────────────┘ │ over │ │ kill playback │
92
+ ┌──────────────────────┐ │ socket │ ▼ │
93
+ │ CI job, cron, hook │──┘ │ FIFO queue ──▶ worker (thread executor) │
94
+ └──────────────────────┘ │ │ │
95
+ ▲ │ ▼ │
96
+ │ "OK\n" ack │ Kokoro TTS ──▶ wav ──▶ mpv ──▶ 🔊 │
97
+ │ (blocking mode only) │ CPU ⇄ GPU │
98
+ └─────────────────────────│ (offloads after idle keepalive) │
99
+ │ │
100
+ │ any failure ──▶ espeak fallback │
101
+ └───────────────────────────────────────────────┘
102
+ ```
103
+
104
+ ## Features
105
+
106
+ - **Fire-and-forget socket design** — newline-terminated UTF-8 over a Unix
107
+ domain socket; trivially scriptable from any language. Optional `OK` ack
108
+ for blocking callers.
109
+ - **Dynamic GPU offload with keepalive** — the model loads on CPU, hops onto
110
+ the GPU for narration bursts, and releases its VRAM (~3 GB) after a
111
+ configurable idle period. If the GPU is full (another job grabbed it), that
112
+ request simply synthesizes on CPU instead of failing.
113
+ - **Interrupt protocol** — an urgent line drains the pending queue, kills
114
+ in-flight playback mid-word, and speaks immediately.
115
+ - **Live volume control** — one socket message, applies from the next line;
116
+ no restart.
117
+ - **Singleton via `flock(2)`** — clients can race to auto-spawn the daemon;
118
+ exactly one wins, the rest exit cleanly. Stale sockets are detected and
119
+ removed on startup.
120
+ - **Graceful fallback** — Kokoro import error, synthesis failure, playback
121
+ failure, or daemon unreachable: the line is spoken by espeak and the event
122
+ is logged. Narration degrades; it never silently vanishes.
123
+ - **One TOML file, env-var overrides, zero-config defaults** — works out of
124
+ the box on CPU with no config file at all.
125
+
126
+ ## Requirements
127
+
128
+ - Linux or macOS (Unix sockets + `flock`), Python ≥ 3.10
129
+ - [mpv](https://mpv.io/) for playback (`apt install mpv`) — or any player,
130
+ via config
131
+ - [espeak](https://espeak.sourceforge.net/) for the fallback voice
132
+ (`apt install espeak`) — optional but recommended
133
+ - A CUDA-capable GPU is **optional**; everything works on CPU
134
+
135
+ ## Install
136
+
137
+ ```bash
138
+ git clone <this-repo> && cd speakd
139
+ pip install .
140
+ ```
141
+
142
+ This installs the `kokoro` TTS package (which pulls in PyTorch) and two
143
+ console commands: `speakd` (the daemon) and `speak` (the client).
144
+
145
+ ## Quickstart
146
+
147
+ ```bash
148
+ # 1. Just speak — the daemon auto-spawns on first use:
149
+ speak "hello from speakd"
150
+
151
+ # 2. Or run the daemon in the foreground to watch it work:
152
+ speakd --device cpu --voice af_heart
153
+
154
+ # 3. Script it:
155
+ speak --blocking "waits until this has been spoken"
156
+ speak --interrupt "queue drained, this plays immediately"
157
+ speak --volume 60 "quieter from now on"
158
+ echo "pipes work too" | speak
159
+ ```
160
+
161
+ From Python:
162
+
163
+ ```python
164
+ from speakd import speak, set_volume
165
+
166
+ speak("checkpoint saved") # ~1 ms, non-blocking
167
+ speak("eval finished", blocking=True) # wait until spoken
168
+ speak("loss is NaN — stopping", interrupt=True) # jump the queue
169
+ set_volume(85)
170
+ ```
171
+
172
+ See [`examples/`](examples/) for runnable demos of narration, interrupts,
173
+ and volume control.
174
+
175
+ ## Configuration
176
+
177
+ Defaults work with no config at all. To customise, copy
178
+ [`config.example.toml`](config.example.toml) to `~/.config/speakd/config.toml`
179
+ (or point `$SPEAKD_CONFIG` at any path). Environment variables override the
180
+ file; CLI flags override both.
181
+
182
+ | TOML key | Env override | Default | Meaning |
183
+ |---|---|---|---|
184
+ | `tts.voice` | `SPEAKD_VOICE` | `af_heart` | Kokoro voice id (`af_*`, `am_*`, `bf_*`, `bm_*`, ...) |
185
+ | `tts.speed` | `SPEAKD_SPEED` | `1.0` | Speech-rate multiplier |
186
+ | `tts.lang_code` | `SPEAKD_LANG` | `a` | Kokoro language code (`a` US English, `b` UK English) |
187
+ | `device.policy` | `SPEAKD_DEVICE` | `auto` | `auto` (dynamic offload) / `cpu` / `gpu` |
188
+ | `device.keepalive_seconds` | `SPEAKD_KEEPALIVE` | `180` | Idle seconds before GPU→CPU offload |
189
+ | `daemon.socket_path` | `SPEAKD_SOCKET` | `$XDG_RUNTIME_DIR/speakd.sock` | Unix socket path |
190
+ | `daemon.socket_mode` | — | `"600"` | Octal permissions on the socket file |
191
+ | `daemon.log_file` | `SPEAKD_LOG_FILE` | `~/.local/state/speakd/daemon.log` | Log target for auto-spawned daemons |
192
+ | `audio.volume` | `SPEAKD_VOLUME` | `100` | Playback volume `0–130` (mpv scale) |
193
+ | `audio.max_playback_seconds` | — | `120` | Kill a single line's playback after this |
194
+ | `audio.player` | — | mpv template | Player argv; `{file}` and `{volume}` are substituted |
195
+ | `fallback.command` | — | espeak template | Fallback argv; `{text}` is substituted; `[]` disables |
196
+ | `client.connect_timeout` | — | `0.5` | Socket connect/send timeout (s) |
197
+ | `client.ack_timeout` | — | `300.0` | `--blocking` wait for the spoken-ack (s) |
198
+ | `client.spawn_wait` | — | `4.0` | Wait for an auto-spawned daemon (s) |
199
+
200
+ `speakd --print-config` shows the fully-resolved effective configuration.
201
+
202
+ ## Wire protocol
203
+
204
+ One newline-terminated UTF-8 line per connection — easy to speak from any
205
+ language without a client library:
206
+
207
+ | Message | Bytes | Effect |
208
+ |---|---|---|
209
+ | Speak | `<text>\n` | Queue the line; daemon replies `OK\n` when spoken |
210
+ | Interrupt | `\x01INTERRUPT\x01<text>\n` | Drain queue, kill playback, speak now |
211
+ | Volume | `\x02VOLUME\x02<int>\n` | Set live volume (0–130) |
212
+
213
+ ```bash
214
+ # speak from raw shell, no client needed:
215
+ printf 'hello from netcat\n' | nc -U "$XDG_RUNTIME_DIR/speakd.sock"
216
+ ```
217
+
218
+ The control markers are ASCII SOH/STX characters that cannot occur in normal
219
+ text, so no escaping is ever needed.
220
+
221
+ ## GPU offload in detail
222
+
223
+ The `auto` policy exists for machines where the GPU has a day job:
224
+
225
+ 1. The model loads on **CPU** at first request.
226
+ 2. Each synthesis tries to move it to the **GPU** first (a few hundred ms,
227
+ then synthesis is much faster). If CUDA is busy or OOM, that line
228
+ synthesizes on CPU — no error, just slower.
229
+ 3. After `keepalive_seconds` (default 180 s) without a request, an idle timer
230
+ moves the model back to **CPU** and calls `torch.cuda.empty_cache()`,
231
+ releasing the VRAM.
232
+
233
+ The effect: during an active narration burst the voice is snappy and
234
+ GPU-accelerated; ten minutes into a silent stretch, your training job has its
235
+ VRAM back. All device moves are serialised with synthesis under one lock, so
236
+ the model can never be moved mid-utterance.
237
+
238
+ ## Troubleshooting
239
+
240
+ | Symptom | Likely cause / fix |
241
+ |---|---|
242
+ | `speak` says *fallback engine used* | Daemon failed to start — check `~/.local/state/speakd/daemon.log`. Most common: `kokoro` not installed in the Python that spawned it (set `SPEAKD_DAEMON_CMD="/path/to/python -m speakd.daemon"`). |
243
+ | No audio, no errors | Is `mpv` installed and does it play a wav from your terminal? Swap `audio.player` if you use a different player. |
244
+ | First line is slow | Cold start: model weights load on first request (a few seconds). Subsequent lines are fast. |
245
+ | Robotic voice instead of Kokoro | That *is* the espeak fallback working as designed — see the first row. |
246
+ | Two daemons after a crash | They cannot coexist: the flock singleton makes the second exit immediately, and stale sockets are cleaned on startup. Delete `<socket>.lock` only if a machine crash left it owned by a dead PID holder (flock releases on process death, so this is near-impossible). |
247
+ | `daemon already running (pid N)` | Working as intended — the running daemon serves all clients. |
248
+ | GPU memory not released | The model offloads after `device.keepalive_seconds` of *no requests*; lower it, or run with `--device cpu`. |
249
+
250
+ ## License
251
+
252
+ [MIT](LICENSE) © 2026 ibrahim Alfa
speakd-0.1.0/README.md ADDED
@@ -0,0 +1,220 @@
1
+ # speakd
2
+
3
+ **A local text-to-speech daemon and CLI for speech notifications from
4
+ long-running jobs.**
5
+
6
+ `speakd` is a Python TTS daemon powered by
7
+ [Kokoro](https://github.com/hexgrad/kokoro) (a fast, high-quality local
8
+ text-to-speech model). Shell scripts, machine-learning training runs, builds,
9
+ cron jobs, CI hooks, and Python programs can send fire-and-forget speech
10
+ notifications over a Unix socket; the caller returns in about a millisecond
11
+ while the daemon queues, synthesizes, and plays each line in order. If anything
12
+ in the audio stack fails, the line degrades to espeak instead of disappearing.
13
+
14
+ It was built to narrate machine-learning training runs on a single-GPU
15
+ workstation, which shaped its defining feature: **the TTS model dynamically
16
+ offloads itself from the GPU** when narration goes quiet, so it never holds
17
+ VRAM hostage from the workload it is narrating.
18
+
19
+ ```
20
+ $ pip install .
21
+ $ speak "training started" # daemon auto-spawns on first use
22
+ $ speak --interrupt "loss is NaN" # cuts off whatever is playing, speaks NOW
23
+ $ make 2>&1 | tail -1 | speak # pipe-friendly
24
+ ```
25
+
26
+ ## Use cases
27
+
28
+ - Add voice alerts to machine-learning training runs when epochs finish,
29
+ checkpoints save, loss becomes NaN, or jobs crash.
30
+ - Turn shell scripts, Makefiles, cron jobs, and CI hooks into spoken status
31
+ updates.
32
+ - Use Kokoro TTS locally without blocking the process that asked for speech.
33
+ - Share one text-to-speech queue across multiple processes so messages do not
34
+ overlap.
35
+ - Release GPU VRAM after narration bursts with dynamic CPU/GPU offload.
36
+
37
+ ## Why a daemon?
38
+
39
+ Calling a TTS library inline is the obvious approach and the wrong one for
40
+ narration: it blocks the caller for seconds per line, loads a model per
41
+ process, and overlapping lines talk over each other. `speakd` inverts this:
42
+
43
+ - **~1 ms per call.** The client writes one line to a Unix socket and returns.
44
+ Narration can sit inside hot loops and signal handlers.
45
+ - **One model, one queue.** A single daemon owns the model and serialises
46
+ playback. Ten processes can narrate concurrently without crosstalk.
47
+ - **Failure-proof by design.** Daemon down? The client spawns it. Spawn fails?
48
+ espeak fallback. No audio at all? The caller still never raises.
49
+
50
+ ## Architecture
51
+
52
+ ```
53
+ any process, any language speakd daemon (one per socket, flock-enforced)
54
+ ┌──────────────────────┐ ┌───────────────────────────────────────────────┐
55
+ │ speak "epoch done" │──┐ │ asyncio Unix-socket server │
56
+ └──────────────────────┘ │ │ │ │
57
+ ┌──────────────────────┐ │ UTF-8 │ ├── volume msg ──▶ live volume │
58
+ │ Python: speak(...) │──┼─ line ─▶│ ├── interrupt ───▶ drain queue + │
59
+ └──────────────────────┘ │ over │ │ kill playback │
60
+ ┌──────────────────────┐ │ socket │ ▼ │
61
+ │ CI job, cron, hook │──┘ │ FIFO queue ──▶ worker (thread executor) │
62
+ └──────────────────────┘ │ │ │
63
+ ▲ │ ▼ │
64
+ │ "OK\n" ack │ Kokoro TTS ──▶ wav ──▶ mpv ──▶ 🔊 │
65
+ │ (blocking mode only) │ CPU ⇄ GPU │
66
+ └─────────────────────────│ (offloads after idle keepalive) │
67
+ │ │
68
+ │ any failure ──▶ espeak fallback │
69
+ └───────────────────────────────────────────────┘
70
+ ```
71
+
72
+ ## Features
73
+
74
+ - **Fire-and-forget socket design** — newline-terminated UTF-8 over a Unix
75
+ domain socket; trivially scriptable from any language. Optional `OK` ack
76
+ for blocking callers.
77
+ - **Dynamic GPU offload with keepalive** — the model loads on CPU, hops onto
78
+ the GPU for narration bursts, and releases its VRAM (~3 GB) after a
79
+ configurable idle period. If the GPU is full (another job grabbed it), that
80
+ request simply synthesizes on CPU instead of failing.
81
+ - **Interrupt protocol** — an urgent line drains the pending queue, kills
82
+ in-flight playback mid-word, and speaks immediately.
83
+ - **Live volume control** — one socket message, applies from the next line;
84
+ no restart.
85
+ - **Singleton via `flock(2)`** — clients can race to auto-spawn the daemon;
86
+ exactly one wins, the rest exit cleanly. Stale sockets are detected and
87
+ removed on startup.
88
+ - **Graceful fallback** — Kokoro import error, synthesis failure, playback
89
+ failure, or daemon unreachable: the line is spoken by espeak and the event
90
+ is logged. Narration degrades; it never silently vanishes.
91
+ - **One TOML file, env-var overrides, zero-config defaults** — works out of
92
+ the box on CPU with no config file at all.
93
+
94
+ ## Requirements
95
+
96
+ - Linux or macOS (Unix sockets + `flock`), Python ≥ 3.10
97
+ - [mpv](https://mpv.io/) for playback (`apt install mpv`) — or any player,
98
+ via config
99
+ - [espeak](https://espeak.sourceforge.net/) for the fallback voice
100
+ (`apt install espeak`) — optional but recommended
101
+ - A CUDA-capable GPU is **optional**; everything works on CPU
102
+
103
+ ## Install
104
+
105
+ ```bash
106
+ git clone <this-repo> && cd speakd
107
+ pip install .
108
+ ```
109
+
110
+ This installs the `kokoro` TTS package (which pulls in PyTorch) and two
111
+ console commands: `speakd` (the daemon) and `speak` (the client).
112
+
113
+ ## Quickstart
114
+
115
+ ```bash
116
+ # 1. Just speak — the daemon auto-spawns on first use:
117
+ speak "hello from speakd"
118
+
119
+ # 2. Or run the daemon in the foreground to watch it work:
120
+ speakd --device cpu --voice af_heart
121
+
122
+ # 3. Script it:
123
+ speak --blocking "waits until this has been spoken"
124
+ speak --interrupt "queue drained, this plays immediately"
125
+ speak --volume 60 "quieter from now on"
126
+ echo "pipes work too" | speak
127
+ ```
128
+
129
+ From Python:
130
+
131
+ ```python
132
+ from speakd import speak, set_volume
133
+
134
+ speak("checkpoint saved") # ~1 ms, non-blocking
135
+ speak("eval finished", blocking=True) # wait until spoken
136
+ speak("loss is NaN — stopping", interrupt=True) # jump the queue
137
+ set_volume(85)
138
+ ```
139
+
140
+ See [`examples/`](examples/) for runnable demos of narration, interrupts,
141
+ and volume control.
142
+
143
+ ## Configuration
144
+
145
+ Defaults work with no config at all. To customise, copy
146
+ [`config.example.toml`](config.example.toml) to `~/.config/speakd/config.toml`
147
+ (or point `$SPEAKD_CONFIG` at any path). Environment variables override the
148
+ file; CLI flags override both.
149
+
150
+ | TOML key | Env override | Default | Meaning |
151
+ |---|---|---|---|
152
+ | `tts.voice` | `SPEAKD_VOICE` | `af_heart` | Kokoro voice id (`af_*`, `am_*`, `bf_*`, `bm_*`, ...) |
153
+ | `tts.speed` | `SPEAKD_SPEED` | `1.0` | Speech-rate multiplier |
154
+ | `tts.lang_code` | `SPEAKD_LANG` | `a` | Kokoro language code (`a` US English, `b` UK English) |
155
+ | `device.policy` | `SPEAKD_DEVICE` | `auto` | `auto` (dynamic offload) / `cpu` / `gpu` |
156
+ | `device.keepalive_seconds` | `SPEAKD_KEEPALIVE` | `180` | Idle seconds before GPU→CPU offload |
157
+ | `daemon.socket_path` | `SPEAKD_SOCKET` | `$XDG_RUNTIME_DIR/speakd.sock` | Unix socket path |
158
+ | `daemon.socket_mode` | — | `"600"` | Octal permissions on the socket file |
159
+ | `daemon.log_file` | `SPEAKD_LOG_FILE` | `~/.local/state/speakd/daemon.log` | Log target for auto-spawned daemons |
160
+ | `audio.volume` | `SPEAKD_VOLUME` | `100` | Playback volume `0–130` (mpv scale) |
161
+ | `audio.max_playback_seconds` | — | `120` | Kill a single line's playback after this |
162
+ | `audio.player` | — | mpv template | Player argv; `{file}` and `{volume}` are substituted |
163
+ | `fallback.command` | — | espeak template | Fallback argv; `{text}` is substituted; `[]` disables |
164
+ | `client.connect_timeout` | — | `0.5` | Socket connect/send timeout (s) |
165
+ | `client.ack_timeout` | — | `300.0` | `--blocking` wait for the spoken-ack (s) |
166
+ | `client.spawn_wait` | — | `4.0` | Wait for an auto-spawned daemon (s) |
167
+
168
+ `speakd --print-config` shows the fully-resolved effective configuration.
169
+
170
+ ## Wire protocol
171
+
172
+ One newline-terminated UTF-8 line per connection — easy to speak from any
173
+ language without a client library:
174
+
175
+ | Message | Bytes | Effect |
176
+ |---|---|---|
177
+ | Speak | `<text>\n` | Queue the line; daemon replies `OK\n` when spoken |
178
+ | Interrupt | `\x01INTERRUPT\x01<text>\n` | Drain queue, kill playback, speak now |
179
+ | Volume | `\x02VOLUME\x02<int>\n` | Set live volume (0–130) |
180
+
181
+ ```bash
182
+ # speak from raw shell, no client needed:
183
+ printf 'hello from netcat\n' | nc -U "$XDG_RUNTIME_DIR/speakd.sock"
184
+ ```
185
+
186
+ The control markers are ASCII SOH/STX characters that cannot occur in normal
187
+ text, so no escaping is ever needed.
188
+
189
+ ## GPU offload in detail
190
+
191
+ The `auto` policy exists for machines where the GPU has a day job:
192
+
193
+ 1. The model loads on **CPU** at first request.
194
+ 2. Each synthesis tries to move it to the **GPU** first (a few hundred ms,
195
+ then synthesis is much faster). If CUDA is busy or OOM, that line
196
+ synthesizes on CPU — no error, just slower.
197
+ 3. After `keepalive_seconds` (default 180 s) without a request, an idle timer
198
+ moves the model back to **CPU** and calls `torch.cuda.empty_cache()`,
199
+ releasing the VRAM.
200
+
201
+ The effect: during an active narration burst the voice is snappy and
202
+ GPU-accelerated; ten minutes into a silent stretch, your training job has its
203
+ VRAM back. All device moves are serialised with synthesis under one lock, so
204
+ the model can never be moved mid-utterance.
205
+
206
+ ## Troubleshooting
207
+
208
+ | Symptom | Likely cause / fix |
209
+ |---|---|
210
+ | `speak` says *fallback engine used* | Daemon failed to start — check `~/.local/state/speakd/daemon.log`. Most common: `kokoro` not installed in the Python that spawned it (set `SPEAKD_DAEMON_CMD="/path/to/python -m speakd.daemon"`). |
211
+ | No audio, no errors | Is `mpv` installed and does it play a wav from your terminal? Swap `audio.player` if you use a different player. |
212
+ | First line is slow | Cold start: model weights load on first request (a few seconds). Subsequent lines are fast. |
213
+ | Robotic voice instead of Kokoro | That *is* the espeak fallback working as designed — see the first row. |
214
+ | Two daemons after a crash | They cannot coexist: the flock singleton makes the second exit immediately, and stale sockets are cleaned on startup. Delete `<socket>.lock` only if a machine crash left it owned by a dead PID holder (flock releases on process death, so this is near-impossible). |
215
+ | `daemon already running (pid N)` | Working as intended — the running daemon serves all clients. |
216
+ | GPU memory not released | The model offloads after `device.keepalive_seconds` of *no requests*; lower it, or run with `--device cpu`. |
217
+
218
+ ## License
219
+
220
+ [MIT](LICENSE) © 2026 ibrahim Alfa
@@ -0,0 +1,62 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "speakd"
7
+ version = "0.1.0"
8
+ description = "Local text-to-speech daemon and CLI for speech notifications from scripts, builds, cron jobs, and ML training runs, powered by Kokoro TTS with GPU offload and espeak fallback."
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ authors = [{ name = "ibrahim Alfa" }]
12
+ requires-python = ">=3.10"
13
+ keywords = [
14
+ "tts",
15
+ "text-to-speech",
16
+ "speech-synthesis",
17
+ "speech-notifications",
18
+ "kokoro",
19
+ "kokoro-tts",
20
+ "daemon",
21
+ "unix-socket",
22
+ "cli",
23
+ "narration",
24
+ "notifications",
25
+ "machine-learning",
26
+ "gpu",
27
+ "cuda",
28
+ "developer-tools",
29
+ ]
30
+ classifiers = [
31
+ "Development Status :: 4 - Beta",
32
+ "Environment :: No Input/Output (Daemon)",
33
+ "Intended Audience :: Developers",
34
+ "License :: OSI Approved :: MIT License",
35
+ "Operating System :: POSIX :: Linux",
36
+ "Operating System :: MacOS",
37
+ "Programming Language :: Python :: 3",
38
+ "Programming Language :: Python :: 3.10",
39
+ "Programming Language :: Python :: 3.11",
40
+ "Programming Language :: Python :: 3.12",
41
+ "Topic :: Multimedia :: Sound/Audio :: Speech",
42
+ "Topic :: System :: Monitoring",
43
+ ]
44
+ dependencies = [
45
+ "kokoro>=0.9",
46
+ "soundfile>=0.12",
47
+ "numpy>=1.24",
48
+ "tomli>=2.0; python_version < '3.11'",
49
+ ]
50
+
51
+ [project.urls]
52
+ Homepage = "https://github.com/I-Alpha/speakd"
53
+ Repository = "https://github.com/I-Alpha/speakd"
54
+ Issues = "https://github.com/I-Alpha/speakd/issues"
55
+ Changelog = "https://github.com/I-Alpha/speakd/blob/master/CHANGELOG.md"
56
+
57
+ [project.scripts]
58
+ speakd = "speakd.daemon:main"
59
+ speak = "speakd.client:main"
60
+
61
+ [tool.setuptools.packages.find]
62
+ where = ["src"]
speakd-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,37 @@
1
+ """speakd — fire-and-forget local TTS narration over a Unix socket.
2
+
3
+ A small daemon that turns text lines into speech with `Kokoro
4
+ <https://github.com/hexgrad/kokoro>`_, plus a zero-dependency client.
5
+ Designed for narrating long-running work (training runs, builds, pipelines)
6
+ without ever blocking or crashing the thing doing the work.
7
+
8
+ Quickstart::
9
+
10
+ from speakd import speak
11
+ speak("experiment finished") # fire-and-forget
12
+ speak("loss is NaN — stopping", interrupt=True)
13
+ """
14
+ from typing import TYPE_CHECKING
15
+
16
+ __version__ = "0.1.0"
17
+
18
+ if TYPE_CHECKING: # real imports for type checkers / IDEs
19
+ from .client import ensure_daemon, set_volume, speak
20
+ from .config import Config, load_config
21
+
22
+ __all__ = ["speak", "set_volume", "ensure_daemon", "Config", "load_config", "__version__"]
23
+
24
+ _CLIENT_ATTRS = ("speak", "set_volume", "ensure_daemon")
25
+ _CONFIG_ATTRS = ("Config", "load_config")
26
+
27
+
28
+ def __getattr__(name: str):
29
+ """Lazy re-exports (PEP 562): keep ``import speakd`` instant and avoid
30
+ eagerly importing submodules that ``python -m speakd.<mod>`` re-executes."""
31
+ if name in _CLIENT_ATTRS:
32
+ from . import client
33
+ return getattr(client, name)
34
+ if name in _CONFIG_ATTRS:
35
+ from . import config
36
+ return getattr(config, name)
37
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")