Class: MusicUtils::Interval

Inherits:
Object
  • Object
show all
Includes:
Scales
Defined in:
lib/music-utils/interval/interval.rb

Overview

This class represents a music interval

Constant Summary

Constants included from Scales

Scales::AUG, Scales::AUGP, Scales::CROMATIC_SCALE, Scales::DFLAT, Scales::DIATONIC_SCALE, Scales::DIM, Scales::DIMP, Scales::DO, Scales::DOF, Scales::DOFF, Scales::DOS, Scales::DOSS, Scales::DSHARP, Scales::FA, Scales::FAF, Scales::FAFF, Scales::FAS, Scales::FASS, Scales::FLAT, Scales::LA, Scales::LAF, Scales::LAFF, Scales::LAS, Scales::LASS, Scales::MAJ, Scales::MAJ_SCALE, Scales::MELODIC_MIN_SCALE, Scales::MI, Scales::MIF, Scales::MIFF, Scales::MIN, Scales::MIS, Scales::MISS, Scales::NATURAL_MIN_SCALE, Scales::PERF, Scales::QUALITIES, Scales::RE, Scales::REF, Scales::REFF, Scales::RES, Scales::RESS, Scales::SHARP, Scales::SI, Scales::SIF, Scales::SIFF, Scales::SIS, Scales::SISS, Scales::SOL, Scales::SOLF, Scales::SOLFF, Scales::SOLS, Scales::SOLSS

Instance Method Summary collapse

Methods included from Scales

diatonic_scale_from, scale

Constructor Details

#initialize(note1, note2, step) ⇒ Interval

Returns a new instance of Interval.



10
11
12
13
14
# File 'lib/music-utils/interval/interval.rb', line 10

def initialize(note1, note2, step)
  @note1, @note1_alt = MusicUtils::Note.parse(note1)
  @note2, @note2_alt = MusicUtils::Note.parse(note2)
  @step = step
end

Instance Method Details

#numberObject

It classifies the diatonic interval



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/music-utils/interval/interval.rb', line 17

def number
  # initialize counter and index of scale
  i = note1_index
  count = 1
  
  count, i = no_unison(count, i)
  
  # Counting notes
  until_find_note2(i) { count += 1 }
  
  count = count + (8 * @step) - 1 if @step > 0
  count
end

#qualityObject

Returns the quality of the interval



70
71
72
73
74
# File 'lib/music-utils/interval/interval.rb', line 70

def quality
  s = ( @step > 0 and semitones - (12 * @step) ) || semitones
  n = ( @step > 0 and number - (7 * @step) ) || number
  QUALITIES[n][s]
end

#semitonesObject

Returns the number of semitones



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/music-utils/interval/interval.rb', line 32

def semitones
  return CROMATIC_SCALE.length if @note1 == @note2
  
  # initialize counter and index of scale
  i = note1_index 
  count = 0
  
  # counting semi-tones
  until_find_note2(i) do |ii|
    # from 'mi' to 'fa' and 'si' to 'do' there 1 semi-tone
    if DIATONIC_SCALE[ii] == Scales::FA or DIATONIC_SCALE[ii] == Scales::DO
      count += 1
    else
      count += 2
    end
  end
  
  count = count + (12 * @step) if @step > 0
  
  # counting notes alterations
  case @note1_alt
    when Scales::FLAT   ; count += 1
    when Scales::SHARP  ; count -= 1
    when Scales::DFLAT  ; count += 2
    when Scales::DSHARP ; count -= 2
  end

  case @note2_alt
    when Scales::FLAT   ; count -= 1
    when Scales::SHARP  ; count += 1
    when Scales::DFLAT  ; count -= 2
    when Scales::DSHARP ; count += 2
  end

  count
end

#shortObject

Returns the class interval in the short notation



77
78
79
# File 'lib/music-utils/interval/interval.rb', line 77

def short
  quality + number.to_s 
end