| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Speech.text;
- namespace Speech {
- public class SpeechCache {
- private List<SpeechCacheEntry> _entry = new List<SpeechCacheEntry>();
- 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<SpeechCacheEntry>(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;
- }
- /// <summary>
- /// Säubert die Cache Einträge.
- /// - Einträge, deren Datei nicht existiet, werden entfernt
- /// - Invalide Einträge werden entfernt
- /// - Entfernt doppelte Einträge
- /// </summary>
- 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<List<SpeechCacheEntry>>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
- //SpeechCacheEntry[] itms = System.Fire.Serialize.Serializer.DeserializeArray<SpeechCacheEntry[]>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
- SpeechCacheEntry[] itms = System.Fire.Serialize.Serializer.DeserializeObject<SpeechCacheEntry[]>(FileName, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8);
- this._entry = new List<SpeechCacheEntry>(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;
- }
- /// <summary>
- /// Generates an unique file name
- /// </summary>
- /// <returns></returns>
- 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)
- ;
- }
- }
- }
|