/// <summary> /// Searches the cache for a particular phrase. /// </summary> /// <param name="phrase">The phrase information.</param> /// <returns>The phrase information if found in the cache or <c>null</c>.</returns> /// <exception cref="ArgumentNullException">Thrown if <pararef name="phrase" /> is <c>null</c>.</exception> /// <exception cref="InvalidOperationException">Thrown if the cache has been stopped.</exception> public Phrase FindPhrase(Phrase phrase) { Phrase existing; if (phrase == null) { throw new ArgumentNullException("phrase"); } // The requested voice might not exist, so get the actual // voice that can be used. phrase.ActualVoice = SpeechEngine.GetVoice(phrase.Voice); if (phrase.ActualVoice == null) { return(null); // No installed voices } lock (syncLock) { if (!isRunning) { throw new InvalidOperationException("[PhraseCache] is stopped."); } if (index.TryGetValue(GetCacheKey(phrase), out existing)) { return(existing); } else { return(null); } } }
/// <summary> /// Adds the genrated phrase audio file passed to the cache. /// </summary> /// <param name="phrase">The phrase being added.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="phrase" /> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown if the <paramref name="phrase" />.<see cref="Path"/> property is <c>null</c>.</exception> /// <exception cref="InvalidOperationException">Thrown if the cache has been stopped or if a one-time phrase is passed.</exception> public void AddPhrase(Phrase phrase) { Phrase existing; if (phrase == null) { throw new ArgumentNullException("phrase"); } if (phrase.Path == null) { throw new ArgumentException("[Path] property cannot be NULL.", "phrase"); } if (phrase.IsOneTime) { throw new InvalidOperationException("[AddPhrase] cannot accept a one-time phrase."); } phrase = phrase.Clone(); // The requested voice might not exist, so get the actual // voice that can be used. phrase.ActualVoice = SpeechEngine.GetVoice(phrase.Voice); if (phrase.ActualVoice == null) { return; // No installed voices } lock (syncLock) { if (!isRunning) { throw new InvalidOperationException("[PhraseCache] is stopped."); } phrase.LastAccessUtc = DateTime.UtcNow; if (index.TryGetValue(GetCacheKey(phrase), out existing)) { // The phrase is already in the index. existing.LastAccessUtc = phrase.LastAccessUtc; } else { index.Add(GetCacheKey(phrase), phrase); } } }