DAEProgress.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using NEROLib;
  8. using System.Runtime.InteropServices;
  9. namespace LYFZ.NeroDiscBurn.NET
  10. {
  11. /// <summary>
  12. /// Summary description for DAEProgress.
  13. /// </summary>
  14. public class DAEProgressForm : System.Windows.Forms.Form
  15. {
  16. private NeroDrive m_drive;
  17. private int m_iStartBlock;
  18. private int m_iLengthInBlocks;
  19. private string m_sFilename;
  20. private int m_iSpeed;
  21. private FileStream m_fs;
  22. private bool m_bAborted;
  23. private _INeroDriveEvents_OnDoneDAEEventHandler m_evOnDoneDAE;
  24. private _INeroDriveEvents_OnProgressEventHandler m_evOnProgress;
  25. private _INeroDriveEvents_OnWriteDAEEventHandler m_evOnWriteDAE;
  26. private System.Windows.Forms.ProgressBar c_Progress;
  27. private System.Windows.Forms.Label c_ProgressPercent;
  28. private System.Windows.Forms.Button c_Cancel;
  29. /// <summary>
  30. /// Required designer variable.
  31. /// </summary>
  32. private System.ComponentModel.Container components = null;
  33. public DAEProgressForm(NeroDrive drive, int iStartBlock, int iLengthInBlocks, string sFilename, int iSpeed)
  34. {
  35. //
  36. // Required for Windows Form Designer support
  37. //
  38. InitializeComponent();
  39. m_bAborted = false;
  40. m_drive = drive;
  41. m_iStartBlock = iStartBlock;
  42. m_iLengthInBlocks = iLengthInBlocks;
  43. m_sFilename = sFilename;
  44. m_iSpeed = iSpeed;
  45. }
  46. /// <summary>
  47. /// Clean up any resources being used.
  48. /// </summary>
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if(components != null)
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60. #region Windows Form Designer generated code
  61. /// <summary>
  62. /// Required method for Designer support - do not modify
  63. /// the contents of this method with the code editor.
  64. /// </summary>
  65. private void InitializeComponent()
  66. {
  67. this.c_Progress = new System.Windows.Forms.ProgressBar();
  68. this.c_ProgressPercent = new System.Windows.Forms.Label();
  69. this.c_Cancel = new System.Windows.Forms.Button();
  70. this.SuspendLayout();
  71. //
  72. // c_Progress
  73. //
  74. this.c_Progress.Location = new System.Drawing.Point(8, 40);
  75. this.c_Progress.Name = "c_Progress";
  76. this.c_Progress.Size = new System.Drawing.Size(296, 23);
  77. this.c_Progress.TabIndex = 1;
  78. //
  79. // c_ProgressPercent
  80. //
  81. this.c_ProgressPercent.Location = new System.Drawing.Point(106, 16);
  82. this.c_ProgressPercent.Name = "c_ProgressPercent";
  83. this.c_ProgressPercent.Size = new System.Drawing.Size(100, 16);
  84. this.c_ProgressPercent.TabIndex = 0;
  85. this.c_ProgressPercent.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  86. //
  87. // c_Cancel
  88. //
  89. this.c_Cancel.Location = new System.Drawing.Point(119, 72);
  90. this.c_Cancel.Name = "c_Cancel";
  91. this.c_Cancel.TabIndex = 2;
  92. this.c_Cancel.Text = "Cancel";
  93. this.c_Cancel.Click += new System.EventHandler(this.c_Cancel_Click);
  94. //
  95. // DAEProgressForm
  96. //
  97. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  98. this.ClientSize = new System.Drawing.Size(312, 104);
  99. this.ControlBox = false;
  100. this.Controls.Add(this.c_Cancel);
  101. this.Controls.Add(this.c_ProgressPercent);
  102. this.Controls.Add(this.c_Progress);
  103. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  104. this.MaximizeBox = false;
  105. this.MinimizeBox = false;
  106. this.Name = "DAEProgressForm";
  107. this.ShowInTaskbar = false;
  108. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  109. this.Text = "DAEProgress";
  110. this.ResumeLayout(false);
  111. }
  112. #endregion
  113. protected override void OnLoad(EventArgs e)
  114. {
  115. base.OnLoad (e);
  116. // Subscribe to events of interest to digital
  117. // audio extraction.
  118. //
  119. m_evOnProgress = new _INeroDriveEvents_OnProgressEventHandler(m_drive_OnProgress);
  120. m_drive.OnProgress += m_evOnProgress;
  121. m_evOnDoneDAE = new _INeroDriveEvents_OnDoneDAEEventHandler(m_drive_OnDoneDAE);
  122. m_drive.OnDoneDAE += m_evOnDoneDAE;
  123. m_evOnWriteDAE = new _INeroDriveEvents_OnWriteDAEEventHandler(m_drive_OnWriteDAE);
  124. m_drive.OnWriteDAE += m_evOnWriteDAE;
  125. // Check to see if the file we are about to extract to
  126. // is WAV or not.
  127. //
  128. bool bIsWAV = m_sFilename.ToLower ().EndsWith (".wav");
  129. if (!bIsWAV)
  130. {
  131. // If not WAV, we consider it to be a RAW file (no header,
  132. // just raw PCM 16-bit stereo data). Create a file to
  133. // store data to.
  134. //
  135. m_fs = File.Create (m_sFilename);
  136. }
  137. // Start the DAE process. Give it a start and end blocks as well as the
  138. // speed. If filename passed is not empty, it will be used for storing
  139. // the audio data in WAV format. If it is empty, our events will be
  140. // called so we can do whatever we want with PCM data.
  141. //
  142. try
  143. {
  144. m_drive.DAE (m_iStartBlock, m_iLengthInBlocks, bIsWAV? m_sFilename: "", m_iSpeed);
  145. }
  146. catch (COMException ex)
  147. {
  148. // If there is an error, display it and close the dialog
  149. // afterwards.
  150. //
  151. MessageBox.Show (this, ex.Message);
  152. this.Close ();
  153. }
  154. }
  155. private void m_drive_OnDoneDAE(ref bool Ok)
  156. {
  157. // This event is called once everything is finished.
  158. // We can tell if it was a success or not.
  159. //
  160. m_drive.OnProgress -= m_evOnProgress;
  161. m_drive.OnDoneDAE -= m_evOnDoneDAE;
  162. m_drive.OnWriteDAE -= m_evOnWriteDAE;
  163. // If there is a file we were using, make sure it is
  164. // closed now.
  165. //
  166. if (m_fs != null)
  167. {
  168. m_fs.Close ();
  169. }
  170. // Show success, failure or abortion and close the dialog
  171. // afterwards.
  172. //
  173. MessageBox.Show (Ok? "DAE succeeded!": (m_bAborted? "DAE aborted!": "DAE failed!"));
  174. this.Close ();
  175. }
  176. private void m_drive_OnProgress(ref int ProgressInPercent, ref bool Abort)
  177. {
  178. // This even will be called periodically to report progress to
  179. // us. We can also take the opportunity to abort here if
  180. // necessary.
  181. //
  182. c_ProgressPercent.Text = ProgressInPercent.ToString () + "%";
  183. c_Progress.Value = ProgressInPercent;
  184. Abort = m_bAborted;
  185. }
  186. private void c_Cancel_Click(object sender, System.EventArgs e)
  187. {
  188. // When Cancel button is clicked, make sure to set the
  189. // abort flag so that when the next event is fired we can
  190. // report it. This will in turn stop the process.
  191. //
  192. m_bAborted = true;
  193. }
  194. private void m_drive_OnWriteDAE(ref int ignore, ref object Data)
  195. {
  196. // This event will be called only if we passed empty filename
  197. // to the DAE method. Make sure we have a file to work with.
  198. //
  199. if (m_fs != null)
  200. {
  201. // The Data is an Object which is actually a VARIANT
  202. // containing a SAFEARRAY of VT_I2 (or shorts). It is
  203. // perfectly safe to cast it to a short[] as that what
  204. // it really is in C#. On the other hand, the file
  205. // writing function accepts only byte[] so we need to
  206. // convert one to the other. A simpe cast won't work
  207. // here.
  208. //
  209. short [] samples = (short []) Data;
  210. byte [] buffer = new byte[samples.Length*2];
  211. // Loop until there are no more samples provided. A
  212. // sample is 16-bits wide. The audio data comes as
  213. // stereo 16-bit samples, which means even samples are
  214. // left and odd samples are right audio channel. We
  215. // convert these into a byte array in a LSB (Intel) order.
  216. //
  217. int i = 0;
  218. foreach (short sample in samples)
  219. {
  220. buffer[i++] = (byte) (sample&0xff);
  221. buffer[i++] = (byte) ((sample>>8)&0xff);
  222. }
  223. // Finally, once we have the byte array ready, we write
  224. // it to the file.
  225. //
  226. m_fs.Write (buffer, 0, buffer.Length);
  227. }
  228. }
  229. }
  230. }