/// <summary> /// The speaking thread body. /// </summary> /// <remarks>This loops on the queue of things to say, and speaks them /// one at a time.</remarks> private void SpeakLoop() { QueuedSpeech utterance; try { while (running) { // Wait for something to show up in the queue. lock (queue) { while (queue.Count == 0) { Monitor.Wait(queue); } utterance = queue.Dequeue(); } // Watch for the special "shutdown" message. if (utterance.message == null && utterance.speaker == null) { running = false; continue; } string thisfile = AUDIOFILE + string.Format("{0}", seq++) + ".wav"; if (seq > 4) { seq = 0; } // Synthesize it into a file. syn.Speak(utterance, thisfile); // TODO Get FMOD working on Mac if (System.Environment.OSVersion.Platform != PlatformID.MacOSX) { // Play back the file control.sound.Play( thisfile, // Name of the file 16000, // Same rate utterance.position, // Location true, // Delete the file when done utterance.isSpatial); // Whether it is inworld or moves with listener } } } catch (Exception e) { Console.WriteLine("Synth shutdown " + e.Message); } }
/// <summary> /// Thread that processes FMOD calls. /// </summary> private void CommandLoop() { // Initialze a bunch of static values UpVector.x = 0.0f; UpVector.y = 1.0f; UpVector.z = 0.0f; ZeroVector.x = 0.0f; ZeroVector.y = 0.0f; ZeroVector.z = 0.0f; allSounds = new Dictionary <IntPtr, MediaObject>(); allChannels = new Dictionary <IntPtr, MediaObject>(); // Initialize the command queue. queue = new Queue <SoundDelegate>(); // Initialize the FMOD sound package InitFMOD(); initDone.Set(); if (!SoundSystemAvailable) { return; } SoundDelegate action = null; while (true) { // Wait for something to show up in the queue. lock (queue) { while (queue.Count == 0) { Monitor.Wait(queue); } action = queue.Dequeue(); } // We have an action, so call it. try { action(); action = null; } catch (Exception e) { Logger.Log("Error in sound action:\n " + e.Message + "\n" + e.StackTrace, Helpers.LogLevel.Error); } } }
/// <summary> /// Removes and returns the object at the beginning of the Queue. /// </summary> /// <param name="timeout">time to wait before returning (in milliseconds)</param> /// <returns>Object in queue.</returns> public T Dequeue(int timeout) { lock (SyncRoot) { while (open && (base.Count == 0)) { if (!Monitor.Wait(SyncRoot, timeout)) { throw new InvalidOperationException("Timeout"); } } if (open) { return(base.Dequeue()); } else { throw new InvalidOperationException("Queue Closed"); } } }
public bool Dequeue(int timeout, ref T obj) { lock (SyncRoot) { while (open && (base.Count == 0)) { if (!Monitor.Wait(SyncRoot, timeout)) { return(false); } } if (open) { obj = base.Dequeue(); return(true); } else { obj = default(T); return(false); } } }