Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;

using BSE.CDDrives;
using BSE.Platten.BO;
using BSE.Platten.Common;
using BSE.Configuration;
using System.Collections.ObjectModel;
using System.Globalization;
using BSE.Platten.Ripper.Properties;


namespace BSE.Platten.Ripper
{
    public partial class RipTracks : BaseForm
    {

        #region EventsPublic

        public event EventHandler<TrackRippingEventArgs> TrackRipped;
        public event System.EventHandler RippingComplete;

        #endregion

        #region Delegates

        private delegate void ReadProcessHandler(ReadProgressEventArgs readProgressEventArgs);
        private delegate void WriteStatusHandler(CTrack track);
        private delegate void WindowCloseHandler();

        #endregion

        #region FieldsPrivate

        private CConfiguration m_configuration;
        private Collection<CTrack> m_trackCollection;
        private uint m_iTotalSize;
        private uint m_iBytesRead;
        private uint m_iBytes2Read;
        private uint m_iTotalBytesRead;
        private string m_strOutputDirectory;
        private CDDrive m_cdDrive;
        private int m_iTrackIndex;
        private CTrackRipper m_trackRipper;
        
        #endregion

        #region Properties

        public string OutputDirectory
        {
            get { return this.m_strOutputDirectory; }
            set
            {
                if (value != null)
                {
                    if (value != this.m_strOutputDirectory)
                    {
                        this.m_strOutputDirectory = value;
                    }
                }
            }
        }

        public CDDrive CDDrive
        {
            get { return this.m_cdDrive; }
            set
            {
                if (value != this.m_cdDrive)
                {
                    this.m_cdDrive = value;
                }
            }
        }

        #endregion

        #region MethodsPublic

        public RipTracks()
        {
            InitializeComponent();
            this.m_lblTotalTrackPercentValue.Text = string.Format(CultureInfo.InvariantCulture,"0 %");
            this.m_lblTrackPercentValue.Text = string.Format(CultureInfo.InvariantCulture,"0 %");
        }

        public RipTracks(CConfiguration configuration, Collection<CTrack> trackCollection)
            : this()
		{
            this.m_configuration = configuration;
            this.m_trackCollection = trackCollection;
            this.m_iTotalSize = GetTotalTracksSize(trackCollection);
		}

        #endregion

        #region MethodsProtected
        #endregion

        #region MethodsPrivate

        private void RipThisTrack(int iIndexTrackToRip, System.IO.FileMode fileMode)
        {
            this.m_trackRipper = null;
            this.m_trackRipper = new CTrackRipper();
            this.m_trackRipper.ReadCDProgess -= new BSE.CDDrives.CdReadProgressEventHandler(this.CdReadProgress);
            this.m_trackRipper.ReadCDProgess += new BSE.CDDrives.CdReadProgressEventHandler(this.CdReadProgress);
            this.m_trackRipper.TrackRippingCanceled -= new EventHandler<TrackRippingEventArgs>(this.TrackRippingCanceled);
            this.m_trackRipper.TrackRippingCanceled += new EventHandler<TrackRippingEventArgs>(this.TrackRippingCanceled);
            this.m_trackRipper.TrackExists -= new EventHandler<TrackRippingEventArgs>(this.TrackExists);
            this.m_trackRipper.TrackExists += new EventHandler<TrackRippingEventArgs>(this.TrackExists);
            this.m_trackRipper.TrackRipping -= new EventHandler<TrackRippingEventArgs>(this.TrackRipping);
            this.m_trackRipper.TrackRipping += new EventHandler<TrackRippingEventArgs>(this.TrackRipping);
            this.m_trackRipper.TrackRipped -= new EventHandler<TrackRippingEventArgs>(this.RipperTrackRipped);
            this.m_trackRipper.TrackRipped += new EventHandler<TrackRippingEventArgs>(this.RipperTrackRipped);
            this.m_trackRipper.StartRipping(
                this,
                this.m_cdDrive,
                this.m_trackCollection[iIndexTrackToRip],
                fileMode,
                this.m_iTotalSize,
                this.m_configuration);
        }

