Sha256: 88239545cacf672fbdff6ae352adebcb238770f2a110537e0e27e16f6374ec55

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

# A scale degree is a number indicating the ordinality of the spelling in the key signature.
# TODO: Rewrite to accept a tonal_center and a scale type.
class HeadMusic::Solmization
  include HeadMusic::Named

  DEFAULT_SOLMIZATION = "solfège"

  RECORDS = YAML.load_file(File.expand_path("solmizations.yml", __dir__)).freeze

  attr_reader :syllables

  def self.get(identifier = nil)
    get_by_name(identifier)
  end

  private_class_method :new

  private

  def initialize(name = nil)
    name = nil if name.empty?
    name ||= DEFAULT_SOLMIZATION
    record = record_for_name(name)
    if record
      initialize_data_from_record(record)
    else
      self.name = name
    end
  end

  def record_for_name(name)
    key = HeadMusic::Utilities::HashKey.for(name)
    RECORDS.detect do |record|
      name_strings = record[:localized_names].map { |localized_name| localized_name[:name] }
      name_keys = name_strings.map { |name_string| HeadMusic::Utilities::HashKey.for(name_string) }
      name_keys.include?(key)
    end
  end

  def initialize_data_from_record(record)
    @syllables = record[:syllables]
    initialize_localized_names(record[:localized_names])
  end

  def initialize_localized_names(list)
    @localized_names = (list || []).map do |name_attributes|
      HeadMusic::Named::LocalizedName.new(**name_attributes.slice(:name, :locale_code, :abbreviation))
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
head_music-4.0.1 lib/head_music/solmization.rb
head_music-4.0.0 lib/head_music/solmization.rb
head_music-3.0.1 lib/head_music/solmization.rb
head_music-3.0.0 lib/head_music/solmization.rb
head_music-2.0.1 lib/head_music/solmization.rb
head_music-2.0.0 lib/head_music/solmization.rb
head_music-1.0.0 lib/head_music/solmization.rb
head_music-0.29.0 lib/head_music/solmization.rb
head_music-0.28.0 lib/head_music/solmization.rb