예제 #1
0
        /// <summary>
        /// Initialize the instance of CART tree manager
        /// 1) load and parse the CART question file
        /// 2) initialize unit description table data from the phoneme of the 
        ///    specified language and engine type.
        /// </summary>
        /// <param name="language">Language.</param>
        /// <param name="engine">Engine type.</param>
        /// <param name="cartQuestionFilePath">CART question file path.</param>
        public void Initialize(Language language, EngineType engine, 
            string cartQuestionFilePath)
        {
            if (string.IsNullOrEmpty(cartQuestionFilePath))
            {
                throw new ArgumentNullException("cartQuestionFilePath");
            }

            _cartQuestionFile = cartQuestionFilePath;
            _metaCart = new MetaCart(language, engine);
            _metaCart.Initialize(_cartQuestionFile);
        }
예제 #2
0
        /// <summary>
        /// Compose CRT file.
        /// </summary>
        /// <param name="language">Language.</param>
        /// <param name="engine">Engine type.</param>
        /// <param name="unitListFile">Unit list file path.</param>
        /// <param name="cartQuestionFile">CART question file path.</param>
        /// <param name="binTreeDir">Binary CART tree directory.</param>
        /// <param name="crtFile">File list path.</param>
        public static void ComposeCrtFile(Language language, EngineType engine,
            string unitListFile, string cartQuestionFile, string binTreeDir,
            string crtFile)
        {
            // parse CART question data file
            MetaCart meta = new MetaCart(language, engine);
            meta.Initialize(cartQuestionFile);
            byte[] metaData = meta.ToBytes();

            // Handle unit list file
            short unitIndex = 1;
            int cartIndexingOffset = 0;
            Collection<CartIndexingSerial> cartIndexingItems = new Collection<CartIndexingSerial>();
            Collection<string> cartFiles = new Collection<string>();
            using (StreamReader sr = new StreamReader(unitListFile))
            {
                string line = string.Empty;
                while ((line = sr.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    // each unit name
                    string unitName = line.Trim();
                    if (string.Compare(unitName, "_sil_", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (unitIndex == 1)
                        {
                            // ignore the first silence unit in the list,
                            // and all un-silence unit will start indexing from 1
                            continue;
                        }
                        else
                        {
                            string message = string.Format(CultureInfo.InvariantCulture,
                                "silence unit _sil_ found in the middle of the unit list file [{0}].",
                                unitListFile);
                            throw new InvalidDataException(message);
                        }
                    }

                    string binCartFile = Path.Combine(binTreeDir, unitName + ".tree");
                    if (!File.Exists(binCartFile))
                    {
                        throw Helper.CreateException(typeof(FileNotFoundException),
                            binCartFile);
                    }

                    cartFiles.Add(binCartFile);
                    FileInfo fi = new FileInfo(binCartFile);
                    CartIndexingSerial cartIndexing = new CartIndexingSerial();
                    cartIndexing.UnitTypeId = unitIndex;
                    cartIndexing.StartOffset = cartIndexingOffset;
                    cartIndexing.CartFileSize = (int)fi.Length;
                    cartIndexingOffset += cartIndexing.CartFileSize;

                    cartIndexingItems.Add(cartIndexing);
                    unitIndex++;
                }
            }

            CartHeaderSerial header = new CartHeaderSerial();
            header.FeatureOffset = (uint)Marshal.SizeOf(typeof(CartHeaderSerial));
            header.FeatureSize = (uint)metaData.Length;

            header.CartIdxOffset = header.FeatureOffset + header.FeatureSize;
            header.CartIdxNum = (uint)cartIndexingItems.Count;
            header.CartDataOffset = header.CartIdxOffset
                + (header.CartIdxNum * (uint)Marshal.SizeOf(typeof(CartIndexingSerial)));

            using (FileStream fs = new FileStream(crtFile, FileMode.Create, FileAccess.Write))
            {
                // write header information of CRT file
                byte[] data = header.ToBytes();
                fs.Write(data, 0, data.Length);

                // write CART question
                fs.Write(metaData, 0, metaData.Length);

                // write CART tree indexing items for each unit CART tree
                foreach (CartIndexingSerial cartIndexingItem in cartIndexingItems)
                {
                    data = cartIndexingItem.ToBytes();
                    fs.Write(data, 0, data.Length);
                }

                // write CART data files
                foreach (string binTreeFile in cartFiles)
                {
                    data = File.ReadAllBytes(binTreeFile);
                    fs.Write(data, 0, data.Length);
                }
            }
        }