Module: Musa::Scales::Scales

Defined in:
lib/musa-dsl/music/scales.rb

Overview

Scale system registry.

The Scales module provides a central registry for scale systems, allowing access by symbol ID or method name.

Registration

Scale systems register themselves using Scales.register:

Scales.register EquallyTempered12ToneScaleSystem, default: true

Access Methods

By symbol:

Scales[:et12]              # => EquallyTempered12ToneScaleSystem
Scales[:et12][440.0]       # => ScaleSystemTuning with A=440Hz

By method name:

Scales.et12                # => EquallyTempered12ToneScaleSystem
Scales.et12[440.0]         # => ScaleSystemTuning with A=440Hz

Default system:

Scales.default_system                      # => The default scale system
Scales.default_system.default_tuning       # => Default tuning (A=440Hz)

Examples:

Accessing scale systems

# Get system by symbol
system = Scales::Scales[:et12]

# Get system by method
system = Scales::Scales.et12

# Get default system
system = Scales::Scales.default_system

Working with tunings

# Get tuning with A=440Hz (default)
tuning = Scales::Scales[:et12][440.0]

# Get tuning with baroque pitch A=415Hz
baroque = Scales::Scales[:et12][415.0]

Building scales

tuning = Scales::Scales.default_system.default_tuning

# C major scale
c_major = tuning.major[60]

# A minor scale
a_minor = tuning.minor[69]

Class Method Summary collapse

Class Method Details

.default_systemClass<ScaleSystem>

Returns the default scale system.

Examples:

Scales.default_system  # => EquallyTempered12ToneScaleSystem

Returns:

  • (Class<ScaleSystem>)

    the default ScaleSystem subclass



150
151
152
# File 'lib/musa-dsl/music/scales.rb', line 150

def self.default_system
  @default_scale_system
end

.extend_metadata(scale_kind_id, **metadata) ⇒ Hash

Convenience method to extend metadata for a scale kind by ID.

Finds the ScaleKind class by its ID symbol and adds custom metadata to it. This is a shortcut for accessing the class directly and calling extend_metadata.

Examples:

Scales.(:major, my_tag: :favorite)
Scales.(:dorian, mood: :nostalgic, suitable_for: [:jazz])

Parameters:

  • scale_kind_id (Symbol)

    the scale kind identifier (e.g., :major, :dorian)

  • metadata (Hash)

    key-value pairs to add as custom metadata

Returns:

  • (Hash)

    the updated custom_metadata hash

Raises:

  • (KeyError)

    if scale kind not found

See Also:



169
170
171
172
173
174
175
176
177
# File 'lib/musa-dsl/music/scales.rb', line 169

def self.(scale_kind_id, **)
  system = default_system
  raise KeyError, "No default scale system registered" unless system

  klass = system.scale_kind_class(scale_kind_id)
  raise KeyError, "Scale kind :#{scale_kind_id} not found" unless klass

  klass.(**)
end

.get(id) ⇒ Class<ScaleSystem> Also known as: []

Retrieves a registered scale system by ID.

Examples:

Scales[:et12]  # => EquallyTempered12ToneScaleSystem

Parameters:

  • id (Symbol)

    the scale system identifier

Returns:

Raises:

  • (KeyError)

    if scale system not found



134
135
136
137
138
# File 'lib/musa-dsl/music/scales.rb', line 134

def self.get(id)
  raise KeyError, "Scale system :#{id} not found" unless @scale_systems.key?(id)

  @scale_systems[id]
end

.register(scale_system, default: nil) ⇒ self

Registers a scale system.

Makes the scale system available via symbol lookup and dynamic method. Optionally marks it as the default system.

Examples:

Scales.register EquallyTempered12ToneScaleSystem, default: true

Parameters:

  • scale_system (Class<ScaleSystem>)

    the ScaleSystem subclass to register

  • default (Boolean) (defaults to: nil)

    whether to set as default system

Returns:

  • (self)


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/musa-dsl/music/scales.rb', line 113

def self.register(scale_system, default: nil)
  @scale_systems ||= {}
  @scale_systems[scale_system.id] = scale_system

  @default_scale_system = scale_system if default

  self.class.define_method scale_system.id do
    scale_system
  end

  self
end