Class: ShlispTools::Scale
- Inherits:
-
Object
- Object
- ShlispTools::Scale
- Includes:
- Enumerable
- Defined in:
- lib/shlisp_tools/scale.rb
Overview
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.
Constant Summary collapse
- DEF_SEP =
The default separator (‘ ’) between scale degrees when scale is printed.
' '
Instance Method Summary collapse
-
#<<(degree) ⇒ Object
Add a new scal degree (Ratio) using string/array notation; i.e., myScale << Ratio.new(5,4)/.
-
#[](idx) ⇒ Object
Return the scale degree at a specified postion (0-based).
-
#add(degree) ⇒ Object
Add a new scale degree (Ratio).
-
#canonical(sep = DEF_SEP) ⇒ Object
Print out the scale in canonical form (reduced rations).
-
#denos(sep = DEF_SEP) ⇒ Object
Print out all the deno(minators), multiplied.
-
#each(&block) ⇒ Object
iterator.
-
#empty? ⇒ Boolean
Return true if scale is empty, false otherwise.
-
#initialize(degrees = nil) ⇒ Scale
constructor
Arg: either nothing, or an array of Ratios.
-
#length ⇒ Object
Return the number of degrees (notes) in the scale.
-
#mul(factor) ⇒ Object
Apply a multiplicative factor to every ratio for Shnth amplitude scaling.
-
#multiplied(sep = DEF_SEP) ⇒ Object
Print out the scale (multiplied).
-
#numes(sep = DEF_SEP) ⇒ Object
Print out all the nume(rator)s, multiplied.
-
#reduced(sep = DEF_SEP) ⇒ Object
:nodoc:.
-
#remove(idx) ⇒ Object
Remove the scale degree at the specified position (0-based).
-
#scale(factor) ⇒ Object
:nodoc:.
-
#scaled(sep = DEF_SEP) ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
Print the scale (multipied).
Constructor Details
#initialize(degrees = nil) ⇒ Scale
Arg: either nothing, or an array of Ratios
13 14 15 16 17 18 19 |
# File 'lib/shlisp_tools/scale.rb', line 13 def initialize(degrees=nil) @degrees = [] if degrees && !degrees.empty? degrees.each { |n| @degrees << n } _sort end end |
Instance Method Details
#<<(degree) ⇒ Object
Add a new scal degree (Ratio) using string/array notation; i.e., myScale << Ratio.new(5,4)/
29 30 31 32 |
# File 'lib/shlisp_tools/scale.rb', line 29 def <<(degree) add(degree) self end |
#[](idx) ⇒ Object
Return the scale degree at a specified postion (0-based).
41 42 43 |
# File 'lib/shlisp_tools/scale.rb', line 41 def [](idx) @degrees[idx] rescue nil end |
#add(degree) ⇒ Object
Add a new scale degree (Ratio).
22 23 24 25 26 |
# File 'lib/shlisp_tools/scale.rb', line 22 def add(degree) @degrees << degree _sort self end |
#canonical(sep = DEF_SEP) ⇒ Object
Print out the scale in canonical form (reduced rations).
91 92 93 |
# File 'lib/shlisp_tools/scale.rb', line 91 def canonical(sep=DEF_SEP) @degrees.inject([]) { |out,d| out << d.to_r.to_s; out }.join(sep) end |
#denos(sep = DEF_SEP) ⇒ Object
Print out all the deno(minators), multiplied.
105 106 107 |
# File 'lib/shlisp_tools/scale.rb', line 105 def denos(sep=DEF_SEP) @degrees.inject([]) { |out,d| out << d.d; out }.join(sep) end |
#each(&block) ⇒ Object
iterator
110 111 112 |
# File 'lib/shlisp_tools/scale.rb', line 110 def each(&block) #:nodoc @degrees.each(&block) end |
#empty? ⇒ Boolean
Return true if scale is empty, false otherwise.
51 52 53 |
# File 'lib/shlisp_tools/scale.rb', line 51 def empty? @degrees.empty? end |
#length ⇒ Object
Return the number of degrees (notes) in the scale.
46 47 48 |
# File 'lib/shlisp_tools/scale.rb', line 46 def length @degrees.length end |
#mul(factor) ⇒ Object
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.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/shlisp_tools/scale.rb', line 58 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) ⇒ Object
Print out the scale (multiplied).
82 83 84 |
# File 'lib/shlisp_tools/scale.rb', line 82 def multiplied(sep=DEF_SEP) @degrees.inject([]) { |out,d| out << d.to_s; out }.join(sep) end |
#numes(sep = DEF_SEP) ⇒ Object
Print out all the nume(rator)s, multiplied.
100 101 102 |
# File 'lib/shlisp_tools/scale.rb', line 100 def numes(sep=DEF_SEP) @degrees.inject([]) { |out,d| out << d.n; out }.join(sep) end |
#reduced(sep = DEF_SEP) ⇒ Object
:nodoc:
95 96 97 |
# File 'lib/shlisp_tools/scale.rb', line 95 def reduced(sep=DEF_SEP) #:nodoc: canonical(sep) end |
#remove(idx) ⇒ Object
Remove the scale degree at the specified position (0-based).
35 36 37 38 |
# File 'lib/shlisp_tools/scale.rb', line 35 def remove(idx) @degrees.delete_at(idx) rescue nil self end |
#scale(factor) ⇒ Object
:nodoc:
72 73 74 |
# File 'lib/shlisp_tools/scale.rb', line 72 def scale(factor) #:nodoc: mul(factor) end |
#scaled(sep = DEF_SEP) ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/shlisp_tools/scale.rb', line 86 def scaled(sep=DEF_SEP) #:nodoc: multiplied(sep) end |
#to_s ⇒ Object
Print the scale (multipied).
77 78 79 |
# File 'lib/shlisp_tools/scale.rb', line 77 def to_s multiplied end |