static void Main(string[] args)
        {
            string fileName = string.Empty;
            string outputPath = string.Empty;
            if (args.Count() == 1)
            {
                fileName = args[0];
                outputPath = fileName.Remove(fileName.Length - Path.GetFileName(fileName).Length);
            }
            else if (args.Count() == 2)
            {
                fileName = args[0];
                outputPath = args[1];
            }
            else
            {
                var v = Assembly.GetExecutingAssembly().GetName().Version;
                WriteLine(
                    string.Format(
                        "SimpleWavSplitterConsole v{0}.{1}.{2}",
                        v.Major, v.Minor, v.Build));
                Write(Environment.NewLine);
                WriteLine("Usage:");
                WriteLine("SimpleWavSplitter.Console <file.wav> [<OutputPath>]");
                Environment.Exit(-1);
            }

            try
            {
                long bytesTotal = 0;
                var splitter = new WavFileSplitter(
                    value => Write(string.Format("\rProgress: {0:0.0}%", value)));
                var sw = Stopwatch.StartNew();
                bytesTotal = splitter.SplitWavFile(fileName, outputPath, CancellationToken.None);
                sw.Stop();
                Write(Environment.NewLine);
                WriteLine(
                    string.Format(
                        "Data bytes processed: {0} ({1} MB)",
                        bytesTotal,
                        Round((double)bytesTotal / (1024 * 1024), 1)));
                WriteLine(string.Format("Elapsed time: {0}", sw.Elapsed));
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                WriteLine(ex.Message);
                WriteLine(ex.StackTrace);
                Environment.Exit(-1);
            }
        }
        private async Task SplitWavFiles(string[] fileNames)
        {
            progress.Value = 0;
            _tokenSource = new CancellationTokenSource();
            CancellationToken ct = _tokenSource.Token;

            var sw = Stopwatch.StartNew();
            var sb = new StringBuilder();

            sb.Append(string.Format("Files to split: {0}\n", fileNames.Count()));
            textOutput.Text = sb.ToString();

            string userOutputPath = textOutputPath.Text;

            if (userOutputPath.EndsWith("\\") == false && userOutputPath.Length > 0)
            {
                userOutputPath += "\\";
            }

            long totalBytesProcessed = await Task<long>.Factory.StartNew(() =>
            {
                ct.ThrowIfCancellationRequested();
                long countBytesTotal = 0;

                var splitter = new WavFileSplitter(
                    value => Dispatcher.UIThread.InvokeAsync(() =>
                    {
                    progress.Value = value;
                        //Thread.Sleep(100);
                    }));

                foreach (string fileName in fileNames)
                {
                    try
                    {
                        string outputPath = userOutputPath.Length > 0 ?
                            userOutputPath :
                            fileName.Remove(fileName.Length - Path.GetFileName(fileName).Length);

                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            sb.Append(
                                string.Format(
                                    "Split file: {0}\n",
                                    Path.GetFileName(fileName)));
                            textOutput.Text = sb.ToString();
                        });

                        countBytesTotal += splitter.SplitWavFile(fileName, outputPath, ct);
                    }
                    catch (Exception ex)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            sb.Append(string.Format("Error: {0}\n", ex.Message));
                            textOutput.Text = sb.ToString();
                        });
                        return countBytesTotal;
                    }
                }
                return countBytesTotal;
            }, ct);

            sw.Stop();
            if (_tokenSource.IsCancellationRequested == false)
            {
                string text = string.Format(
                    "Done.\nData bytes processed: {0} ({1} MB)\nElapsed time: {2}\n",
                    totalBytesProcessed,
                    Math.Round((double)totalBytesProcessed / (1024 * 1024), 1),
                    sw.Elapsed);
                sb.Append(text);
                textOutput.Text = sb.ToString();
            }
        }
Exemple #3
0
        /// <summary>
        /// Split WAV files.
        /// </summary>
        /// <param name="files">The file names.</param>
        /// <param name="path">The output path.</param>
        /// <param name="setProgress">Set progress value action.</param>
        /// <param name="setOutput">Set output string action.</param>
        /// <returns></returns>
        public async Task SplitWavFiles(string[] files, string path, Action <double> setProgress, Action <string> setOutput)
        {
            setProgress(0);
            _tokenSource = new CancellationTokenSource();
            CancellationToken ct = _tokenSource.Token;

            var sw = Stopwatch.StartNew();
            var sb = new StringBuilder();

            sb.Append(string.Format("Files to split: {0}\n", files.Count()));
            setOutput(sb.ToString());

            if (path.EndsWith("\\") == false && path.Length > 0)
            {
                path += "\\";
            }

            long totalBytesProcessed = await Task <long> .Factory.StartNew(() =>
            {
                ct.ThrowIfCancellationRequested();
                long countBytesTotal = 0;

                var splitter = new WavFileSplitter(setProgress);

                foreach (string fileName in files)
                {
                    try
                    {
                        string outputPath = path.Length > 0 ?
                                            path :
                                            fileName.Remove(fileName.Length - Path.GetFileName(fileName).Length);

                        sb.Append(
                            string.Format(
                                "Split file: {0}\n",
                                Path.GetFileName(fileName)));

                        setOutput(sb.ToString());

                        countBytesTotal += splitter.SplitWavFile(fileName, outputPath, ct);
                    }
                    catch (Exception ex)
                    {
                        sb.Append(string.Format("Error: {0}\n", ex.Message));

                        setOutput(sb.ToString());

                        return(countBytesTotal);
                    }
                }
                return(countBytesTotal);
            }, ct);

            sw.Stop();
            if (_tokenSource.IsCancellationRequested == false)
            {
                string text = string.Format(
                    "Done.\nData bytes processed: {0} ({1} MB)\nElapsed time: {2}\n",
                    totalBytesProcessed,
                    Math.Round((double)totalBytesProcessed / (1024 * 1024), 1),
                    sw.Elapsed);
                sb.Append(text);
                setOutput(sb.ToString());
            }
        }