Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Autor VBService (pseudonim z forum Zelenta Forum Pasja Informatyki)
- Autor pomysłu na kod Mariusz M
- Jak skompilować kod z linii poleceń ?
- csc /out:e:\code\app.exe /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Speech.dll"
- e:\code\app.cs
- Jeżeli chcemy dokonać konwersji wyjściowego pliku wav na inny to najłatwiej to zrobić
- z użyciem zewnętrznego programu np ffmpeg
- ffmpeg -i input-file.wav -vn -ar 44100 -ac 2 -b:a 192k output-file.mp3
- */
- using System;
- using System.IO;
- using System.Speech.Synthesis;
- using System.Threading;
- using System.Runtime.InteropServices;
- namespace ConsoleApplication1
- {
- class Program
- {
- static Boolean isSpeakCompleted = false;
- static int sentencesLength = 0;
- [DllImport("kernel32.dll")]
- private static extern bool SetConsoleTitle(string title);
- public static void Main(string[] args)
- {
- InfoOnTitle();
- if (args.Length == 0)
- {
- Console.WriteLine("Usage: program.exe inputfile.txt [outputfile.wav]");
- Console.ReadKey();
- return;
- }
- if (!File.Exists(args[0]))
- {
- Console.WriteLine("Usage: program.exe inputfile.txt [outputfile.wav]");
- Console.WriteLine("Podany plik: {0} - nie istnieje", args[0]);
- Console.ReadKey();
- return;
- }
- try
- {
- SpeechSynthesizer synth = new SpeechSynthesizer();
- synth.SpeakCompleted += synth_SpeakCompleted;
- using (StreamReader sr = new StreamReader(args[0]))
- {
- String text = sr.ReadToEnd();
- synth.SelectVoice("Microsoft Paulina Desktop");
- synth.Volume = 100;
- synth.Rate = 0;
- if (args.Length == 2)
- {
- synth.SetOutputToWaveFile(args[1]);
- synth.Speak(text);
- }
- synth.SetOutputToDefaultAudioDevice();
- Boolean canSpeak = true;
- string[] sentences = text.Split(new char[] { ',', '.', '\n' }, StringSplitOptions.RemoveEmptyEntries);
- sentencesLength = sentences.Length;
- int sentencesIndex = 0;
- while (!isSpeakCompleted)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo keyInfo = Console.ReadKey(true);
- if (keyInfo.Key == ConsoleKey.UpArrow)
- {
- synth.Volume += (synth.Volume < 100) ? 5 : 0;
- InfoOnTitle("Volume: " + synth.Volume);
- }
- if (keyInfo.Key == ConsoleKey.DownArrow)
- {
- synth.Volume += (synth.Volume > 0) ? -5 : 0;
- InfoOnTitle("Volume: " + synth.Volume);
- }
- if(keyInfo.Key == ConsoleKey.Escape)
- isSpeakCompleted = true;
- if (keyInfo.Key == ConsoleKey.W)
- {
- synth.Rate += (synth.Rate < 10) ? 1 : 0;
- InfoOnTitle("Rate: " + synth.Rate);
- }
- if (keyInfo.Key == ConsoleKey.S)
- {
- synth.Rate += (synth.Rate > -10) ? -1 : 0;
- InfoOnTitle("Rate: " + synth.Rate);
- }
- if (keyInfo.Key == ConsoleKey.Spacebar)
- {
- canSpeak = !canSpeak;
- if (canSpeak)
- {
- synth.Resume();
- InfoOnTitle();
- }
- else
- {
- synth.Pause();
- InfoOnTitle("Paused");
- }
- }
- }
- if (canSpeak && sentencesIndex < sentences.Length)
- synth.SpeakAsync(sentences[sentencesIndex++]);
- else
- Thread.Sleep(10);
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("The file could not be read");
- Console.WriteLine(e.Message);
- }
- Console.WriteLine("Press Enter to exit");
- Console.ReadKey();
- }
- static void InfoOnTitle(string title = "")
- {
- SetConsoleTitle(title);
- }
- private static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
- {
- if (--sentencesLength <= 0)
- isSpeakCompleted = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment