From f133c82c859163f13cd31980e350fbbe06e07217 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Fri, 11 Oct 2024 08:41:55 +0100 Subject: [PATCH 1/2] Refactor VoskRecognizer usage to ensure proper disposal Encapsulate VoskRecognizer instantiation within a using statement to guarantee resource disposal. This change improves memory management and aligns with best practices for resource handling. Also, maintain the existing logic structure for audio file processing. Signed-off-by: Ivandro Jao --- src/ui/Forms/AudioToText/VoskDictate.cs | 62 +++++++++++++------------ 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/ui/Forms/AudioToText/VoskDictate.cs b/src/ui/Forms/AudioToText/VoskDictate.cs index 4558b7200..0aa4ddb38 100644 --- a/src/ui/Forms/AudioToText/VoskDictate.cs +++ b/src/ui/Forms/AudioToText/VoskDictate.cs @@ -69,44 +69,46 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText _model = new Model(modelFileName); } - var rec = new VoskRecognizer(_model, 16000.0f); - rec.SetMaxAlternatives(0); - rec.SetWords(true); - var list = new List(); - var buffer = new byte[4096]; - using (var source = File.OpenRead(WaveFileName)) + using (var rec = new VoskRecognizer(_model, 16000.0f)) { - int bytesRead; - while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) + rec.SetMaxAlternatives(0); + rec.SetWords(true); + var list = new List(); + var buffer = new byte[4096]; + using (var source = File.OpenRead(WaveFileName)) { - if (rec.AcceptWaveform(buffer, bytesRead)) + int bytesRead; + while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { - var res = rec.Result(); - var results = VoskAudioToText.ParseJsonToResult(res); - list.AddRange(results); - } - else - { - var res = rec.PartialResult(); + if (rec.AcceptWaveform(buffer, bytesRead)) + { + var res = rec.Result(); + var results = VoskAudioToText.ParseJsonToResult(res); + list.AddRange(results); + } + else + { + var res = rec.PartialResult(); + } } } - } - var finalResult = rec.FinalResult(); - var finalResults = VoskAudioToText.ParseJsonToResult(finalResult); - list.AddRange(finalResults); + var finalResult = rec.FinalResult(); + var finalResults = VoskAudioToText.ParseJsonToResult(finalResult); + list.AddRange(finalResults); - try - { - File.Delete(WaveFileName); - WaveFileName = null; - } - catch - { - // ignore - } + try + { + File.Delete(WaveFileName); + WaveFileName = null; + } + catch + { + // ignore + } - return ResultListToText(list); + return ResultListToText(list); + } } private static string ResultListToText(List list) From 0b257818fa6ecdffd6b3c07e47595fe3e14f6a10 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Fri, 11 Oct 2024 08:42:25 +0100 Subject: [PATCH 2/2] Refactor: Discard unused variable in VoskDictate.cs Replaced the unused variable 'res' with a discard operator in the PartialResult call to enhance code clarity and maintainability by explicitly indicating the result is not used. Signed-off-by: Ivandro Jao --- src/ui/Forms/AudioToText/VoskDictate.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/Forms/AudioToText/VoskDictate.cs b/src/ui/Forms/AudioToText/VoskDictate.cs index 0aa4ddb38..fc387dc95 100644 --- a/src/ui/Forms/AudioToText/VoskDictate.cs +++ b/src/ui/Forms/AudioToText/VoskDictate.cs @@ -88,7 +88,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText } else { - var res = rec.PartialResult(); + _ = rec.PartialResult(); } } }