using System; using System.Collections.Generic; using System.Text; using Speech.text; namespace Speech { public class SpeechCache { private List _entry = new List(); private string _tempDir = "./cache/audio"; private string _useDirectory; public SpeechCache() { this.Directory = this._tempDir; } public SpeechCacheEntry[] Entry { get { return this._entry.ToArray(); } set { this._entry = new List(value); } } public string Directory { get { return this._useDirectory; } set { System.Diagnostics.Debug.WriteLine("Set Cache Directory: '" + value + "'"); this._tempDir = value; System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(value); System.Diagnostics.Debug.WriteLine("- Direcotry is: '" + di.FullName + "'"); if(!di.Exists) { System.Diagnostics.Debug.WriteLine("- Create directory"); di.Create(); } this._useDirectory = di.FullName; } } public int Count { get => this._entry.Count; } public void Clear() { foreach(SpeechCacheEntry entry in _entry) if(System.IO.File.Exists(entry.File) && entry.File != "") System.IO.File.Delete(entry.File); this._entry.Clear(); } public SpeechCacheEntry find(string text) { text = text.ToLower().Trim(); foreach (SpeechCacheEntry sce in this._entry) if (sce.Text == text) return sce; return null; } public SpeechCacheEntry find(string text, eMood mood) { text = text.ToLower().Trim(); foreach (SpeechCacheEntry sce in this._entry) if (sce.Text == text && (mood & sce.Mood)>0) return sce; return null; } /// /// Säubert die Cache Einträge. /// - Einträge, deren Datei nicht existiet, werden entfernt /// - Invalide Einträge werden entfernt /// - Entfernt doppelte Einträge /// private void Clean() { foreach (SpeechCacheEntry entry in _entry) { bool bRemove = false; if ( !System.IO.File.Exists(entry.File) || entry.File == "" || entry.Text == "" ) bRemove = true; // TODO if(bRemove) { if (System.IO.File.Exists(entry.File) && entry.File!="") System.IO.File.Delete(entry.File); this._entry.Remove(entry); } } } public SpeechCacheEntry Add(string text) { SpeechCacheEntry sce = this.find(text); if (sce == null) { sce = new SpeechCacheEntry(); sce.Text = text.ToLower().Trim(); sce.Mood = eMood.All; sce.Time = DateTime.Now; sce.LastAccess = DateTime.Now; sce.File = this._useDirectory + "/" + this.generateFileName("wav"); this._entry.Add(sce); } return sce; } public SpeechCacheEntry Add(string text, eMood Mood) { SpeechCacheEntry sce = this.find(text); if (sce == null) { sce = new SpeechCacheEntry(); sce.Text = text.ToLower().Trim(); sce.Mood = Mood; sce.Time = DateTime.Now; sce.LastAccess = DateTime.Now; sce.File = this._useDirectory + "/" + this.generateFileName("wav"); this._entry.Add(sce); } return sce; } public bool Load(string FileName) { if (System.IO.File.Exists(FileName) && FileName != "") { System.Diagnostics.Debug.WriteLine("Load('"+FileName+"')"); //this._entry = System.Fire.Serialize.Serializer.DeserializeArray>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8); //SpeechCacheEntry[] itms = System.Fire.Serialize.Serializer.DeserializeArray(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8); SpeechCacheEntry[] itms = System.Fire.Serialize.Serializer.DeserializeObject(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8); this._entry = new List(itms); //this.Clean(); return true; } return false; } public bool Save(string FileName) { //if (!System.IO.File.Exists(FileName) && FileName != "") { //this.Clean(); System.Fire.Serialize.Serializer.SerializeArray(FileName, this._entry.ToArray(), System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8); return true; //} //return false; } /// /// Generates an unique file name /// /// public string generateFileName() { return generateFileName(".tmp"); } public string generateFileName(string file_extention) { string cr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random rnd = new Random(); return this.DateTimeToCrypt() + "_" + cr.Substring(rnd.Next(0,cr.Length-1),1) + cr.Substring(rnd.Next(0, cr.Length - 1),1) + cr.Substring(rnd.Next(0, cr.Length - 1),1) + cr.Substring(rnd.Next(0, cr.Length - 1),1) + "_" + rnd.Next(0, 999999).ToString().PadLeft(6, '0') + "."+file_extention; } protected string DateTimeToCrypt() { return this.DateTimeToCrypt(DateTime.Now); } protected string DateTimeToCrypt(DateTime dt) { string cr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int y = dt.Year; int M = dt.Month; int d = dt.Day; int h = dt.Hour; int i = dt.Minute; int s = dt.Second; int f = dt.Millisecond; int y1 = (int)Math.Floor((decimal)(y / cr.Length)); int y2 = y - (y1 * cr.Length); int sum1 = (M * d) % ( cr.Length - 1); int sum2 = (h * i * s) % (cr.Length - 1); return cr.Substring(y1, 1) + cr.Substring(y2, 1) + cr.Substring(M, 1) + cr.Substring(d, 1) + cr.Substring(h, 1) + cr.Substring(i, 1) + cr.Substring(s, 1) + cr.Substring(f % (cr.Length - 1), 1) + cr.Substring(sum1, 1) + cr.Substring(sum2, 1) ; } } }