SpeechCache.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Speech.text;
  5. namespace Speech {
  6. public class SpeechCache {
  7. private List<SpeechCacheEntry> _entry = new List<SpeechCacheEntry>();
  8. private string _tempDir = "./cache/audio";
  9. private string _useDirectory;
  10. public SpeechCache() {
  11. this.Directory = this._tempDir;
  12. }
  13. public SpeechCacheEntry[] Entry {
  14. get {
  15. return this._entry.ToArray();
  16. }
  17. set {
  18. this._entry = new List<SpeechCacheEntry>(value);
  19. }
  20. }
  21. public string Directory {
  22. get {
  23. return this._useDirectory;
  24. }
  25. set {
  26. System.Diagnostics.Debug.WriteLine("Set Cache Directory: '" + value + "'");
  27. this._tempDir = value;
  28. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(value);
  29. System.Diagnostics.Debug.WriteLine("- Direcotry is: '" + di.FullName + "'");
  30. if(!di.Exists) {
  31. System.Diagnostics.Debug.WriteLine("- Create directory");
  32. di.Create();
  33. }
  34. this._useDirectory = di.FullName;
  35. }
  36. }
  37. public int Count {
  38. get => this._entry.Count;
  39. }
  40. public void Clear() {
  41. foreach(SpeechCacheEntry entry in _entry)
  42. if(System.IO.File.Exists(entry.File) && entry.File != "")
  43. System.IO.File.Delete(entry.File);
  44. this._entry.Clear();
  45. }
  46. public SpeechCacheEntry find(string text) {
  47. text = text.ToLower().Trim();
  48. foreach (SpeechCacheEntry sce in this._entry)
  49. if (sce.Text == text)
  50. return sce;
  51. return null;
  52. }
  53. public SpeechCacheEntry find(string text, eMood mood) {
  54. text = text.ToLower().Trim();
  55. foreach (SpeechCacheEntry sce in this._entry)
  56. if (sce.Text == text && (mood & sce.Mood)>0)
  57. return sce;
  58. return null;
  59. }
  60. /// <summary>
  61. /// Säubert die Cache Einträge.
  62. /// - Einträge, deren Datei nicht existiet, werden entfernt
  63. /// - Invalide Einträge werden entfernt
  64. /// - Entfernt doppelte Einträge
  65. /// </summary>
  66. private void Clean() {
  67. foreach (SpeechCacheEntry entry in _entry) {
  68. bool bRemove = false;
  69. if (
  70. !System.IO.File.Exists(entry.File) ||
  71. entry.File == "" ||
  72. entry.Text == ""
  73. )
  74. bRemove = true;
  75. // TODO
  76. if(bRemove) {
  77. if (System.IO.File.Exists(entry.File) && entry.File!="")
  78. System.IO.File.Delete(entry.File);
  79. this._entry.Remove(entry);
  80. }
  81. }
  82. }
  83. public SpeechCacheEntry Add(string text) {
  84. SpeechCacheEntry sce = this.find(text);
  85. if (sce == null) {
  86. sce = new SpeechCacheEntry();
  87. sce.Text = text.ToLower().Trim();
  88. sce.Mood = eMood.All;
  89. sce.Time = DateTime.Now;
  90. sce.LastAccess = DateTime.Now;
  91. sce.File = this._useDirectory + "/" + this.generateFileName("wav");
  92. this._entry.Add(sce);
  93. }
  94. return sce;
  95. }
  96. public SpeechCacheEntry Add(string text, eMood Mood) {
  97. SpeechCacheEntry sce = this.find(text);
  98. if (sce == null) {
  99. sce = new SpeechCacheEntry();
  100. sce.Text = text.ToLower().Trim();
  101. sce.Mood = Mood;
  102. sce.Time = DateTime.Now;
  103. sce.LastAccess = DateTime.Now;
  104. sce.File = this._useDirectory + "/" + this.generateFileName("wav");
  105. this._entry.Add(sce);
  106. }
  107. return sce;
  108. }
  109. public bool Load(string FileName) {
  110. if (System.IO.File.Exists(FileName) && FileName != "") {
  111. System.Diagnostics.Debug.WriteLine("Load('"+FileName+"')");
  112. //this._entry = System.Fire.Serialize.Serializer.DeserializeArray<List<SpeechCacheEntry>>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
  113. //SpeechCacheEntry[] itms = System.Fire.Serialize.Serializer.DeserializeArray<SpeechCacheEntry[]>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
  114. SpeechCacheEntry[] itms = System.Fire.Serialize.Serializer.DeserializeObject<SpeechCacheEntry[]>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
  115. this._entry = new List<SpeechCacheEntry>(itms);
  116. //this.Clean();
  117. return true;
  118. }
  119. return false;
  120. }
  121. public bool Save(string FileName) {
  122. //if (!System.IO.File.Exists(FileName) && FileName != "") {
  123. //this.Clean();
  124. System.Fire.Serialize.Serializer.SerializeArray(FileName, this._entry.ToArray(), System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
  125. return true;
  126. //}
  127. //return false;
  128. }
  129. /// <summary>
  130. /// Generates an unique file name
  131. /// </summary>
  132. /// <returns></returns>
  133. public string generateFileName() {
  134. return generateFileName(".tmp");
  135. }
  136. public string generateFileName(string file_extention) {
  137. string cr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  138. Random rnd = new Random();
  139. 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;
  140. }
  141. protected string DateTimeToCrypt() {
  142. return this.DateTimeToCrypt(DateTime.Now);
  143. }
  144. protected string DateTimeToCrypt(DateTime dt) {
  145. string cr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  146. int y = dt.Year;
  147. int M = dt.Month;
  148. int d = dt.Day;
  149. int h = dt.Hour;
  150. int i = dt.Minute;
  151. int s = dt.Second;
  152. int f = dt.Millisecond;
  153. int y1 = (int)Math.Floor((decimal)(y / cr.Length));
  154. int y2 = y - (y1 * cr.Length);
  155. int sum1 = (M * d) % ( cr.Length - 1);
  156. int sum2 = (h * i * s) % (cr.Length - 1);
  157. return cr.Substring(y1, 1) + cr.Substring(y2, 1) +
  158. cr.Substring(M, 1) +
  159. cr.Substring(d, 1) +
  160. cr.Substring(h, 1) +
  161. cr.Substring(i, 1) +
  162. cr.Substring(s, 1) +
  163. cr.Substring(f % (cr.Length - 1), 1) +
  164. cr.Substring(sum1, 1) +
  165. cr.Substring(sum2, 1)
  166. ;
  167. }
  168. }
  169. }