Beispiel #1
0
        private void OnSoxProcessOutputReceived(object sender, DataReceivedEventArgs received)
        {
            if (received.Data != null)
            {
                if (OnProgress != null)
                {
                    Match matchProgress = SoxProcess.ProgressRegex.Match(received.Data);

                    if (matchProgress.Success)
                    {
                        try
                        {
                            UInt16   progress   = Convert.ToUInt16(double.Parse(matchProgress.Groups[1].Value, CultureInfo.InvariantCulture));
                            TimeSpan processed  = Utils.TimeSpanFromString(matchProgress.Groups[2].Value);
                            TimeSpan remaining  = Utils.TimeSpanFromString(matchProgress.Groups[3].Value);
                            UInt64   outputSize = FormattedSize.ToUInt64(matchProgress.Groups[4].Value);

                            ProgressEventArgs eventArgs = new ProgressEventArgs(progress, processed, remaining, outputSize);
                            OnProgress(sender, eventArgs);

                            if (eventArgs.Abort)
                            {
                                Abort();
                            }

                            return;
                        }

                        catch (Exception ex)
                        {
                            throw new SoxUnexpectedOutputException(received.Data, ex);
                        }
                    }
                }

                CheckForLogMessage(received.Data);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets information about the given file.
        /// </summary>
        /// <returns>File information as a <see cref="SoxSharp.AudioInfo"/> instance.</returns>
        /// <param name="inputFile">Input file.</param>
        public AudioInfo GetInfo(string inputFile)
        {
            if (!File.Exists(inputFile))
            {
                throw new FileNotFoundException("File not found: " + inputFile);
            }

            soxProcess_ = SoxProcess.Create(Path);

            lastError_       = null;
            lastErrorSource_ = null;

            try
            {
                soxProcess_.StartInfo.RedirectStandardOutput = true;

                if (inputFile.Contains(" "))
                {
                    if ((Environment.OSVersion.Platform == PlatformID.Win32NT) ||
                        (Environment.OSVersion.Platform == PlatformID.Win32Windows) ||
                        (Environment.OSVersion.Platform == PlatformID.Win32S) ||
                        (Environment.OSVersion.Platform == PlatformID.WinCE))
                    {
                        soxProcess_.StartInfo.Arguments = "--info \"" + inputFile + "\"";
                    }
                    else
                    {
                        soxProcess_.StartInfo.Arguments = "--info '" + inputFile + "'";
                    }
                }
                else
                {
                    soxProcess_.StartInfo.Arguments = "--info " + inputFile;
                }

                soxProcess_.Start();

                LastCommand = Path + " " + soxProcess_.StartInfo.Arguments;

                string output = soxProcess_.StandardOutput.ReadToEnd();

                if (String.IsNullOrEmpty(output))
                {
                    output = soxProcess_.StandardError.ReadToEnd();
                }

                if (soxProcess_.WaitForExit(10000) == false)
                {
                    throw new TimeoutException("SoX response timeout");
                }

                CheckForLogMessage(output);

                if (output != null)
                {
                    Match matchInfo = SoxProcess.InfoRegex.Match(output);

                    if (matchInfo.Success)
                    {
                        try
                        {
                            UInt16   channels   = Convert.ToUInt16(double.Parse(matchInfo.Groups[1].Value, CultureInfo.InvariantCulture));
                            UInt32   sampleRate = Convert.ToUInt32(double.Parse(matchInfo.Groups[2].Value, CultureInfo.InvariantCulture));
                            UInt16   sampleSize = Convert.ToUInt16(double.Parse(new string(matchInfo.Groups[3].Value.Where(Char.IsDigit).ToArray()), CultureInfo.InvariantCulture));
                            TimeSpan duration   = Utils.TimeSpanFromString(matchInfo.Groups[4].Value);
                            UInt64   size       = FormattedSize.ToUInt64(matchInfo.Groups[5].Value);
                            UInt32   bitRate    = FormattedSize.ToUInt32(matchInfo.Groups[6].Value);
                            string   encoding   = matchInfo.Groups[7].Value;

                            return(new AudioInfo(channels, sampleRate, sampleSize, duration, size, bitRate, encoding));
                        }

                        catch (Exception ex)
                        {
                            throw new SoxUnexpectedOutputException(output, ex);
                        }
                    }
                }

                throw new SoxUnexpectedOutputException(output != null ? output : "No output received");
            }

            finally
            {
                if (soxProcess_ != null)
                {
                    soxProcess_.Dispose();
                    soxProcess_ = null;
                }
            }
        }