Skip to content

Commit

Permalink
Merge pull request vipenti#5 from KronosPNG/Fix_silence_threshold
Browse files Browse the repository at this point in the history
I overengineered it as always, REALLY easy fix
  • Loading branch information
KronosPNG authored Aug 18, 2024
2 parents 69f1e25 + 0c9ee55 commit 04f04e8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
1 change: 1 addition & 0 deletions Mevaterse_Classroom_2/Assets/Resources/Student.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5a6c19b1f5ab657429832cc2a8fe01c0, type: 3}
m_Name:
m_EditorClassIdentifier:
question: {fileID: 0}
--- !u!82 &-4635132013586665566
AudioSource:
m_ObjectHideFlags: 0
Expand Down
44 changes: 44 additions & 0 deletions Mevaterse_Classroom_2/Assets/Scenes/ClassroomScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -7482,6 +7482,50 @@ Light:
m_UseViewFrustumForShadowCasterCull: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!1 &616283177
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 616283179}
- component: {fileID: 616283178}
m_Layer: 0
m_Name: StudentHandler
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &616283178
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 616283177}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 762677b1cb063d440b05bd390903ec84, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &616283179
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 616283177}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.3524388, y: -3.3399148, z: -16.541456}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 18
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!54 &623062995
Rigidbody:
m_ObjectHideFlags: 0
Expand Down
60 changes: 10 additions & 50 deletions Mevaterse_Classroom_2/Assets/Scripts/PlayerVoiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ public class PlayerVoiceController : MonoBehaviourPunCallbacks

private QuestionDispatcher questionDispatcher;

private Timer leniencyTimer, // Timer for alllowing brief pauses of "leniencyPeriod" in speech
recordingTimer; // Timer stopping recording after "maxRecordingTime" seconds
private const int leniencyPeriod = 3; // time for leniency in seconds
private const float leniencyPeriod = 3.0f; // time for leniency in seconds
private const int maxRecordingTime = 40; // time for max recording in seconds
private const int minRecordingTime = 3; // time for min recording in seconds
private float silenceTimer = 0.0f; // time of silence in seconds

private void Start()
{
Expand Down Expand Up @@ -93,7 +92,7 @@ public void Update()
{
isTimerActivable = true;

SuspendTimer(leniencyTimer);
silenceTimer = 0.0f; // reset the silence timer
}

// If the microphone is not recording start recording
Expand All @@ -103,8 +102,7 @@ public void Update()
isTimerActivable = true;

Debug.Log("Starting recording");
outputSource.clip = Microphone.Start(null, false, maxRecordingTime + 1, 44100);
StartOrResetTimer(recordingTimer, (maxRecordingTime) * 1000, SetStopRecordingFlag); //after maxRecordingTime seconds stop recording
outputSource.clip = Microphone.Start(null, false, maxRecordingTime, 44100);
}
}

Expand All @@ -115,18 +113,16 @@ public void Update()
info.text = "";

// If the microphone is recording and there is a pause in speech start the leniency timer
if(Microphone.IsRecording(null) && isTimerActivable){
if(Microphone.IsRecording(null)){
isTimerActivable = false;
Debug.Log("Starting leniency timer");

StartOrResetTimer(leniencyTimer, (leniencyPeriod) * 1000, SetStopRecordingFlag); // after leniencyPeriod seconds of silence stop recording
silenceTimer += Time.deltaTime;
}
}

// If the stopRecordingFlag is set to true stop recording
if(stopRecordingFlag){
stopRecordingFlag = false;
if(silenceTimer >= leniencyPeriod){
StopRecording();
silenceTimer = 0.0f;
}
}

Expand Down Expand Up @@ -165,8 +161,8 @@ private void StopRecording(){
Debug.Log("Stopping recording");

// Suspend the timers to prevent them from stopping the already stopped recording
SuspendTimer(recordingTimer);
SuspendTimer(leniencyTimer);
// SuspendTimer(recordingTimer);
// SuspendTimer(leniencyTimer);

// Capture the current clip position in terms of samples recorded
int position = Microphone.GetPosition(null);
Expand Down Expand Up @@ -206,42 +202,6 @@ private void StopRecording(){
AudioClip.Destroy(outputSource.clip);
}

// Starts or resets a timer with its time and callback function
private void StartOrResetTimer(Timer timer = null, int time = 1000, Action callbackFunction = null)
{
// If the callback function is null throw an exception
if(callbackFunction == null)
{
throw new ArgumentNullException(nameof(callbackFunction), " -Callback function in StartOrResetTimer cannot be null");
}

// Convert Action to TimerCallback with a lambda expression
TimerCallback timerCallback = state => callbackFunction();

// If the timer is null create a new timer
if (timer == null)
{
timer = new Timer(timerCallback, null, time, Timeout.Infinite);
}

else
{
// Reset the timer
timer.Change(time, Timeout.Infinite);
}
}

// Suspends a timer
private void SuspendTimer(Timer timer = null)
{
timer?.Change(Timeout.Infinite, Timeout.Infinite);
}

// Sets the stopRecordingFlag to true
private void SetStopRecordingFlag(){
stopRecordingFlag = true;
}

public override void OnPlayerEnteredRoom(Player newPlayer)
{
if (!photonView.IsMine) return;
Expand Down

0 comments on commit 04f04e8

Please sign in to comment.