in Scripts/Runtime/Wit.cs [388:469]
private void OnMicSampleReady(int sampleCount, float[] sample, float levelMax)
{
if (null == TranscriptionProvider || !TranscriptionProvider.OverrideMicLevel)
{
OnMicLevelChanged(levelMax);
}
if (null != _micDataBuffer)
{
if (_isSoundWakeActive && levelMax > _runtimeConfiguration.soundWakeThreshold)
{
_lastSampleMarker = _micDataBuffer.CreateMarker(
(int) (-_runtimeConfiguration.micBufferLengthInSeconds * 1000 *
_runtimeConfiguration.sampleLengthInMs));
}
byte[] data = Convert(sample);
_micDataBuffer.Push(data, 0, data.Length);
if (data.Length > 0)
{
events.OnByteDataReady?.Invoke(data, 0, data.Length);
for(int i = 0; null != _dataReadyHandlers && i < _dataReadyHandlers.Length; i++)
{
_dataReadyHandlers[i].OnWitDataReady(data, 0, data.Length);
}
}
#if DEBUG_SAMPLE
sampleFile.Write(data, 0, data.Length);
#endif
}
if (IsRequestActive && _recordingRequest.IsRequestStreamActive)
{
if (null != _micDataBuffer && _micDataBuffer.Capacity > 0)
{
if (null == _writeBuffer)
{
_writeBuffer = new byte[sample.Length * 2];
}
// Flush the marker buffer to catch up
int read;
while ((read = _lastSampleMarker.Read(_writeBuffer, 0, _writeBuffer.Length, true)) > 0)
{
_recordingRequest.Write(_writeBuffer, 0, read);
events.OnByteDataSent?.Invoke(_writeBuffer, 0, read);
for (int i = 0; null != _dataSentHandlers && i < _dataSentHandlers.Length; i++)
{
_dataSentHandlers[i].OnWitDataSent(_writeBuffer, 0, read);
}
}
}
else
{
byte[] sampleBytes = Convert(sample);
_recordingRequest.Write(sampleBytes, 0, sampleBytes.Length);
}
if (_receivedTranscription)
{
if (Time.time - _lastWordTime >
_runtimeConfiguration.minTranscriptionKeepAliveTimeInSeconds)
{
Debug.Log("Deactivated due to inactivity. No new words detected.");
DeactivateRequest(events.OnStoppedListeningDueToInactivity);
}
}
else if (Time.time - _lastMinVolumeLevelTime >
_runtimeConfiguration.minKeepAliveTimeInSeconds)
{
Debug.Log("Deactivated input due to inactivity.");
DeactivateRequest(events.OnStoppedListeningDueToInactivity);
}
}
else if (_isSoundWakeActive && levelMax > _runtimeConfiguration.soundWakeThreshold)
{
events.OnMinimumWakeThresholdHit?.Invoke();
_isSoundWakeActive = false;
ActivateImmediately(_currentRequestOptions);
}
}