Skip to content

Commit

Permalink
Fix: UI freeze when switching mic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyfish committed Jun 19, 2024
1 parent 6bf4e05 commit 208b7f3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
28 changes: 15 additions & 13 deletions MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AutoHotkey.Interop;
using System.Diagnostics;
using System.Globalization;
using AutoHotkey.Interop;
using EdgeTTS;
using NAudio.Wave;
using System.Diagnostics;
using System.Globalization;

namespace HellDivers2OneKeyStratagem;

Expand All @@ -18,7 +18,7 @@ public MainForm()
private void SetTooltipFont()
{
toolTip.OwnerDraw = true;
toolTip.Popup += (sender, args) =>
toolTip.Popup += (_, args) =>
{
var text = toolTip.GetToolTip(args.AssociatedControl);
args.ToolTipSize = TextRenderer.MeasureText(text, Font);
Expand Down Expand Up @@ -113,9 +113,7 @@ private async Task LoadByLanguage()

private async Task ResetVoiceCommand()
{
_voiceCommand?.Stop();
_voiceCommand?.Dispose();
_voiceCommand = null;
await StopVoiceTrigger();

if (Settings.EnableSpeechTrigger)
await StartSpeechTrigger();
Expand Down Expand Up @@ -203,10 +201,14 @@ private void PlayStratagemVoice(string stratagemName)
PlayVoice(Path.Combine(VoiceRootPath, Settings.Language, Settings.VoiceName, stratagemName + ".mp3"));
}

private void StopVoiceTrigger()
private async Task StopVoiceTrigger()
{
_voiceCommand?.Stop();
_voiceCommand = null;
if (_voiceCommand != null)
{
await _voiceCommand.Stop();
_voiceCommand.Dispose();
_voiceCommand = null;
}
}

private void LoadVoiceNames()
Expand Down Expand Up @@ -848,7 +850,7 @@ private async void enableSpeechTriggerCheckBox_Click(object sender, EventArgs e)
if (Settings.EnableSpeechTrigger)
await StartSpeechTrigger();
else
StopVoiceTrigger();
await StopVoiceTrigger();
}

speechSubSettingsFlowLayoutPanel.Visible = Settings.EnableSpeechTrigger;
Expand All @@ -857,12 +859,12 @@ private async void enableSpeechTriggerCheckBox_Click(object sender, EventArgs e)

private bool _isClosing;

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
private async void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_isClosing = true;

StopAutoHotkeyScript();
StopVoiceTrigger();
await StopVoiceTrigger();
}

private void enableSetFKeyBySpeechCheckBox_Click(object sender, EventArgs e)
Expand Down
66 changes: 39 additions & 27 deletions Tools/VoiceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,27 @@ public void Start()
_isRecognizing = true;
}

public void Stop()
public async Task Stop()
{
_recognizer.RecognizeAsyncStop();
if (!_isRecognizing)
return;

_recognizer.RecognizeAsyncCancel();

while (_isRecognizing)
await Task.Delay(100);
}

public void Dispose()
{
if (_isRecognizing)
_recognizer.RecognizeAsyncCancel();
_recognizer.Dispose();

_waveInEvent?.StopRecording();
_waveInEvent?.Dispose();
_audioStreamer?.Dispose();

GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -198,30 +211,33 @@ private static WaveFormat GetWaveFormat(SupportedWaveFormat eFormat, out int sam
return new WaveFormat(samplesRate, bitsPerSample, nChannel);
}

public async Task UseMic(int deviceIndex)
public async Task<bool> UseMic(int deviceIndex)
{
//settings
var device = WaveInEvent.GetCapabilities(deviceIndex);
SupportedWaveFormat supportFormat = 0;
foreach (SupportedWaveFormat format in Enum.GetValues(typeof(SupportedWaveFormat)))
if (device.SupportsWaveFormat(format) && format <= SupportedWaveFormat.WAVE_FORMAT_48S16 && (int)Math.Log2((int)format) % 2 == 0) //找个单通道的
supportFormat = format; //取最后一个支持的。

if (supportFormat == 0)
return false;

var isRecognizingBeforeChangingMic = _isRecognizing;
if (_isRecognizing)
await Stop();

//cleanup
if (_waveInEvent != null)
{
_waveInEvent.StopRecording();
_waveInEvent.Dispose();
}
_waveInEvent?.Dispose();

if (_audioStreamer != null)
{
_audioStreamer.Close();
await _audioStreamer.DisposeAsync();
}

//settings
_waveInEvent = new WaveInEvent();
_waveInEvent.DeviceNumber = deviceIndex; //device.ID;
var device = WaveInEvent.GetCapabilities(deviceIndex);
SupportedWaveFormat supportFormat = 0;
foreach (SupportedWaveFormat format in Enum.GetValues(typeof(SupportedWaveFormat)))
if (device.SupportsWaveFormat(format) && format <= SupportedWaveFormat.WAVE_FORMAT_48S16 && (int)Math.Log2((int)format) % 2 == 0) //找个单通道的
supportFormat = format; //取最后一个支持的。

_waveInEvent.WaveFormat = GetWaveFormat(
supportFormat, out var samplesRate, out var nChannel, out _,
out var eBitsPerSample, out var eChannel);
Expand All @@ -235,31 +251,27 @@ public async Task UseMic(int deviceIndex)

var audioFormat = new SpeechAudioFormatInfo(nChannel * samplesRate, eBitsPerSample, eChannel);

//start record microphone
_waveInEvent.StartRecording();
_recognizer.SetInputToAudioStream(_audioStreamer, audioFormat);

//restart _recognizer
if (_isRecognizing)
{
Stop();
while (_isRecognizing)
await Task.Delay(100);

_recognizer.SetInputToAudioStream(_audioStreamer, audioFormat);
if (isRecognizingBeforeChangingMic)
Start();
}

return true;
}

public async Task UseMic(string deviceName)
public async Task<bool> UseMic(string deviceName)
{
for (var i = 0; i < WaveInEvent.DeviceCount; i++)
{
var device = WaveInEvent.GetCapabilities(i);
if (device.ProductName != deviceName)
continue;

await UseMic(i);
break;
return await UseMic(i);
}

return false;
}
}

0 comments on commit 208b7f3

Please sign in to comment.