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::DIATONIC_SCALE_AUX, 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::PENTATONIC_MAJ, 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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scales

cromatic_index, diatonic_aux_scale_from, diatonic_scale_from, scale, scale_from

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

Class Method Details

.high_note(from, short) ⇒ Object

Returns the higher note of an interval calculates from its first note and number and quality in short notation



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/music-utils/interval/interval.rb', line 73

def self.high_note(from, short)
  quality, number = parse_short(short)
  
  while number > 7
    number = number - 7
  end
  
  from_note, from_alter = Note.parse(from)

  to_note = Scales.diatonic_scale_from(from_note)[number - 1]
  intervals = Scales::QUALITIES[number].key(quality)
  intervals += alter_impact_in_semitones(from_alter, false)
  
  from_note_index = Scales.cromatic_index(from_note.to_sym)
  to_note_index = from_note_index + intervals

  while to_note_index > 11
    to_note_index = to_note_index - 12
  end

  Scales::CROMATIC_SCALE[to_note_index].select do |note|  
    raw_note, alter = Note.parse(note)
    raw_note == to_note
  end.first
end

.parse_short(sn) ⇒ Object

Short notation parser method



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/music-utils/interval/interval.rb', line 100

def self.parse_short(sn)
  short = sn.to_s

  quality = short[0..1]
  number = short[2..3].to_i
  
  if quality == Scales::DIMP or quality == Scales::AUGP
    # nothing to do
  else
    quality = short[0]
    number = short[1..2].to_i
  end
  [quality, number]
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



59
60
61
62
63
# File 'lib/music-utils/interval/interval.rb', line 59

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
# 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
  count += Interval.alter_impact_in_semitones(@note1_alt, true)
  count += Interval.alter_impact_in_semitones(@note2_alt, false)
  
  count
end

#shortObject

Returns the class interval in the short notation



66
67
68
# File 'lib/music-utils/interval/interval.rb', line 66

def short
  quality + number.to_s 
end