Class: ShlispTools::Scale

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(degrees = nil) ⇒ Scale

Arg: either nothing, or an array of Ratios



13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Method Details

#<<(degree) ⇒ Object

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



33
34
35
36
# File 'lib/shlisp_tools/scale.rb', line 33

def <<(degree)
  add(degree)
  self
end

#[](idx) ⇒ Object

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



45
46
47
# File 'lib/shlisp_tools/scale.rb', line 45

def [](idx)
  @degrees[idx] rescue nil
end

#add(degree) ⇒ Object

Add a new scale degree (Ratio).



26
27
28
29
30
# File 'lib/shlisp_tools/scale.rb', line 26

def add(degree)
  @degrees << degree
  _sort
  self
end

#canonical(sep = DEF_SEP) ⇒ Object

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



95
96
97
# 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) ⇒ Object

Print out all the deno(minators), multiplied.



109
110
111
# 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) ⇒ Object

iterator



114
115
116
# File 'lib/shlisp_tools/scale.rb', line 114

def each(&block) #:nodoc
  @degrees.each(&block)
end

#empty?Boolean

Return true if scale is empty, false otherwise.

Returns:

  • (Boolean)


55
56
57
# File 'lib/shlisp_tools/scale.rb', line 55

def empty?
  @degrees.empty?
end

#lengthObject

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



50
51
52
# File 'lib/shlisp_tools/scale.rb', line 50

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.



62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Print out the scale (multiplied).



86
87
88
# 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) ⇒ Object

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



104
105
106
# File 'lib/shlisp_tools/scale.rb', line 104

def numes(sep=DEF_SEP)
  @degrees.inject([]) { |out,d| out << d.n; out }.join(sep)
end

#reduced(sep = DEF_SEP) ⇒ Object

:nodoc:



99
100
101
# File 'lib/shlisp_tools/scale.rb', line 99

def reduced(sep=DEF_SEP) #:nodoc:
  canonical(sep)
end

#remove(idx) ⇒ Object

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



39
40
41
42
# File 'lib/shlisp_tools/scale.rb', line 39

def remove(idx)
  @degrees.delete_at(idx) rescue nil
  self
end

#scale(factor) ⇒ Object

:nodoc:



76
77
78
# File 'lib/shlisp_tools/scale.rb', line 76

def scale(factor) #:nodoc:
  mul(factor)
end

#scaled(sep = DEF_SEP) ⇒ Object

:nodoc:



90
91
92
# File 'lib/shlisp_tools/scale.rb', line 90

def scaled(sep=DEF_SEP) #:nodoc:
  multiplied(sep)
end

#to_sObject

Print the scale (multipied).



81
82
83
# File 'lib/shlisp_tools/scale.rb', line 81

def to_s
  multiplied
end