class Musicality::Function

Constants

DOMAIN_MAX

Attributes

domain[R]

Public Class Methods

new(domain = (DOMAIN_MIN...DOMAIN_MAX), memoize: true, &at_block) click to toggle source
# File lib/musicality/notation/util/function.rb, line 17
def initialize domain = (DOMAIN_MIN...DOMAIN_MAX), memoize: true, &at_block
  raise ArgumentError unless domain.last > domain.first
  @domain = domain
  raise ArgumentError unless block_given?
  raise ArgumentError unless at_block.arity == 1
  @at_block = at_block

  @memoize = memoize
  @memoized = {}
end
transform_domains(start_domain, end_domain, x) click to toggle source

x in start domain, transformed to x in end domain

# File lib/musicality/notation/util/function.rb, line 11
def self.transform_domains start_domain, end_domain, x
  perc = (x - start_domain.first) / (start_domain.last - start_domain.first).to_f
  return perc * (end_domain.last - end_domain.first) + end_domain.first
end

Public Instance Methods

==(other) click to toggle source
# File lib/musicality/notation/util/function.rb, line 41
def ==(other)
  @domain == other.domain
end
at(x) click to toggle source
# File lib/musicality/notation/util/function.rb, line 28
def at(x)
  raise DomainError unless @domain.include?(x)
  if @memoize
    if @memoized.has_key? x
      @memoized[x]
    else
      @memoized[x] = @at_block.call(x)
    end
  else
    @at_block.call(x)
  end
end
sample(xrange, srate) click to toggle source
# File lib/musicality/notation/util/function.rb, line 6
def sample xrange, srate
  xrange.step(Rational(1,srate)).map { |x| at(x) }
end