Class: HeadMusic::Content::CantusFirmus::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/content/cantus_firmus/example.rb

Overview

Sample cantus firmus examples from various pedagogical sources. These are traditional melodies used for teaching counterpoint.

Constant Summary collapse

EXAMPLES_DATA =
YAML.load_file(File.expand_path("examples.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:) ⇒ Example

Returns a new instance of Example.



43
44
45
46
47
48
49
# File 'lib/head_music/content/cantus_firmus/example.rb', line 43

def initialize(data:)
  @slug = data["slug"]
  @source = Source.get(data["source"])
  @tonal_center = data["tonal_center"]
  @mode = data["mode"]&.to_sym
  @pitches = data["pitches"] || []
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/head_music/content/cantus_firmus/example.rb', line 9

def mode
  @mode
end

#pitchesObject (readonly)

Returns the value of attribute pitches.



9
10
11
# File 'lib/head_music/content/cantus_firmus/example.rb', line 9

def pitches
  @pitches
end

#slugObject (readonly)

Returns the value of attribute slug.



9
10
11
# File 'lib/head_music/content/cantus_firmus/example.rb', line 9

def slug
  @slug
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/head_music/content/cantus_firmus/example.rb', line 9

def source
  @source
end

#tonal_centerObject (readonly)

Returns the value of attribute tonal_center.



9
10
11
# File 'lib/head_music/content/cantus_firmus/example.rb', line 9

def tonal_center
  @tonal_center
end

Class Method Details

.allObject



12
13
14
15
16
# File 'lib/head_music/content/cantus_firmus/example.rb', line 12

def all
  @all ||= EXAMPLES_DATA["cantus_firmus_examples"].map do |data|
    new(data: data)
  end
end

.by_mode(mode_name) ⇒ Object



33
34
35
36
# File 'lib/head_music/content/cantus_firmus/example.rb', line 33

def by_mode(mode_name)
  normalized_mode = mode_name.to_s.downcase
  all.select { |example| example.mode.to_s.downcase == normalized_mode }
end

.by_source(source_identifier) ⇒ Object



22
23
24
25
26
27
# File 'lib/head_music/content/cantus_firmus/example.rb', line 22

def by_source(source_identifier)
  source = Source.get(source_identifier)
  return [] unless source

  all.select { |example| example.source == source }
end

.by_tonal_center(tonal_center_name) ⇒ Object



38
39
40
# File 'lib/head_music/content/cantus_firmus/example.rb', line 38

def by_tonal_center(tonal_center_name)
  all.select { |example| example.tonal_center.to_s == tonal_center_name.to_s }
end

.find_by_slug(slug) ⇒ Object



18
19
20
# File 'lib/head_music/content/cantus_firmus/example.rb', line 18

def find_by_slug(slug)
  all.find { |example| example.slug == slug.to_s }
end

.sourcesObject



29
30
31
# File 'lib/head_music/content/cantus_firmus/example.rb', line 29

def sources
  all.map(&:source).uniq
end

Instance Method Details

#lengthObject



51
52
53
# File 'lib/head_music/content/cantus_firmus/example.rb', line 51

def length
  pitches.length
end

#to_sObject



55
56
57
# File 'lib/head_music/content/cantus_firmus/example.rb', line 55

def to_s
  "#{tonal_center} #{mode} (#{source})"
end