Skip to content

Fix detect silence to work with smaller audio chunks #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Examples/ServiceExamples/Scripts/ExampleStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public bool Active
{
if (value && !_speechToText.IsListening)
{
_speechToText.DetectSilence = false;
_speechToText.DetectSilence = true;
_speechToText.EnableWordConfidence = true;
_speechToText.EnableTimestamps = true;
_speechToText.SilenceThreshold = 0.1f;
_speechToText.SilenceThreshold = 0.01f;
_speechToText.MaxAlternatives = 0;
_speechToText.EnableInterimResults = true;
_speechToText.OnError = OnError;
Expand Down Expand Up @@ -177,7 +177,7 @@ private IEnumerator RecordingHandler()

// Create AudioData and use the samples we just created
AudioData record = new AudioData();
record.MaxLevel = Mathf.Max(samples);
record.MaxLevel = Mathf.Max(Mathf.Abs(Mathf.Min(samples)), Mathf.Max(samples));
record.Clip = AudioClip.Create("Recording", chunkSize, _recording.channels, _recordingHZ, false);
record.Clip.SetData(samples, 0);

Expand Down
14 changes: 13 additions & 1 deletion Scripts/Services/SpeechToText/v1/SpeechToText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public class SpeechToText : IWatsonService
private string _acoustic_customization_id = null;
private float _customization_weight = 0.3f;
private bool _streamMultipart = false; // If true sets `Transfer-Encoding` header of multipart request to `chunked`.
private float _silenceDuration = 0.0f;
private float _silenceCutoff = 1.0f;

private fsSerializer _serializer = new fsSerializer();
private Credentials _credentials = null;
Expand Down Expand Up @@ -465,7 +467,17 @@ public void OnListen(AudioData clip)
SendStart();
}

if (!DetectSilence || clip.MaxLevel >= _silenceThreshold)
// If silence persists for _silenceCutoff seconds, send stop and discard clips until audio resumes
if (DetectSilence && clip.MaxLevel < _silenceThreshold)
{
_silenceDuration += clip.Clip.length;
}
else
{
_silenceDuration = 0.0f;
}

if (!DetectSilence || _silenceDuration < _silenceCutoff)
{
if (_listenActive)
{
Expand Down