internal IAudioSource GetAudioSource(int sourceIndex, bool pipe) { SourceInfo sourceInfo = _sources[sourceIndex]; IAudioSource audioSource; if (sourceInfo.Path == null) { audioSource = new SilenceGenerator(sourceInfo.Offset + sourceInfo.Length); } else { if (_isCD) { _ripper.Position = 0; //audioSource = _ripper; if (pipe) audioSource = new AudioPipe(_ripper, 0x100000, false, ThreadPriority.Highest); else audioSource = _ripper; } else if (_isArchive) audioSource = AudioReadWrite.GetAudioSource(sourceInfo.Path, OpenArchive(sourceInfo.Path, false), _config); else audioSource = AudioReadWrite.GetAudioSource(sourceInfo.Path, null, _config); } if (sourceInfo.Offset != 0) { try { audioSource.Position = sourceInfo.Offset; } catch(Exception ex) { audioSource.Close(); throw ex; } } //if (!(audioSource is AudioPipe) && !(audioSource is UserDefinedReader) && _config.separateDecodingThread) if (!(audioSource is AudioPipe) && pipe) audioSource = new AudioPipe(audioSource, 0x10000); return audioSource; }
static int Main(string[] args) { TextWriter stdout = Console.Out; Console.SetOut(Console.Error); var settings = new FLACCLWriterSettings(); TimeSpan lastPrint = TimeSpan.FromMilliseconds(0); bool debug = false, quiet = false; string stereo_method = null; string window_function = null; string input_file = null; string output_file = null; string device_type = null; int min_partition_order = -1, max_partition_order = -1, min_lpc_order = -1, max_lpc_order = -1, min_fixed_order = -1, max_fixed_order = -1, min_precision = -1, max_precision = -1, orders_per_window = -1, orders_per_channel = -1, blocksize = -1; int input_len = 4096, input_val = 0, input_bps = 16, input_ch = 2, input_rate = 44100; int level = -1, padding = -1, vbr_mode = -1; bool do_seektable = true; bool estimate_window = false; bool buffered = false; bool ok = true; int intarg; for (int arg = 0; arg < args.Length; arg++) { if (args[arg].Length == 0) ok = false; else if (args[arg] == "--debug") debug = true; else if ((args[arg] == "-q" || args[arg] == "--quiet")) quiet = true; else if (args[arg] == "--verify") settings.DoVerify = true; else if (args[arg] == "--no-seektable") do_seektable = false; else if (args[arg] == "--slow-gpu") settings.GPUOnly = false; else if (args[arg] == "--fast-gpu") settings.DoRice = true; else if (args[arg] == "--no-md5") settings.DoMD5 = false; else if (args[arg] == "--buffered") buffered = true; else if (args[arg] == "--cpu-threads") { int val = settings.CPUThreads; ok = (++arg < args.Length) && int.TryParse(args[arg], out val); settings.CPUThreads = val; } else if (args[arg] == "--group-size" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) settings.GroupSize = intarg; else if (args[arg] == "--task-size" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) settings.TaskSize = intarg; else if (args[arg] == "--define" && arg + 2 < args.Length) settings.Defines += "#define " + args[++arg] + " " + args[++arg] + "\n"; else if (args[arg] == "--opencl-platform" && ++arg < args.Length) settings.Platform = args[arg]; else if (args[arg] == "--mapped-memory") settings.MappedMemory = true; else if (args[arg] == "--opencl-type" && ++arg < args.Length) device_type = args[arg]; else if (args[arg] == "--input-length" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) input_len = intarg; else if (args[arg] == "--input-value" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) input_val = intarg; else if (args[arg] == "--input-bps" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) input_bps = intarg; else if (args[arg] == "--input-channels" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) input_ch = intarg; else if ((args[arg] == "-o" || args[arg] == "--output") && ++arg < args.Length) output_file = args[arg]; else if ((args[arg] == "-s" || args[arg] == "--stereo") && ++arg < args.Length) stereo_method = args[arg]; else if ((args[arg] == "-w" || args[arg] == "--window") && ++arg < args.Length) window_function = args[arg]; else if ((args[arg] == "-r" || args[arg] == "--partition-order") && ++arg < args.Length) { ok = (args[arg].Split(',').Length == 2 && int.TryParse(args[arg].Split(',')[0], out min_partition_order) && int.TryParse(args[arg].Split(',')[1], out max_partition_order)) || int.TryParse(args[arg], out max_partition_order); } else if ((args[arg] == "-l" || args[arg] == "--lpc-order") && ++arg < args.Length) { ok = (args[arg].Split(',').Length == 2 && int.TryParse(args[arg].Split(',')[0], out min_lpc_order) && int.TryParse(args[arg].Split(',')[1], out max_lpc_order)) || int.TryParse(args[arg], out max_lpc_order); } else if (args[arg] == "--fixed-order" && ++arg < args.Length) { ok = (args[arg].Split(',').Length == 2 && int.TryParse(args[arg].Split(',')[0], out min_fixed_order) && int.TryParse(args[arg].Split(',')[1], out max_fixed_order)) || int.TryParse(args[arg], out max_fixed_order); } else if ((args[arg] == "-c" || args[arg] == "--max-precision") && ++arg < args.Length) { ok = (args[arg].Split(',').Length == 2 && int.TryParse(args[arg].Split(',')[0], out min_precision) && int.TryParse(args[arg].Split(',')[1], out max_precision)) || int.TryParse(args[arg], out max_precision); } else if ((args[arg] == "-v" || args[arg] == "--vbr")) ok = (++arg < args.Length) && int.TryParse(args[arg], out vbr_mode); else if (args[arg] == "--orders-per-window" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) orders_per_window = intarg; else if (args[arg] == "--orders-per-channel" && ++arg < args.Length && int.TryParse(args[arg], out intarg)) orders_per_channel = intarg; else if (args[arg] == "--estimate-window") estimate_window = true; else if ((args[arg] == "-b" || args[arg] == "--blocksize") && ++arg < args.Length && int.TryParse(args[arg], out intarg)) blocksize = intarg; else if ((args[arg] == "-p" || args[arg] == "--padding") && ++arg < args.Length && int.TryParse(args[arg], out intarg)) padding = intarg; else if (args[arg] != "-" && args[arg][0] == '-' && int.TryParse(args[arg].Substring(1), out level)) ok = level >= 0 && level <= 11; else if ((args[arg][0] != '-' || args[arg] == "-") && input_file == null) input_file = args[arg]; else ok = false; if (!ok) break; } if (!quiet) { Console.WriteLine("{0}, Copyright (C) 2010 Grigory Chudov.", FLACCLWriter.vendor_string); Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to"); Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details."); } if (!ok || input_file == null) { Usage(); return 1; } if (((input_file == "-" || Path.GetExtension(input_file) == ".flac") && output_file == null)) { Console.WriteLine(); Console.WriteLine("Output file not specified."); Console.WriteLine(); Usage(); return 2; } IAudioSource audioSource; try { if (input_file == "-") audioSource = new WAVReader("", Console.OpenStandardInput()); else if (input_file == "nul") audioSource = new SilenceGenerator(new AudioPCMConfig(input_bps, input_ch, input_rate), input_len, input_val); else if (File.Exists(input_file) && Path.GetExtension(input_file) == ".wav") audioSource = new WAVReader(input_file, null); else if (File.Exists(input_file) && Path.GetExtension(input_file) == ".flac") audioSource = new FlakeReader(input_file, null); else { Usage(); return 2; } } catch (Exception ex) { Usage(); Console.WriteLine(""); Console.WriteLine("Error: {0}.", ex.Message); return 3; } if (buffered) audioSource = new AudioPipe(audioSource, FLACCLWriter.MAX_BLOCKSIZE); if (output_file == null) output_file = Path.ChangeExtension(input_file, "flac"); FLACCLWriter encoder = new FLACCLWriter((output_file == "-" || output_file == "nul") ? "" : output_file, output_file == "-" ? Console.OpenStandardOutput() : output_file == "nul" ? new NullStream() : null, audioSource.PCM); encoder.FinalSampleCount = audioSource.Length; IAudioDest audioDest = encoder; AudioBuffer buff = new AudioBuffer(audioSource, FLACCLWriter.MAX_BLOCKSIZE); try { if (device_type != null) settings.DeviceType = (OpenCLDeviceType)(Enum.Parse(typeof(OpenCLDeviceType), device_type, true)); encoder.Settings = settings; if (level >= 0) encoder.CompressionLevel = level; if (stereo_method != null) encoder.StereoMethod = Flake.LookupStereoMethod(stereo_method); if (window_function != null) encoder.WindowFunction = Flake.LookupWindowFunction(window_function); if (min_partition_order >= 0) encoder.MinPartitionOrder = min_partition_order; if (max_partition_order >= 0) encoder.MaxPartitionOrder = max_partition_order; if (min_lpc_order >= 0) encoder.MinLPCOrder = min_lpc_order; if (max_lpc_order >= 0) encoder.MaxLPCOrder = max_lpc_order; if (min_fixed_order >= 0) encoder.MinFixedOrder = min_fixed_order; if (max_fixed_order >= 0) encoder.MaxFixedOrder = max_fixed_order; if (max_precision >= 0) encoder.MaxPrecisionSearch = max_precision; if (min_precision >= 0) encoder.MinPrecisionSearch = min_precision; if (blocksize >= 0) encoder.BlockSize = blocksize; if (padding >= 0) encoder.Padding = padding; if (vbr_mode >= 0) encoder.VBRMode = vbr_mode; if (orders_per_window >= 0) encoder.OrdersPerWindow = orders_per_window; if (orders_per_channel >= 0) encoder.OrdersPerChannel = orders_per_channel; if (estimate_window) encoder.EstimateWindow = estimate_window; encoder.DoSeekTable = do_seektable; } catch (Exception ex) { Usage(); Console.WriteLine(""); Console.WriteLine("Error: {0}.", ex.Message); return 3; } if (!quiet) { Console.WriteLine("Filename : {0}", input_file); Console.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.PCM.SampleRate, audioSource.PCM.ChannelCount, audioSource.PCM.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.PCM.SampleRate)); } DateTime start = DateTime.Now; try { audioDest.Write(buff); start = DateTime.Now; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); TimeSpan elapsed = DateTime.Now - start; if (!quiet) { if ((elapsed - lastPrint).TotalMilliseconds > 60) { Console.Error.Write("\rProgress : {0:00}%; {1:0.00}x; {2}/{3}", 100.0 * audioSource.Position / audioSource.Length, audioSource.Position / elapsed.TotalSeconds / audioSource.PCM.SampleRate, elapsed, TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds / audioSource.Position * audioSource.Length) ); lastPrint = elapsed; } } } audioDest.Close(); } catch (OpenCLNet.OpenCLBuildException ex) { Console.Error.Write("\r \r"); Console.WriteLine("Error : {0}", ex.Message); Console.WriteLine("{0}", ex.BuildLogs[0]); if (debug) using (StreamWriter sw = new StreamWriter("debug.txt", true)) sw.WriteLine("{0}\n{1}\n{2}", ex.Message, ex.StackTrace, ex.BuildLogs[0]); audioDest.Delete(); audioSource.Close(); return 4; } #if !DEBUG catch (Exception ex) { Console.Error.Write("\r \r"); Console.WriteLine("Error : {0}", ex.Message); if (debug) using (StreamWriter sw = new StreamWriter("debug.txt", true)) sw.WriteLine("{0}\n{1}", ex.Message, ex.StackTrace); audioDest.Delete(); audioSource.Close(); return 4; } #endif TimeSpan totalElapsed = DateTime.Now - start; if (!quiet) { Console.Error.Write("\r \r"); Console.WriteLine("Results : {0:0.00}x; {2} bytes in {1} seconds;", audioSource.Position / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate, totalElapsed, encoder.TotalSize ); } audioSource.Close(); if (debug) { Console.SetOut(stdout); Console.Out.WriteLine("{0}\t{1}\t{2}\t{3}\t{4} ({5})\t{6}/{7}+{12}{13}\t{8}..{9}\t{10}\t{11}", encoder.TotalSize, encoder.UserProcessorTime.TotalSeconds > 0 ? encoder.UserProcessorTime.TotalSeconds : totalElapsed.TotalSeconds, (encoder.StereoMethod.ToString() + (encoder.OrdersPerChannel == 32 ? "" : "(" + encoder.OrdersPerChannel.ToString() + ")")).PadRight(15), encoder.WindowFunction.ToString().PadRight(15), encoder.MaxPartitionOrder, settings.GPUOnly ? "GPU" : "CPU", encoder.OrdersPerWindow, encoder.MaxLPCOrder, encoder.MinPrecisionSearch, encoder.MaxPrecisionSearch, encoder.BlockSize, encoder.VBRMode, encoder.MaxFixedOrder - encoder.MinFixedOrder + 1, encoder.DoConstant ? "c" : "" ); } return 0; }