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
11 using System.Windows.Forms;
\r
14 using HandBrake.ApplicationServices.Parsing;
\r
17 using AudioTrack = Model.AudioTrack;
\r
20 /// The AudioPanel Control
\r
22 public partial class AudioPanel : UserControl
\r
25 private const string AC3Passthru = "AC3 Passthru";
\r
27 private const string DTSPassthru = "DTS Passthru";
\r
30 /// Initializes a new instance of the <see cref="AudioPanel"/> class.
\r
31 /// Create a new instance of the Audio Panel
\r
35 InitializeComponent();
\r
36 drp_audioMix.SelectedItem = "Dolby Pro Logic II";
\r
37 drp_audioSample.SelectedIndex = 1;
\r
41 /// The audio list has changed
\r
43 public event EventHandler AudioListChanged;
\r
46 /// Get the audio panel
\r
48 /// <returns>A listview containing the audio tracks</returns>
\r
49 public DataGridView GetAudioPanel()
\r
55 /// Set the File Container. This funciton is used to limit the available options for each file container.
\r
57 /// <param name="path">
\r
60 public void SetContainer(string path)
\r
62 string oldval = drp_audioEncoder.Text;
\r
64 drp_audioEncoder.Items.Clear();
\r
65 drp_audioEncoder.Items.Add("AAC (faac)");
\r
66 drp_audioEncoder.Items.Add("MP3 (lame)");
\r
67 drp_audioEncoder.Items.Add(AC3Passthru);
\r
68 drp_audioEncoder.Items.Add("AC3 (ffmpeg)");
\r
70 if (path.Contains("MKV"))
\r
72 drp_audioEncoder.Items.Add(DTSPassthru);
\r
73 drp_audioEncoder.Items.Add("Vorbis (vorbis)");
\r
76 if (!drp_audioEncoder.Text.Contains(oldval))
\r
77 drp_audioEncoder.SelectedIndex = 0;
\r
79 drp_audioEncoder.SelectedItem = oldval;
\r
81 // Make sure the table is updated with new audio codecs
\r
82 // Defaults to AAC encoding.
\r
83 foreach (DataGridViewRow row in audioList.Rows)
\r
85 if (!drp_audioEncoder.Items.Contains(row.Cells[2].Value))
\r
86 row.Cells[2].Value = drp_audioEncoder.Items[0].ToString();
\r
91 /// Checks if the settings used required the .m4v (rather than .mp4) extension
\r
93 /// <returns>True if m4v is required</returns>
\r
94 public bool RequiresM4V()
\r
96 return this.audioList.Rows.Cast<DataGridViewRow>().Any(row => row.Cells[2].Value.ToString().Contains("AC3"));
\r
100 /// Load an arraylist of AudioTrack items into the list.
\r
102 /// <param name="audioTracks">List of audio tracks</param>
\r
103 public void LoadTracks(ArrayList audioTracks)
\r
107 if (audioTracks == null)
\r
110 foreach (AudioTrack track in audioTracks)
\r
112 DataGridViewRow newTrack = new DataGridViewRow();
\r
113 newTrack.CreateCells(audioList);
\r
114 newTrack.Cells[0].Value = GetNewID().ToString();
\r
115 newTrack.Cells[1].Value = "Automatic";
\r
116 newTrack.Cells[2].Value = track.Encoder;
\r
117 newTrack.Cells[3].Value = track.MixDown;
\r
118 newTrack.Cells[4].Value = track.SampleRate;
\r
119 newTrack.Cells[5].Value = track.Encoder.Contains(AC3Passthru) || track.Encoder.Contains(DTSPassthru) ? "Auto" : track.Bitrate;
\r
120 newTrack.Cells[6].Value = track.DRC;
\r
121 AddTrackForPreset(newTrack);
\r
124 this.AutomaticTrackSelection();
\r
128 /// Set the Track list dropdown from the parsed title captured during the scan
\r
130 /// <param name="selectedTitle">The selected title</param>
\r
131 /// <param name="preset">A preset</param>
\r
132 public void SetTrackListFromPreset(Title selectedTitle, Preset preset)
\r
134 if (selectedTitle.AudioTracks.Count == 0)
\r
136 audioList.Rows.Clear();
\r
137 drp_audioTrack.Items.Clear();
\r
138 drp_audioTrack.Items.Add("None Found");
\r
139 drp_audioTrack.SelectedIndex = 0;
\r
143 // Setup the Audio track source dropdown with the new audio tracks.
\r
144 drp_audioTrack.Items.Clear();
\r
145 drp_audioTrack.Items.AddRange(selectedTitle.AudioTracks.ToArray());
\r
147 // Add any tracks the preset has, if there is a preset and no audio tracks in the list currently
\r
148 if (audioList.Rows.Count == 0 && preset != null)
\r
150 QueryParser parsed = QueryParser.Parse(preset.Query);
\r
151 foreach (AudioTrack audioTrack in parsed.AudioInformation)
\r
153 DataGridViewRow newTrack = new DataGridViewRow();
\r
154 newTrack.CreateCells(audioList);
\r
155 newTrack.Cells[0].Value = GetNewID().ToString();
\r
156 newTrack.Cells[1].Value = (audioTrack.Track);
\r
157 newTrack.Cells[2].Value = (audioTrack.Encoder);
\r
158 newTrack.Cells[3].Value = (audioTrack.MixDown);
\r
159 newTrack.Cells[4].Value = (audioTrack.SampleRate);
\r
160 newTrack.Cells[5].Value = (audioTrack.Bitrate);
\r
161 newTrack.Cells[6].Value = (audioTrack.DRC);
\r
162 audioList.Rows.Add(newTrack);
\r
166 this.AutomaticTrackSelection();
\r
169 private void AutomaticTrackSelection()
\r
171 // Handle Native Language and "Dub Foreign language audio" and "Use Foreign language audio and Subtitles" Options
\r
172 if (Properties.Settings.Default.NativeLanguage == "Any")
\r
174 drp_audioTrack.SelectedIndex = 0;
\r
175 foreach (DataGridViewRow item in audioList.Rows)
\r
177 if (this.drp_audioTrack.SelectedItem != null)
\r
179 item.Cells[1].Value = this.drp_audioTrack.SelectedItem.ToString();
\r
185 int mode = Properties.Settings.Default.DubMode;
\r
190 // Dub Foreign Language Audio
\r
191 // Select the prefered language audio, or the first track if it doesn't exist.
\r
193 foreach (object item in drp_audioTrack.Items)
\r
195 if (item.ToString().Contains(Properties.Settings.Default.NativeLanguage))
\r
197 drp_audioTrack.SelectedIndex = i;
\r
204 if (drp_audioTrack.SelectedItem != null)
\r
205 foreach (DataGridViewRow item in audioList.Rows)
\r
206 item.Cells[1].Value = drp_audioTrack.SelectedItem.ToString();
\r
209 drp_audioTrack.SelectedIndex = 0;
\r
210 if (drp_audioTrack.SelectedItem != null)
\r
211 foreach (DataGridViewRow item in audioList.Rows)
\r
212 item.Cells[1].Value = drp_audioTrack.SelectedItem.ToString();
\r
218 // Select the first track which is hopefully the default and foreign track.
\r
219 drp_audioTrack.SelectedIndex = 0;
\r
221 if (drp_audioTrack.SelectedItem != null)
\r
222 foreach (DataGridViewRow item in audioList.Rows)
\r
223 item.Cells[1].Value = drp_audioTrack.SelectedItem.ToString();
\r
229 // Control and ListView
\r
232 /// one of the controls has changed. Event handler
\r
234 /// <param name="sender">
\r
237 /// <param name="e">
\r
240 private void ControlChanged(object sender, EventArgs e)
\r
242 Control ctl = (Control)sender;
\r
246 case "drp_audioTrack":
\r
247 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
249 audioList.SelectedRows[0].Cells[1].Value = drp_audioTrack.Text;
\r
251 // If the track isn't AC3, and the encoder is, change it.
\r
252 if (audioList.SelectedRows[0].Cells[2].Value.ToString().Contains(AC3Passthru) && !audioList.SelectedRows[0].Cells[1].Value.ToString().Contains("(AC3)"))
\r
255 drp_audioEncoder.SelectedIndex = 0;
\r
258 // If the track isn't DTS, and the encoder is, change it.
\r
259 if (audioList.SelectedRows[0].Cells[2].Value.ToString().Contains(DTSPassthru) && !audioList.SelectedRows[0].Cells[1].Value.ToString().Contains("DTS"))
\r
262 drp_audioEncoder.SelectedIndex = 0;
\r
266 case "drp_audioEncoder":
\r
270 // Configure the widgets with values
\r
271 if (drp_audioEncoder.Text.Contains(AC3Passthru) || drp_audioEncoder.Text.Contains(DTSPassthru))
\r
273 drp_audioMix.Enabled =
\r
274 drp_audioBitrate.Enabled = drp_audioSample.Enabled = tb_drc.Enabled = false;
\r
275 lbl_bitrate.Enabled =
\r
277 lbl_drcHeader.Enabled = lbl_mixdown.Enabled = lbl_sampleRate.Enabled = false;
\r
281 drp_audioMix.Enabled =
\r
282 drp_audioBitrate.Enabled = drp_audioSample.Enabled = tb_drc.Enabled = true;
\r
283 lbl_bitrate.Enabled =
\r
285 lbl_drcHeader.Enabled = lbl_mixdown.Enabled = lbl_sampleRate.Enabled = true;
\r
288 // Update an item in the Audio list if required.
\r
289 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
290 audioList.SelectedRows[0].Cells[2].Value = drp_audioEncoder.Text;
\r
292 case "drp_audioMix":
\r
295 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
296 audioList.SelectedRows[0].Cells[3].Value = drp_audioMix.Text;
\r
298 case "drp_audioSample":
\r
299 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
300 audioList.SelectedRows[0].Cells[4].Value = drp_audioSample.Text;
\r
302 case "drp_audioBitrate":
\r
303 // Update an item in the Audio list if required.
\r
304 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
305 audioList.SelectedRows[0].Cells[5].Value = drp_audioBitrate.Text;
\r
309 if (tb_drc.Value == 0) value = 0;
\r
311 value = ((tb_drc.Value - 1) / 10.0) + 1;
\r
313 lbl_drc.Text = value.ToString();
\r
315 // Update an item in the Audio list if required.
\r
316 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
318 audioList.SelectedRows[0].Cells[6].Value = value.ToString();
\r
319 audioList.Select();
\r
324 audioList.Select();
\r
328 /// The Audio List selected index changed event handler
\r
330 /// <param name="sender">
\r
333 /// <param name="e">
\r
336 private void audioList_SelectionChanged(object sender, EventArgs e)
\r
338 // Set the dropdown controls based on the selected item in the Audio List.
\r
339 if (audioList.Rows.Count != 0 && audioList.SelectedRows.Count != 0)
\r
341 drp_audioTrack.SelectedItem = audioList.SelectedRows[0].Cells[1].Value;
\r
342 drp_audioEncoder.SelectedItem = audioList.SelectedRows[0].Cells[2].Value;
\r
343 drp_audioMix.SelectedItem = audioList.SelectedRows[0].Cells[3].Value;
\r
344 drp_audioSample.SelectedItem = audioList.SelectedRows[0].Cells[4].Value;
\r
345 drp_audioBitrate.SelectedItem = audioList.SelectedRows[0].Cells[5].Value;
\r
348 double.TryParse(audioList.SelectedRows[0].Cells[6].Value.ToString(), out drcValue);
\r
350 drcValue = ((drcValue * 10) + 1) - 10;
\r
351 int.TryParse(drcValue.ToString(), out drcCalculated);
\r
352 tb_drc.Value = drcCalculated;
\r
353 lbl_drc.Text = audioList.SelectedRows[0].Cells[6].Value.ToString();
\r
355 AudioTrackGroup.Text = "Selected Track: " + audioList.SelectedRows[0].Cells[0].Value;
\r
358 AudioTrackGroup.Text = "Selected Track: None (Click \"Add Track\" to add)";
\r
364 /// The Add Audio Track button event handler
\r
366 /// <param name="sender">
\r
369 /// <param name="e">
\r
372 private void btn_addAudioTrack_Click(object sender, EventArgs e)
\r
374 if (drp_audioTrack.Text == "None Found")
\r
377 "Your source appears to have no audio tracks that HandBrake supports.",
\r
379 MessageBoxButtons.OK,
\r
380 MessageBoxIcon.Warning);
\r
385 if (tb_drc.Value != 0)
\r
386 value = ((tb_drc.Value - 1) / 10.0) + 1;
\r
388 // Create a new row for the Audio list based on the currently selected items in the dropdown.
\r
389 DataGridViewRow newTrack = new DataGridViewRow();
\r
390 newTrack.CreateCells(audioList);
\r
391 newTrack.Cells[0].Value = GetNewID().ToString();
\r
392 newTrack.Cells[1].Value = drp_audioTrack.Text;
\r
393 newTrack.Cells[2].Value = drp_audioEncoder.Text;
\r
394 newTrack.Cells[3].Value = drp_audioMix.Text;
\r
395 newTrack.Cells[4].Value = drp_audioSample.Text;
\r
396 newTrack.Cells[5].Value = drp_audioBitrate.Text;
\r
397 newTrack.Cells[6].Value = value.ToString();
\r
398 audioList.Rows.Add(newTrack);
\r
400 // The Audio List has changed to raise the event.
\r
401 if (this.AudioListChanged != null)
\r
402 this.AudioListChanged(this, new EventArgs());
\r
404 // Select the newly added track and select the control
\r
405 audioList.ClearSelection();
\r
406 audioList.Rows[audioList.Rows.Count - 1].Selected = true;
\r
407 audioList.Select();
\r
411 /// The Remove Track button event handler
\r
413 /// <param name="sender">
\r
416 /// <param name="e">
\r
419 private void btn_RemoveAudioTrack_Click(object sender, EventArgs e)
\r
427 /// The Audio List Move Up menu item
\r
429 /// <param name="sender">
\r
432 /// <param name="e">
\r
435 private void audioList_moveup_Click(object sender, EventArgs e)
\r
441 /// The audio list move down menu item
\r
443 /// <param name="sender">
\r
446 /// <param name="e">
\r
449 private void audioList_movedown_Click(object sender, EventArgs e)
\r
455 /// The audio list remove menu item
\r
457 /// <param name="sender">
\r
460 /// <param name="e">
\r
463 private void audioList_remove_Click(object sender, EventArgs e)
\r
468 // Private Functions
\r
471 /// Add track for preset
\r
473 /// <param name="item">
\r
476 private void AddTrackForPreset(DataGridViewRow item)
\r
478 audioList.Rows.Add(item);
\r
479 if (this.AudioListChanged != null)
\r
480 this.AudioListChanged(this, new EventArgs());
\r
484 /// Clear the audio list
\r
486 private void ClearAudioList()
\r
488 audioList.Rows.Clear();
\r
489 if (this.AudioListChanged != null)
\r
490 this.AudioListChanged(this, new EventArgs());
\r
494 /// Get a new ID for the next audio track
\r
499 private int GetNewID()
\r
501 return audioList.Rows.Count + 1;
\r
505 /// Remove an audio track from the list
\r
507 private void RemoveTrack()
\r
509 // Remove the Item and reselect the control if the following conditions are met.
\r
510 if (audioList.SelectedRows.Count != 0)
\r
512 // The Audio List is about to change so raise the event.
\r
513 if (this.AudioListChanged != null)
\r
514 this.AudioListChanged(this, new EventArgs());
\r
516 // Record the current selected index.
\r
517 int currentPosition = audioList.SelectedRows[0].Index;
\r
519 audioList.Rows.Remove(audioList.SelectedRows[0]);
\r
521 // Now reslect the correct item and give focus to the audio list.
\r
522 if (audioList.Rows.Count != 0)
\r
524 audioList.ClearSelection();
\r
525 if (currentPosition <= (audioList.Rows.Count - 1))
\r
526 audioList.Rows[currentPosition].Selected = true;
\r
527 else if (currentPosition > (audioList.Rows.Count - 1))
\r
528 audioList.Rows[audioList.Rows.Count - 1].Selected = true;
\r
530 audioList.Select();
\r
532 // Regenerate the ID numers
\r
533 ReGenerateListIDs();
\r
538 /// Move an audio track up or down the audio list
\r
540 /// <param name="up">
\r
543 private void MoveTrack(bool up)
\r
545 if (audioList.SelectedRows.Count == 0) return;
\r
547 DataGridViewRow item = audioList.SelectedRows[0];
\r
548 int index = item.Index;
\r
553 if (index < audioList.Rows.Count || (audioList.Rows.Count > index && index >= 0))
\r
555 audioList.Rows.Remove(item);
\r
556 audioList.Rows.Insert(index, item);
\r
557 audioList.ClearSelection();
\r
558 item.Selected = true;
\r
563 /// Regenerate all the audio track id's on the audio list
\r
565 private void ReGenerateListIDs()
\r
568 foreach (DataGridViewRow item in audioList.Rows)
\r
570 item.Cells[0].Value = i.ToString();
\r
576 /// Set the bitrate dropdown
\r
578 private void SetBitrate()
\r
581 drp_audioBitrate.Items.Remove("Auto");
\r
582 drp_audioBitrate.Items.Remove("192");
\r
583 drp_audioBitrate.Items.Remove("224");
\r
584 drp_audioBitrate.Items.Remove("256");
\r
585 drp_audioBitrate.Items.Remove("320");
\r
586 drp_audioBitrate.Items.Remove("384");
\r
587 drp_audioBitrate.Items.Remove("448");
\r
588 drp_audioBitrate.Items.Remove("640");
\r
589 drp_audioBitrate.Items.Remove("768");
\r
591 switch (drp_audioEncoder.Text)
\r
594 max = drp_audioMix.Text.Contains("6 Channel") ? 768 : 320;
\r
599 case "Vorbis (vorbis)":
\r
602 case "AC3 (ffmpeg)":
\r
606 drp_audioBitrate.Items.Add("Auto");
\r
607 drp_audioBitrate.SelectedItem = "Auto";
\r
608 drp_audioSample.SelectedItem = "Auto";
\r
611 drp_audioBitrate.Items.Add("Auto");
\r
612 drp_audioBitrate.SelectedItem = "Auto";
\r
613 drp_audioSample.SelectedItem = "Auto";
\r
619 drp_audioBitrate.Items.Add("192");
\r
620 drp_audioBitrate.Items.Add("224");
\r
621 drp_audioBitrate.Items.Add("256");
\r
622 drp_audioBitrate.Items.Add("320");
\r
627 drp_audioBitrate.Items.Add("384");
\r
632 drp_audioBitrate.Items.Add("448");
\r
633 drp_audioBitrate.Items.Add("640");
\r
638 drp_audioBitrate.Items.Add("768");
\r
641 if (drp_audioBitrate.SelectedItem == null && audioList.SelectedRows.Count == 0)
\r
642 drp_audioBitrate.SelectedIndex = drp_audioBitrate.Items.Count - 1;
\r
644 if (drp_audioBitrate.SelectedItem == null && audioList.SelectedRows.Count != 0 && (string)this.audioList.SelectedRows[0].Cells[5].Value == "Auto")
\r
646 drp_audioBitrate.SelectedItem = "160";
\r
651 /// Set the mixdown dropdown
\r
653 private void SetMixDown()
\r
655 drp_audioMix.Items.Clear();
\r
656 drp_audioMix.Items.Add("Mono");
\r
657 drp_audioMix.Items.Add("Stereo");
\r
658 drp_audioMix.Items.Add("Dolby Surround");
\r
659 drp_audioMix.Items.Add("Dolby Pro Logic II");
\r
660 drp_audioMix.Items.Add("6 Channel Discrete");
\r
661 drp_audioMix.Items.Add(AC3Passthru);
\r
662 drp_audioMix.Items.Add(DTSPassthru);
\r
664 drp_audioMix.SelectedItem = "Dolby Pro Logic II";
\r
666 switch (drp_audioEncoder.Text)
\r
669 drp_audioMix.Items.Remove(AC3Passthru);
\r
670 drp_audioMix.Items.Remove(DTSPassthru);
\r
673 drp_audioMix.Items.Remove("6 Channel Discrete");
\r
674 drp_audioMix.Items.Remove(AC3Passthru);
\r
675 drp_audioMix.Items.Remove(DTSPassthru);
\r
677 case "Vorbis (vorbis)":
\r
678 drp_audioMix.Items.Remove(AC3Passthru);
\r
679 drp_audioMix.Items.Remove(DTSPassthru);
\r
681 case "AC3 (ffmpeg)":
\r
682 drp_audioMix.Items.Remove(AC3Passthru);
\r
683 drp_audioMix.Items.Remove(DTSPassthru);
\r
685 case "AC3 Passthru":
\r
686 drp_audioMix.SelectedItem = AC3Passthru;
\r
688 case "DTS Passthru":
\r
689 drp_audioMix.SelectedItem = DTSPassthru;
\r