Class: Musa::Scales::Scale
- Inherits:
-
Object
- Object
- Musa::Scales::Scale
- Extended by:
- Forwardable
- Defined in:
- lib/musa-dsl/music/scales.rb
Instance Attribute Summary collapse
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#root_pitch ⇒ Object
readonly
Returns the value of attribute root_pitch.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](grade_or_symbol) ⇒ Object
- #absolut ⇒ Object
- #chromatic ⇒ Object
- #grade_of(grade_or_string_or_symbol) ⇒ Object
-
#initialize(kind, root_pitch:) ⇒ Scale
constructor
A new instance of Scale.
- #inspect ⇒ Object (also: #to_s)
- #note_of_pitch(pitch, allow_chromatic: nil, allow_nearest: nil) ⇒ Object
- #octave(octave) ⇒ Object
- #offset_of_interval(interval_name) ⇒ Object
- #parse_grade(neuma_grade) ⇒ Object
- #root ⇒ Object
Constructor Details
#initialize(kind, root_pitch:) ⇒ Scale
Returns a new instance of Scale.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/musa-dsl/music/scales.rb', line 270 def initialize(kind, root_pitch:) @notes_by_grade = {} @notes_by_pitch = {} @kind = kind @root_pitch = root_pitch @kind.class.grades_functions.each do |name| define_singleton_method name do self[name] end end freeze end |
Instance Attribute Details
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
289 290 291 |
# File 'lib/musa-dsl/music/scales.rb', line 289 def kind @kind end |
#root_pitch ⇒ Object (readonly)
Returns the value of attribute root_pitch.
289 290 291 |
# File 'lib/musa-dsl/music/scales.rb', line 289 def root_pitch @root_pitch end |
Instance Method Details
#==(other) ⇒ Object
414 415 416 417 418 |
# File 'lib/musa-dsl/music/scales.rb', line 414 def ==(other) self.class == other.class && @kind == other.kind && @root_pitch == other.root_pitch end |
#[](grade_or_symbol) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/musa-dsl/music/scales.rb', line 309 def [](grade_or_symbol) raise ArgumentError, "grade_or_symbol '#{grade_or_symbol}' should be a Integer, String or Symbol" unless grade_or_symbol.is_a?(Symbol) || grade_or_symbol.is_a?(String) || grade_or_symbol.is_a?(Integer) wide_grade, sharps = grade_of(grade_or_symbol) unless @notes_by_grade.key?(wide_grade) octave = wide_grade / @kind.class.grades grade = wide_grade % @kind.class.grades pitch = @root_pitch + octave * @kind.tuning.notes_in_octave + @kind.class.pitches[grade][:pitch] note = NoteInScale.new self, grade, octave, pitch @notes_by_grade[wide_grade] = @notes_by_pitch[pitch] = note end @notes_by_grade[wide_grade].sharp(sharps) end |
#absolut ⇒ Object
299 300 301 |
# File 'lib/musa-dsl/music/scales.rb', line 299 def absolut @kind[0] end |
#chromatic ⇒ Object
295 296 297 |
# File 'lib/musa-dsl/music/scales.rb', line 295 def chromatic @kind.tuning.chromatic[@root_pitch] end |
#grade_of(grade_or_string_or_symbol) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/musa-dsl/music/scales.rb', line 333 def grade_of(grade_or_string_or_symbol) name, wide_grade, accidentals = parse_grade(grade_or_string_or_symbol) grade = @kind.class.grade_of_function name if name octave = wide_grade / @kind.class.grades if wide_grade grade = wide_grade % @kind.class.grades if wide_grade octave ||= 0 return octave * @kind.class.grades + grade, accidentals end |
#inspect ⇒ Object Also known as: to_s
420 421 422 |
# File 'lib/musa-dsl/music/scales.rb', line 420 def inspect "<Scale: kind = #{@kind} root_pitch = #{@root_pitch}>" end |
#note_of_pitch(pitch, allow_chromatic: nil, allow_nearest: nil) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/musa-dsl/music/scales.rb', line 374 def note_of_pitch(pitch, allow_chromatic: nil, allow_nearest: nil) allow_chromatic ||= false allow_nearest ||= false note = @notes_by_pitch[pitch] unless note pitch_offset = pitch - @root_pitch pitch_offset_in_octave = pitch_offset % @kind.tuning.scale_system.notes_in_octave pitch_offset_octave = pitch_offset / @kind.tuning.scale_system.notes_in_octave grade = @kind.class.pitches.find_index { |pitch_definition| pitch_definition[:pitch] == pitch_offset_in_octave } if grade wide_grade = pitch_offset_octave * @kind.class.grades + grade note = self[wide_grade] elsif allow_nearest sharps = 0 until note note = note_of_pitch(pitch - (sharps += 1) * @kind.tuning.scale_system.part_of_tone_size) note ||= note_of_pitch(pitch + sharps * @kind.tuning.scale_system.part_of_tone_size) end elsif allow_chromatic nearest = note_of_pitch(pitch, allow_nearest: true) note = chromatic.note_of_pitch(pitch).with_background(scale: self, grade: nearest.grade, octave: nearest.octave, sharps: (pitch - nearest.pitch) / @kind.tuning.scale_system.part_of_tone_size) end end note end |
#octave(octave) ⇒ Object
303 304 305 306 307 |
# File 'lib/musa-dsl/music/scales.rb', line 303 def octave(octave) raise ArgumentError, "#{octave} is not integer" unless octave == octave.to_i @kind[@root_pitch + octave * @kind.class.grades] end |
#offset_of_interval(interval_name) ⇒ Object
410 411 412 |
# File 'lib/musa-dsl/music/scales.rb', line 410 def offset_of_interval(interval_name) @kind.tuning.offset_of_interval(interval_name) end |
#parse_grade(neuma_grade) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/musa-dsl/music/scales.rb', line 346 def parse_grade(neuma_grade) name = wide_grade = nil accidentals = 0 case neuma_grade when Symbol, String match = /\A(?<name>[^[#|_]]*)(?<accidental_sharps>#*)(?<accidental_flats>_*)\Z/.match neuma_grade.to_s if match if match[:name] == match[:name].to_i.to_s wide_grade = match[:name].to_i else name = match[:name].to_sym unless match[:name].empty? end accidentals = match[:accidental_sharps].length - match[:accidental_flats].length else name = neuma_grade.to_sym unless (neuma_grade.nil? || neuma_grade.empty?) end when Numeric wide_grade = neuma_grade.to_i else raise ArgumentError, "Cannot eval #{neuma_grade} as name or grade position." end return name, wide_grade, accidentals end |
#root ⇒ Object
291 292 293 |
# File 'lib/musa-dsl/music/scales.rb', line 291 def root self[0] end |