File size: 16,523 Bytes
090f72c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# Reachy Mini Home Assistant Voice Assistant - Architecture Design

## 1. System Architecture Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Application Layer                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Home         β”‚  β”‚ Web UI       β”‚  β”‚ Console      β”‚           β”‚
β”‚  β”‚ Assistant    β”‚  β”‚ (Gradio)     β”‚  β”‚ Interface    β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Business Logic Layer                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Voice        β”‚  β”‚ Motion       β”‚  β”‚ State        β”‚           β”‚
β”‚  β”‚ Manager      β”‚  β”‚ Controller   β”‚  β”‚ Manager      β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                           β”‚
β”‚  β”‚ ESPHome      β”‚  β”‚ Event        β”‚                           β”‚
β”‚  β”‚ Handler      β”‚  β”‚ Dispatcher   β”‚                           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Services Layer                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Wake Word    β”‚  β”‚ Audio        β”‚  β”‚ Motion       β”‚           β”‚
β”‚  β”‚ Detector     β”‚  β”‚ Processor    β”‚  β”‚ Queue        β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚  β”‚ ESPHome Protocol (Audio Streaming to/from HA)       β”‚       β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Hardware Abstraction Layer                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Audio        β”‚  β”‚ Motion       β”‚  β”‚ Camera       β”‚           β”‚
β”‚  β”‚ Adapter      β”‚  β”‚ Adapter      β”‚  β”‚ Adapter      β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                           β”‚
β”‚  β”‚ Reachy Mini  β”‚  β”‚ ESPHome      β”‚                           β”‚
β”‚  β”‚ SDK Wrapper  β”‚  β”‚ Protocol     β”‚                           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Reachy Mini Hardware + Home Assistant                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚ Microphone   β”‚  β”‚ Head Motors  β”‚  β”‚ Camera       β”‚           β”‚
β”‚  β”‚ Array (4)    β”‚  β”‚ (6 DOF)      β”‚  β”‚ (Wide)       β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                           β”‚
β”‚  β”‚ Speaker      β”‚  β”‚ Antennas     β”‚                           β”‚
β”‚  β”‚ (5W)         β”‚  β”‚ (2)          β”‚                           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β”‚
β”‚                                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚ Home Assistant (STT/TTS Processing)                 β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## 2. Core Design Principles

