/// <summary>
        /// Extract epoch data from EGG files.
        /// </summary>
        /// <param name="mapFile">Map file list.</param>
        /// <param name="wave16kDir">16k Hz waveform directory.</param>
        /// <param name="egg16kDir">16k Hz EGG directory.</param>
        /// <param name="epochDir">Epoch directory.</param>
        /// <param name="skipExist">Falg to indicate whether skipping existing target file.</param>
        public static void EggToEpoch(string mapFile, string wave16kDir,
            string egg16kDir, string epochDir, bool skipExist)
        {
            // Const parameter used to generate epoch from wave16k and egg16k file.
            const double FilterLowFreq = 60.0;
            const double FilterHighFreq = 3600.0;
            const int BandPassOrder = 1000;
            const int LarEpochMinPitch = 110;
            const int LarEpochMaxPitch = 500;
            const int FrameSize = checked((int)(0.025f * 16000));
            const int LpcOrder = 20;
            const int AdjustFreqOffset = 10;

            // Validate file/dir parameter
            if (!File.Exists(mapFile))
            {
                throw Helper.CreateException(typeof(FileNotFoundException),
                    mapFile);
            }

            if (!Directory.Exists(wave16kDir))
            {
                throw Helper.CreateException(typeof(DirectoryNotFoundException),
                    wave16kDir);
            }

            if (!Directory.Exists(egg16kDir))
            {
                throw Helper.CreateException(typeof(DirectoryNotFoundException),
                    egg16kDir);
            }

            if (string.IsNullOrEmpty(epochDir))
            {
                throw new ArgumentNullException("epochDir");
            }

            FileListMap fileMap = new FileListMap();
            fileMap.Load(mapFile);

            ConsoleLogger logger = new ConsoleLogger();

            // Validate the consistence between the EGG and waveform files
            DataErrorSet errorSet = VoiceFont.ValidateWaveAlignment(fileMap, wave16kDir,
                egg16kDir, "EGG");
            if (errorSet.Errors.Count > 0)
            {
                foreach (DataError error in errorSet.Errors)
                {
                    logger.LogLine(error.ToString());

                    if (fileMap.Map.ContainsKey(error.SentenceId))
                    {
                        fileMap.Map.Remove(error.SentenceId);
                    }
                }
            }

            // Performance converting
            Helper.EnsureFolderExist(epochDir);
            foreach (string sid in fileMap.Map.Keys)
            {
                string wave16kFile = Path.Combine(wave16kDir,
                    fileMap.Map[sid] + ".wav");
                string egg16kFile = Path.Combine(egg16kDir,
                    fileMap.Map[sid] + ".wav");
                string epochFile = Path.Combine(epochDir,
                    fileMap.Map[sid] + ".epoch");

                if (!File.Exists(wave16kFile) || !File.Exists(egg16kFile))
                {
                    if (!File.Exists(wave16kFile))
                    {
                        logger.LogLine("Can't find file : {0}", wave16kFile);
                    }
                    else if (!File.Exists(egg16kFile))
                    {
                        logger.LogLine("Can't find file : {0}", egg16kFile);
                    }

                    continue;
                }

                Helper.EnsureFolderExistForFile(epochFile);

                // Performance EGG to Epoch conversion while
                // 1. not skipping the existing epoch file is asked
                // 2. Or the target epoch file does not exist
                // 3. Or the target epoch file is with zero length
                if (!skipExist ||
                    !File.Exists(epochFile) ||
                    new FileInfo(epochFile).Length == 0)
                {
                    EggAcousticFeature.Egg2Epoch(wave16kFile, egg16kFile,
                        epochFile, FilterLowFreq, FilterHighFreq, BandPassOrder,
                        LarEpochMinPitch, LarEpochMaxPitch, FrameSize, LpcOrder,
                        AdjustFreqOffset);
                }
            }
        }
