OSDN Git Service

#39441 エンコーディング指定の文字列を少し変更。全部大文字だとうまくいかないことがあった。
[dtxmania/dtxmania.git] / DTX2WAV / Form1.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using System.IO;
11 using System.Diagnostics;
12 using System.Runtime.InteropServices;
13
14 namespace DTX2WAV
15 {
16         public partial class Main : Form
17         {
18                 Form_Recording formRecording;
19                 Process p_DTXMania = null;
20                 bool bOpenedEncodingSettingTab = false;
21
22                 public Main()
23                 {
24                         InitializeComponent();
25                         bindingSource_BGM.DataSource = new VolumeSlider();
26                         bindingSource_SE.DataSource = new VolumeSlider();
27                         bindingSource_Drums.DataSource = new VolumeSlider();
28                         bindingSource_Guitar.DataSource = new VolumeSlider();
29                         bindingSource_Bass.DataSource = new VolumeSlider();
30                         bindingSource_Master.DataSource = new VolumeSlider();
31                         bindingSource_Ogg_Q.DataSource = new VolumeSlider();
32                 }
33
34                 /// <summary>
35                 /// メインウインドウの表示時に実行
36                 /// タイトルバーに、アプリ名とリリース番号を表示する
37                 /// 設定値の復元
38                 /// </summary>
39                 /// <param name="sender"></param>
40                 /// <param name="e"></param>
41                 private void Main_Shown(object sender, EventArgs e)
42                 {
43                         #region [ タイトルバーの設定 ]
44                         System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
45                         int ver_asm_major = asm.GetName().Version.Major;
46                         this.Text = "DTX2WAV Rel" + ver_asm_major.ToString("D3");
47                         #endregion
48
49                         #region [ 設定値の復元 ]
50                         // アプリのverup時は旧バージョンの設定を引き継ぐ。
51                         // さもなくば、前回終了時の設定値を引き継ぐ。
52                         if (Properties.Settings.Default.IsUpgrade == false)
53                         {
54                                 // Upgradeを実行する
55                                 Properties.Settings.Default.Upgrade();
56
57                                 // 「Upgradeを実行した」という情報を設定する
58                                 Properties.Settings.Default.IsUpgrade = true;
59
60                                 // 現行バージョンの設定を保存する
61                                 Properties.Settings.Default.Save();
62                         }
63                         else
64                         {
65                                 // 設定値の復元
66                                 Properties.Settings.Default.Reload();
67                         }
68                         #endregion
69
70                         #region [ 復元した設定値を、Formに反映する ]
71                         numericUpDown_BGM.Value            = Properties.Settings.Default.nVol_BGM;
72                         numericUpDown_SE.Value             = Properties.Settings.Default.nVol_SE;
73                         numericUpDown_Drums.Value          = Properties.Settings.Default.nVol_Drums;
74                         numericUpDown_Guitar.Value         = Properties.Settings.Default.nVol_Guitar;
75                         numericUpDown_Bass.Value           = Properties.Settings.Default.nVol_Bass;
76                         numericUpDown_Master.Value         = Properties.Settings.Default.nVol_Master;
77                         checkBox_MonitorSound.Checked      = Properties.Settings.Default.bMonitorSound;
78                         comboBox_AudioFormat.SelectedIndex = Properties.Settings.Default.nAudioFormat;
79                         checkBox_overwriteCheck.Checked    = Properties.Settings.Default.bOverwriteCheck;
80
81                         numericUpDown_Ogg_Q.Value          = Properties.Settings.Default.nOgg_Q;        // この設定は後でもう一度実施する。tabControl1_SelectedIndexChanged()へ。
82                         comboBox_MP3_bps.SelectedIndex     = Properties.Settings.Default.nMP3_bps;
83                         #endregion
84                 }
85
86                 private void button_browseDTX_Click(object sender, EventArgs e)
87                 {
88                         OpenFileDialog ofd = new OpenFileDialog();
89
90                         ofd.Filter = Properties.Resources.ofdFilter;
91                         ofd.FilterIndex = 1;
92                         ofd.Title = Properties.Resources.ofdTitle;
93
94                         ofd.RestoreDirectory = false;
95
96                         ofd.CheckFileExists = true;
97                         ofd.CheckPathExists = true;
98
99                         if (ofd.ShowDialog() == DialogResult.OK)
100                         {
101                                 textBox_BrowseDTX.Text = ofd.FileName;
102                                 UpdateOutPath(ofd.FileName);
103                         }
104                 }
105
106                 private void UpdateOutPath(string infile)
107                 {
108                         string outpath = Path.Combine(
109                                                                 Path.GetDirectoryName(infile),
110                                                                 Path.GetFileNameWithoutExtension(infile) + "." + comboBox_AudioFormat.Text.ToLower()
111                         );
112                         textBox_BrowseAudio.Text = outpath;
113                 }
114
115                 private void button_browseWAV_Click(object sender, EventArgs e)
116                 {
117                         SaveFileDialog sfd = new SaveFileDialog();
118
119                         string filter = "";
120                         switch (comboBox_AudioFormat.Text)
121                         {
122                                 case "WAV":
123                                         filter = Properties.Resources.sfdFilterWAV;
124                                         break;
125                                 case "OGG":
126                                         filter = Properties.Resources.sfdFilterOGG;
127                                         break;
128                                 case "MP3":
129                                         filter = Properties.Resources.sfdFilterMP3;
130                                         break;
131                                 default:
132                                         filter = Properties.Resources.sfdFilterALL;
133                                         break;
134                         }
135                         sfd.Filter = filter;
136                         sfd.FilterIndex = 1;
137                         sfd.DefaultExt = "." + comboBox_AudioFormat.Text.ToLower();
138                         sfd.Title = Properties.Resources.sfdTitle;
139
140                         sfd.RestoreDirectory = false;
141                         sfd.AddExtension = true;
142
143                         sfd.OverwritePrompt = true;
144                         sfd.CheckFileExists = false;
145                         sfd.CheckPathExists = false;
146
147                         if (sfd.ShowDialog() == DialogResult.OK)
148                         {
149                                 textBox_BrowseAudio.Text = sfd.FileName;
150                         }
151                 }
152
153
154                 /// <summary>
155                 /// 変換を実行。DTXManiaGRをDTX2WAVモードで呼び出す。
156                 /// </summary>
157                 /// <param name="sender"></param>
158                 /// <param name="e"></param>
159                 private void button_Convert_Click(object sender, EventArgs e)
160                 {
161                         #region [ in/outファイル名など、必要な設定がなされているかをチェック ]
162                         if (!File.Exists(textBox_BrowseDTX.Text))
163                         {
164                                 MessageBox.Show(Properties.Resources.errNoDTXFileText, Properties.Resources.errNoDTXFileCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
165                                 return;
166                         }
167                         if (textBox_BrowseAudio.Text == "")
168                         {
169                                 MessageBox.Show(Properties.Resources.errNoOutFileText, Properties.Resources.errNoOutFileCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
170                                 return;
171                         }
172
173                         string inext = Path.GetExtension(textBox_BrowseAudio.Text).ToLower();
174                         if (inext == ".dtx" || inext == ".gda" || inext == ".g2d")
175                         {
176                                 MessageBox.Show(Properties.Resources.errIllegalExtentionText, Properties.Resources.errIllegalExtentionCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
177                                 return;
178                         }
179                         if (checkBox_overwriteCheck.Checked)
180                         {
181                                 if (File.Exists(textBox_BrowseAudio.Text))
182                                 {
183                                         DialogResult result = MessageBox.Show(Properties.Resources.confirmOverwriteText, Properties.Resources.confirmOverwriteCaption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
184                                         if (result == DialogResult.Cancel || result == DialogResult.No)
185                                         {
186                                                 return;
187                                         }
188                                 }
189                         }
190                         #endregion
191
192                         #region [ 録音用にDTXManiaプロセスを起動する ]
193                         p_DTXMania = new System.Diagnostics.Process();
194
195                         //イベントハンドラがフォームを作成したスレッドで実行されるようにする
196                         p_DTXMania.SynchronizingObject = this;
197                         //イベントハンドラの追加
198                         p_DTXMania.Exited += new EventHandler(p_Exited);
199                         p_DTXMania.EnableRaisingEvents = true;
200
201                         //アプリ名と引数の情報を設定
202                         p_DTXMania.StartInfo.FileName = "DTXManiaGR.exe";
203                         p_DTXMania.StartInfo.Arguments  = $"-E{comboBox_AudioFormat.Text.ToUpper()},";
204                         p_DTXMania.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
205
206                         switch (comboBox_AudioFormat.Text.ToUpper())
207                         {
208                                 case "WAV":
209                                         p_DTXMania.StartInfo.Arguments +=  "48000,192,";        // freqとbitrate、DTXMania側ではいずれも無視される
210                                         break;
211                                 case "OGG":
212                                         p_DTXMania.StartInfo.Arguments += $"48000,{numericUpDown_Ogg_Q.Value},";
213                                         break;
214                                 case "MP3":
215                                         p_DTXMania.StartInfo.Arguments += $"48000,{comboBox_MP3_bps.Text},";
216                                         break;
217                                 default:
218                                         p_DTXMania.StartInfo.Arguments +=  "48000,192,";
219                                         break;
220                         }
221
222                         p_DTXMania.StartInfo.Arguments += $"{numericUpDown_BGM.Value},{numericUpDown_SE.Value},{numericUpDown_Drums.Value},{numericUpDown_Guitar.Value},{numericUpDown_Bass.Value},{numericUpDown_Master.Value},";
223                         p_DTXMania.StartInfo.Arguments += $"\"{textBox_BrowseAudio.Text}\",\"{textBox_BrowseDTX.Text}\"";
224
225                         //起動する
226                         try
227                         {
228                                 p_DTXMania.Start();
229                         }
230                         catch (Exception)
231                         {
232                                 MessageBox.Show(Properties.Resources.errFailedLaunchingDTXManiaText, Properties.Resources.errFailedLaunchingDTXManiaCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
233                                 p_DTXMania.Close();
234                                 p_DTXMania.Dispose();
235                                 p_DTXMania = null;
236                                 return;
237                         }
238                         #endregion
239
240                         //モーダルで変換中ダイアログを表示して、処理をいったん止める(キャンセル or 正常終了イベント待ち)
241                         formRecording = new Form_Recording();
242                         //formRecording.StartPosition = FormStartPosition.CenterParent;
243                         formRecording.ShowDialog(this);
244                         this.Activate();
245
246                         if (formRecording != null)
247                         {
248                                 //フォームでCancelボタンが押されると、ここに来る
249                                 //変換終了時のイベントで正常終了する場合は、p_Exited()で終了して、fがnullになって、ここはスキップされる
250                                 formRecording.Dispose();
251                                 formRecording = null;
252                         }
253                 }
254
255                 private void p_Exited(object sender, EventArgs e)
256                 {
257                         if (formRecording != null)
258                         {
259                                 formRecording.Dispose();
260                                 formRecording = null;
261                         }
262                         
263                         // DTXManiaプロセスの返り値を確認し、それに応じたダイアログを表示する
264                         if (p_DTXMania != null)
265                         {
266                                 //MessageBox.Show(p_DTXMania.ExitCode.ToString());
267
268                                 switch (p_DTXMania.ExitCode)
269                                 {
270                                         case 0:         // 正常終了
271                                                 using (var f = new Form_Finished_OK())
272                                                 {
273                                                         f.ShowDialog(this);
274                                                 }
275                                                 break;
276
277                                         case 10010:             // Cancel
278                                                 using (var f = new Form_Finished_Fail())
279                                                 {
280                                                         f.ShowDialog(this);
281                                                 }
282                                                 break;
283
284                                         default:                // DTXMania本体を強制終了した、など
285                                                 break;
286
287                                 }
288                         }
289                         p_DTXMania.Close();
290                         p_DTXMania.Dispose();
291                         p_DTXMania = null;
292                 }
293
294                 /// <summary>
295                 /// Cancel押下時は、アプリ終了
296                 /// </summary>
297                 /// <param name="sender"></param>
298                 /// <param name="e"></param>
299                 private void button_Cancel_Click(object sender, EventArgs e)
300                 {
301                         Application.Exit();
302                 }
303
304
305                 /// <summary>
306                 /// 出力フォーマットを変更した場合は、出力パスの拡張子も更新する
307                 /// </summary>
308                 /// <param name="sender"></param>
309                 /// <param name="e"></param>
310                 private void comboBox_AudioFormat_SelectedIndexChanged(object sender, EventArgs e)
311                 {
312                         string outpath = textBox_BrowseAudio.Text;
313                         if (outpath == "")
314                         {
315                                 return;
316                         }
317
318                         textBox_BrowseAudio.Text =
319                                 Path.Combine(
320                                         Path.GetDirectoryName(outpath),
321                                         Path.GetFileNameWithoutExtension(outpath) + "." + comboBox_AudioFormat.Text.ToLower()
322                         );
323                 }
324
325                 
326                 
327                 
328                 /// <summary>
329                 /// NumericUpDown とTrackBar にバインドされるデータ
330                 /// </summary>
331                 public class VolumeSlider : INotifyPropertyChanged
332                 {
333                         /// <summary>
334                         /// INotifyPropertyChanged から継承したイベントデリゲート
335                         /// </summary>
336                         public event PropertyChangedEventHandler PropertyChanged;
337                         /// <summary>
338                         /// イベント通知
339                         /// </summary>
340                         /// <param name="info"></param>
341                         private void NotifyPropertyChanged(String info)
342                         {
343                                 if (PropertyChanged != null)
344                                 {
345                                         PropertyChanged(this, new PropertyChangedEventArgs(info));
346                                 }
347                         }
348                         int _value;
349                         public int Value
350                         {
351                                 get { return _value; }
352                                 set
353                                 {
354                                         if (value != _value)
355                                         {
356                                                 _value = value;
357                                                 // このプロパティ名を渡してイベント通知
358                                                 NotifyPropertyChanged("Value");
359                                         }
360                                 }
361                         }
362
363                 }
364
365                 /// <summary>
366                 /// アプリ終了時に、設定値を保存
367                 /// </summary>
368                 /// <param name="sender"></param>
369                 /// <param name="e"></param>
370                 private void Main_FormClosing(object sender, FormClosingEventArgs e)
371                 {
372
373                         #region [ Formの設定値を、Propertiesに退避する ]
374                         Properties.Settings.Default.nVol_BGM      = (int)numericUpDown_BGM.Value;
375                         Properties.Settings.Default.nVol_SE       = (int)numericUpDown_SE.Value;
376                         Properties.Settings.Default.nVol_Drums    = (int)numericUpDown_Drums.Value;
377                         Properties.Settings.Default.nVol_Guitar   = (int)numericUpDown_Guitar.Value;
378                         Properties.Settings.Default.nVol_Bass     = (int)numericUpDown_Bass.Value;
379                         Properties.Settings.Default.nVol_Master   = (int)numericUpDown_Master.Value;
380                         Properties.Settings.Default.bMonitorSound = checkBox_MonitorSound.Checked;
381                         Properties.Settings.Default.nAudioFormat  = comboBox_AudioFormat.SelectedIndex;
382                         Properties.Settings.Default.nOgg_Q        = (int)numericUpDown_Ogg_Q.Value;
383                         Properties.Settings.Default.nMP3_bps      = comboBox_MP3_bps.SelectedIndex;
384                         Properties.Settings.Default.bOverwriteCheck = checkBox_overwriteCheck.Checked;
385                         #endregion
386
387                         Properties.Settings.Default.Save();
388                 }
389
390
391                 #region #28821 2014.1.23 yyagi add: 外部からの文字列メッセージ送受信 定数定義
392                 [StructLayout(LayoutKind.Sequential)]
393                 public struct COPYDATASTRUCT
394                 {
395                         public IntPtr dwData;
396                         public UInt32 cbData;
397                         public IntPtr lpData;
398                 }
399                 #endregion
400                 /// <summary>
401                 /// メッセージを受信する
402                 /// </summary>
403                 /// <param name="m"></param>
404                 protected override void WndProc(ref Message m)
405                 {
406                         if (m.Msg == 0x004A) //WM_COPYDATA
407                         {
408                                 COPYDATASTRUCT cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
409                                 string strMessage = Marshal.PtrToStringUni(cds.lpData);
410                                 formRecording.label_state.Text = strMessage;    // Form_Recordingにメッセージの内容を伝える
411                         }
412                         base.WndProc(ref m);
413                 }
414
415
416
417                 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
418                 {
419                         // Encoding Settingsタブにある、numericUpDownかTrackbarの値は、
420                         // Form_Shown()で初期化した値が、
421                         // タブの初回オープン時にゼロクリアされてしまう模様。
422                         // そのため、この値だけは、タブの初回オープン時に初期化する。
423                         if (tabControl1.SelectedIndex == 1 && bOpenedEncodingSettingTab == false)
424                         {
425                                 bOpenedEncodingSettingTab = true;
426                                 numericUpDown_Ogg_Q.Value = Properties.Settings.Default.nOgg_Q;
427                         }
428                 }
429
430                 /// <summary>
431                 /// 入力DTX欄にファイルをドラッグしたときの動作 (マウスカーソルを変更する)
432                 /// </summary>
433                 /// <param name="sender"></param>
434                 /// <param name="e"></param>
435                 private void textBox_BrowseDTX_DragEnter(object sender, DragEventArgs e)
436                 {
437                         //コントロール内にドラッグされたとき実行される
438                         if (e.Data.GetDataPresent(DataFormats.FileDrop))
439                         {
440                                 //ドラッグされたデータ形式を調べ、ファイル1個のときはコピーとする
441                                 string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
442                                 if (fileName.Length == 1 &&
443                                         (Path.GetExtension(fileName[0]).ToLower() == ".dtx" ||
444                                          Path.GetExtension(fileName[0]).ToLower() == ".gda" ||
445                                          Path.GetExtension(fileName[0]).ToLower() == ".g2d"
446                                         ))
447                                 {
448                                         e.Effect = DragDropEffects.Copy;
449                                 }
450                                 else
451                                 {
452                                         e.Effect = DragDropEffects.None;                //ファイルが複数ある場合は受け付けない
453                                 }
454                         }
455                         else
456                         {
457                                 e.Effect = DragDropEffects.None;                        //ファイル以外は受け付けない
458                         }
459                 }
460
461                 private void textBox_BrowseDTX_DragDrop(object sender, DragEventArgs e)
462                 {
463                         //コントロール内にドロップされたとき実行される
464                         //ドロップされたファイル名(1個)を取得する
465                         string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
466                         textBox_BrowseDTX.Text = fileName[0];
467
468                         UpdateOutPath(fileName[0]);
469                 }
470         }
471         
472 }
473