class ShlispTools::Scale

A scale is a series of Ratio instances, always maintained in canonical (reduced) form and ascending order, but available with amplitude scaling for use in a Shlisp/Shnth context.

Constants

DEF_SEP

The default separator (‘ ’) between scale degrees when scale is printed.

Public Class Methods

new(degrees=nil) click to toggle source

Arg: either nothing, or an array of Ratios

# File lib/shlisp_tools/scale.rb, line 13
def initialize(degrees=nil)
  @degrees = []
  if degrees && !degrees.empty?
    degrees.each do |n|
      n = Ratio::parse_new(n) if n.is_a?(String)
      raise "Bad scale degree: #{n.inspect}" unless n.is_a?(Ratio)
      @degrees << n
    end
    _sort
  end
end

Public Instance Methods

<<(degree) click to toggle source

Add a new scal degree (Ratio) using string/array notation; i.e., myScale << Ratio.new(5,4)/

# File lib/shlisp_tools/scale.rb, line 33
def <<(degree)
  add(degree)
  self
end
[](idx) click to toggle source

Return the scale degree at a specified postion (0-based).

# File lib/shlisp_tools/scale.rb, line 45
def [](idx)
  @degrees[idx] rescue nil
end
add(degree) click to toggle source

Add a new scale degree (Ratio).

# File lib/shlisp_tools/scale.rb, line 26
def add(degree)
  @degrees << degree
  _sort
  self
end
canonical(sep=DEF_SEP) click to toggle source

Print out the scale in canonical form (reduced rations).

# File lib/shlisp_tools/scale.rb, line 95
def canonical(sep=DEF_SEP)
  @degrees.inject([]) { |out,d| out << d.to_r.to_s; out }.join(sep)
end
denos(sep=DEF_SEP) click to toggle source

Print out all the deno(minators), multiplied.

# File lib/shlisp_tools/scale.rb, line 109
def denos(sep=DEF_SEP)
  @degrees.inject([]) { |out,d| out << d.d; out }.join(sep)
end
each(&block) click to toggle source

iterator

# File lib/shlisp_tools/scale.rb, line 114
def each(&block) #:nodoc
  @degrees.each(&block)
end
empty?() click to toggle source

Return true if scale is empty, false otherwise.

# File lib/shlisp_tools/scale.rb, line 55
def empty?
  @degrees.empty?
end
length() click to toggle source

Return the number of degrees (notes) in the scale.

# File lib/shlisp_tools/scale.rb, line 50
def length
  @degrees.length
end
mul(factor) click to toggle source

Apply a multiplicative factor to every ratio for Shnth amplitude scaling. For example, myScale.scale(100) transforms 1/1 to 100/100. Other notes are brought into as close a range as possible while keeping all terms in the 1..255 range.

# File lib/shlisp_tools/scale.rb, line 62
def mul(factor)
  unless empty?
    @degrees.each_with_index do |note,i|
      if i == 0
        note.mul(factor)
      else
        tmp = ((factor > note.deno) ? factor : factor*2)
        note.mul((tmp / note.deno).to_i)
      end
    end
  end
  self
end
multiplied(sep=DEF_SEP) click to toggle source

Print out the scale (multiplied).

# File lib/shlisp_tools/scale.rb, line 86
def multiplied(sep=DEF_SEP)
  @degrees.inject([]) { |out,d| out << d.to_s; out }.join(sep)
end
numes(sep=DEF_SEP) click to toggle source

Print out all the nume(rator)s, multiplied.

# File lib/shlisp_tools/scale.rb, line 104
def numes(sep=DEF_SEP)
  @degrees.inject([]) { |out,d| out << d.n; out }.join(sep)
end
remove(idx) click to toggle source

Remove the scale degree at the specified position (0-based).

# File lib/shlisp_tools/scale.rb, line 39
def remove(idx)
  @degrees.delete_at(idx) rescue nil
  self
end
to_s() click to toggle source

Print the scale (multipied).

# File lib/shlisp_tools/scale.rb, line 81
def to_s
  multiplied
end

Private Instance Methods

_sort() click to toggle source
# File lib/shlisp_tools/scale.rb, line 119
def _sort
  @degrees.sort!
end