Class: Musa::Scales::NoteInScale

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/music/scales.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scale, grade, octave, pitch, background_scale: nil, background_grade: nil, background_octave: nil, background_sharps: nil) ⇒ NoteInScale

Returns a new instance of NoteInScale.

Parameters:

  • scale (Scale)
  • grade
  • octave (Integer)
  • pitch (Number)

    pitch of the note, based on MIDI note numbers. Can be Integer, Rational or Float to express fractions of a semitone



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/musa-dsl/music/scales.rb', line 433

def initialize(scale, grade, octave, pitch, background_scale: nil, background_grade: nil, background_octave: nil, background_sharps: nil)
  @scale = scale
  @grade = grade
  @octave = octave
  @pitch = pitch

  @background_scale = background_scale
  @background_grade = background_grade
  @background_octave = background_octave
  @background_sharps = background_sharps

  @scale.kind.tuning.scale_system.scale_kind_classes.each_key do |name|
    define_singleton_method name do
      scale(name)
    end
  end
end

Instance Attribute Details

#background_scaleObject (readonly)

Returns the value of attribute background_scale.



475
476
477
# File 'lib/musa-dsl/music/scales.rb', line 475

def background_scale
  @background_scale
end

#background_sharpsObject (readonly)

Returns the value of attribute background_sharps.



481
482
483
# File 'lib/musa-dsl/music/scales.rb', line 481

def background_sharps
  @background_sharps
end

#gradeObject (readonly)

Returns the value of attribute grade.



451
452
453
# File 'lib/musa-dsl/music/scales.rb', line 451

def grade
  @grade
end

#pitchObject (readonly)

Returns the value of attribute pitch.



451
452
453
# File 'lib/musa-dsl/music/scales.rb', line 451

def pitch
  @pitch
end

Instance Method Details

#==(other) ⇒ Object



568
569
570
571
572
573
574
# File 'lib/musa-dsl/music/scales.rb', line 568

def ==(other)
  self.class == other.class &&
      @scale == other.scale &&
      @grade == other.grade &&
      @octave == other.octave &&
      @pitch == other.pitch
end

#background_noteObject



477
478
479
# File 'lib/musa-dsl/music/scales.rb', line 477

def background_note
  @background_scale[@background_grade + (@background_octave || 0) * @background_scale.kind.class.grades] if @background_grade
end

#chord(*feature_values, allow_chromatic: nil, **features_hash) ⇒ Object



561
562
563
564
565
566
# File 'lib/musa-dsl/music/scales.rb', line 561

def chord(*feature_values, allow_chromatic: nil, **features_hash)
  features = { size: :triad } if feature_values.empty? && features_hash.empty?
  features ||= ChordDefinition.features_from(feature_values, features_hash)

  Musa::Chords::Chord.new(root: self, allow_chromatic: allow_chromatic, features: features)
end

#down(interval_name_or_interval, natural_or_chromatic = nil) ⇒ Object



527
528
529
# File 'lib/musa-dsl/music/scales.rb', line 527

def down(interval_name_or_interval, natural_or_chromatic = nil)
  up(interval_name_or_interval, natural_or_chromatic, sign: -1)
end

#flat(count = nil) ⇒ Object



536
537
538
539
# File 'lib/musa-dsl/music/scales.rb', line 536

def flat(count = nil)
  count ||= 1
  sharp(-count)
end

#frequencyObject



541
542
543
# File 'lib/musa-dsl/music/scales.rb', line 541

def frequency
  @scale.kind.tuning.frequency_of_pitch(@pitch, @scale.root)
end

#functionsObject



453
454
455
# File 'lib/musa-dsl/music/scales.rb', line 453

def functions
  @scale.kind.class.pitches[grade][:functions]
end

#inspectObject Also known as: to_s



576
577
578
# File 'lib/musa-dsl/music/scales.rb', line 576

def inspect
  "<NoteInScale: grade = #{@grade} octave = #{@octave} pitch = #{@pitch} scale = (#{@scale.kind.class.name} on #{scale.root_pitch})>"
end

#octave(octave = nil) ⇒ Object



457
458
459
460
461
462
463
464
465
# File 'lib/musa-dsl/music/scales.rb', line 457

def octave(octave = nil)
  if octave.nil?
    @octave
  else
    raise ArgumentError, "#{octave} is not integer" unless octave == octave.to_i

    @scale[@grade + (@octave + octave) * @scale.kind.class.grades]
  end
end

#on(scale) ⇒ Object



557
558
559
# File 'lib/musa-dsl/music/scales.rb', line 557

def on(scale)
  scale.note_of_pitch @pitch
end

#scale(kind_id_or_kind = nil) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
# File 'lib/musa-dsl/music/scales.rb', line 545

def scale(kind_id_or_kind = nil)
  if kind_id_or_kind.nil?
    @scale
  else
    if kind_id_or_kind.is_a? ScaleKind
      kind_id_or_kind[@pitch]
    else
      @scale.kind.tuning[kind_id_or_kind][@pitch]
    end
  end
end

#sharp(count = nil) ⇒ Object



531
532
533
534
# File 'lib/musa-dsl/music/scales.rb', line 531

def sharp(count = nil)
  count ||= 1
  calculate_note_of_pitch(@pitch, count)
end

#up(interval_name_or_interval, natural_or_chromatic = nil, sign: nil) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/musa-dsl/music/scales.rb', line 487

def up(interval_name_or_interval, natural_or_chromatic = nil, sign: nil)

  sign ||= 1

  if interval_name_or_interval.is_a?(Numeric)
    natural_or_chromatic ||= :natural
  else
    natural_or_chromatic = :chromatic
  end

  if natural_or_chromatic == :chromatic
    interval = if interval_name_or_interval.is_a?(Symbol)
                 @scale.kind.tuning.offset_of_interval(interval_name_or_interval)
               else
                 interval_name_or_interval
               end

    calculate_note_of_pitch(@pitch, sign * interval)
  else
    @scale[@grade + sign * interval_name_or_interval]
  end
end

#wide_gradeObject



483
484
485
# File 'lib/musa-dsl/music/scales.rb', line 483

def wide_grade
  @grade + @octave * @scale.kind.class.grades
end

#with_background(scale:, grade: nil, octave: nil, sharps: nil) ⇒ Object



467
468
469
470
471
472
473
# File 'lib/musa-dsl/music/scales.rb', line 467

def with_background(scale:, grade: nil, octave: nil, sharps: nil)
  NoteInScale.new(@scale, @grade, @octave, @pitch,
                  background_scale: scale,
                  background_grade: grade,
                  background_octave: octave,
                  background_sharps: sharps)
end