def encode_mp3_from_wav(original_path, mp3_path, options={})
      logger.info "encode_mp3_from_wav: #{original_path}, #{mp3_path}, #{options.inspect}"

      check_local_file(original_path)

      options.to_options!
      # parse the wave to see what values to use if not overridden by the options
      wf = WaveFile.parse(original_path)
      fmt = wf.chunks[:fmt]

      input_path = '-'

      mp3_sample_rate = if MP3_SAMPLE_RATES.include?(options[:sample_rate].to_s)
        options[:sample_rate].to_s
      elsif MP3_SAMPLE_RATES.include?(fmt.sample_rate.to_s)
        logger.debug "sample_rate:  fmt.sample_rate = #{fmt.sample_rate}"
        fmt.sample_rate.to_s
      else
        '44100'
      end
      logger.debug "mp3_sample_rate: #{options[:sample_rate]}, #{fmt.sample_rate}"

      mode = if LAME_MODES.include?(options[:channel_mode])
        options[:channel_mode] #use the channel mode from the options if specified
      elsif fmt.number_of_channels <= 1
        'm' # default to monoaural for 1 channel input
      else
        'j' # default to joint stereo for 2 channel input
      end
      channel_mode = "-m #{mode}"

      # if mono selected, but input is in stereo, need to specify downmix to 1 channel for sox
      downmix = (mode == 'm' && fmt.number_of_channels > 1) ? '-c 1' : ''

      # if sample rate different, change that as well in sox before piping to lame
      resample = (mp3_sample_rate.to_i != fmt.sample_rate.to_i) ? "-r #{mp3_sample_rate} " : ''
      logger.debug "resample: #{resample} from comparing #{mp3_sample_rate} #{fmt.sample_rate}"

      # output to wav (-t wav) has a warning
      # '/usr/local/bin/sox wav: Length in output .wav header will be wrong since can't seek to fix it'
      # that messsage can safely be ignored, wa output is easier/safer for lame to recognize, so worth ignoring this message
      prefix_command = "#{bin(:sox)} '#{original_path}' -t wav #{resample} #{downmix} - | "

      kbps = if options[:per_channel_bit_rate]
        options[:per_channel_bit_rate].to_i * ((mode == 'm') ? 1 : 2)
      elsif options[:bit_rate]
        options[:bit_rate].to_i
      else
        0
      end

      kbps = if MP3_BITRATES.include?(kbps)
        kbps
      elsif mode == 'm'
        128 # default for monoaural is 128 kbps
      else
        256 # default for stereo/dual channel is 256 kbps
      end
      bit_rate = "--cbr -b #{kbps}"

      ##
      # execute the command
      ##
      output_options = "#{channel_mode} #{bit_rate}"

      command = "#{prefix_command} #{bin(:lame)} -S #{output_options} #{input_path} '#{mp3_path}'"

      out, err = run_command(command)

      unless out.split("\n")[-1] =~ LAME_SUCCESS_RE
        raise "encode_mp3_from_wav - lame completion unsuccessful: #{out}"
      end

      err.split("\n").each do |l|
        if l =~ LAME_ERROR_RE
          raise "encode_mp3_from_wav - lame response had fatal error: #{l}"
        end
      end
      logger.debug "encode_mp3_from_wav: end!"

      check_local_file(mp3_path)

      true
    end
def encode_mp3_from_wav(original_path, mp3_path, options={})
      logger.info "encode_mp3_from_wav: #{original_path}, #{mp3_path}, #{options.inspect}"

      check_local_file(original_path)

      options.to_options!
      # parse the wave to see what values to use if not overridden by the options
      wf = WaveFile.parse(original_path)
      fmt = wf.chunks[:fmt]

      input_path = '-'

      mp3_sample_rate = if MP3_SAMPLE_RATES.include?(options[:sample_rate].to_s)
        options[:sample_rate].to_s
      elsif MP3_SAMPLE_RATES.include?(fmt.sample_rate.to_s)
        logger.debug "sample_rate:  fmt.sample_rate = #{fmt.sample_rate}"
        fmt.sample_rate.to_s
      else
        '44100'
      end
      logger.debug "mp3_sample_rate: #{options[:sample_rate]}, #{fmt.sample_rate}"

      mode = if LAME_MODES.include?(options[:channel_mode])
        options[:channel_mode] #use the channel mode from the options if specified
      elsif fmt.number_of_channels <= 1
        'm' # default to monoaural for 1 channel input
      else
        'j' # default to joint stereo for 2 channel input
      end
      channel_mode = "-m #{mode}"

      # if mono selected, but input is in stereo, need to specify downmix to 1 channel for sox
      downmix = (mode == 'm' && fmt.number_of_channels > 1) ? '-c 1' : ''

      # if sample rate different, change that as well in sox before piping to lame
      resample = (mp3_sample_rate.to_i != fmt.sample_rate.to_i) ? "-r #{mp3_sample_rate} " : ''
      logger.debug "resample: #{resample} from comparing #{mp3_sample_rate} #{fmt.sample_rate}"

      # output to wav (-t wav) has a warning
      # '/usr/local/bin/sox wav: Length in output .wav header will be wrong since can't seek to fix it'
      # that messsage can safely be ignored, wa output is easier/safer for lame to recognize, so worth ignoring this message
      prefix_command = "#{bin(:sox)} '#{original_path}' -t wav #{resample} #{downmix} - | "

      kbps = if options[:per_channel_bit_rate]
        options[:per_channel_bit_rate].to_i * ((mode == 'm') ? 1 : 2)
      elsif options[:bit_rate]
        options[:bit_rate].to_i
      else
        0
      end

      kbps = if MP3_BITRATES.include?(kbps)
        kbps
      elsif mode == 'm'
        128 # default for monoaural is 128 kbps
      else
        256 # default for stereo/dual channel is 256 kbps
      end
      bit_rate = "--cbr -b #{kbps}"

      ##
      # execute the command
      ##
      output_options = "#{channel_mode} #{bit_rate}"

      command = "#{prefix_command} #{bin(:lame)} -S #{output_options} #{input_path} '#{mp3_path}'"

      out, err = run_command(command)

      unless out.split("\n")[-1] =~ LAME_SUCCESS_RE
        raise "encode_mp3_from_wav - lame completion unsuccessful: #{out}"
      end

      err.split("\n").each do |l|
        if l =~ LAME_ERROR_RE
          raise "encode_mp3_from_wav - lame response had fatal error: #{l}"
        end
      end
      logger.debug "encode_mp3_from_wav: end!"

      check_local_file(mp3_path)

      true
    end
valid options
 :sample_rate
 :bit_rate
 :channel_mode