lua: added EVT_MENU_LONG on Taranis (#3991)
[opentx.git] / sound / recorder / MainWindow.xaml.cs
blob60eea16576e8ce4c7b19cd39e0ad2cc04a3415d7
1 /* This file is part of OpenTX Recorder.
2 * OpenTX Recorder 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 Recorder 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 Recorder. If not, see <http://www.gnu.org/licenses/>.
15 * Copyright 2014 Kjell Kernen */
18 using System;
19 using System.IO;
20 using System.Collections.Generic;
21 using System.Collections.ObjectModel;
22 using System.Linq;
23 using System.Media;
24 using System.Threading;
25 using System.Windows;
26 using System.Windows.Controls;
27 using System.Windows.Input;
28 using System.Windows.Navigation;
29 using System.Runtime.InteropServices;
30 using WaveLib;
31 using System.Net;
34 namespace OpenTXrecorder
36 public partial class MainWindow : Window
38 Environment env;
39 SentenceTables tables = new SentenceTables();
40 public Sentences sentences { get; set; }
41 public Languages languages { get; set; }
43 WavFileWriter filewriter;
44 WaveInRecorder recorder;
45 byte[] recordingBuffer;
46 static int recSamplerate = 16000; // 8000, 16000 or 32000
47 static int recBits = 16; // 8 or 16
48 static int recChannels = 1; // 1 or 2
49 static int recBuffersize = 1024 * 2;
50 public bool isRecording { get; set; }
53 public MainWindow()
55 this.DataContext = this;
56 sentences = new Sentences();
57 languages = new Languages();
58 recordingBuffer = new byte[recBuffersize];
60 InitializeComponent();
62 // Start by displaying Splash Screen
63 SplashScreen splash = new SplashScreen("recorder_logo.png");
64 splash.Show(true);
65 Thread.Sleep(1500);
67 languages.Add("English", "en");
68 languages.Add("Czech", "cz");
69 languages.Add("German", "de");
70 languages.Add("French", "fr");
71 languages.Add("Italian", "it");
72 languages.Add("Polish", "pl");
73 languages.Add("Portuguese", "pt");
74 languages.Add("Swedish", "se");
75 languages.Add("Slovak", "sk");
76 languages.Add("Spanish", "es");
78 env = new Environment(languages[0]);
80 cbLanguages.SelectedIndex = 0; // Note: Sets current langugage -> triggers loadlanguage()
83 private void loadLanguage()
85 string[] system_strings;
86 string[] other_strings;
88 try
90 system_strings = System.IO.File.ReadAllLines(env.systemSounds);
91 other_strings = System.IO.File.ReadAllLines(env.otherSounds);
93 catch (IOException)
95 system_strings = tables.default_system_strings[tables.toInt(env.lang.sName)];
96 other_strings = tables.default_other_strings[tables.toInt(env.lang.sName)];
98 sentences.Clear();
100 foreach (string str in system_strings)
101 sentences.Add(str, env.sysDir);
103 sentences.Add(@";^ System Sounds ^;", "");
105 foreach (string str in other_strings)
106 sentences.Add(str, env.baseDir);
109 private void saveLanguage()
111 System.IO.Directory.CreateDirectory(env.sysDir);
112 StreamWriter sw = File.CreateText(env.systemSounds);
113 int i;
114 for (i = 0; sentences[i].fileName != ""; i++)
116 sw.WriteLine(sentences[i].toRaw());
118 sw.Close();
119 sw = File.CreateText(env.otherSounds);
120 for (i++; i < sentences.Count; i++)
122 sw.WriteLine(sentences[i].toRaw());
124 sw.Close();
127 private void switchLanguage(object sender, SelectionChangedEventArgs e)
129 env = new Environment(((Language)e.AddedItems[0])); // AddedItems is a strange name. It contains the last selection
130 loadLanguage();
133 private void about(object sender, MouseButtonEventArgs e)
135 AboutWindow aboutWindow = new AboutWindow();
136 aboutWindow.ShowDialog();
139 private void addSentence(object sender, RoutedEventArgs e)
141 int i = 0;
142 string newFile;
145 newFile = "new_file_" + i.ToString();
146 i++;
148 while (env.fileExists(newFile));
149 sentences.Add(new Sentence(newFile + ";New Description;New Voice Message", env.baseDir));
151 // Extremely ugly - direct access to the listview to scroll down and select the new object
152 this.lvSentences.SelectedIndex = this.lvSentences.Items.Count - 1;
153 this.lvSentences.ScrollIntoView(this.lvSentences.SelectedItem);
156 private void play(object sender, EventArgs e)
158 if (this.lvSentences.SelectedItems.Count < 1)
159 return;
161 if (isRecording)
162 stop(null, null);
164 Sentence sentence = (Sentence)this.lvSentences.SelectedItem;
165 if (sentence.fileExists)
169 SoundPlayer player = new SoundPlayer(sentence.fullPath);
170 player.Play();
172 catch (Exception) { }; // Catch and disregard all media exceptions
176 private void record(object sender, RoutedEventArgs e)
178 if (this.lvSentences.SelectedItems.Count < 1) return;
180 if (isRecording)
181 stop(sender, e);
182 else
184 saveLanguage();
185 isRecording = true;
186 lblRecording.Visibility = System.Windows.Visibility.Visible;
187 Sentence sentence = (Sentence)this.lvSentences.SelectedItem;
188 string path = sentence.fullPath;
189 Directory.CreateDirectory(Path.GetDirectoryName(path)); // Create directory if it doesn't exist
190 System.IO.File.WriteAllText(path, ""); // Create and flush the file
192 filewriter = new WavFileWriter(path, recSamplerate, recBits, recChannels);
193 WaveFormat fmt = new WaveFormat(recSamplerate, recBits, recChannels);
194 recorder = new WaveInRecorder(-1, fmt, recBuffersize, this.DataArrived);
198 private void stop(object sender, RoutedEventArgs e)
200 if (!isRecording || this.lvSentences.SelectedItems.Count < 1)
201 return;
203 isRecording = false;
204 recorder.Close();
205 filewriter.Close();
208 int index = this.lvSentences.SelectedIndex;
209 lblRecording.Visibility = System.Windows.Visibility.Hidden;
210 Sentence sentence = (Sentence)this.lvSentences.SelectedItem;
211 loadLanguage(); // Called to refresh the sentence data of the current langugae
212 this.lvSentences.SelectedIndex = index;
214 wavProcessor processor = new wavProcessor();
215 int noiceLevel = (int)this.noiceLevelSlider.Value;
216 processor.StripSilence(sentence.fullPath, noiceLevel);
217 processor.ToneIn(sentence.fullPath);
218 processor.ToneOut(sentence.fullPath);
219 processor.SpeedUp(sentence.fullPath, (int)this.speedSlider.Value);
222 private void DataArrived(IntPtr data, int size)
224 Marshal.Copy(data, recordingBuffer, 0, size);
225 filewriter.Write(recordingBuffer, size);
229 public class Environment
231 public Language lang { get; set; }
232 public string baseDir { get { return @"SOUNDS\" + lang.sName + @"\"; } }
233 public string sysDir { get { return @"SOUNDS\" + lang.sName + @"\SYSTEM\"; } }
234 public string otherSounds { get { return baseDir + "other_sounds.txt"; } }
235 public string systemSounds { get { return sysDir + "system_sounds.txt"; } }
237 public bool fileExists(string fName)
239 if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + baseDir + fName + ".wav"))
240 return true;
241 if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + sysDir + fName + ".wav"))
242 return true;
243 return false;
246 public Environment(Language language)
248 lang = language;
252 // Data container classes
254 public class Languages : ObservableCollection<Language>
256 public void Add(string longer, string shorter)
258 this.Add(new Language { lName = longer, sName = shorter });
262 public class Language
264 public string lName { get; set; }
265 public string sName { get; set; }
268 public class Sentences : ObservableCollection<Sentence>
270 public void Add(string rawString, string dirPath)
272 this.Add(new Sentence(rawString, dirPath));
276 public class Sentence
278 public bool fileExists { get; set; }
279 public string fileName { get; set; }
280 public string description { get; set; }
281 public string voiceString { get; set; }
282 public string path { get; set; }
284 public Sentence(string rawString, string dirPath)
286 string[] words = rawString.Split(';');
287 fileName = words[0].TrimStart(' ', '\"');
288 description = words[1];
289 voiceString = words[2].TrimEnd('\"', ',', ' ');
290 path = dirPath;
291 fileExists = File.Exists(fullPath);
294 public string fullPath
296 get { return System.AppDomain.CurrentDomain.BaseDirectory + path + fileName + ".wav"; }
299 public string toRaw()
301 return "\"" + fileName + ";" + description + ";" + voiceString + "\",";