Skip to content

Commit

Permalink
fix: fix high cpu occupy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxlpzqc committed Jun 21, 2023
1 parent e837f70 commit 7b0b5da
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions TMSpeech.GUI/SpeechCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TMSpeech.GUI
Expand Down Expand Up @@ -88,50 +89,41 @@ public void Init(string encoder, string decoder, string joiner, string tokens, s

while (!disposed)
{
try
while (recognizer.IsReady(s))
{
if (recognizer.IsReady(s))
recognizer.Decode(s);
var is_endpoint = recognizer.IsEndpoint(s);
var text = recognizer.GetResult(s).Text;
recognizer.Decode(s);
}
var is_endpoint = recognizer.IsEndpoint(s);
var text = recognizer.GetResult(s).Text;

if (!string.IsNullOrEmpty(text))
if (!string.IsNullOrEmpty(text))
{
var item = new TextInfo(text);
TextChanged?.Invoke(this, new SpeechEventArgs()
{
var item = new TextInfo(text);
TextChanged?.Invoke(this, new SpeechEventArgs()
{
Text = item,
});
CurrentText = text;
Text = item,
});
CurrentText = text;

if (is_endpoint || text.Length >= 80)
if (is_endpoint || text.Length >= 80)
{
AllText.Add(item);
if (!string.IsNullOrEmpty(savefile))
{
AllText.Add(item);
if (!string.IsNullOrEmpty(savefile))
try
{
try
{
File.AppendAllText(savefile, string.Format("{0:T}: {1}\n", item.Time, item.Text));
}
catch { }
}

recognizer.Reset(s);
UpdateList?.Invoke(this, EventArgs.Empty);
File.AppendAllText(savefile, string.Format("{0:T}: {1}\n", item.Time, item.Text));
}
catch { }
}

recognizer.Reset(s);
UpdateList?.Invoke(this, EventArgs.Empty);
}
}
catch
{
break;
}
Thread.Sleep(20);
}
}

private bool disposed = false;

public void Dispose()
{
if (capture != null)
{
capture.StopRecording();
Expand All @@ -141,7 +133,12 @@ public void Dispose()
recognizer.Dispose();
capture = null;
recognizer = null;
}

private bool disposed = false;

public void Dispose()
{
disposed = true;
}
}
Expand Down

0 comments on commit 7b0b5da

Please sign in to comment.