Skip to content

Commit

Permalink
Refactored possible bugs. awaiting test.
Browse files Browse the repository at this point in the history
+ made IsProcessingOutputs accurate.
+ refactored unnecessary lines.
  • Loading branch information
Sam committed Apr 19, 2018
1 parent 9dfcaf9 commit 1e1ca36
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 67 deletions.
10 changes: 6 additions & 4 deletions OptionalDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SETextToSpeechMod.LookUpTables;
using SETextToSpeechMod;
using SETextToSpeechMod.Processing;
using SETextToSpeechMod.Output;

namespace SETextToSpeechMod
{
Expand Down Expand Up @@ -126,10 +127,11 @@ static void Main()
{
debugger.entryPoint.UpdateBeforeSimulation();
}
debugger.StoreDictionaryWords (debugger.entryPoint.OutputManager.Speeches[testVoice].MainProcess.PossibleDebugOutput.DictionaryWords);
debugger.StoreRuleBasedWords (debugger.entryPoint.OutputManager.Speeches[testVoice].MainProcess.PossibleDebugOutput.RuleBasedWords);
debugger.PrintResults(debugger.entryPoint.OutputManager.Speeches[testVoice].MainProcess.Pronunciation.WrongFormatMatches,
debugger.entryPoint.OutputManager.Speeches[testVoice].MainProcess.Pronunciation.WrongFormatNonMatches);
SpeechTask currentTask = debugger.entryPoint.OutputManager.Speeches[testVoice];
debugger.StoreDictionaryWords (currentTask.Worker.PossibleDebugOutput.DictionaryWords);
debugger.StoreRuleBasedWords (currentTask.Worker.PossibleDebugOutput.RuleBasedWords);
debugger.PrintResults(currentTask.Worker.Pronunciation.WrongFormatMatches,
currentTask.Worker.Pronunciation.WrongFormatNonMatches);
debugger.entryPoint.Dispose();
}

Expand Down
87 changes: 52 additions & 35 deletions Output/OutputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ public List <SpeechTask> Speeches
{
get
{
return speechesFields;
return speechesField;
}
}


private readonly List <SpeechTask> speechesFields = new List <SpeechTask>();
private readonly List <SpeechTask> speechesField = new List <SpeechTask>();
private SoundPlayer soundPlayerRef;
TaskFactory taskFactory = new TaskFactory();
private AttendanceManager attendanceManager = AttendanceManager.GetSingleton();

public static bool IsDebugging { get; private set;}
public bool IsProcessingOutputs { get; private set;}
public bool IsProcessingOutputs { get; private set;}
public bool WasShutDown {get; private set;}

int timer;
int[] typeIndexes;
bool managerWasShutdown;

