Skip to content

Commit 3ca476e

Browse files
authored
Merge pull request #722 from dfengpo/master
修复C# CalculateProb方法计算句子EndOffset的bug
2 parents 7de4629 + 7b0aaa1 commit 3ca476e

File tree

4 files changed

+201
-179
lines changed

4 files changed

+201
-179
lines changed

examples/csharp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void Main(string[] args)
2121
MIN_SPEECH_DURATION_MS, MAX_SPEECH_DURATION_SECONDS, MIN_SILENCE_DURATION_MS, SPEECH_PAD_MS);
2222
List<SileroSpeechSegment> speechTimeList = vadDetector.GetSpeechSegmentList(new FileInfo(EXAMPLE_WAV_FILE));
2323
//Console.WriteLine(speechTimeList.ToJson());
24-
StringBuilder sb = new StringBuilder();
24+
StringBuilder sb = new();
2525
foreach (var speechSegment in speechTimeList)
2626
{
2727
sb.Append($"start second: {speechSegment.StartSecond}, end second: {speechSegment.EndSecond}\n");

examples/csharp/SileroVadDetector.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,26 @@ public List<SileroSpeechSegment> GetSpeechSegmentList(FileInfo wavFile)
5353
{
5454
Reset();
5555

56-
using (var audioFile = new AudioFileReader(wavFile.FullName))
57-
{
58-
List<float> speechProbList = new List<float>();
59-
this._audioLengthSamples = (int)(audioFile.Length / 2);
60-
float[] buffer = new float[this._windowSizeSample];
61-
62-
while (audioFile.Read(buffer, 0, buffer.Length) > 0)
63-
{
64-
float speechProb = _model.Call(new[] { buffer }, _samplingRate)[0];
65-
speechProbList.Add(speechProb);
66-
}
56+
using var audioFile = new AudioFileReader(wavFile.FullName);
57+
List<float> speechProbList = [];
58+
this._audioLengthSamples = (int)(audioFile.Length / 2);
59+
float[] buffer = new float[this._windowSizeSample];
6760

68-
return CalculateProb(speechProbList);
61+
while (audioFile.Read(buffer, 0, buffer.Length) > 0)
62+
{
63+
float speechProb = _model.Call([buffer], _samplingRate)[0];
64+
speechProbList.Add(speechProb);
6965
}
66+
67+
return CalculateProb(speechProbList);
7068
}
7169

7270
private List<SileroSpeechSegment> CalculateProb(List<float> speechProbList)
7371
{
74-
List<SileroSpeechSegment> result = new List<SileroSpeechSegment>();
72+
List<SileroSpeechSegment> result = [];
7573
bool triggered = false;
7674
int tempEnd = 0, prevEnd = 0, nextStart = 0;
77-
SileroSpeechSegment segment = new SileroSpeechSegment();
75+
SileroSpeechSegment segment = new();
7876

7977
for (int i = 0; i < speechProbList.Count; i++)
8078
{
@@ -164,7 +162,8 @@ private List<SileroSpeechSegment> CalculateProb(List<float> speechProbList)
164162

165163
if (segment.StartOffset != null && (_audioLengthSamples - segment.StartOffset) > _minSpeechSamples)
166164
{
167-
segment.EndOffset = _audioLengthSamples;
165+
//segment.EndOffset = _audioLengthSamples;
166+
segment.EndOffset = speechProbList.Count * _windowSizeSample;
168167
result.Add(segment);
169168
}
170169

@@ -182,7 +181,7 @@ private List<SileroSpeechSegment> CalculateProb(List<float> speechProbList)
182181
int silenceDuration = nextItem.StartOffset.Value - item.EndOffset.Value;
183182
if (silenceDuration < 2 * _speechPadSamples)
184183
{
185-
item.EndOffset = item.EndOffset + (silenceDuration / 2);
184+
item.EndOffset += (silenceDuration / 2);
186185
nextItem.StartOffset = Math.Max(0, nextItem.StartOffset.Value - (silenceDuration / 2));
187186
}
188187
else
@@ -200,9 +199,9 @@ private List<SileroSpeechSegment> CalculateProb(List<float> speechProbList)
200199
return MergeListAndCalculateSecond(result, _samplingRate);
201200
}
202201

203-
private List<SileroSpeechSegment> MergeListAndCalculateSecond(List<SileroSpeechSegment> original, int samplingRate)
202+
private static List<SileroSpeechSegment> MergeListAndCalculateSecond(List<SileroSpeechSegment> original, int samplingRate)
204203
{
205-
List<SileroSpeechSegment> result = new List<SileroSpeechSegment>();
204+
List<SileroSpeechSegment> result = [];
206205
if (original == null || original.Count == 0)
207206
{
208207
return result;
@@ -216,7 +215,10 @@ private List<SileroSpeechSegment> MergeListAndCalculateSecond(List<SileroSpeechS
216215
for (int i = 1; i < original.Count; i++)
217216
{
218217
SileroSpeechSegment segment = original[i];
218+
if (i == 235)
219+
{
219220

221+
}
220222
if (segment.StartOffset > right)
221223
{
222224
result.Add(new SileroSpeechSegment(left, right,
@@ -242,7 +244,7 @@ private List<SileroSpeechSegment> MergeListAndCalculateSecond(List<SileroSpeechS
242244
return result;
243245
}
244246

245-
private float CalculateSecondByOffset(int offset, int samplingRate)
247+
private static float CalculateSecondByOffset(int offset, int samplingRate)
246248
{
247249
float secondValue = offset * 1.0f / samplingRate;
248250
return (float)Math.Floor(secondValue * 1000.0f) / 1000.0f;

0 commit comments

Comments
 (0)