Module: NoteFrequencies

Defined in:
lib/note_frequencies.rb,
lib/note_frequencies/version.rb

Constant Summary collapse

DEFAULT_TUNING =
440
DEFAULT_ROUND =
2
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.delta(name) ⇒ Object

delta in steps from A (relative)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/note_frequencies.rb', line 53

def self.delta(name)
  @deltas ||= begin
    {
      :a => 0,
      :b => 2,
      :c => -9,
      :d => -7,
      :e => -5, 
      :f => -4,
      :g => -2
    }
  end
  @deltas[name.to_sym]
end

.delta_from_name(name) ⇒ Object

delta in steps from a’ (absolute)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/note_frequencies.rb', line 20

def self.delta_from_name(name)
  note, alt, octave = name.scan(/([a-g])(eses|isis|bb|##|es|is|b|#|x)?(\d|(,+)|('+))?/).first
  # puts octave.inspect
  octave_delta = if octave
    if octave.start_with?("'")
      octave.length - 1
    elsif octave.start_with?(",")
      -octave.length - 1
    else
      octave.to_i - 4
    end
  else
    -1
  end
  # puts octave_delta.inspect
  alt_delta = if (alt == 'is' || alt == '#')
    1
  elsif (alt == 'isis' || alt == '##' || alt == 'x')
    2
  elsif (alt == 'es' || alt == 'b')
    -1
  else
    0
  end
  delta(note) + alt_delta + 12 * octave_delta
end

.frequency(delta_from_a, conf = {}) ⇒ Object

frequency from delta



9
10
11
12
13
14
15
16
17
# File 'lib/note_frequencies.rb', line 9

def self.frequency(delta_from_a, conf = {})
  conf = {
    :tuning => DEFAULT_TUNING,
    :round => DEFAULT_ROUND
  }.update(conf)
  tuning = conf[:tuning]
  freq = tuning * (2**(delta_from_a.to_f/12))
  round(freq, conf[:round])
end

.frequency_from_name(name, conf = {}) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/note_frequencies.rb', line 68

def self.frequency_from_name(name, conf = {})
  conf = {
    :tuning => DEFAULT_TUNING,
    :round => DEFAULT_ROUND
  }.update(conf)
  frequency(delta_from_name(name), conf)
end

.round(q, r) ⇒ Object



47
48
49
50
# File 'lib/note_frequencies.rb', line 47

def self.round(q, r)
  pow = 10**r
  (q*pow).round/pow.to_f
end