Exemple #1
0
        static Oto ParseOto(string line)
        {
            if (!line.Contains("="))
            {
                return(null);
            }
            var parts = line.Split('=');

            if (parts.Length != 2)
            {
                return(null);
            }
            var wav = parts[0].Trim();

            parts = parts[1].Split(',');
            if (parts.Length != 6)
            {
                return(null);
            }
            var ext    = Path.GetExtension(wav);
            var result = new Oto {
                OrigWav = wav,
                Wav     = HashPath(wav.Replace(ext, "")) + ext,
                Name    = parts[0].Trim()
            };

            double.TryParse(parts[1], out result.Offset);
            double.TryParse(parts[2], out result.Consonant);
            double.TryParse(parts[3], out result.Cutoff);
            double.TryParse(parts[4], out result.Preutter);
            double.TryParse(parts[5], out result.Overlap);
            return(result);
        }
        public static Oto ParseOto(string line)
        {
            const string format = "<wav>=<alias>,<offset>,<consonant>,<cutoff>,<preutter>,<overlap>";

            if (string.IsNullOrWhiteSpace(line))
            {
                return(null);
            }
            var parts = line.Split('=');

            if (parts.Length != 2)
            {
                throw new FileFormatException($"Line does not match format {format}.");
            }
            var wav = parts[0].Trim();

            parts = parts[1].Split(',');
            if (parts.Length != 6)
            {
                throw new FileFormatException($"Line does not match format {format}.");
            }
            var ext    = Path.GetExtension(wav);
            var result = new Oto {
                Wav   = wav,
                Alias = parts[0].Trim()
            };

            if (string.IsNullOrEmpty(result.Alias))
            {
                result.Alias = wav.Replace(ext, "");
            }
            result.Phonetic = result.Alias;
            if (!ParseDouble(parts[1], out result.Offset))
            {
                throw new FileFormatException($"Failed to parse offset. Format is {format}.");
            }
            if (!ParseDouble(parts[2], out result.Consonant))
            {
                throw new FileFormatException($"Failed to parse consonant. Format is {format}.");
            }
            if (!ParseDouble(parts[3], out result.Cutoff))
            {
                throw new FileFormatException($"Failed to parse cutoff. Format is {format}.");
            }
            if (!ParseDouble(parts[4], out result.Preutter))
            {
                throw new FileFormatException($"Failed to parse preutter. Format is {format}.");
            }
            if (!ParseDouble(parts[5], out result.Overlap))
            {
                throw new FileFormatException($"Failed to parse overlap. Format is {format}.");
            }
            return(result);
        }
Exemple #3
0
        static OtoSet ParseOtoSet(StreamReader streamReader)
        {
            OtoSet otoSet = new OtoSet();

            while (!streamReader.EndOfStream)
            {
                var line = streamReader.ReadLine();
                Oto oto  = ParseOto(line);
                if (oto != null)
                {
                    otoSet.Otos.Add(oto);
                }
            }
            return(otoSet);
        }
        public static OtoSet ParseOtoSet(Stream stream, string filePath, Encoding encoding)
        {
            OtoSet otoSet;

            using (var reader = new StreamReader(stream, encoding)) {
                var fileLoc = new FileLoc {
                    file = filePath, lineNumber = 0
                };
                otoSet = new OtoSet()
                {
                    File = filePath,
                };
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    fileLoc.line = line;
                    try {
                        Oto oto = ParseOto(line);
                        if (oto != null)
                        {
                            otoSet.Otos.Add(oto);
                        }
                    } catch (Exception e) {
                        Log.Error(e, $"Failed to parse\n{fileLoc}");
                        otoSet.Errors.Add($"Oto error:\n{fileLoc}");
                    }
                    fileLoc.line = null;
                    fileLoc.lineNumber++;
                }
            }
            // Use filename as alias if not in oto.
            var knownFiles = otoSet.Otos.Select(oto => oto.Wav).ToHashSet();

            foreach (var wav in Directory.EnumerateFiles(Path.GetDirectoryName(filePath), "*.wav", SearchOption.TopDirectoryOnly))
            {
                var file = Path.GetFileName(wav);
                if (!knownFiles.Contains(file))
                {
                    var oto = new Oto {
                        Alias = Path.GetFileNameWithoutExtension(file),
                        Wav   = file,
                    };
                    oto.Phonetic = oto.Alias;
                    otoSet.Otos.Add(oto);
                }
            }
            return(otoSet);
        }
Exemple #5
0
        static OtoSet ParseOtoSet(StreamReader streamReader, FileLoc fileLoc)
        {
            OtoSet otoSet = new OtoSet();

            while (!streamReader.EndOfStream)
            {
                var line = streamReader.ReadLine();
                fileLoc.line = line;
                try {
                    Oto oto = ParseOto(line);
                    if (oto != null)
                    {
                        otoSet.Otos.Add(oto);
                    }
                } catch (Exception e) {
                    throw new FileFormatException($"Failed to parse\n{fileLoc}", e);
                }
                fileLoc.line = null;
                fileLoc.lineNumber++;
            }
            return(otoSet);
        }