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 <Ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-10-11 08:41:55 +01:00
parent 172b47cdcb
commit f133c82c85

View File

@ -69,7 +69,8 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
_model = new Model(modelFileName); _model = new Model(modelFileName);
} }
var rec = new VoskRecognizer(_model, 16000.0f); using (var rec = new VoskRecognizer(_model, 16000.0f))
{
rec.SetMaxAlternatives(0); rec.SetMaxAlternatives(0);
rec.SetWords(true); rec.SetWords(true);
var list = new List<ResultText>(); var list = new List<ResultText>();
@ -108,6 +109,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
return ResultListToText(list); return ResultListToText(list);
} }
}
private static string ResultListToText(List<ResultText> list) private static string ResultListToText(List<ResultText> list)
{ {