Class: JazzModel::Key

Inherits:
Base
  • Object
show all
Defined in:
lib/jazz_model/key.rb

Overview

A key represents a specific theoretic tone mapped to a pitch and a letter index. Keys are defined for all letters A-G and all sharp/flat values within two semitons (e.g. Dbb, D Double-Flat enharmonic with C).

Each key is associated an index between 0-11 to represent the actual pitch within the chromatic scale.

Each key is also assigned a “letter index” which represents a relative letter value. This index is crucial as it disambiguates theoretical tone differences such as Eb and D#. Doing so also supports correct double-sharp and double-flat interpretations which are rare but theoretically different from their enharmonic counterparts.

Constant Summary collapse

Letters =
['C' => 0, 'D' => 1, 'E' => 2, 'F' => 3, 'G' => 4, 'A' => 5, 'B' => 6]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

load_definitions

Class Method Details

.[](value) ⇒ Object

Finds a key object based on the name: Key



30
31
32
# File 'lib/jazz_model/key.rb', line 30

def self.[](value)
  self.from_name(value) || self.from_index(value)
end

.defaultObject



39
40
41
# File 'lib/jazz_model/key.rb', line 39

def self.default
  self['C']
end

.from_index(value, preferred_letter = nil) ⇒ Object

Finds a key given a tonal index 0-11 and a letter index (to disambiguate enharmonic keys)



18
19
20
# File 'lib/jazz_model/key.rb', line 18

def self.from_index(value, preferred_letter = nil)
  self.all.find {|k| k.index == value && (preferred_letter.nil? || k.letter_index == preferred_letter)}
end

.from_name(value) ⇒ Object

Finds a key object given the name (such as Eb)



23
24
25
# File 'lib/jazz_model/key.rb', line 23

def self.from_name(value)
  self.all.find {|k| k.name == value}
end

.primariesObject

Returns an array of the 12 primary keys (definitions around the cycle of fourths)



35
36
37
# File 'lib/jazz_model/key.rb', line 35

def self.primaries
  self.find_all_by_primary(true)
end

Instance Method Details

#enharmonic_with?(another_key) ⇒ Boolean

Tests whether the current key is enharmonic with another_key.

Returns:

  • (Boolean)


44
45
46
# File 'lib/jazz_model/key.rb', line 44

def enharmonic_with?(another_key)
  self.index == another_key.index
end