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
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
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 a new scale degree (Ratio
).
# File lib/shlisp_tools/scale.rb, line 26 def add(degree) @degrees << degree _sort self end
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
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
iterator
# File lib/shlisp_tools/scale.rb, line 114 def each(&block) #:nodoc @degrees.each(&block) end
Return true if scale is empty, false otherwise.
# File lib/shlisp_tools/scale.rb, line 55 def empty? @degrees.empty? end
Return the number of degrees (notes) in the scale.
# File lib/shlisp_tools/scale.rb, line 50 def length @degrees.length end
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
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
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 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
Print the scale (multipied).
# File lib/shlisp_tools/scale.rb, line 81 def to_s multiplied end
Private Instance Methods
# File lib/shlisp_tools/scale.rb, line 119 def _sort @degrees.sort! end