# Copyright (C) 2013-2016 all contributors # License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt) # frozen_string_literal: true # encoding: binary require_relative '../../dtas' require_relative '../source' require_relative '../replaygain' require_relative '../xs' # this is usually one input file class DTAS::Source::Sox # :nodoc: require_relative 'file' include DTAS::Source::File include DTAS::XS extend DTAS::XS SOX_DEFAULTS = COMMAND_DEFAULTS.merge( "command" => 'exec sox "$INFILE" $SOXFMT - $TRIMFX $RGFX', "tryorder" => 0, ) # we use this to be less noisy when seeking a file def try_to_fail_harder(infile, s, cmd) msg = nil case s when %r{\A0\s*\z} then msg = "detected zero samples" when Process::Status then msg = "failed with #{s.exitstatus}" end if msg return if @last_failed == infile @last_failed = infile return warn("`#{xs(cmd)}' #{msg}") end true end def initialize @last_failed = nil command_init(SOX_DEFAULTS) end def try(infile, offset = nil, trim = nil) err = "".b cmd = %W(soxi -s #{infile}) s = qx(@env.dup, cmd, err_str: err, no_raise: true) return if err =~ /soxi FAIL formats:/ try_to_fail_harder(infile, s, cmd) or return source_file_dup(infile, offset, trim) end def format @format ||= DTAS::Format.from_file(@env, @infile) end def duration samples / format.rate.to_f end # This is the number of samples according to the samples in the source # file itself, not the decoded output def samples @samples ||= qx(@env, %W(soxi -s #@infile)).to_i rescue => e warn e.message 0 end # just run soxi -a def __load_comments tmp = {} case @infile when String qx(@env, %W(soxi -a #@infile)).split("\n").each do |line| key, value = line.split('=', 2) key && value or next # TODO: multi-line/multi-value/repeated tags tmp[key.upcase.freeze] = value end end tmp end def src_spawn(player_format, rg_state, opts) raise "BUG: #{self.inspect}#src_spawn called twice" if @to_io e = @env.merge!(player_format.to_env) e["INFILE"] = xs(@infile) # make sure these are visible to the "current" command... e["TRIMFX"] = trimfx e["RGFX"] = rg_state.effect(self) || nil e.merge!(@rg.to_env) if @rg @pid = dtas_spawn(e, command_string, opts) end def to_hsh to_hash.delete_if { |k,v| v == SOX_DEFAULTS[k] } end def source_defaults SOX_DEFAULTS end end