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 NeroFiddlesCOM.NET { /// /// Summary description for DAEProgress. /// 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; /// /// Required designer variable. /// 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; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 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); } } } }