2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace Handbrake.Controls
\r
9 using System.Collections;
\r
10 using System.Collections.Generic;
\r
11 using System.Globalization;
\r
13 using System.Windows.Forms;
\r
16 using HandBrake.ApplicationServices.Model;
\r
17 using HandBrake.ApplicationServices.Parsing;
\r
18 using HandBrake.ApplicationServices.Utilities;
\r
20 using AudioTrack = HandBrake.ApplicationServices.Model.Encoding.AudioTrack;
\r
23 /// The AudioPanel Control
\r
25 public partial class AudioPanel : UserControl
\r
27 private static readonly CultureInfo Culture = new CultureInfo("en-US", false);
\r
29 private const string AC3Passthru = "AC3 Passthru";
\r
31 private const string DTSPassthru = "DTS Passthru";
\r
34 /// Initializes a new instance of the <see cref="AudioPanel"/> class.
\r
35 /// Create a new instance of the Audio Panel
\r
39 InitializeComponent();
\r
40 drp_audioMix.SelectedItem = "Dolby Pro Logic II";
\r
41 drp_audioSample.SelectedIndex = 1;
\r
45 /// The audio list has changed
\r
47 public event EventHandler AudioListChanged;
\r
50 /// Get the audio panel
\r
52 /// <returns>A listview containing the audio tracks</returns>
\r
53 public DataGridView GetAudioPanel()
\r
59 /// Set the File Container. This funciton is used to limit the available options for each file container.
\r
61 /// <param name="path">
\r
64 public void SetContainer(string path)
\r
66 string oldval = drp_audioEncoder.Text;
\r
68 drp_audioEncoder.Items.Clear();
\r
69 drp_audioEncoder.Items.Add("AAC (faac)");
\r
70 drp_audioEncoder.Items.Add("MP3 (lame)");
\r
71 drp_audioEncoder.Items.Add(AC3Passthru);
\r
72 drp_audioEncoder.Items.Add("AC3 (ffmpeg)");
\r
74 if (path.Contains("MKV"))
\r
76 drp_audioEncoder.Items.Add(DTSPassthru);
\r
77 drp_audioEncoder.Items.Add("Vorbis (vorbis)");
\r
80 if (!drp_audioEncoder.Text.Contains(oldval))
\r
81 drp_audioEncoder.SelectedIndex = 0;
\r
83 drp_audioEncoder.SelectedItem = oldval;
\r
85 // Make sure the table is updated with new audio codecs
\r
86 // Defaults to AAC encoding.
\r
87 foreach (DataGridViewRow row in audioList.Rows)
\r
89 if (!drp_audioEncoder.Items.Contains(row.Cells[2].Value))
\r
90 row.Cells[2].Value = drp_audioEncoder.Items[0].ToString();
\r
95 /// Checks if the settings used required the .m4v (rather than .mp4) extension
\r
97 /// <returns>True if m4v is required</returns>
\r
98 public bool RequiresM4V()
\r
100 return this.audioList.Rows.Cast<DataGridViewRow>().Any(row => row.Cells[2].Value.ToString().Contains("AC3"));
\r
104 /// Load an arraylist of AudioTrack items into the list.
\r
106 /// <param name="audioTracks">List of audio tracks</param>
\r
107 public void LoadTracks(List<AudioTrack> audioTracks)
\r
111 if (audioTracks == null)
\r
114 foreach (AudioTrack track in audioTracks)
\r
116 DataGridViewRow newTrack = new DataGridViewRow();
\r
117 newTrack.CreateCells(audioList);
\r
118 newTrack.Cells[0].Value = GetNewID().ToString();
\r
119 newTrack.Cells[1].Value = "Automatic";
\r
120 newTrack.Cells[2].Value = track.Encoder;
\r
121 newTrack.Cells[3].Value = track.MixDown;
\r
122 newTrack.Cells[4].Value = track.SampleRate;
\r
123 newTrack.Cells[5].Value = track.Encoder.Contains(AC3Passthru) || track.Encoder.Contains(DTSPassthru) ? "Auto" : track.Bitrate;
\r
124 newTrack.Cells[6].Value = track.DRC;
\r
125 AddTrackForPreset(newTrack);
\r
128 this.AutomaticTrackSelection();
\r
132 /// Set the Track list dropdown from the parsed title captured during the scan
\r
134 /// <param name="selectedTitle">The selected title</param>
\r
135 /// <param name="preset">A preset</param>
\r
136 public void SetTrackListFromPreset(Title selectedTitle, Preset preset)
\r
138 if (selectedTitle.AudioTracks.Count == 0)
\r
140 audioList.Rows.Clear();
\r
141 drp_audioTrack.Items.Clear();
\r
142 drp_audioTrack.Items.Add("None Found");
\r
143 drp_audioTrack.SelectedIndex = 0;
\r
147 // Setup the Audio track source dropdown with the new audio tracks.
\r
148 drp_audioTrack.Items.Clear();
\r
149 drp_audioTrack.Items.AddRange(selectedTitle.AudioTracks.ToArray());
\r
151 // Add any tracks the preset has, if there is a preset and no audio tracks in the list currently
\r
152 if (audioList.Rows.Count == 0 && preset != null)
\r
154 EncodeTask parsed = QueryParserUtility.Parse(preset.Query);
\r
155 foreach (AudioTrack audioTrack in parsed.AudioTracks)
\r
157 DataGridViewRow newTrack = new DataGridViewRow();
\r
158 newTrack.CreateCells(audioList);
\r
159 newTrack.Cells[0].Value = GetNewID().ToString();
\r
160 newTrack.Cells[1].Value = (audioTrack.Track);
\r
161 newTrack.Cells[2].Value = (audioTrack.Encoder);
\r
162 newTrack.Cells[3].Value = (audioTrack.MixDown);
\r
163 newTrack.Cells[4].Value = (audioTrack.SampleRate);
\r
164 newTrack.Cells[5].Value = (audioTrack.Bitrate);
\r
165 newTrack.Cells[6].Value = (audioTrack.DRC);
\r
166 audioList.Rows.Add(newTrack);
\r
170 this.AutomaticTrackSelection();
\r
173 private void AutomaticTrackSelection()
\r
175 // Handle Native Language and "Dub Foreign language audio" and "Use Foreign language audio and Subtitles" Options
\r
176 if (Properties.Settings.Default.NativeLanguage == "Any")
\r
178 drp_audioTrack.SelectedIndex = 0;
\r
179 foreach (DataGridViewRow item in audioList.Rows)
\r
181 if (this.drp_audioTrack.SelectedItem != null)
\r
183 item.Cells[1].Value = this.drp_audioTrack.SelectedItem.ToString();
\r
189 int mode = Properties.Settings.Default.DubMode;
\r
194 // Dub Foreign Language Audio
\r
195 // Select the prefered language audio, or the first track if it doesn't exist.
\r
197 foreach (object item in drp_audioTrack.Items)
\r
199 if (item.ToString().Contains(Properties.Settings.Default.NativeLanguage))
\r
201 drp_audioTrack.SelectedIndex = i;
\r
208 if (drp_audioTrack.SelectedItem != null)
\r
209 foreach (DataGridViewRow item in audioList.Rows)
\r
210 item.Cells[1].Value = drp_audioTrack.SelectedItem.ToString();
\r
213 drp_audioTrack.SelectedIndex = 0;
\r
214 if (drp_audioTrack.SelectedItem != null)
\r
215 foreach (DataGridViewRow item in audioList.Rows)
\r
216 item.Cells[1].Value = drp_audioTrack.SelectedItem.ToString();
\r
222 // Select the first track which is hopefully the default and foreign track.
\r
223 drp_audioTrack.SelectedIndex = 0;
\r
225 if (drp_audioTrack.SelectedItem != null)
\r
226 foreach (DataGridViewRow item in audioList.Rows)
\r
227 item.Cells[1].Value = drp_audioTrack.SelectedItem.ToString();
\r
233 // Control and ListView
\r
236 /// one of the controls has changed. Event handler
\r
238 /// <param name="sender">
\r
241 /// <param name="e">
\r
244 private void ControlChanged(object sender, EventArgs e)
\r
246 Control ctl = (Control)sender;
\r
250 case "drp_audioTrack":
\r
251 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
253 audioList.SelectedRows[0].Cells[1].Value = drp_audioTrack.Text;
\r
255 // If the track isn't AC3, and the encoder is, change it.
\r
256 if (audioList.SelectedRows[0].Cells[2].Value.ToString().Contains(AC3Passthru) && !audioList.SelectedRows[0].Cells[1].Value.ToString().Contains("(AC3)"))
\r
259 drp_audioEncoder.SelectedIndex = 0;
\r
262 // If the track isn't DTS, and the encoder is, change it.
\r
263 if (audioList.SelectedRows[0].Cells[2].Value.ToString().Contains(DTSPassthru) && !audioList.SelectedRows[0].Cells[1].Value.ToString().Contains("DTS"))
\r
266 drp_audioEncoder.SelectedIndex = 0;
\r
270 case "drp_audioEncoder":
\r
274 // Configure the widgets with values
\r
275 if (drp_audioEncoder.Text.Contains(AC3Passthru) || drp_audioEncoder.Text.Contains(DTSPassthru))
\r
277 drp_audioMix.Enabled =
\r
278 drp_audioBitrate.Enabled = drp_audioSample.Enabled = tb_drc.Enabled = false;
\r
279 lbl_bitrate.Enabled =
\r
281 lbl_drcHeader.Enabled = lbl_mixdown.Enabled = lbl_sampleRate.Enabled = false;
\r
285 drp_audioMix.Enabled =
\r
286 drp_audioBitrate.Enabled = drp_audioSample.Enabled = tb_drc.Enabled = true;
\r
287 lbl_bitrate.Enabled =
\r
289 lbl_drcHeader.Enabled = lbl_mixdown.Enabled = lbl_sampleRate.Enabled = true;
\r
292 // Update an item in the Audio list if required.
\r
293 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
294 audioList.SelectedRows[0].Cells[2].Value = drp_audioEncoder.Text;
\r
296 case "drp_audioMix":
\r
299 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
300 audioList.SelectedRows[0].Cells[3].Value = drp_audioMix.Text;
\r
302 case "drp_audioSample":
\r
303 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
304 audioList.SelectedRows[0].Cells[4].Value = drp_audioSample.Text;
\r
306 case "drp_audioBitrate":
\r
307 // Update an item in the Audio list if required.
\r
308 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
309 audioList.SelectedRows[0].Cells[5].Value = drp_audioBitrate.Text;
\r
313 if (tb_drc.Value == 0) value = 0;
\r
315 value = ((tb_drc.Value - 1) / 10.0) + 1;
\r
317 lbl_drc.Text = value.ToString();
\r
319 // Update an item in the Audio list if required.
\r
320 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
322 audioList.SelectedRows[0].Cells[6].Value = value.ToString();
\r
323 audioList.Select();
\r
328 audioList.Select();
\r
332 /// The Audio List selected index changed event handler
\r
334 /// <param name="sender">
\r
337 /// <param name="e">
\r
340 private void audioList_SelectionChanged(object sender, EventArgs e)
\r
342 // Set the dropdown controls based on the selected item in the Audio List.
\r
343 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
345 drp_audioTrack.SelectedItem = audioList.SelectedRows[0].Cells[1].Value;
\r
346 drp_audioEncoder.SelectedItem = audioList.SelectedRows[0].Cells[2].Value;
\r
347 drp_audioMix.SelectedItem = audioList.SelectedRows[0].Cells[3].Value;
\r
348 drp_audioSample.SelectedItem = audioList.SelectedRows[0].Cells[4].Value;
\r
349 drp_audioBitrate.SelectedItem = audioList.SelectedRows[0].Cells[5].Value;
\r
352 double.TryParse(audioList.SelectedRows[0].Cells[6].Value.ToString(), out drcValue);
\r
354 drcValue = ((drcValue * 10) + 1) - 10;
\r
355 int.TryParse(drcValue.ToString(Culture), out drcCalculated);
\r
356 tb_drc.Value = drcCalculated;
\r
357 lbl_drc.Text = audioList.SelectedRows[0].Cells[6].Value.ToString();
\r
359 AudioTrackGroup.Text = "Selected Track: " + audioList.SelectedRows[0].Cells[0].Value;
\r
362 AudioTrackGroup.Text = "Selected Track: None (Click \"Add Track\" to add)";
\r
368 /// The Add Audio Track button event handler
\r
370 /// <param name="sender">
\r
373 /// <param name="e">
\r
376 private void btn_addAudioTrack_Click(object sender, EventArgs e)
\r
378 if (drp_audioTrack.Text == "None Found")
\r
381 "Your source appears to have no audio tracks that HandBrake supports.",
\r
383 MessageBoxButtons.OK,
\r
384 MessageBoxIcon.Warning);
\r
389 if (tb_drc.Value != 0)
\r
390 value = ((tb_drc.Value - 1) / 10.0) + 1;
\r
392 // Create a new row for the Audio list based on the currently selected items in the dropdown.
\r
393 DataGridViewRow newTrack = new DataGridViewRow();
\r
394 newTrack.CreateCells(audioList);
\r
395 newTrack.Cells[0].Value = GetNewID().ToString();
\r
396 newTrack.Cells[1].Value = drp_audioTrack.Text;
\r
397 newTrack.Cells[2].Value = drp_audioEncoder.Text;
\r
398 newTrack.Cells[3].Value = drp_audioMix.Text;
\r
399 newTrack.Cells[4].Value = drp_audioSample.Text;
\r
400 newTrack.Cells[5].Value = drp_audioBitrate.Text;
\r
401 newTrack.Cells[6].Value = value.ToString();
\r
402 audioList.Rows.Add(newTrack);
\r
404 // The Audio List has changed to raise the event.
\r
405 if (this.AudioListChanged != null)
\r
406 this.AudioListChanged(this, new EventArgs());
\r
408 // Select the newly added track and select the control
\r
409 audioList.ClearSelection();
\r
410 audioList.Rows[audioList.Rows.Count - 1].Selected = true;
\r
411 audioList.Select();
\r
415 /// The Remove Track button event handler
\r
417 /// <param name="sender">
\r
420 /// <param name="e">
\r
423 private void btn_RemoveAudioTrack_Click(object sender, EventArgs e)
\r
431 /// The Audio List Move Up menu item
\r
433 /// <param name="sender">
\r
436 /// <param name="e">
\r
439 private void audioList_moveup_Click(object sender, EventArgs e)
\r
445 /// The audio list move down menu item
\r
447 /// <param name="sender">
\r
450 /// <param name="e">
\r
453 private void audioList_movedown_Click(object sender, EventArgs e)
\r
459 /// The audio list remove menu item
\r
461 /// <param name="sender">
\r
464 /// <param name="e">
\r
467 private void audioList_remove_Click(object sender, EventArgs e)
\r
472 // Private Functions
\r
475 /// Add track for preset
\r
477 /// <param name="item">
\r
480 private void AddTrackForPreset(DataGridViewRow item)
\r
482 audioList.Rows.Add(item);
\r
483 if (this.AudioListChanged != null)
\r
484 this.AudioListChanged(this, new EventArgs());
\r
488 /// Clear the audio list
\r
490 private void ClearAudioList()
\r
492 audioList.Rows.Clear();
\r
493 if (this.AudioListChanged != null)
\r
494 this.AudioListChanged(this, new EventArgs());
\r
498 /// Get a new ID for the next audio track
\r
503 private int GetNewID()
\r
505 return audioList.Rows.Count + 1;
\r
509 /// Remove an audio track from the list
\r
511 private void RemoveTrack()
\r
513 // Remove the Item and reselect the control if the following conditions are met.
\r
514 if (audioList.SelectedRows.Count != 0)
\r
516 // The Audio List is about to change so raise the event.
\r
517 if (this.AudioListChanged != null)
\r
518 this.AudioListChanged(this, new EventArgs());
\r
520 // Record the current selected index.
\r
521 int currentPosition = audioList.SelectedRows[0].Index;
\r
523 audioList.Rows.Remove(audioList.SelectedRows[0]);
\r
525 // Now reslect the correct item and give focus to the audio list.
\r
526 if (audioList.Rows.Count != 0)
\r
528 audioList.ClearSelection();
\r
529 if (currentPosition <= (audioList.Rows.Count - 1))
\r
530 audioList.Rows[currentPosition].Selected = true;
\r
531 else if (currentPosition > (audioList.Rows.Count - 1))
\r
532 audioList.Rows[audioList.Rows.Count - 1].Selected = true;
\r
534 audioList.Select();
\r
536 // Regenerate the ID numers
\r
537 ReGenerateListIDs();
\r
542 /// Move an audio track up or down the audio list
\r
544 /// <param name="up">
\r
547 private void MoveTrack(bool up)
\r
549 if (audioList.SelectedRows.Count == 0) return;
\r
551 DataGridViewRow item = audioList.SelectedRows[0];
\r
552 int index = item.Index;
\r
557 if (index < audioList.Rows.Count || (audioList.Rows.Count > index && index >= 0))
\r
559 audioList.Rows.Remove(item);
\r
560 audioList.Rows.Insert(index, item);
\r
561 audioList.ClearSelection();
\r
562 item.Selected = true;
\r
567 /// Regenerate all the audio track id's on the audio list
\r
569 private void ReGenerateListIDs()
\r
572 foreach (DataGridViewRow item in audioList.Rows)
\r
574 item.Cells[0].Value = i.ToString();
\r
580 /// Set the bitrate dropdown
\r
582 private void SetBitrate()
\r
585 drp_audioBitrate.Items.Remove("Auto");
\r
586 drp_audioBitrate.Items.Remove("192");
\r
587 drp_audioBitrate.Items.Remove("224");
\r
588 drp_audioBitrate.Items.Remove("256");
\r
589 drp_audioBitrate.Items.Remove("320");
\r
590 drp_audioBitrate.Items.Remove("384");
\r
591 drp_audioBitrate.Items.Remove("448");
\r
592 drp_audioBitrate.Items.Remove("640");
\r
593 drp_audioBitrate.Items.Remove("768");
\r
595 switch (drp_audioEncoder.Text)
\r
598 max = drp_audioMix.Text.Contains("6 Channel") ? 768 : 320;
\r
603 case "Vorbis (vorbis)":
\r
606 case "AC3 (ffmpeg)":
\r
610 drp_audioBitrate.Items.Add("Auto");
\r
611 drp_audioBitrate.SelectedItem = "Auto";
\r
612 drp_audioSample.SelectedItem = "Auto";
\r
615 drp_audioBitrate.Items.Add("Auto");
\r
616 drp_audioBitrate.SelectedItem = "Auto";
\r
617 drp_audioSample.SelectedItem = "Auto";
\r
623 drp_audioBitrate.Items.Add("192");
\r
624 drp_audioBitrate.Items.Add("224");
\r
625 drp_audioBitrate.Items.Add("256");
\r
626 drp_audioBitrate.Items.Add("320");
\r
631 drp_audioBitrate.Items.Add("384");
\r
636 drp_audioBitrate.Items.Add("448");
\r
637 drp_audioBitrate.Items.Add("640");
\r
642 drp_audioBitrate.Items.Add("768");
\r
645 if (drp_audioBitrate.SelectedItem == null && audioList.SelectedRows.Count == 0)
\r
646 drp_audioBitrate.SelectedIndex = drp_audioBitrate.Items.Count - 1;
\r
648 if (drp_audioBitrate.SelectedItem == null && audioList.SelectedRows.Count != 0 && (string)this.audioList.SelectedRows[0].Cells[5].Value == "Auto")
\r
650 drp_audioBitrate.SelectedItem = "160";
\r
655 /// Set the mixdown dropdown
\r
657 private void SetMixDown()
\r
659 drp_audioMix.Items.Clear();
\r
660 drp_audioMix.Items.Add("Mono");
\r
661 drp_audioMix.Items.Add("Stereo");
\r
662 drp_audioMix.Items.Add("Dolby Surround");
\r
663 drp_audioMix.Items.Add("Dolby Pro Logic II");
\r
664 drp_audioMix.Items.Add("6 Channel Discrete");
\r
665 drp_audioMix.Items.Add(AC3Passthru);
\r
666 drp_audioMix.Items.Add(DTSPassthru);
\r
668 drp_audioMix.SelectedItem = "Dolby Pro Logic II";
\r
670 switch (drp_audioEncoder.Text)
\r
673 drp_audioMix.Items.Remove(AC3Passthru);
\r
674 drp_audioMix.Items.Remove(DTSPassthru);
\r
677 drp_audioMix.Items.Remove("6 Channel Discrete");
\r
678 drp_audioMix.Items.Remove(AC3Passthru);
\r
679 drp_audioMix.Items.Remove(DTSPassthru);
\r
681 case "Vorbis (vorbis)":
\r
682 drp_audioMix.Items.Remove(AC3Passthru);
\r
683 drp_audioMix.Items.Remove(DTSPassthru);
\r
685 case "AC3 (ffmpeg)":
\r
686 drp_audioMix.Items.Remove(AC3Passthru);
\r
687 drp_audioMix.Items.Remove(DTSPassthru);
\r
689 case "AC3 Passthru":
\r
690 drp_audioMix.SelectedItem = AC3Passthru;
\r
692 case "DTS Passthru":
\r
693 drp_audioMix.SelectedItem = DTSPassthru;
\r