def encode_mp2_from_wav(original_path, mp2_path, options={})
      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]

      wav_sample_size = fmt.sample_bits

      # twolame can only handle up to 16 for floating point (seems to convert to 16 internaly anyway)
      # "Note: the 32-bit samples are currently scaled down to 16-bit samples internally."
      # libtwolame.h  twolame_encode_buffer_float32 http://www.twolame.org/doc/twolame_8h.html#8e77eb0f22479f8ec1bd4f1b042f9cd9
      if (fmt.compression_code.to_i == PCM_FLOATING_COMPRESSION && fmt.sample_bits > 32)
        wav_sample_size = 16
      end

      # input options
      prefix_command = ''
      raw_input      = ''
      sample_rate    = "--samplerate #{fmt.sample_rate}"
      sample_bits    = "--samplesize #{wav_sample_size}"
      channels       = "--channels #{fmt.number_of_channels}"
      input_path     = "'#{original_path}'"

      # output options
      mp2_sample_rate = if MP2_SAMPLE_RATES.include?(options[:sample_rate].to_s)
        options[:sample_rate]
      elsif MP2_SAMPLE_RATES.include?(fmt.sample_rate.to_s)
        fmt.sample_rate.to_s
      else
        '44100'
      end

      if mp2_sample_rate.to_i != fmt.sample_rate.to_i
        prefix_command = "#{bin(:sox)} '#{original_path}' -t raw -r #{mp2_sample_rate} - | "
        input_path = '-'
        raw_input = '--raw-input'
      end

      mode = if TWOLAME_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
        's' # default to joint stereo for 2 channel input
      end
      channel_mode = "--mode #{mode}"

      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 MP2_BITRATES.include?(kbps)
        kbps
      elsif mode == 'm' || (mode =='a' && fmt.number_of_channels <= 1)
        128 # default for monoaural is 128 kbps
      else
        256 # default for stereo/dual channel is 256 kbps
      end
      bit_rate = "--bitrate #{kbps}"

      downmix = (mode == 'm' && fmt.number_of_channels > 1) ? '--downmix' : ''

      # default these headers when options not present
      protect = (options.key?(:protect) && !options[:protect] ) ? '' : '--protect'
      copyright = (options.key?(:copyright) && !options[:copyright] ) ? '' : '--copyright'
      original = (options.key?(:original) && !options[:original] ) ? '--non-original' : '--original'
      emphasis = (options.key?(:emphasis)) ? "--deemphasis #{options[:emphasis]}" : '--deemphasis n'

      ##
      # execute the command
      ##
      input_options = "#{raw_input} #{sample_rate} #{sample_bits} #{channels}"
      output_options = "#{channel_mode} #{bit_rate} #{downmix} #{protect} #{copyright} #{original} #{emphasis}"

      command = "#{prefix_command} #{bin(:twolame)} -t 0 #{input_options} #{output_options} #{input_path} '#{mp2_path}'"
      out, err = run_command(command)
      unless out.split("\n").last =~ TWOLAME_SUCCESS_RE
        raise "encode_mp2_from_wav - twolame response on transcoding was not recognized as a success, #{out}, #{err}"
      end

      # make sure there is a file at the end of this
      check_local_file(mp2_path)

      true
    end
def encode_mp2_from_wav(original_path, mp2_path, options={})
      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]

      wav_sample_size = fmt.sample_bits

      # twolame can only handle up to 16 for floating point (seems to convert to 16 internaly anyway)
      # "Note: the 32-bit samples are currently scaled down to 16-bit samples internally."
      # libtwolame.h  twolame_encode_buffer_float32 http://www.twolame.org/doc/twolame_8h.html#8e77eb0f22479f8ec1bd4f1b042f9cd9
      if (fmt.compression_code.to_i == PCM_FLOATING_COMPRESSION && fmt.sample_bits > 32)
        wav_sample_size = 16
      end

      # input options
      prefix_command = ''
      raw_input      = ''
      sample_rate    = "--samplerate #{fmt.sample_rate}"
      sample_bits    = "--samplesize #{wav_sample_size}"
      channels       = "--channels #{fmt.number_of_channels}"
      input_path     = "'#{original_path}'"

      # output options
      mp2_sample_rate = if MP2_SAMPLE_RATES.include?(options[:sample_rate].to_s)
        options[:sample_rate]
      elsif MP2_SAMPLE_RATES.include?(fmt.sample_rate.to_s)
        fmt.sample_rate.to_s
      else
        '44100'
      end

      if mp2_sample_rate.to_i != fmt.sample_rate.to_i
        prefix_command = "#{bin(:sox)} '#{original_path}' -t raw -r #{mp2_sample_rate} - | "
        input_path = '-'
        raw_input = '--raw-input'
      end

      mode = if TWOLAME_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
        's' # default to joint stereo for 2 channel input
      end
      channel_mode = "--mode #{mode}"

      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 MP2_BITRATES.include?(kbps)
        kbps
      elsif mode == 'm' || (mode =='a' && fmt.number_of_channels <= 1)
        128 # default for monoaural is 128 kbps
      else
        256 # default for stereo/dual channel is 256 kbps
      end
      bit_rate = "--bitrate #{kbps}"

      downmix = (mode == 'm' && fmt.number_of_channels > 1) ? '--downmix' : ''

      # default these headers when options not present
      protect = (options.key?(:protect) && !options[:protect] ) ? '' : '--protect'
      copyright = (options.key?(:copyright) && !options[:copyright] ) ? '' : '--copyright'
      original = (options.key?(:original) && !options[:original] ) ? '--non-original' : '--original'
      emphasis = (options.key?(:emphasis)) ? "--deemphasis #{options[:emphasis]}" : '--deemphasis n'

      ##
      # execute the command
      ##
      input_options = "#{raw_input} #{sample_rate} #{sample_bits} #{channels}"
      output_options = "#{channel_mode} #{bit_rate} #{downmix} #{protect} #{copyright} #{original} #{emphasis}"

      command = "#{prefix_command} #{bin(:twolame)} -t 0 #{input_options} #{output_options} #{input_path} '#{mp2_path}'"
      out, err = run_command(command)
      unless out.split("\n").last =~ TWOLAME_SUCCESS_RE
        raise "encode_mp2_from_wav - twolame response on transcoding was not recognized as a success, #{out}, #{err}"
      end

      # make sure there is a file at the end of this
      check_local_file(mp2_path)

      true
    end
valid options
 :sample_rate
 :bit_rate
 :per_channel_bit_rate
 :channel_mode
 :protect
 :copyright
 :original
 :emphasis