Class: Sox::Combiner

Inherits:
Object
  • Object
show all
Defined in:
lib/sox/combiner.rb

Overview

Combines input files. Technically it calls sox with --combine option, but allows you not to care about the rates and numbers of channels in the input files. It converts them to the same rates/channels using temporary mediate files.

Examples:

# Concatenate
combiner = Sox::Combiner.new('in1.mp3', 'in2.ogg', 'in3.wav', :combine => :concatenate)
combiner.write('out.mp3')

Defined Under Namespace

Classes: BaseStrategy, ProcessSubstitutionStrategy, TmpFileStrategy

Constant Summary collapse

DEFAULT_OPTIONS =

Default options

{
  # Method to be used for combining sounds. See --combine of sox tool.
  :combine => :concatenate,

  # Number of channels in the output file.
  :channels => 1,

  # Rate(samples per seconds) of the output file.
  :rate => 22050,

  # Apply norm effect on output.
  :norm => false,

  # Strategy to convert input files into files with the same rates
  # and channels.
  :strategy => :process_substitution
}
STRATEGIES =

Mapping of strategy names and their implementations

{
  :tmp_file             => TmpFileStrategy,
  :process_substitution => ProcessSubstitutionStrategy
}

Instance Method Summary collapse

Constructor Details

#initialize(input_files, options = {}) ⇒ Combiner

Returns a new instance of Combiner.

Parameters:

  • input_files (Array<String>)

    input files

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :combine (Symbol)

    value for --combine sox option. Use underscore instead of hyphen, e.g. :mix_power.

  • :channels (Integer)

    number of channels in output file.

  • :rate (Integer)

    rate of output file

  • :norm (Boolean)

    apply norm effect on output.

  • :strategy (Symbol)

    strategy to treat temporary files, default is :process_substitution which reduces disk IO.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
# File 'lib/sox/combiner.rb', line 53

def initialize(input_files, options = {})
  raise(ArgumentError, "Input files are missing") if input_files.empty?

  opts           = DEFAULT_OPTIONS.merge(options)
  strategy_name  = opts.delete(:strategy)
  strategy_class = STRATEGIES[strategy_name]
  raise(ArgumentError, "Unknown strategy #{strategy_name.inspect}") unless strategy_class

  @strategy      = strategy_class.new(input_files, opts)
end

Instance Method Details

#write(output_file) ⇒ void

This method returns an undefined value.

Run sox command and write output to file.

Parameters:

  • output_file (String)

    path of output file



69
70
71
# File 'lib/sox/combiner.rb', line 69

def write(output_file)
  @strategy.write(output_file)
end