        private void CdReadProgress(object sender, ReadProgressEventArgs ea)
        {
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new ReadProcessHandler(ReadProcess), ea);
            }
        }

        private void TrackExists(object sender, TrackRippingEventArgs e)
        {
            string strException = String.Format(CultureInfo.CurrentUICulture,Resources.IDS_FileToRipExistsException, e.Track.FileFullName);
            DialogResult dialogResult = GlobalizedMessageBox.Show(
                this,
                strException,
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (dialogResult == DialogResult.Yes)
            {
                RipThisTrack(this.m_iTrackIndex, System.IO.FileMode.Create);
            }
            else
            {
                if (this.IsHandleCreated == true)
                {
                    this.Invoke(new WindowCloseHandler(WindowCloseEvent));
                }
            }
        }

        private void TrackRippingCanceled(object sender, TrackRippingEventArgs e)
        {
            GlobalizedMessageBox.Show(this,Resources.IDS_RippingAbortException, MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (e.Track != null)
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(e.Track.FileFullName);
                if (fileInfo.Exists)
                {
                    fileInfo.Delete();
                }
            }
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new WindowCloseHandler(WindowCloseEvent));
            }
        }

        private void TrackRipping(object sender, TrackRippingEventArgs e)
        {
            if (this.IsHandleCreated == true)
            {
                this.Invoke(new WriteStatusHandler(WriteStatusText), e.Track);
            }
        }

        private void RipperTrackRipped(object sender, TrackRippingEventArgs e)
        {
            //das Ende des Rip- Vorgangs wird an das aufrufenden Formular zur�ck gegeben
            if (this.TrackRipped != null)
            {
                this.TrackRipped(sender, new TrackRippingEventArgs(e.Track));
            }
            if (MoveNext() == true)
            {
                RipThisTrack(this.m_iTrackIndex, System.IO.FileMode.CreateNew);
            }
            else
            {
                if (this.IsHandleCreated == true)
                {
                    this.Invoke(new WindowCloseHandler(WindowCloseEvent));
                }
            }
        }

        private void WindowCloseEvent()
        {
            this.Close();
        }

        private static uint GetTotalTracksSize(Collection<CTrack> trackCollection)
        {
            uint iTotalTracksSize = 0;
            foreach (CTrack track in trackCollection)
            {
                iTotalTracksSize += track.TrackSize;
            }
            return iTotalTracksSize;
        }

        private void m_btnCancel_Click(object sender, EventArgs e)
        {
            if (this.m_trackRipper != null)
            {
                this.m_trackRipper.CancelRipping();
            }
        }

        private void CRipTracks_Load(object sender, EventArgs e)
        {
            this.RipThisTrack(this.m_iTrackIndex, System.IO.FileMode.CreateNew);
        }

        private void CRipTracks_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.m_trackRipper != null)
            {
                System.Threading.Thread threadTrackRipper =
                    this.m_trackRipper.Thread();
                if (threadTrackRipper != null && threadTrackRipper.IsAlive)
                {
                    this.m_trackRipper.CancelRipping();
                }
            }
            if (this.RippingComplete != null)
            {
                this.RippingComplete(this, e);
            }
        }

        private void ReadProcess(ReadProgressEventArgs readProgressEventArgs)
        {
            if (readProgressEventArgs.BytesRead == 0)
            {
                this.m_iBytes2Read = 0;
                this.m_iBytes2Read = readProgressEventArgs.Bytes2Read;
            }
            if (this.m_iBytes2Read == readProgressEventArgs.BytesRead)
            {
                this.m_iTotalBytesRead += readProgressEventArgs.BytesRead;
            }

            this.m_iBytesRead = this.m_iTotalBytesRead + readProgressEventArgs.BytesRead;

            ulong lPercentTotal = ((ulong)this.m_iBytesRead * 100) / this.m_iTotalSize;
            if (lPercentTotal <= 100)
            {
                this.m_pgTotalTime.Value = (int)lPercentTotal;
                this.m_lblTotalTrackPercentValue.Text = (double)lPercentTotal + " %";
            }

            ulong lPercent = ((ulong)readProgressEventArgs.BytesRead * 100) / readProgressEventArgs.Bytes2Read;
            this.m_pgTime.Value = (int)lPercent;
            this.m_lblTrackPercentValue.Text = (double)lPercent + " %";
        }

        private void WriteStatusText(CTrack track)
        {
            if (this.m_iTrackIndex == 0)
            {
                //Index 0, die erste Zeile wird geschrieben
                this.m_txtStatus.AppendText(string.Format(
                    CultureInfo.CurrentUICulture,
                    Resources.IDS_RippingTrackInformation,
                    track.Title,
                    track.FileFullName));
            }
            else
            {
                //Index > 0, die Zeilen werden angeh�ngt
                this.m_txtStatus.AppendText(
                    Environment.NewLine + string.Format(
                    CultureInfo.CurrentUICulture,
                    Resources.IDS_RippingTrackInformation,
                    track.Title,
                    track.FileFullName));
            }
        }

        public bool MoveNext()
        {
            if (this.m_trackCollection == null || this.m_trackCollection.Count == 0)
            {
                return false;
            }

            if (this.m_iTrackIndex < this.m_trackCollection.Count - 1)
            {
                this.m_iTrackIndex++;
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region internal class CTrackRipper

        internal class CTrackRipper
        {
            #region EventsPublic

            public event BSE.CDDrives.CdReadProgressEventHandler ReadCDProgess;
            public event EventHandler<TrackRippingEventArgs> TrackRipping;
            public event EventHandler<TrackRippingEventArgs> TrackRipped;
            public event EventHandler<TrackRippingEventArgs> TrackRippingCanceled;
            public event EventHandler<TrackRippingEventArgs> TrackExists;

            #endregion

            #region FieldsPrivate

            private BSE.CDDrives.CDDrive m_cdDrive;
            private BSE.Platten.BO.CTrack m_track;
            private Thread m_ripThread;
            private BSE.Configuration.CConfiguration m_configuration;
            private System.IO.FileMode m_fileMode = FileMode.CreateNew;
            private AudioWriter m_audioWriter;
            private System.Timers.Timer m_timer;
            private bool m_bCancelRipping;
            private uint m_iTotalTracksSize;
            private IWin32Window m_owner;

            #endregion

            #region MethodsPublic

            public void StartRipping(
                IWin32Window owner,
                CDDrive cdDrive,
                CTrack track,
                FileMode fileMode,
                uint iTotalTracksSize,
                CConfiguration configuration)
            {
                this.m_owner = owner;
                this.m_cdDrive = cdDrive;
                this.m_track = track;
                this.m_fileMode = fileMode;
                this.m_iTotalTracksSize = iTotalTracksSize;
                this.m_configuration = configuration;
                
                this.m_ripThread = new Thread(new ThreadStart(RipThisTrack));
                this.m_ripThread.Start();

                this.m_timer = new System.Timers.Timer();
                this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer_Elapsed);
                this.m_timer.Enabled = true;
                this.m_timer.Start();
            }

            public Thread Thread()
            {
                return this.m_ripThread;
            }

            public void CancelRipping()
            {
                if (this.m_bCancelRipping == false)
                {
                    this.m_bCancelRipping = true;
                    if (this.TrackRippingCanceled != null)
                    {
                        this.TrackRippingCanceled(this, new TrackRippingEventArgs(this.m_track));
                    }
                }
            }

            #endregion

            #region MethodsProtected
            #endregion

            #region MethodsPrivate

            private void RipThisTrack()
            {
                try
                {
                    this.m_cdDrive.LockCD();
                    using (FileStream fileStream = new FileStream(this.m_track.FileFullName, this.m_fileMode, System.IO.FileAccess.Write))
                    {
                        try
                        {
                            CWaveFormat waveFormat = null;
                            BSE.Platten.BO.CAudioFormat.AudioFormat eAudioFormat = this.m_track.UsedAudioFormat;
                            switch (eAudioFormat)
                            {
                                case BSE.Platten.BO.CAudioFormat.AudioFormat.Wav:
                                    WaveConfigurationControl waveWriter = new WaveConfigurationControl(this.m_configuration);
                                    waveFormat = waveWriter.AudioWriterConfiguration.WaveFormat;
                                    this.m_audioWriter = new WaveWriter(fileStream, waveFormat);
                                    break;
                                case BSE.Platten.BO.CAudioFormat.AudioFormat.Mp3:
                                    waveFormat = new CWaveFormat(
                                        Constants.WavSamplesPerSecDefaultValue,
                                        Constants.WavBitsPerSampleDefaultValue,
                                        Constants.WavChannelsDefaultValue); //Set default values
                                    CMP3ConfigurationControl mp3Writer = new CMP3ConfigurationControl(this.m_configuration);
                                    MP3Configuration mp3WriterConfiguration =
                                        mp3Writer.AudioWriterConfiguration as MP3Configuration;
                                    this.m_audioWriter = new CMP3Writer(
                                        fileStream, waveFormat, mp3WriterConfiguration.Mp3Configuration);
                                    break;
                            }
                            //event wenn das Rippen beginnt
                            if (this.TrackRipping != null)
                            {
                                this.TrackRipping(this, new TrackRippingEventArgs(this.m_track));
                            }

                            this.m_cdDrive.ReadTrack(
                                this.m_track.CDIndex,
                                new CdDataReadEventHandler(this.WriteWaveData),
                                new BSE.CDDrives.CdReadProgressEventHandler(this.CdReadProgress),
                                this.m_iTotalTracksSize);
                        }
                        finally
                        {
#if DEBUG
                            Trace.WriteLine("this.m_audioWriter.Close");
#endif
                            if (this.m_audioWriter != null)
                            {
                                this.m_audioWriter.Flush();
                                this.m_audioWriter.Close();
                                this.m_audioWriter = null;
                            }
                        }
                    }
                }
                catch (System.IO.IOException ioException)
                {
                    this.m_bCancelRipping = true;
                    if (this.m_fileMode == System.IO.FileMode.CreateNew)
                    {
                        if (this.m_ripThread != null && this.m_ripThread.IsAlive)
                        {
                            if (this.TrackExists != null)
                            {
                                this.TrackExists(this, new TrackRippingEventArgs(this.m_track));
                            }
                        }
                    }
                    else
                    {
                        this.m_bCancelRipping = true;
                        GlobalizedMessageBox.Show(
                            this.m_owner,
                            ioException.Message,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }
                }
                catch (Exception exception)
                {
                    this.m_bCancelRipping = true;
                    GlobalizedMessageBox.Show(
                        this.m_owner,
                        exception.Message,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
                finally
                {
                    this.m_cdDrive.UnLockCD();
                }
            }

            private void CdReadProgress(object sender, BSE.CDDrives.ReadProgressEventArgs ea)
            {
                ea.CancelRead |= this.m_bCancelRipping;
                if (this.ReadCDProgess != null)
                {
                    this.ReadCDProgess(this, new BSE.CDDrives.ReadProgressEventArgs(
                        ea.Bytes2Read,
                        ea.BytesRead,
                        ea.Seconds2Read,
                        0));
                }
            }

            private void WriteWaveData(object sender, DataReadEventArgs ea)
            {
                if (this.m_audioWriter != null)
                {
#if DEBUG
                    Trace.WriteLine("this.m_audioWriter.Write");
#endif
                    this.m_audioWriter.Write(ea.Data, 0, (int)ea.DataSize);
                }
            }

            private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                if (this.m_ripThread != null)
                {
                    if (this.m_ripThread.ThreadState == System.Threading.ThreadState.Stopped)
                    {
                        this.m_timer.Stop();
                        if (this.m_bCancelRipping == false)
                        {
                            if (this.TrackRipped != null)
                            {
                                this.TrackRipped(this, new TrackRippingEventArgs(this.m_track));
                            }
                        }
                    }
                }
            }

            #endregion

        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions