/// <summary> /// Spawns a new SoX process using the specified options in this instance. /// </summary> /// <param name="inputFiles">Audio files to be processed.</param> /// <param name="outputFile">Output file.</param> /// <param name="combination">How to combine the input files.</param> public void Process(InputFile[] inputFiles, string outputFile, CombinationType combination) { soxProcess_ = SoxProcess.Create(Path); lastError_ = null; lastErrorSource_ = null; try { soxProcess_.ErrorDataReceived += OnSoxProcessOutputReceived; soxProcess_.OutputDataReceived += OnSoxProcessOutputReceived; List <string> args = new List <string>(); // Global options. if (Buffer.HasValue) { args.Add("--buffer " + Buffer.Value); } if (Multithreaded.HasValue) { args.Add(Multithreaded.Value ? "--multi-threaded" : "--single-threaded"); } if (!String.IsNullOrEmpty(CustomArgs)) { args.Add(CustomArgs); } switch (combination) { case CombinationType.Concatenate: args.Add("--combine concatenate"); break; case CombinationType.Merge: args.Add("--combine merge"); break; case CombinationType.Mix: args.Add("--combine mix"); break; case CombinationType.MixPower: args.Add("--combine mix-power"); break; case CombinationType.Multiply: args.Add("--combine multiply"); break; case CombinationType.Sequence: args.Add("--combine sequence"); break; default: // Do nothing. break; } args.Add("--show-progress"); // Input options and files. if ((inputFiles != null) && (inputFiles.Length > 0)) { foreach (InputFile inputFile in inputFiles) { args.Add(inputFile.ToString()); } } else { args.Add("--null"); } // Output options and file. args.Add(Output.ToString()); if (!string.IsNullOrEmpty(outputFile)) { if (outputFile.Contains(" ")) { if ((Environment.OSVersion.Platform == PlatformID.Win32NT) || (Environment.OSVersion.Platform == PlatformID.Win32Windows) || (Environment.OSVersion.Platform == PlatformID.Win32S) || (Environment.OSVersion.Platform == PlatformID.WinCE)) { args.Add("\"" + outputFile + "\""); } else { args.Add("'" + outputFile + "'"); } } else { args.Add(outputFile); } } else { args.Add("--null"); } // Effects. foreach (IBaseEffect effect in Effects) { args.Add(effect.ToString()); } // Custom effects. args.Add(CustomEffects); soxProcess_.StartInfo.Arguments = String.Join(" ", args); LastCommand = Path + " " + soxProcess_.StartInfo.Arguments; try { soxProcess_.Start(); soxProcess_.BeginOutputReadLine(); soxProcess_.BeginErrorReadLine(); soxProcess_.WaitForExit(); } catch (Exception ex) { throw new SoxException("Cannot spawn SoX process", ex); } if (!String.IsNullOrEmpty(lastError_)) { if (String.IsNullOrEmpty(lastErrorSource_)) { throw new SoxException(lastError_); } switch (lastErrorSource_) { case "getopt": throw new SoxException("Invalid parameter: " + lastError_); default: throw new SoxException("Processing error: " + lastError_); } } } finally { if (soxProcess_ != null) { soxProcess_.Dispose(); soxProcess_ = null; } } }
/// <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; } } }