improved Horus rotary encoder driver and generic behavior (#5510)
[opentx.git] / sound / speaker / MainWindow.xaml.cs
blob22d8dddfb7aedf7349a7eca845855387c3049556
1 /* This file is part of OpenTX Speaker.
2 * OpenTX Speaker is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * OpenTX Speaker is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with OpenTX Speaker. If not, see <http://www.gnu.org/licenses/>.
15 * Copyright 2014 Kjell Kernen */
17 using System;
18 using System.IO;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using System.Threading;
23 using System.Windows;
24 using System.Windows.Controls;
25 using System.Windows.Data;
26 using System.Windows.Documents;
27 using System.Windows.Input;
28 using System.Windows.Media;
29 using System.Windows.Media.Imaging;
30 using System.Windows.Navigation;
31 using System.Windows.Shapes;
32 using System.Speech.Synthesis;
33 using System.Speech.AudioFormat;
35 namespace OpenTXspeak
37 public partial class MainWindow : Window
39 SentenceTables tables = new SentenceTables();
40 Languages languages = new Languages();
41 Language curLang;
42 Sentences sentences = new Sentences();
43 SpeechSynthesizer synth = new SpeechSynthesizer();
44 int added_files = 0;
46 public MainWindow()
48 SplashScreen splash = new SplashScreen("speaker_logo.png");
49 splash.Show(true);
50 Thread.Sleep(1500);
52 InitializeComponent();
54 lvSentences.ItemsSource = sentences;
55 cbLanguages.ItemsSource = languages;
57 foreach (InstalledVoice voice in synth.GetInstalledVoices())
59 cbVoices.Items.Add(voice.VoiceInfo.Name);
61 cbVoices.SelectedIndex = 0;
63 languages.Add("English", "en");
64 languages.Add("Czech", "cz");
65 languages.Add("German", "de");
66 languages.Add("French", "fr");
67 languages.Add("Italian", "it");
68 languages.Add("Polish", "pl");
69 languages.Add("Portuguese", "pt");
70 languages.Add("Swedish", "se");
71 languages.Add("Slovak", "sk");
72 languages.Add("Spanish", "es");
73 curLang = languages[0];
74 cbLanguages.SelectedIndex = 0; // Note: Sets current langugage -> triggers loadlanguage()
77 private void loadLanguage()
79 string[] system_strings;
80 string[] other_strings;
82 try
84 system_strings = System.IO.File.ReadAllLines(@"SOUNDS\" + curLang.sName + @"\SYSTEM\system_sounds.txt");
85 other_strings = System.IO.File.ReadAllLines(@"SOUNDS\" + curLang.sName + @"\other_sounds.txt");
87 catch (IOException)
89 system_strings = tables.default_system_strings[tables.toInt(curLang.sName)];
90 other_strings = tables.default_other_strings[tables.toInt(curLang.sName)];
92 sentences.RemoveRange(0, sentences.Count);
94 foreach (string str in system_strings)
95 sentences.Add(str);
97 sentences.Add(@";^ System Sounds ^;");
99 foreach (string str in other_strings)
100 sentences.Add(str);
102 lvSentences.Items.Refresh(); // Workaround - Two way binding is better
105 private void cbLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
107 curLang = (Language)e.AddedItems[0];
108 loadLanguage();
111 private void lvSentences_MouseDoubleClick(object sender, MouseButtonEventArgs e)
113 if (this.lvSentences.SelectedItems.Count < 1)
114 return;
116 synth.SetOutputToDefaultAudioDevice();
117 synth.Rate = (int)this.voiceRateSlider.Value;
118 synth.Volume = (int)this.voiceVolumeSlider.Value;
120 Sentence sentence = (Sentence)this.lvSentences.SelectedItem;
121 synth.SelectVoice(cbVoices.Text);
122 try { synth.Speak(sentence.voiceString); }
123 catch (Exception){} // Workaround - Some TTS engines crashes on destruction
126 private void openTXLogo_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
128 AboutWindow aboutWindow = new AboutWindow();
129 aboutWindow.ShowDialog();
132 private void InstallVoices_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
134 InstallVoicesWindow installVoicesWindow = new InstallVoicesWindow();
135 installVoicesWindow.ShowDialog();
138 private void buttonPlay_Click(object sender, RoutedEventArgs e)
140 lvSentences_MouseDoubleClick(sender, null);
143 private void buttonAddItem_Click(object sender, RoutedEventArgs e)
145 added_files++;
146 sentences.Add(new Sentence("new_file_" + added_files.ToString() + ";New Description;New Voice Message"));
147 this.lvSentences.Items.Refresh();
148 this.lvSentences.SelectedIndex = this.lvSentences.Items.Count - 1;
149 this.lvSentences.ScrollIntoView(this.lvSentences.SelectedItem);
152 private void buttonSaveFiles_Click(object sender, RoutedEventArgs e)
154 Cursor oldCursor = this.Cursor;
155 this.Cursor = Cursors.Wait;
157 synth.SelectVoice(cbVoices.Text);
158 synth.Rate = (int)this.fileRateSlider.Value;
159 synth.Volume = (int)this.fileVolumeSlider.Value;
161 int sampleRate = 32000;
162 if ((bool)this.rb16khz.IsChecked)
163 sampleRate = 16000;
164 else if ((bool)this.rb8khz.IsChecked)
165 sampleRate = 8000;
167 SpeechAudioFormatInfo audioFormat = new SpeechAudioFormatInfo(sampleRate, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
169 System.IO.Directory.CreateDirectory(@"SOUNDS\" + curLang.sName + @"\SYSTEM");
170 StreamWriter sw = File.CreateText(@"SOUNDS\" + curLang.sName + @"\SYSTEM\system_sounds.txt");
171 int i;
172 for (i = 0; sentences[i].fileName != ""; i++)
174 sw.WriteLine(sentences[i].toRaw());
175 synth.SetOutputToWaveFile(@"SOUNDS\" + curLang.sName + @"\SYSTEM\" + sentences[i].fileName + ".wav", audioFormat);
176 try { synth.Speak(sentences[i].voiceString); }
177 catch (Exception){} // Workaround - Some TTS engines crashes on destruction
179 sw.Close();
180 sw = File.CreateText(@"SOUNDS\" + curLang.sName + @"\other_sounds.txt");
181 for (i++; i < sentences.Count; i++)
183 sw.WriteLine(sentences[i].toRaw());
184 synth.SetOutputToWaveFile(@"SOUNDS\" + curLang.sName + @"\" + sentences[i].fileName + ".wav", audioFormat);
185 try { synth.Speak(sentences[i].voiceString); }
186 catch (Exception){} // Workaround - Some TTS engines crashes on destruction
188 sw.Close();
189 synth.SetOutputToNull(); // Workaround - The synth does not release the sound file like it should
190 this.Cursor = oldCursor;
194 public class Language
196 public string lName { get; set; }
197 public string sName { get; set; }
200 public class Languages : List<Language>
202 public void Add(string longer, string shorter)
204 this.Add(new Language { lName = longer, sName = shorter });
208 public class Sentence
210 public string fileName { get; set; }
211 public string description { get; set; }
212 public string voiceString { get; set; }
214 public Sentence(string rawString)
216 string[] words = rawString.Split(';');
217 fileName = words[0].TrimStart(' ', '\"');
218 description = words[1];
219 voiceString = words[2].TrimEnd('\"', ',', ' ');
222 public string toRaw()
224 return "\"" +fileName + ";" + description + ";" + voiceString + "\",";
228 public class Sentences : List<Sentence>
230 public void Add(string rawString)
232 this.Add(new Sentence(rawString));