Ejemplo n.º 1
0
        private void GenerateTTS(Configuration cfg)
        {
            this.InitDestinationDirectory(cfg);

            using (var synthesizer = new SpeechSynthesizer())
            {
                this.InitializeSpeechSynthesizer(cfg, synthesizer);

                var xmlDoc = this.LoadXmlDocument(cfg);
                var rows = this.LoadRows(cfg, xmlDoc);

                foreach (var row in rows)
                {
                    string id = this.GetElementValue(cfg, row, cfg.IDElementName);
                    Console.WriteLine(id);

                    byte i = 0;
                    foreach (var textElementName in cfg.TextElementNames)
                    {
                        string textElement = this.GetElementValue(cfg, row, textElementName);
                        char postFix = (char)((byte)'A' + i);
                        string filename = cfg.Mp3FilePrefix + id + postFix + ".mp3";
                        string fullFilename = Path.Combine(cfg.DestinationDirectory, filename);
                        this.SpeakToMp3(cfg, synthesizer, fullFilename, textElement);
                        i++;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void ConvertWavStreamToMp3File(Configuration cfg, ref MemoryStream ms, string savetofilename)
        {
            //rewind to beginning of stream
            ms.Seek(0, SeekOrigin.Begin);

            using (var retMs = new MemoryStream())
            {
                using (var rdr = new WaveFileReader(ms))
                {
                    using (var wtr = new LameMP3FileWriter(savetofilename, rdr.WaveFormat, cfg.MP3BitRate))
                    {
                        rdr.CopyTo(wtr);
                        wtr.Close();
                    }

                    rdr.Close();

                    Console.WriteLine(savetofilename + " ok.");
                }
            }
        }
Ejemplo n.º 3
0
        private string GetElementValue(Configuration cfg, XElement row, string elementName)
        {
            var element = row.Element(elementName);
            if (element == null)
            {
                throw new ApplicationException(
                    string.Format(
                        "Can't find '{0}' node in the loaded XML file '{1}'.",
                        elementName,
                        cfg.SourceXmlFileName));
            }

            return element.Value;
        }
Ejemplo n.º 4
0
        private void SpeakToMp3(Configuration cfg, SpeechSynthesizer reader, string fileName, string speakText)
        {
            //save to memory stream
            MemoryStream ms = new MemoryStream();
            reader.SetOutputToWaveStream(ms);

            //do speaking
            reader.Speak(speakText);

            //now convert to mp3 using LameEncoder or shell out to audiograbber
            ConvertWavStreamToMp3File(cfg, ref ms, fileName);
        }
Ejemplo n.º 5
0
 private XElement LoadXmlDocument(Configuration cfg)
 {
     XElement xmlDoc;
     try
     {
         xmlDoc = XElement.Load(cfg.SourceXmlFileName);
     }
     catch (Exception ex)
     {
         throw new ApplicationException(string.Format("Couldn't load the XML file '{0}'.", cfg.SourceXmlFileName), ex);
     }
     return xmlDoc;
 }
Ejemplo n.º 6
0
 private IEnumerable<XElement> LoadRows(Configuration cfg, XElement xmlDoc)
 {
     var rows = xmlDoc.Descendants(cfg.RowElementName);
     if (rows == null)
     {
         throw new ApplicationException(
             string.Format(
                 "Can't find '{0}' nodes in the loaded XML file '{1}'.",
                 cfg.RowElementName,
                 cfg.SourceXmlFileName));
     }
     return rows;
 }
Ejemplo n.º 7
0
        private void InitializeSpeechSynthesizer(Configuration cfg, SpeechSynthesizer synthesizer)
        {
            synthesizer.SelectVoice(cfg.SelectedVoice);

            //set some settings
            synthesizer.Volume = 100;
            synthesizer.Rate = 0; //medium
        }
Ejemplo n.º 8
0
        private void InitDestinationDirectory(Configuration cfg)
        {
            if (string.IsNullOrEmpty(cfg.DestinationDirectory))
            {
                cfg.DestinationDirectory = ".";
            }

            try
            {
                cfg.DestinationDirectory = Path.GetFullPath(cfg.DestinationDirectory);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(
                    string.Format("Invalid DestinationDirectory! '{0}'", cfg.DestinationDirectory),
                    ex);
            }

            if (!Directory.Exists(cfg.DestinationDirectory))
            {
                Directory.CreateDirectory(cfg.DestinationDirectory);
            }
        }