Beispiel #2
0
        public void ParseConfig(XmlElement config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            Debug.Assert(!string.IsNullOrEmpty(config.GetAttribute("language")));
            Debug.Assert(!string.IsNullOrEmpty(config.GetAttribute("engine")));
            _primaryLanguage = Localor.StringToLanguage(
                                            config.GetAttribute("language"));
            _engineType = (EngineType)Enum.Parse(typeof(EngineType),
                                            config.GetAttribute("engine"));

            XmlElement eleLangData = config.SelectSingleNode("languageData") as XmlElement;
            VoiceCreationLanguageData languageData = new VoiceCreationLanguageData();
            if (eleLangData != null)
            {
                languageData.ParseLanguageDataFromXmlElement(true, eleLangData);
                languageData.SetLanguageData(_primaryLanguage);
            }
            else
            {
                languageData.CartQuestions = config.SelectSingleNode("question/@path").InnerText;
            }

            _voiceName = config.GetAttribute("voiceName");
            _tokenId = config.GetAttribute("tokenId");

            _fontPath = config.SelectSingleNode("font/@path").InnerText;

            ScriptFile = Localor.CreateScriptFile(_primaryLanguage, _engineType);
            ScriptFile.Load(config.SelectSingleNode("script/@path").InnerText);

            FileMap = new FileListMap();
            FileMap.Load(config.SelectSingleNode("filemap/@path").InnerText);

            _weightTable = new WeightTable(_primaryLanguage, _engineType);
            _weightTable.Load(config.SelectSingleNode("weighttable/@path").InnerText);

            _cartTreeManager = new CartTreeManager();
            _cartTreeManager.CartTreeDir = config.SelectSingleNode("treedir/@path").InnerText;
            if (!Directory.Exists(_cartTreeManager.CartTreeDir))
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "The treeDir path does not exist at [{0}]",
                    _cartTreeManager.CartTreeDir);
                throw new DirectoryNotFoundException(message);
            }

            _cartTreeManager.CartQuestionFile = languageData.CartQuestions;
            if (!File.Exists(_cartTreeManager.CartQuestionFile))
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "The tree question file path does not exist at [{0}]",
                    _cartTreeManager.CartQuestionFile);
                throw new DirectoryNotFoundException(message);
            }

            _cartTreeManager.UnitDescriptFile = config.SelectSingleNode("unitdescript/@path").InnerText;
            if (!File.Exists(_cartTreeManager.UnitDescriptFile))
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "The unit description file path does not exist at [{0}]",
                    _cartTreeManager.UnitDescriptFile);
                throw new DirectoryNotFoundException(message);
            }

            _unitFeatureFilePath = config.SelectSingleNode("wavesequence/@path").InnerText;
            if (!File.Exists(_unitFeatureFilePath))
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "The wave sequence file path does not exist at [{0}]",
                    _unitFeatureFilePath);
                throw new DirectoryNotFoundException(message);
            }

            _wave16kDirectories.Clear();
            foreach (XmlNode dirNode in config.SelectNodes("wave16k/@path"))
            {
                string waveDir = dirNode.InnerText.Trim();
                if (!Directory.Exists(waveDir))
                {
                    string message = string.Format(CultureInfo.InvariantCulture,
                        "The wave16k path does not exist at [{0}]",
                        waveDir);
                    throw new DirectoryNotFoundException(message);
                }

                _wave16kDirectories.Add(waveDir);
            }

            _segmentDirectories.Clear();
            foreach (XmlNode dirNode in config.SelectNodes("segment/@path"))
            {
                string alignmentDir = dirNode.InnerText.Trim();
                if (!Directory.Exists(alignmentDir))
                {
                    string message = string.Format(CultureInfo.InvariantCulture,
                        "The alignment path does not exist at [{0}]",
                        alignmentDir);
                    throw new DirectoryNotFoundException(message);
                }

                _segmentDirectories.Add(alignmentDir);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="F0ExtractorCOSMOS" /> class.
        /// </summary>
        /// <param name="fileMap">File list map.</param>
        /// <param name="waveDir">Input wave directory.</param>
        /// <param name="workDir">Output data direcotry.</param>
        /// <param name="getF0Tool">The path of get_f0.exe file.</param>
        /// <param name="getF0Config">The path of get_f0.config file.</param>
        /// <param name="straightTool">The path of straight_all.exe file.</param>
        /// <param name="svmScaleTool">The path of svm-scale.exe file.</param>
        /// <param name="svmPredictTool">The path of svm-predit.exe file.</param>
        /// <param name="modelFilePath">Svm model file path.</param>
        /// <param name="minF0">The min f0 value.</param>
        /// <param name="maxF0">The max f0 value.</param>
        /// <param name="secondsPerFrame">The length of one frame in seconds.</param>
        /// <param name="samplesPerSecond">The sample number in one second.</param>
        /// <param name="dimension">Uv feature dimension.</param>
        /// <param name="frameBias">Frame bias of f0.</param>
        /// <param name="logger">The logger.</param>
        /// <param name="enableCosmos">Enable cosmos.</param>
        /// <param name="cosmosPath">Cosmos path.</param>
        /// <param name="fileSS">The structured stream file.</param>
        /// <exception cref="DirectoryNotFoundException">Exception.</exception>
        /// <exception cref="FileNotFoundException">Exception.</exception>
        /// <exception cref="ArgumentNullException">Exception.</exception>
        /// <exception cref="ArgumentException">Exception</exception>
        public F0ExtractorCOSMOS(string fileMap, string waveDir, string workDir, string getF0Tool, string getF0Config, string straightTool, string svmScaleTool, string svmPredictTool, string modelFilePath,
            float minF0, float maxF0, float secondsPerFrame, int samplesPerSecond, int dimension, int frameBias, ILogger logger,
            bool enableCosmos = false, string cosmosPath = " ", string fileSS = " ")
        {
            // check input arguments
            Helper.ThrowIfDirectoryNotExist(waveDir);
            Helper.ThrowIfFileNotExist(modelFilePath);

            // check dimensions
            if (dimension != BasicDimension && dimension != ExpandDimension && dimension != DeltaDimension)
            {
                throw new ArgumentException("Invalid dimension value, it should be 6/18/54");
            }

            // check f0 range
            if (minF0 > maxF0 || minF0 < 0)
            {
                throw new ArgumentException("Invalid F0 range.");
            }

            // check seconds per frame
            if (secondsPerFrame <= Minimum)
            {
                throw new ArgumentException("Invalid seconds of per frame.");
            }

            // check sample rate 
            if (samplesPerSecond <= Minimum)
            {
                throw new ArgumentException("Invalid sample rate.");
            }

            // check frame bias 
            if (frameBias < -10 || frameBias > 10)
            {
                throw new ArgumentException("Frame bias of f0 should be in the range [-10, 10].");
            }

            // assign values to fields
            WorkDir = workDir;
            Helper.EnsureFolderExist(workDir);
            WaveDir = waveDir;
            ModelFilePath = modelFilePath;
            Dimension = dimension;
            MinF0Value = minF0;
            MaxF0Value = maxF0;
            SecondsPerFrame = secondsPerFrame;
            FrameShift = (int)Math.Ceiling(samplesPerSecond * secondsPerFrame);
            FrameLength = FrameShift * 2;
            FrameBias = frameBias;

            // check logger
            Logger = (logger == null) ? new NullLogger() : logger;

            // get file list
            if (string.IsNullOrWhiteSpace(fileMap) || !File.Exists(fileMap))
            {
                FileMap = FileListMap.CreateInstance(waveDir, string.Empty.AppendExtensionName(FileExtensions.Waveform.ToLowerInvariant()));
            }
            else
            {
                FileMap = new FileListMap();
                FileMap.Load(fileMap);
            }

            if (FileMap.Map.Count == 0)
            {
                throw new ArgumentException("Empty wave folder!");
            }

            EnableCosmos = enableCosmos;
            CosmosPath = cosmosPath;
            FileSS = fileSS;

            // make intermediate directories
            IntermediateDir = Path.Combine(workDir, "Intermediate");
            GetF0Tool = getF0Tool;
            Helper.ThrowIfFileNotExist(GetF0Tool);
            GetF0Config = getF0Config;
            Helper.ThrowIfFileNotExist(GetF0Config);
            StraightTool = straightTool;
            Helper.ThrowIfFileNotExist(StraightTool);
            SvmScaleTool = svmScaleTool;
            Helper.ThrowIfFileNotExist(SvmScaleTool);
            SvmPredictTool = svmPredictTool;
            Helper.ThrowIfFileNotExist(SvmPredictTool);
            F0NccfDir = Path.Combine(IntermediateDir, "get_f0");
            Helper.EnsureFolderExist(F0NccfDir);
            RealtedFeaDir = Path.Combine(IntermediateDir, "relatedFeatures");
            Helper.EnsureFolderExist(RealtedFeaDir);
            NccfDir = Path.Combine(IntermediateDir, "nccf");
            Helper.EnsureFolderExist(NccfDir);
            LPCDir = Path.Combine(IntermediateDir, "lpc");
            Helper.EnsureFolderExist(LPCDir);
            ResidualDir = Path.Combine(IntermediateDir, "residual");
            Helper.EnsureFolderExist(ResidualDir);
            F0Dir = Path.Combine(IntermediateDir, "if0");
            Helper.EnsureFolderExist(F0Dir);
            MergedFeaDir = Path.Combine(IntermediateDir, "mergedFea");
            Helper.EnsureFolderExist(MergedFeaDir);
            ExpandFeaDir = Path.Combine(IntermediateDir, "expandedFea");
            Helper.EnsureFolderExist(ExpandFeaDir);
            SvmFeaDir = Path.Combine(IntermediateDir, "formatFea");
            Helper.EnsureFolderExist(SvmFeaDir);
            ScaledSvmFeaDir = Path.Combine(IntermediateDir, "scaledFea");
            Helper.EnsureFolderExist(ScaledSvmFeaDir);
            UVDir = Path.Combine(IntermediateDir, "uv");
            Helper.EnsureFolderExist(UVDir);
            SmoothedF0Dir = Path.Combine(WorkDir, "sf0");
            Helper.EnsureFolderExist(SmoothedF0Dir);
        }
        /// <summary>
        /// Check the file map is invalid or not.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns>Bool.</returns>
        public static bool IsValidFileListMap(string filePath)
        {
            bool isValidFileListMap = true;
            FileListMap fileListMap = new FileListMap();
            if (string.IsNullOrEmpty(filePath))
            {
                isValidFileListMap = false;
            }
            else
            {
                try
                {
                    fileListMap.Load(filePath);
                }
                catch
                {
                    isValidFileListMap = false;
                }
            }

            return isValidFileListMap;
        }