123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.IO;
- using NEROLib;
- using System.Runtime.InteropServices;
- namespace LYFZ.NeroDiscBurn.NET
- {
- /// <summary>
- /// Summary description for DAEProgress.
- /// </summary>
- public class DAEProgressForm : System.Windows.Forms.Form
- {
- private NeroDrive m_drive;
- private int m_iStartBlock;
- private int m_iLengthInBlocks;
- private string m_sFilename;
- private int m_iSpeed;
- private FileStream m_fs;
- private bool m_bAborted;
- private _INeroDriveEvents_OnDoneDAEEventHandler m_evOnDoneDAE;
- private _INeroDriveEvents_OnProgressEventHandler m_evOnProgress;
- private _INeroDriveEvents_OnWriteDAEEventHandler m_evOnWriteDAE;
- private System.Windows.Forms.ProgressBar c_Progress;
- private System.Windows.Forms.Label c_ProgressPercent;
- private System.Windows.Forms.Button c_Cancel;
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.Container components = null;
- public DAEProgressForm(NeroDrive drive, int iStartBlock, int iLengthInBlocks, string sFilename, int iSpeed)
- {
- //
- // Required for Windows Form Designer support
- //
- InitializeComponent();
- m_bAborted = false;
- m_drive = drive;
- m_iStartBlock = iStartBlock;
- m_iLengthInBlocks = iLengthInBlocks;
- m_sFilename = sFilename;
- m_iSpeed = iSpeed;
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.c_Progress = new System.Windows.Forms.ProgressBar();
- this.c_ProgressPercent = new System.Windows.Forms.Label();
- this.c_Cancel = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // c_Progress
- //
- this.c_Progress.Location = new System.Drawing.Point(8, 40);
- this.c_Progress.Name = "c_Progress";
- this.c_Progress.Size = new System.Drawing.Size(296, 23);
- this.c_Progress.TabIndex = 1;
- //
- // c_ProgressPercent
- //
- this.c_ProgressPercent.Location = new System.Drawing.Point(106, 16);
- this.c_ProgressPercent.Name = "c_ProgressPercent";
- this.c_ProgressPercent.Size = new System.Drawing.Size(100, 16);
- this.c_ProgressPercent.TabIndex = 0;
- this.c_ProgressPercent.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // c_Cancel
- //
- this.c_Cancel.Location = new System.Drawing.Point(119, 72);
- this.c_Cancel.Name = "c_Cancel";
- this.c_Cancel.TabIndex = 2;
- this.c_Cancel.Text = "Cancel";
- this.c_Cancel.Click += new System.EventHandler(this.c_Cancel_Click);
- //
- // DAEProgressForm
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(312, 104);
- this.ControlBox = false;
- this.Controls.Add(this.c_Cancel);
- this.Controls.Add(this.c_ProgressPercent);
- this.Controls.Add(this.c_Progress);
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
- this.MaximizeBox = false;
- this.MinimizeBox = false;
- this.Name = "DAEProgressForm";
- this.ShowInTaskbar = false;
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
- this.Text = "DAEProgress";
- this.ResumeLayout(false);
- }
- #endregion
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad (e);
- // Subscribe to events of interest to digital
- // audio extraction.
- //
- m_evOnProgress = new _INeroDriveEvents_OnProgressEventHandler(m_drive_OnProgress);
- m_drive.OnProgress += m_evOnProgress;
- m_evOnDoneDAE = new _INeroDriveEvents_OnDoneDAEEventHandler(m_drive_OnDoneDAE);
- m_drive.OnDoneDAE += m_evOnDoneDAE;
- m_evOnWriteDAE = new _INeroDriveEvents_OnWriteDAEEventHandler(m_drive_OnWriteDAE);
- m_drive.OnWriteDAE += m_evOnWriteDAE;
- // Check to see if the file we are about to extract to
- // is WAV or not.
- //
- bool bIsWAV = m_sFilename.ToLower ().EndsWith (".wav");
- if (!bIsWAV)
- {
- // If not WAV, we consider it to be a RAW file (no header,
- // just raw PCM 16-bit stereo data). Create a file to
- // store data to.
- //
- m_fs = File.Create (m_sFilename);
- }
- // Start the DAE process. Give it a start and end blocks as well as the
- // speed. If filename passed is not empty, it will be used for storing
- // the audio data in WAV format. If it is empty, our events will be
- // called so we can do whatever we want with PCM data.
- //
- try
- {
- m_drive.DAE (m_iStartBlock, m_iLengthInBlocks, bIsWAV? m_sFilename: "", m_iSpeed);
- }
- catch (COMException ex)
- {
- // If there is an error, display it and close the dialog
- // afterwards.
- //
- MessageBox.Show (this, ex.Message);
- this.Close ();
- }
- }
- private void m_drive_OnDoneDAE(ref bool Ok)
- {
- // This event is called once everything is finished.
- // We can tell if it was a success or not.
- //
- m_drive.OnProgress -= m_evOnProgress;
- m_drive.OnDoneDAE -= m_evOnDoneDAE;
- m_drive.OnWriteDAE -= m_evOnWriteDAE;
- // If there is a file we were using, make sure it is
- // closed now.
- //
- if (m_fs != null)
- {
- m_fs.Close ();
- }
- // Show success, failure or abortion and close the dialog
- // afterwards.
- //
- MessageBox.Show (Ok? "DAE succeeded!": (m_bAborted? "DAE aborted!": "DAE failed!"));
- this.Close ();
- }
- private void m_drive_OnProgress(ref int ProgressInPercent, ref bool Abort)
- {
- // This even will be called periodically to report progress to
- // us. We can also take the opportunity to abort here if
- // necessary.
- //
- c_ProgressPercent.Text = ProgressInPercent.ToString () + "%";
- c_Progress.Value = ProgressInPercent;
- Abort = m_bAborted;
- }
- private void c_Cancel_Click(object sender, System.EventArgs e)
- {
- // When Cancel button is clicked, make sure to set the
- // abort flag so that when the next event is fired we can
- // report it. This will in turn stop the process.
- //
- m_bAborted = true;
- }
- private void m_drive_OnWriteDAE(ref int ignore, ref object Data)
- {
- // This event will be called only if we passed empty filename
- // to the DAE method. Make sure we have a file to work with.
- //
- if (m_fs != null)
- {
- // The Data is an Object which is actually a VARIANT
- // containing a SAFEARRAY of VT_I2 (or shorts). It is
- // perfectly safe to cast it to a short[] as that what
- // it really is in C#. On the other hand, the file
- // writing function accepts only byte[] so we need to
- // convert one to the other. A simpe cast won't work
- // here.
- //
- short [] samples = (short []) Data;
- byte [] buffer = new byte[samples.Length*2];
- // Loop until there are no more samples provided. A
- // sample is 16-bits wide. The audio data comes as
- // stereo 16-bit samples, which means even samples are
- // left and odd samples are right audio channel. We
- // convert these into a byte array in a LSB (Intel) order.
- //
- int i = 0;
- foreach (short sample in samples)
- {
- buffer[i++] = (byte) (sample&0xff);
- buffer[i++] = (byte) ((sample>>8)&0xff);
- }
- // Finally, once we have the byte array ready, we write
- // it to the file.
- //
- m_fs.Write (buffer, 0, buffer.Length);
- }
- }
- }
- }
|