Skip to content

Commit f449363

Browse files
committed
feat: 双端新增多语种语音识别模型选择、切换与删除管理
1 parent 979c009 commit f449363

25 files changed

Lines changed: 1430 additions & 125 deletions

‎src/HuiYi.Android/Core/AndroidAppModel.cs‎

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,17 @@ public static IReadOnlyList<string> SystemLanguages()
143143
return [CultureInfo.CurrentUICulture.Name];
144144
}
145145

146+
public AsrModel ActiveAsrModel => AsrModels.Parse(Settings.AsrModel);
147+
public bool ModelsReady => Models.IsReady(ActiveAsrModel);
148+
146149
/// Ensures the speech recognizer and meeting controller are loaded; returns false when models are missing or fail to load.
147150
public async Task<bool> EnsureEnginesAsync()
148151
{
149152
Task<bool> task;
150153
lock (engineGate)
151154
{
152155
if (Meeting != null) return true;
153-
if (!Models.Ready) return false;
156+
if (!ModelsReady) return false;
154157
if (loading == null)
155158
{
156159
isLoading = true;
@@ -190,11 +193,16 @@ private bool LoadCore()
190193
{
191194
var watch = System.Diagnostics.Stopwatch.StartNew();
192195
var threads = Math.Clamp(System.Environment.ProcessorCount / 2, 2, 4);
193-
rec = new Recognizer(Models, threads: threads)
196+
var asrModel = ActiveAsrModel;
197+
var asrLang = Settings.AsrLanguage;
198+
rec = new Recognizer(Models, threads: threads, model: asrModel, language: asrLang)
194199
{
195200
Fix = text => Settings.GlossaryOn ? Glossary.FixWithDetails(text) : (text, [])
196201
};
197-
listen = new ListenEngine(rec, Models, TimeSpan.FromMilliseconds(600));
202+
// Whisper decodes autoregressively over a 30 s window: disable live partials on mobile so
203+
// slow decoders never spiral into latency backlog; SenseVoice keeps 600 ms live drafts.
204+
var partialInterval = AsrModels.IsWhisper(asrModel) ? TimeSpan.Zero : TimeSpan.FromMilliseconds(600);
205+
listen = new ListenEngine(rec, Models, partialInterval);
198206
// Voiceprints load lazily on first use; without the model, lines stay unlabelled.
199207
Voiceprints? Prints()
200208
{
@@ -256,7 +264,7 @@ private bool LoadCore()
256264
listenEngine = listen;
257265
Meeting = meeting;
258266
}
259-
Log.Debug($"app: recognizer loaded in {watch.ElapsedMilliseconds} ms (threads={threads})");
267+
Log.Debug($"app: recognizer ({AsrModels.IdOf(asrModel)}, lang={asrLang}) loaded in {watch.ElapsedMilliseconds} ms (threads={threads})");
260268
return true;
261269
}
262270
catch
@@ -271,6 +279,61 @@ private bool LoadCore()
271279
}
272280
}
273281

282+
/// Switches the active ASR model (and/or Whisper language hint) when idle, unloading old engines.
283+
public async Task<bool> SwitchAsrModelAsync(AsrModel nextModel, string? language = null)
284+
{
285+
if (IsListening) return false;
286+
Settings.AsrModel = AsrModels.IdOf(nextModel);
287+
if (language != null) Settings.AsrLanguage = language;
288+
Settings.Save();
289+
Task<bool>? pending;
290+
lock (engineGate) pending = loading;
291+
if (pending != null)
292+
{
293+
try { await pending; } catch { }
294+
}
295+
DisposeCachedEngines();
296+
Ui(() => StateChanged?.Invoke());
297+
if (Models.IsReady(nextModel))
298+
{
299+
var ok = await EnsureEnginesAsync();
300+
Ui(() => StateChanged?.Invoke());
301+
return ok;
302+
}
303+
return true;
304+
}
305+
306+
private void DisposeCachedEngines()
307+
{
308+
Recognizer? rec;
309+
Voiceprints? prints;
310+
ListenEngine? listen;
311+
lock (engineGate)
312+
{
313+
rec = Recognizer;
314+
prints = voiceprints;
315+
listen = listenEngine;
316+
Recognizer = null;
317+
voiceprints = null;
318+
printsFailed = false;
319+
listenEngine = null;
320+
Meeting = null;
321+
loading = null;
322+
}
323+
try { listen?.Dispose(); } catch { }
324+
try { prints?.Dispose(); } catch { }
325+
try { rec?.Dispose(); } catch { }
326+
}
327+
328+
/// Deletes a non-active ASR model's files to free device storage.
329+
public bool DeleteAsrModel(AsrModel target)
330+
{
331+
if (IsListening || target == ActiveAsrModel) return false;
332+
Models.DeleteAsr(target);
333+
Ui(() => StateChanged?.Invoke());
334+
return true;
335+
}
336+
274337
public Task StartMeeting(Context context)
275338
{
276339
ArgumentNullException.ThrowIfNull(context);

‎src/HuiYi.Android/Resources/layout/activity_setup.xml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,21 @@
201201
android:layout_height="wrap_content"
202202
android:layout_marginTop="8dp"
203203
android:lineSpacingMultiplier="1.35"
204-
android:text="包含 SenseVoice 语音识别、Silero VAD 语音检测与 3D-Speaker 声纹模型,全部校验 SHA-256。"
204+
android:text="包含语音识别、Silero VAD 语音检测与 3D-Speaker 声纹模型,全部校验 SHA-256。"
205205
android:textColor="@color/huiyi_muted"
206206
android:textSize="14sp" />
207207

208+
<LinearLayout
209+
android:id="@+id/setupModelChoices"
210+
android:layout_width="match_parent"
211+
android:layout_height="wrap_content"
212+
android:layout_marginTop="14dp"
213+
android:orientation="vertical" />
214+
208215
<com.google.android.material.card.MaterialCardView
209216
android:layout_width="match_parent"
210217
android:layout_height="wrap_content"
211-
android:layout_marginTop="20dp"
218+
android:layout_marginTop="10dp"
212219
app:cardBackgroundColor="@color/huiyi_surface"
213220
app:cardCornerRadius="14dp"
214221
app:cardElevation="0dp"

‎src/HuiYi.Android/UI/LiveFragment.cs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using HuiYi.App.Core;
2323
using HuiYi.App.Platform;
2424
using HuiYi.Localization;
25+
using HuiYi.Speech;
2526
using Fragment = AndroidX.Fragment.App.Fragment;
2627
using Uri = Android.Net.Uri;
2728

@@ -519,7 +520,9 @@ private void RenderReadyState()
519520
if (readyPostProcessText != null)
520521
readyPostProcessText.Text = L.T("正在校对发言人与生成纪要…");
521522

522-
var ready = Model.Models.Ready;
523+
var active = Model.ActiveAsrModel;
524+
var activeInfo = AsrModels.InfoOf(active);
525+
var ready = Model.ModelsReady;
523526
if (readyModelDot != null)
524527
readyModelDot.Background = MakeCircle(ready ? ColorOnAir : ColorWarn);
525528
if (readyModelStatus != null)
@@ -530,8 +533,8 @@ private void RenderReadyState()
530533
if (readyModelDetail != null)
531534
{
532535
readyModelDetail.Text = ready
533-
? L.T("识别模型已就绪:中文、英语、日语、韩语、粤语,以及发言人区分")
534-
: L.F("约 {0:0} MB · 只需下载一次", Model.Models.TotalBytes / 1_000_000.0);
536+
? L.F("当前模型:{0}", L.T(activeInfo.NameKey))
537+
: L.F("约 {0:0} MB · 可随时在设置中切换或增删模型", Model.Models.TotalBytesFor(active) / 1_000_000.0);
535538
}
536539
if (readyModelActionBtn != null)
537540
{
@@ -607,7 +610,7 @@ private void OnStartClicked()
607610
{
608611
if (Model.IsListening || startingMeeting || !IsAdded) return;
609612

610-
if (!Model.Models.Ready)
613+
if (!Model.ModelsReady)
611614
{
612615
Model.Say(L.T("请先完成识别模型下载"));
613616
(Activity as MainActivity)?.OpenSetup();

‎src/HuiYi.Android/UI/RecordsFragment.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ private bool CheckCanImportMedia()
10851085
ShowToast(L.T("当前有后台任务正在处理,请稍后再试"));
10861086
return false;
10871087
}
1088-
if (!Model.Models.Ready)
1088+
if (!Model.ModelsReady)
10891089
{
10901090
ShowToast(L.T("请先完成识别模型下载"));
10911091
if (Activity is MainActivity main) main.OpenSetup();

0 commit comments

Comments
 (0)