### 2.1 Based on linux-voice-assistant
This project is based on the architecture of [OHF-Voice/linux-voice-assistant](https://github.com/OHF-Voice/linux-voice-assistant), with key features:

- **STT/TTS Handled by Home Assistant**: Audio data is transmitted to Home Assistant via ESPHome protocol for speech recognition and synthesis
- **Local Wake Word Detection**: Uses microWakeWord or openWakeWord for offline wake word detection
- **ESPHome Protocol Communication**: Communicates with Home Assistant via ESPHome protocol
- **Motion Control Enhancement**: Integrates Reachy Mini's motion control capabilities

### 2.2 Architecture Characteristics
- **Modular Design**: Audio, voice, motion, and ESPHome modules are independent
- **Asynchronous Processing**: Uses asyncio for high-performance asynchronous processing
- **State Management**: Centralized state management (ServerState)
- **Event-Driven**: Event-based communication mechanism

## 3. Module Design

### 3.1 Audio Module (audio/)

**Responsibilities**:
- Audio device management (microphone, speaker)
- Audio recording and playback
- Audio format conversion (16KHz mono PCM)

**Interfaces**:

```python
class AudioAdapter(ABC):
    """Audio device adapter abstract base class"""
    
    @abstractmethod
    async def list_input_devices(self) -> List[AudioDevice]:
        """List available audio input devices"""
        pass
    
    @abstractmethod
    async def list_output_devices(self) -> List[AudioDevice]:
        """List available audio output devices"""
        pass
    
    @abstractmethod
    async def start_recording(self, device: str, callback: Callable) -> None:
        """Start audio recording"""
        pass
    
    @abstractmethod
    async def stop_recording(self) -> None:
        """Stop audio recording"""
        pass
    
    @abstractmethod
    async def play_audio(self, audio_data: bytes, device: str) -> None:
        """Play audio"""
        pass
```

**Key Components**:
- `adapter.py`: Audio device adapter implementation
- `processor.py`: Audio processor (format conversion, buffering)

### 3.2 Voice Module (voice/)

**Responsibilities**:
- Wake word detection (local offline)
- STT (Speech-to-Text) - backup implementation
- TTS (Text-to-Speech) - backup implementation

**Interfaces**:

```python
class WakeWordDetector(ABC):
    """Wake word detector abstract base class"""
    
    @abstractmethod
    async def load_model(self, model_path: str) -> None:
        """Load wake word model"""
        pass
    
    @abstractmethod
    async def detect(self, audio_chunk: bytes) -> bool:
        """Detect wake word in audio chunk"""
        pass
    
    @abstractmethod
    async def set_sensitivity(self, sensitivity: float) -> None:
        """Set detection sensitivity"""
        pass
```

**Key Components**:
- `detector.py`: Wake word detector (microWakeWord/openWakeWord)
- `stt.py`: STT engine (Whisper - backup)
- `tts.py`: TTS engine (Piper - backup)

### 3.3 Motion Module (motion/)

**Responsibilities**:
- Head motion control (6 DOF)
- Antenna animation
- Motion queue management (priority-based)
- Speech-reactive motions

**Interfaces**:

```python
class MotionController(ABC):
    """Motion controller abstract base class"""
    
    @abstractmethod
    async def connect(self, host: str, wireless: bool) -> None:
        """Connect to Reachy Mini"""
        pass
    
    @abstractmethod
    async def move_head(self, pose: HeadPose, duration: float) -> None:
        """Move head to specified pose"""
        pass
    
    @abstractmethod
    async def set_antenna(self, antenna_id: int, angle: float) -> None:
        """Set antenna angle"""
        pass
    
    @abstractmethod
    async def play_emotion(self, emotion: str) -> None:
        """Play emotion"""
        pass
```

**Key Components**:
- `controller.py`: Motion controller implementation
- `queue.py`: Motion queue manager (priority-based)

### 3.4 ESPHome Module (esphome/)

**Responsibilities**:
- ESPHome protocol server implementation
- Audio streaming to/from Home Assistant
- Event handling (wake word, TTS start/end, STT result)
- mDNS service discovery

**Interfaces**:

```python
class ESPHomeServer(ABC):
    """ESPHome server abstract base class"""
    
    @abstractmethod
    async def start(self, host: str, port: int) -> None:
        """Start ESPHome server"""
        pass
    
    @abstractmethod
    async def stop(self) -> None:
        """Stop ESPHome server"""
        pass
    
    @abstractmethod
    async def send_audio(self, audio_data: bytes) -> None:
        """Send audio to Home Assistant"""
        pass
    
    @abstractmethod
    async def on_event(self, event: ESPHomeEvent) -> None:
        """Handle ESPHome event"""
        pass
```

**Key Components**:
- `protocol.py`: ESPHome protocol definitions
- `server.py`: ESPHome server implementation

### 3.5 Configuration Module (config/)

**Responsibilities**:
- Configuration file management
- Environment variable management
- Default configuration

**Interfaces**:

```python
class ConfigManager:
    """Configuration manager"""
    
    def __init__(self, config_path: str):
        """Initialize configuration manager"""
        pass
    
    def load(self) -> Dict:
        """Load configuration"""
        pass
    
    def save(self, config: Dict) -> None:
        """Save configuration"""
        pass
    
    def get(self, key: str, default=None) -> Any:
        """Get configuration value"""
        pass
```

**Key Components**:
- `manager.py`: Configuration manager implementation

## 4. Data Flow

### 4.1 Wake Word Detection Flow

```
Microphone Input (16kHz PCM)
    ↓
Audio Chunk (1024 samples)
    ↓
Wake Word Detector
    β”œβ”€ microWakeWord Features
    └─ openWakeWord Features
    ↓
Detection
    β”œβ”€ microWakeWord: probability > cutoff
    └─ openWakeWord: probability > 0.5
    ↓
Refractory Period Check (2 seconds)
    ↓
Trigger Wakeup Event
    ↓
ESPHome Server β†’ Home Assistant
```

### 4.2 Audio Streaming Flow (to Home Assistant)

```
Microphone Input
    ↓
Audio Chunk
    ↓
ESPHome Server
    ↓
VoiceAssistantAudio Message
    ↓
Home Assistant (STT Processing)
    ↓
VoiceAssistantEvent (STT Result)
```

### 4.3 TTS Audio Flow (from Home Assistant)

```
Home Assistant (TTS Processing)
    ↓
VoiceAssistantEvent (TTS Start)
    ↓
ESPHome Server
    ↓
Motion Controller (Speech-reactive motions)
    ↓
VoiceAssistantAudio (TTS Audio)
    ↓
Speaker Playback
    ↓
VoiceAssistantEvent (TTS End)
```

## 5. State Management

### 5.1 ServerState

Centralized state management:

```python
class ServerState:
    """Server global state"""
    
    # Application info
    name: str
    mac_address: str
    
    # Audio
    audio_queue: Queue
    audio_input_device: Optional[str]
    audio_output_device: Optional[str]
    
    # Voice
    wake_words: Dict[str, WakeWordDetector]
    active_wake_words: List[str]
    stop_word: WakeWordDetector
    
    # Motion
    motion_controller: MotionController
    motion_queue: MotionQueue
    
    # ESPHome
    esphome_server: ESPHomeServer
    esphome_connected: bool
    
    # Status
    is_streaming_audio: bool
    is_playing_tts: bool
```

## 6. Deployment Architecture

### 6.1 Running on Reachy Mini

```
Reachy Mini (Raspberry Pi 4)
β”œβ”€β”€ Application (This Project)
β”‚   β”œβ”€β”€ Audio Module
β”‚   β”œβ”€β”€ Voice Module
β”‚   β”œβ”€β”€ Motion Module
β”‚   └── ESPHome Module
β”œβ”€β”€ Reachy Mini Hardware
β”‚   β”œβ”€β”€ 4 Microphones
β”‚   β”œβ”€β”€ 5W Speaker
β”‚   β”œβ”€β”€ Head Motors (6 DOF)
β”‚   └── Antennas (2)
└── Network
    └── ESPHome Protocol (Port 6053)
        β””β†’ Home Assistant
```

### 6.2 Home Assistant Integration

```
Home Assistant
β”œβ”€β”€ ESPHome Integration
β”‚   β””β†’ Reachy Mini (ESPHome Server)
β”œβ”€β”€ Voice Assistant
β”‚   β”œβ”€β”€ STT Service
β”‚   └── TTS Service
└── Automations
    β””β†’ Voice Commands
```

## 7. Performance Considerations

### 7.1 Latency Targets

- **Wake Word Detection**: < 500ms
- **Audio Streaming**: < 100ms
- **TTS Playback**: < 200ms
- **Motion Response**: < 100ms

### 7.2 Resource Requirements

- **CPU**: Raspberry Pi 4 (4 cores)
- **RAM**: 4GB minimum
- **Network**: Stable WiFi/Ethernet connection

## 8. Security Considerations

### 8.1 ESPHome Security

- Use encrypted connections (TLS)
- Implement authentication (if required)
- Validate all incoming messages

### 8.2 Audio Privacy

- Audio data is transmitted only when wake word is detected
- Support for local-only mode (no audio transmission)
- Clear audio recording indicators

## 9. Future Extensions

### 9.1 Additional Features

- Face tracking (camera integration)
- Visual recognition (SmolVLM2)
- Advanced emotions (dance library)
- Multi-language support

### 9.2 Performance Optimizations

- GPU acceleration for wake word detection
- Audio preprocessing on hardware
- Motion trajectory optimization

---

**Note**: This architecture document is the English version of ARCHITECTURE.md. For the Chinese version, see ARCHITECTURE.md.