MariuszM

TTSPaulina

Oct 1st, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. /*
  2. Autor VBService (pseudonim z forum Zelenta Forum Pasja Informatyki)
  3. Autor pomysłu na kod Mariusz M
  4. Jak skompilować kod z linii poleceń ?
  5. csc /out:e:\code\app.exe /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Speech.dll"
  6. e:\code\app.cs
  7. Jeżeli chcemy dokonać konwersji wyjściowego pliku wav na inny to najłatwiej to zrobić
  8. z użyciem zewnętrznego programu np ffmpeg
  9. ffmpeg -i input-file.wav -vn -ar 44100 -ac 2 -b:a 192k output-file.mp3
  10. */
  11.  
  12. using System;
  13. using System.IO;
  14. using System.Speech.Synthesis;
  15. using System.Threading;
  16. using System.Runtime.InteropServices;
  17.  
  18. namespace ConsoleApplication1
  19. {
  20.     class Program
  21.     {
  22.         static Boolean isSpeakCompleted = false;
  23.         static int sentencesLength = 0;
  24.  
  25.         [DllImport("kernel32.dll")]
  26.         private static extern bool SetConsoleTitle(string title);
  27.  
  28.         public static void Main(string[] args)
  29.         {
  30.             InfoOnTitle();
  31.              
  32.             if (args.Length == 0)
  33.             {
  34.                 Console.WriteLine("Usage: program.exe inputfile.txt [outputfile.wav]");
  35.                 Console.ReadKey();
  36.                 return;
  37.             }
  38.  
  39.             if (!File.Exists(args[0]))
  40.             {
  41.                 Console.WriteLine("Usage: program.exe inputfile.txt [outputfile.wav]");
  42.                 Console.WriteLine("Podany plik: {0} - nie istnieje", args[0]);
  43.                 Console.ReadKey();
  44.                 return;
  45.             }
  46.  
  47.             try
  48.             {
  49.                 SpeechSynthesizer synth = new SpeechSynthesizer();
  50.                 synth.SpeakCompleted += synth_SpeakCompleted;
  51.  
  52.                 using (StreamReader sr = new StreamReader(args[0]))
  53.                 {
  54.                     String text = sr.ReadToEnd();
  55.                     synth.SelectVoice("Microsoft Paulina Desktop");
  56.                     synth.Volume = 100;
  57.                     synth.Rate = 0;
  58.                    
  59.                     if (args.Length == 2)
  60.                     {
  61.                         synth.SetOutputToWaveFile(args[1]);
  62.                         synth.Speak(text);
  63.                     }
  64.  
  65.                     synth.SetOutputToDefaultAudioDevice();
  66.  
  67.                     Boolean canSpeak = true;
  68.                     string[] sentences = text.Split(new char[] { ',', '.', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  69.                     sentencesLength = sentences.Length;
  70.                     int sentencesIndex = 0;
  71.  
  72.                     while (!isSpeakCompleted)
  73.                     {
  74.                        
  75.                         if (Console.KeyAvailable)
  76.                         {
  77.                             ConsoleKeyInfo keyInfo = Console.ReadKey(true);
  78.                             if (keyInfo.Key == ConsoleKey.UpArrow)
  79.                             {
  80.                                 synth.Volume += (synth.Volume < 100) ? 5 : 0;
  81.                                 InfoOnTitle("Volume: " + synth.Volume);
  82.                             }
  83.  
  84.                             if (keyInfo.Key == ConsoleKey.DownArrow)
  85.                             {
  86.                                 synth.Volume += (synth.Volume > 0) ? -5 : 0;
  87.                                 InfoOnTitle("Volume: " + synth.Volume);
  88.                             }
  89.                            
  90.                             if(keyInfo.Key == ConsoleKey.Escape)
  91.                                 isSpeakCompleted = true;
  92.                            
  93.                             if (keyInfo.Key == ConsoleKey.W)
  94.                             {
  95.                                 synth.Rate += (synth.Rate < 10) ? 1 : 0;
  96.                                 InfoOnTitle("Rate: " + synth.Rate);
  97.                             }
  98.  
  99.                             if (keyInfo.Key == ConsoleKey.S)
  100.                             {
  101.                                 synth.Rate += (synth.Rate > -10) ? -1 : 0;
  102.                                 InfoOnTitle("Rate: " + synth.Rate);
  103.                             }
  104.                            
  105.                             if (keyInfo.Key == ConsoleKey.Spacebar)
  106.                             {
  107.                                 canSpeak = !canSpeak;
  108.                                 if (canSpeak)
  109.                                 {
  110.                                     synth.Resume();
  111.                                     InfoOnTitle();
  112.                                 }
  113.                                 else
  114.                                 {
  115.                                     synth.Pause();
  116.                                     InfoOnTitle("Paused");
  117.                                 }
  118.                             }
  119.                         }
  120.  
  121.                         if (canSpeak && sentencesIndex < sentences.Length)
  122.                             synth.SpeakAsync(sentences[sentencesIndex++]);
  123.                         else
  124.                             Thread.Sleep(10);
  125.                     }
  126.                 }
  127.             }
  128.             catch (Exception e)
  129.             {
  130.                 Console.WriteLine("The file could not be read");
  131.                 Console.WriteLine(e.Message);
  132.             }
  133.  
  134.             Console.WriteLine("Press Enter to exit");
  135.             Console.ReadKey();
  136.         }
  137.  
  138.         static void InfoOnTitle(string title = "")
  139.         {
  140.             SetConsoleTitle(title);
  141.         }
  142.  
  143.         private static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
  144.         {
  145.             if (--sentencesLength <= 0)
  146.                 isSpeakCompleted = true;
  147.         }
  148.     }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment