livekit-plugins-alkove 0.2.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.
- livekit_plugins_alkove-0.2.0/.gitignore +3 -0
- livekit_plugins_alkove-0.2.0/LICENSE +202 -0
- livekit_plugins_alkove-0.2.0/NOTICE +27 -0
- livekit_plugins_alkove-0.2.0/PKG-INFO +56 -0
- livekit_plugins_alkove-0.2.0/README.md +37 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/__init__.py +25 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/focus.py +91 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/log.py +3 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/models.py +16 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/py.typed +0 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/vad.py +168 -0
- livekit_plugins_alkove-0.2.0/livekit/plugins/alkove/version.py +1 -0
- livekit_plugins_alkove-0.2.0/pyproject.toml +32 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Alkove
|
|
2
|
+
Copyright 2026 Eddy Bordi
|
|
3
|
+
|
|
4
|
+
This product includes software developed by third parties:
|
|
5
|
+
|
|
6
|
+
- alkove-focus-1-preview is derived from GTCRN
|
|
7
|
+
(https://github.com/Xiaobin-Rong/gtcrn), under the MIT License:
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2024 Rong Xiaobin
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: livekit-plugins-alkove
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Alkove for LiveKit Agents: Focus keeps the user's voice (noise cancellation), Cue finds speech (VAD), on the agent's CPU
|
|
5
|
+
Project-URL: Homepage, https://alkove.ai
|
|
6
|
+
Project-URL: Repository, https://github.com/alkove-ai/alkove
|
|
7
|
+
Author: Eddy Bordi
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
License-File: NOTICE
|
|
11
|
+
Keywords: livekit,noise-cancellation,on-device,vad,voice,voice-agent,voice-isolation
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Requires-Dist: alkove<0.3,>=0.2
|
|
16
|
+
Requires-Dist: livekit-agents<2,>=1.8.8
|
|
17
|
+
Requires-Dist: numpy>=1.21
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# livekit-plugins-alkove
|
|
21
|
+
|
|
22
|
+
[Alkove](https://alkove.ai) for [LiveKit Agents](https://docs.livekit.io/agents/): Focus keeps the user's voice and removes the others and the noise, Cue finds where they speak. Both run on the agent's CPU, with no server and no key, with the chain and the speech rules of Alkove's web SDK.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pip install livekit-plugins-alkove
|
|
28
|
+
python my_agent.py download-files # Focus and Cue into the cache, checked by their sha256
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Use
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from livekit.agents import AgentSession, room_io
|
|
35
|
+
from livekit.plugins import alkove
|
|
36
|
+
|
|
37
|
+
session = AgentSession(vad=alkove.VAD.load(), ...)
|
|
38
|
+
await session.start(
|
|
39
|
+
agent,
|
|
40
|
+
room=ctx.room,
|
|
41
|
+
room_options=room_io.RoomOptions(
|
|
42
|
+
audio_input=room_io.AudioInputOptions(noise_cancellation=alkove.Focus()),
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- **`alkove.Focus()`** is a `rtc.FrameProcessor`. It keeps the closest voice, or with `voiceprint=` the voice of that print; `strength=` and `normalize=` work as in the web SDK. It works at the session's sample rate (24 kHz by default) and gives each frame back at the size it came in, 20 ms behind: whole 10 ms frames, as LiveKit's gain control needs. The models are read from the cache `download-files` filled when it is created.
|
|
48
|
+
- **`alkove.VAD.load()`** gives one speech probability every 30 ms, with `speech=alkove.SpeechParams(confidence=0.5, exit_confidence=0.1, start_secs=0.2, stop_secs=0.6)` by default. It counts as the turn detector's `min_silence_duration` (`stop_secs`). Behind `Focus`, it hears only the user.
|
|
49
|
+
|
|
50
|
+
The models are loaded once per process and shared by every session. Pass `models=alkove.Models.load(["focus", "cue"], dir=...)` to read them from your own folder.
|
|
51
|
+
|
|
52
|
+
Tested with livekit-agents 1.8.8.
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
Apache-2.0. See [NOTICE](./NOTICE).
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# livekit-plugins-alkove
|
|
2
|
+
|
|
3
|
+
[Alkove](https://alkove.ai) for [LiveKit Agents](https://docs.livekit.io/agents/): Focus keeps the user's voice and removes the others and the noise, Cue finds where they speak. Both run on the agent's CPU, with no server and no key, with the chain and the speech rules of Alkove's web SDK.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pip install livekit-plugins-alkove
|
|
9
|
+
python my_agent.py download-files # Focus and Cue into the cache, checked by their sha256
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Use
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from livekit.agents import AgentSession, room_io
|
|
16
|
+
from livekit.plugins import alkove
|
|
17
|
+
|
|
18
|
+
session = AgentSession(vad=alkove.VAD.load(), ...)
|
|
19
|
+
await session.start(
|
|
20
|
+
agent,
|
|
21
|
+
room=ctx.room,
|
|
22
|
+
room_options=room_io.RoomOptions(
|
|
23
|
+
audio_input=room_io.AudioInputOptions(noise_cancellation=alkove.Focus()),
|
|
24
|
+
),
|
|
25
|
+
)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- **`alkove.Focus()`** is a `rtc.FrameProcessor`. It keeps the closest voice, or with `voiceprint=` the voice of that print; `strength=` and `normalize=` work as in the web SDK. It works at the session's sample rate (24 kHz by default) and gives each frame back at the size it came in, 20 ms behind: whole 10 ms frames, as LiveKit's gain control needs. The models are read from the cache `download-files` filled when it is created.
|
|
29
|
+
- **`alkove.VAD.load()`** gives one speech probability every 30 ms, with `speech=alkove.SpeechParams(confidence=0.5, exit_confidence=0.1, start_secs=0.2, stop_secs=0.6)` by default. It counts as the turn detector's `min_silence_duration` (`stop_secs`). Behind `Focus`, it hears only the user.
|
|
30
|
+
|
|
31
|
+
The models are loaded once per process and shared by every session. Pass `models=alkove.Models.load(["focus", "cue"], dir=...)` to read them from your own folder.
|
|
32
|
+
|
|
33
|
+
Tested with livekit-agents 1.8.8.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
Apache-2.0. See [NOTICE](./NOTICE).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Alkove for LiveKit Agents: `Focus` as noise cancellation, `VAD` as the session's VAD, both on the agent's CPU."""
|
|
2
|
+
|
|
3
|
+
from alkove import Models, SpeechParams
|
|
4
|
+
from livekit.agents import Plugin
|
|
5
|
+
|
|
6
|
+
from .focus import Focus
|
|
7
|
+
from .log import logger
|
|
8
|
+
from .models import models
|
|
9
|
+
from .vad import VAD, VADStream
|
|
10
|
+
from .version import __version__
|
|
11
|
+
|
|
12
|
+
__all__ = ["Focus", "Models", "SpeechParams", "VAD", "VADStream", "models", "__version__"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AlkovePlugin(Plugin):
|
|
16
|
+
def __init__(self) -> None:
|
|
17
|
+
super().__init__(__name__, __version__, __package__, logger)
|
|
18
|
+
|
|
19
|
+
def download_files(self) -> None:
|
|
20
|
+
import alkove
|
|
21
|
+
|
|
22
|
+
alkove.fetch(["focus", "cue"])
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
Plugin.register_plugin(AlkovePlugin())
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from livekit import rtc
|
|
8
|
+
|
|
9
|
+
import alkove
|
|
10
|
+
|
|
11
|
+
from .models import models as shared_models
|
|
12
|
+
|
|
13
|
+
# LiveKit's gain control takes whole 10 ms frames: each frame goes out the size it came in, this far behind
|
|
14
|
+
LEAD = 0.02
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Focus(rtc.FrameProcessor[rtc.AudioFrame]):
|
|
18
|
+
"""Keeps the user's voice, the closest one or the voice of a print, and removes the others and the noise: pass it as `AudioInputOptions(noise_cancellation=Focus())`."""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
*,
|
|
23
|
+
models: Optional[alkove.Models] = None,
|
|
24
|
+
voiceprint: Any = None,
|
|
25
|
+
strength: float = 1.0,
|
|
26
|
+
normalize: bool = False,
|
|
27
|
+
) -> None:
|
|
28
|
+
# read now, off the frames' path: `download-files` filled the cache
|
|
29
|
+
self._models = models or shared_models(download=False)
|
|
30
|
+
self._voiceprint = voiceprint
|
|
31
|
+
self._strength = strength
|
|
32
|
+
self._normalize = normalize
|
|
33
|
+
self._enabled = True
|
|
34
|
+
self._live: Optional[alkove.Live] = None
|
|
35
|
+
self._rate = 0
|
|
36
|
+
self._held = np.zeros(0, dtype=np.float32)
|
|
37
|
+
self._track: Optional[str] = None
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def enabled(self) -> bool:
|
|
41
|
+
return self._enabled
|
|
42
|
+
|
|
43
|
+
@enabled.setter
|
|
44
|
+
def enabled(self, value: bool) -> None:
|
|
45
|
+
# LiveKit skips a disabled processor: it comes back on a fresh stream
|
|
46
|
+
if value and not self._enabled:
|
|
47
|
+
self._live = None
|
|
48
|
+
self._enabled = value
|
|
49
|
+
|
|
50
|
+
def set_voiceprint(self, embedding: Any) -> None:
|
|
51
|
+
"""Applied at the user's next silence; None goes back to the closest voice."""
|
|
52
|
+
self._voiceprint = embedding
|
|
53
|
+
if self._live is not None:
|
|
54
|
+
self._live.set_voiceprint(embedding)
|
|
55
|
+
|
|
56
|
+
def set_strength(self, s: float) -> None:
|
|
57
|
+
self._strength = s
|
|
58
|
+
if self._live is not None:
|
|
59
|
+
self._live.set_strength(s)
|
|
60
|
+
|
|
61
|
+
def _process(self, frame: rtc.AudioFrame) -> rtc.AudioFrame:
|
|
62
|
+
if self._live is None or frame.sample_rate != self._rate:
|
|
63
|
+
self._rate = frame.sample_rate
|
|
64
|
+
self._live = alkove.Live(self._models, self._rate, normalize=self._normalize, strength=self._strength, voiceprint=self._voiceprint)
|
|
65
|
+
self._held = np.zeros(math.ceil(self._rate * LEAD), dtype=np.float32)
|
|
66
|
+
ch = frame.num_channels
|
|
67
|
+
pcm = np.frombuffer(frame.data, dtype=np.int16)
|
|
68
|
+
mono = pcm if ch == 1 else pcm.reshape(-1, ch).astype(np.float64).mean(axis=1) / 32768
|
|
69
|
+
out, _ = self._live.push(mono)
|
|
70
|
+
held = np.concatenate((self._held, out))
|
|
71
|
+
n = frame.samples_per_channel
|
|
72
|
+
if len(held) < n:
|
|
73
|
+
held = np.concatenate((np.zeros(n - len(held), dtype=np.float32), held))
|
|
74
|
+
data = alkove.to_int16(held[:n])
|
|
75
|
+
self._held = held[n:]
|
|
76
|
+
if ch > 1:
|
|
77
|
+
data = np.repeat(data, ch)
|
|
78
|
+
return rtc.AudioFrame(data=data.tobytes(), sample_rate=self._rate, num_channels=ch, samples_per_channel=len(data) // ch)
|
|
79
|
+
|
|
80
|
+
# another track: the next frame starts a fresh stream
|
|
81
|
+
def _on_stream_info_updated(self, *, room_name: str, participant_identity: str, publication_sid: str) -> None:
|
|
82
|
+
if publication_sid != self._track:
|
|
83
|
+
self._track = publication_sid
|
|
84
|
+
self._live = None
|
|
85
|
+
|
|
86
|
+
def _on_stream_info_cleared(self) -> None:
|
|
87
|
+
self._track = None
|
|
88
|
+
self._live = None
|
|
89
|
+
|
|
90
|
+
def _close(self) -> None:
|
|
91
|
+
self._live = None
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import alkove
|
|
5
|
+
|
|
6
|
+
_lock = threading.Lock()
|
|
7
|
+
_models: Optional[alkove.Models] = None
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def models(*, download: bool = True) -> alkove.Models:
|
|
11
|
+
"""Focus and Cue, loaded once for the process and shared by every session: from the cache `download-files` filled, checked by their sha256; `download=False` on the event loop."""
|
|
12
|
+
global _models
|
|
13
|
+
with _lock:
|
|
14
|
+
if _models is None:
|
|
15
|
+
_models = alkove.Models.load(["focus", "cue"], download=download)
|
|
16
|
+
return _models
|
|
File without changes
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from livekit import agents, rtc
|
|
8
|
+
|
|
9
|
+
import alkove
|
|
10
|
+
|
|
11
|
+
from .models import models as shared_models
|
|
12
|
+
|
|
13
|
+
WINDOW = 0.03
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class VAD(agents.vad.VAD):
|
|
17
|
+
"""Alkove Cue as the session's VAD: a speech probability every 30 ms; speech starts and ends where the web SDK and `alkove.Processor` say so."""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
*,
|
|
22
|
+
models: Optional[alkove.Models] = None,
|
|
23
|
+
speech: Optional[alkove.SpeechParams] = None,
|
|
24
|
+
prefix_padding_duration: float = 0.5,
|
|
25
|
+
max_buffered_speech: float = 60.0,
|
|
26
|
+
) -> None:
|
|
27
|
+
super().__init__(capabilities=agents.vad.VADCapabilities(update_interval=WINDOW))
|
|
28
|
+
# read now, off the frames' path: `download-files` filled the cache
|
|
29
|
+
self._models = models or shared_models(download=False)
|
|
30
|
+
self.speech = speech or alkove.SpeechParams()
|
|
31
|
+
self.prefix_padding_duration = prefix_padding_duration
|
|
32
|
+
self.max_buffered_speech = max_buffered_speech
|
|
33
|
+
# read by the turn detector
|
|
34
|
+
self.min_silence_duration = self.speech.stop_secs
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def load(cls, **kwargs) -> "VAD":
|
|
38
|
+
"""For prewarm: the models are loaded, or downloaded, now (`models=` to give your own)."""
|
|
39
|
+
return cls(**{**kwargs, "models": kwargs.get("models") or shared_models()})
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def model(self) -> str:
|
|
43
|
+
return alkove.MANIFEST["models"]["cue"]["name"]
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def provider(self) -> str:
|
|
47
|
+
return "alkove"
|
|
48
|
+
|
|
49
|
+
def stream(self) -> "VADStream":
|
|
50
|
+
return VADStream(self)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class VADStream(agents.vad.VADStream):
|
|
54
|
+
def __init__(self, vad: VAD) -> None:
|
|
55
|
+
self._opts = vad
|
|
56
|
+
super().__init__(vad)
|
|
57
|
+
|
|
58
|
+
async def _main_task(self) -> None:
|
|
59
|
+
v = self._opts
|
|
60
|
+
rate = 0
|
|
61
|
+
processor: Optional[alkove.Processor] = None
|
|
62
|
+
tracker = alkove.SpeechTracker(v.speech, WINDOW)
|
|
63
|
+
# the input since `base`, at its own rate, mono int16
|
|
64
|
+
audio = np.zeros(0, dtype=np.int16)
|
|
65
|
+
base = 0
|
|
66
|
+
onset: Optional[float] = None
|
|
67
|
+
speech_acc = silence_acc = 0.0
|
|
68
|
+
speech_pub = silence_pub = 0.0
|
|
69
|
+
|
|
70
|
+
def reset() -> None:
|
|
71
|
+
nonlocal processor, tracker, audio, base, onset, speech_acc, silence_acc, speech_pub, silence_pub
|
|
72
|
+
processor = alkove.Processor(v._models, cue=True, speech=v.speech, trace=True, rate=rate) if rate else None
|
|
73
|
+
tracker = alkove.SpeechTracker(v.speech, WINDOW)
|
|
74
|
+
audio = np.zeros(0, dtype=np.int16)
|
|
75
|
+
base = 0
|
|
76
|
+
onset = None
|
|
77
|
+
speech_acc = silence_acc = speech_pub = silence_pub = 0.0
|
|
78
|
+
|
|
79
|
+
def at(t: float) -> int:
|
|
80
|
+
return round(t * rate)
|
|
81
|
+
|
|
82
|
+
def frame(start: float, end: float) -> rtc.AudioFrame:
|
|
83
|
+
data = audio[max(0, at(start) - base) : max(0, at(end) - base)]
|
|
84
|
+
return rtc.AudioFrame(data=data.tobytes(), sample_rate=rate, num_channels=1, samples_per_channel=len(data))
|
|
85
|
+
|
|
86
|
+
async for f in self._input_ch:
|
|
87
|
+
if isinstance(f, self._FlushSentinel):
|
|
88
|
+
reset()
|
|
89
|
+
continue
|
|
90
|
+
if not isinstance(f, rtc.AudioFrame):
|
|
91
|
+
continue
|
|
92
|
+
if f.sample_rate != rate:
|
|
93
|
+
rate = f.sample_rate
|
|
94
|
+
reset()
|
|
95
|
+
assert processor is not None
|
|
96
|
+
pcm = np.frombuffer(f.data, dtype=np.int16)
|
|
97
|
+
if f.num_channels > 1:
|
|
98
|
+
pcm = alkove.to_int16(pcm.reshape(-1, f.num_channels).astype(np.float64).mean(axis=1) / 32768)
|
|
99
|
+
audio = np.concatenate([audio, pcm])
|
|
100
|
+
t0 = time.perf_counter()
|
|
101
|
+
part = processor.push(pcm)
|
|
102
|
+
rows = part.trace["cue"] if part.trace else []
|
|
103
|
+
spent = (time.perf_counter() - t0) / max(1, len(rows))
|
|
104
|
+
for row in rows:
|
|
105
|
+
k = round(row["t"] / WINDOW)
|
|
106
|
+
p = row["speech"]
|
|
107
|
+
now = (k + 1) * WINDOW
|
|
108
|
+
speaking = tracker.confirmed
|
|
109
|
+
if speaking:
|
|
110
|
+
speech_pub += WINDOW
|
|
111
|
+
else:
|
|
112
|
+
silence_pub += WINDOW
|
|
113
|
+
self._event_ch.send_nowait(
|
|
114
|
+
agents.vad.VADEvent(
|
|
115
|
+
type=agents.vad.VADEventType.INFERENCE_DONE,
|
|
116
|
+
samples_index=(k + 1) * 480,
|
|
117
|
+
timestamp=now,
|
|
118
|
+
speech_duration=speech_pub,
|
|
119
|
+
silence_duration=silence_pub,
|
|
120
|
+
frames=[frame(k * WINDOW, now)],
|
|
121
|
+
probability=p,
|
|
122
|
+
inference_duration=spent,
|
|
123
|
+
speaking=speaking,
|
|
124
|
+
raw_accumulated_silence=silence_acc,
|
|
125
|
+
raw_accumulated_speech=speech_acc,
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
if p >= v.speech.confidence or (tracker.active and p >= v.speech.exit_confidence):
|
|
129
|
+
speech_acc += WINDOW
|
|
130
|
+
silence_acc = 0.0
|
|
131
|
+
else:
|
|
132
|
+
silence_acc += WINDOW
|
|
133
|
+
speech_acc = 0.0
|
|
134
|
+
e = tracker.push(p, k)
|
|
135
|
+
if e is not None and e["type"] == "speechstart":
|
|
136
|
+
onset = e["start"]
|
|
137
|
+
speech_pub, silence_pub = now - onset, 0.0
|
|
138
|
+
self._event_ch.send_nowait(
|
|
139
|
+
agents.vad.VADEvent(
|
|
140
|
+
type=agents.vad.VADEventType.START_OF_SPEECH,
|
|
141
|
+
samples_index=(k + 1) * 480,
|
|
142
|
+
timestamp=now,
|
|
143
|
+
speech_duration=speech_pub,
|
|
144
|
+
silence_duration=0.0,
|
|
145
|
+
frames=[frame(onset - v.prefix_padding_duration, now)],
|
|
146
|
+
speaking=True,
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
elif e is not None:
|
|
150
|
+
speech_pub, silence_pub = 0.0, now - e["end"]
|
|
151
|
+
self._event_ch.send_nowait(
|
|
152
|
+
agents.vad.VADEvent(
|
|
153
|
+
type=agents.vad.VADEventType.END_OF_SPEECH,
|
|
154
|
+
samples_index=(k + 1) * 480,
|
|
155
|
+
timestamp=now,
|
|
156
|
+
speech_duration=e["end"] - e["start"],
|
|
157
|
+
silence_duration=silence_pub,
|
|
158
|
+
frames=[frame(e["start"] - v.prefix_padding_duration, now)],
|
|
159
|
+
speaking=False,
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
onset = None
|
|
163
|
+
# keep the prefix while quiet, at most `max_buffered_speech` of speech
|
|
164
|
+
keep = v.prefix_padding_duration + 1 if onset is None else v.max_buffered_speech + v.prefix_padding_duration
|
|
165
|
+
drop = len(audio) - at(keep)
|
|
166
|
+
if drop > 0:
|
|
167
|
+
audio = audio[drop:]
|
|
168
|
+
base += drop
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.0"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "livekit-plugins-alkove"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Alkove for LiveKit Agents: Focus keeps the user's voice (noise cancellation), Cue finds speech (VAD), on the agent's CPU"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
authors = [{ name = "Eddy Bordi" }]
|
|
14
|
+
keywords = ["voice", "livekit", "noise-cancellation", "voice-isolation", "vad", "voice-agent", "on-device"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Topic :: Multimedia :: Sound/Audio :: Speech",
|
|
18
|
+
]
|
|
19
|
+
dependencies = ["alkove>=0.2,<0.3", "livekit-agents>=1.8.8,<2", "numpy>=1.21"]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://alkove.ai"
|
|
23
|
+
Repository = "https://github.com/alkove-ai/alkove"
|
|
24
|
+
|
|
25
|
+
[tool.hatch.version]
|
|
26
|
+
path = "livekit/plugins/alkove/version.py"
|
|
27
|
+
|
|
28
|
+
[tool.hatch.build.targets.wheel]
|
|
29
|
+
packages = ["livekit"]
|
|
30
|
+
|
|
31
|
+
[tool.hatch.build.targets.sdist]
|
|
32
|
+
include = ["/livekit"]
|