public OutputManager (SoundPlayer inputEmitter, bool isDebugging)
{
Expand All @@ -62,20 +63,20 @@ private void FactoryReset()
{
IsProcessingOutputs = false;
typeIndexes = new int[PossibleOutputs.Collection.Count];
speechesFields.Clear();
speechesField.Clear();

for (int i = 0; i < PossibleOutputs.Collection.Count; i++)
{
for (int k = 0; k < TOTAL_SIMULTANEOUS_SPEECHES; k++)
{
if (PossibleOutputs.Collection[i] == PossibleOutputs.MarekType)
{
speechesFields.Add (new SpeechTask (new MarekVoice (soundPlayerRef)));
speechesField.Add (new SpeechTask (new MarekVoice (soundPlayerRef)));
}

else if (PossibleOutputs.Collection[i] == PossibleOutputs.HawkingType)
{
speechesFields.Add (new SpeechTask (new MarekVoice (soundPlayerRef)));
speechesField.Add (new SpeechTask (new MarekVoice (soundPlayerRef)));
}

else if (PossibleOutputs.Collection[i] == PossibleOutputs.GLADOSType)
Expand All @@ -88,41 +89,56 @@ private void FactoryReset()

public void Run()
{
if (IsProcessingOutputs == true &&
managerWasShutdown == false)
if (WasShutDown == false)
{
IsProcessingOutputs = false;
int lastIndex = Speeches.Count - 1;
int tasksCompleted = 0;

for (int i = 0; i < speechesFields.Count; i++)
{
if (speechesFields[i].MainProcess.HasAnOrder)
Parallel.For(0, lastIndex, (index) => {
if (Speeches[index].Worker.HasAnOrder)
{
IsProcessingOutputs = true;

if (speechesFields[i].ReturnInfo.Status == TaskStatus.Created) //assuming async calls have matching length to speechesField
if (Speeches[index].ReturnInfo.Status == TaskStatus.Created) //assuming async calls have matching length to Speeches
{
int savedIndex = i; //fixed strange bug where i goes out of bounds even though the for loop prevents that; Weird!

taskFactory.StartNew (() => {
speechesFields[savedIndex].RunAsync(); //fixed bug where there was a single returned task from all speeches.
Speeches[index].Run(); //fixed bug where there was a single returned task from all speeches.
},
speechesFields[i].TaskCanceller.Token
Speeches[index].TaskCanceller.Token
);
}

else if(Speeches[index].ReturnInfo.Status == TaskStatus.RanToCompletion)
{
tasksCompleted++;
}
SetSoundTimer();
}
}

if (timer <= 0) //a little timer will prevent the emitter from oscillating between your eyes.
{
timer = UPDATES_INTERVAL;
soundPlayerRef.UpdatePosition (attendanceManager.LocalPlayer);
}
if(index == lastIndex &&
tasksCompleted == Speeches.Count)
{
IsProcessingOutputs = false;
}
});
}
}

else
{
timer--;
}
/// <summary>
/// a little timer will prevent the emitter from oscillating between your eyes.
/// </summary>
private void SetSoundTimer()
{
if (timer <= 0)
{
timer = UPDATES_INTERVAL;
soundPlayerRef.UpdatePosition (attendanceManager.LocalPlayer);
}

else
{
timer--;
}
}

/// <summary>
Expand Down Expand Up @@ -157,13 +173,12 @@ public bool CreateNewSpeech (string validVoiceTypeName, Sentence inputSentence)
{
bool outcome = default (bool);

if (managerWasShutdown == false)
if (WasShutDown == false)
{
int currentTypeIndex = GetOutputTypesIndex (validVoiceTypeName);

if (currentTypeIndex != DOESNT_EXIST)
{
IsProcessingOutputs = true;
{
int firstTypeInstance = currentTypeIndex * TOTAL_SIMULTANEOUS_SPEECHES;

if (typeIndexes[currentTypeIndex] >= TOTAL_SIMULTANEOUS_SPEECHES)
Expand All @@ -172,20 +187,22 @@ public bool CreateNewSpeech (string validVoiceTypeName, Sentence inputSentence)
}
int newSpeechIndex = firstTypeInstance + typeIndexes[currentTypeIndex];
typeIndexes[currentTypeIndex]++;
speechesFields[newSpeechIndex].FactoryReset (inputSentence);
Speeches[newSpeechIndex].FactoryReset (inputSentence);
outcome = true;
IsProcessingOutputs = true;
}
}
return outcome;
}

public void DisposeOfUnsafe()
{
managerWasShutdown = true;
WasShutDown = true;
IsProcessingOutputs = false;

for (int i = 0; i < speechesFields.Count; i++)
for (int i = 0; i < Speeches.Count; i++)
{
speechesFields[i].Dispose();
Speeches[i].Dispose();
}
}
}
Expand Down
28 changes: 13 additions & 15 deletions Output/SoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,27 @@ public void UpdateVolume (float newVolume)
}
}

public async Task PlaySentence (List <TimelineClip> timeline)
public void PlaySentence (List <TimelineClip> timeline)
{
if (timeline.Count > 0)
{
int currentClip = default (int);
var timelineClone = timeline.ToArray(); //assume that timeline is kill during Task.Run

await Task.Run (() => {
for (int i = 0; i <= timelineClone[timelineClone.Length - 1].StartPoint; i++)
{
int rngBonk = numberGenerator.Next (CHANCE_OF_CLANG);
for (int i = 0; i <= timelineClone[timelineClone.Length - 1].StartPoint; i++)
{
int rngBonk = numberGenerator.Next (CHANCE_OF_CLANG);

if (rngBonk == CHANCE_OF_CLANG - 1)
{
PlayClip (BONK); //repent you fucking sinner. CLANG
}
if (rngBonk == CHANCE_OF_CLANG - 1)
{
PlayClip (BONK); //repent you fucking sinner. CLANG
}

if (i == timelineClone[currentClip].StartPoint)
{
PlayClip (timelineClone[currentClip].ClipsSound);
}
}
});
if (i == timelineClone[currentClip].StartPoint)
{
PlayClip (timelineClone[currentClip].ClipsSound);
}
}
}
}

Expand Down
29 changes: 18 additions & 11 deletions Output/SpeechTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

namespace SETextToSpeechMod.Output
{
public class SpeechTask
public class SpeechTask : IDisposable
{
public bool IsDisposed
{
get; private set;
}

/// <summary>
/// Generating Object which is the primary focus of a SpeechTask.
/// </summary>
public TimelineFactory MainProcess { get; private set;}

public TimelineFactory Worker { get; private set;}
public Task ReturnInfo { get; private set;}

/// <summary>
Expand All @@ -25,22 +29,21 @@ public class SpeechTask

public SpeechTask (TimelineFactory itsSpeech)
{
MainProcess = itsSpeech;
ReturnInfo = new Task (() => {return;}); //just in case the default value of IsCompleted is not true.
Worker = itsSpeech;
ReturnInfo = new Task (() => {return;});
TaskCanceller = new CancellationTokenSource();
}

public async Task RunAsync()
public void Run()
{
ReturnInfo = MainProcess.RunAsync();
await ReturnInfo;
ReturnInfo = Worker.RunAsync();
}

public void FactoryReset (Sentence inputSentence)
{
TaskCanceller.Cancel();
RenewCancellationSource();
MainProcess.FactoryReset (inputSentence); //reuses instances of sentencefactory instead of instantiating every new sentence.
Worker.FactoryReset (inputSentence); //reuses instances of sentencefactory instead of instantiating every new sentence.
}

/// <summary>
Expand All @@ -54,8 +57,12 @@ public void RenewCancellationSource()

public void Dispose()
{
TaskCanceller.Cancel();
TaskCanceller.Dispose();
if(IsDisposed == false)
{
IsDisposed = true;
TaskCanceller.Cancel();
TaskCanceller.Dispose();
}
}
}
}
4 changes: 2 additions & 2 deletions Processing/TimelineFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ await Task.Run (() => {
AddPhonemes (i);
}
});
await soundPlayerRef.PlaySentence (timelinesField);
soundPlayerRef.PlaySentence (timelinesField);
IsBusy = false;
HasAnOrder = false;
}
}
}

private void AddPhonemes (int currentIndex)
Expand Down

0 comments on commit 1e1ca36

Please sign in to comment.