Sha256: 46467f174bd94f2671fde92e8f9ead8119da0d5f9dc2b8ea1952022002dd676c

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module Serinette
  # Gives mixed in classes a method for randomizing effect options
  module SoxOptions
    # Takes a sox effect config and returns randomized Hash.
    #
    # @param config [Array] Sox effect options config
    #
    # @return [string] Randomized effect hash to be used with Sox::Cmd.set_effects
    def self.randomize_options_as_string(configs)
      configs.map do |o|
        stringify_option(o)
      end.compact.join(' ')
    end

    # Takes an individual option and checks for type, (if flag, its either on of
    # off; if traig, randomize within the range given
    #
    # @param option [Option] Sox effect option
    # Looks like this:
    #   {
    #     type: :flag,
    #     name: 'wet-only',
    #     value: '-w'
    #   }
    # or this:
    #   {
    #     type: :trait,
    #     name: 'reverberance',
    #     range: (0..100), <== this can be an array or range, and it should sample
    #     default: 50
    #   {
    #
    # @return [string] Randomized effect hash to be used with Sox::Cmd.set_effects
    def self.stringify_option(option)
      case option[:type]
      when :flag
        return [true, false].sample ? option[:value] : nil
      when :trait # TODO add check for proc here
        return process_trait(option[:value])
      else
        fail Serinette::Error, '#stringify_option requires type of flag or trait'
      end
    end

    def self.process_trait(trait)
      case trait
      when Range
        return trait.to_a.sample
      when Array
        return trait.sample
      when Proc
        return trait.call
      else
        msg = '#process_trait requires trait of type Array, Range, or Proc'
        fail Serinette::Error, msg
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
serinette-0.0.3 lib/mixins/sox_options.rb
serinette-0.0.2 lib/mixins/sox_options.rb
serinette-0.0.0.pre lib/mixins/sox_options.rb