Module: Musa::Datasets::GDV

Includes:
AbsD, Helper
Defined in:
lib/musa-dsl/datasets/gdv.rb

Constant Summary collapse

NaturalKeys =
(NaturalKeys + [:grade, :sharps, :octave, :velocity, :silence]).freeze
VELOCITY_MAP =

TODO create a customizable MIDI velocity to score dynamics bidirectional conversor ppp = 16 … fff = 127 (-5 … 4) the standard used by Musescore 3 and others starts at ppp = 16

[1, 8, 16, 33, 49, 64, 80, 96, 112, 127].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbsD

#duration, #forward_duration, is_compatible?, #note_duration, to_AbsD

Methods included from E

#valid?, #validate!

Instance Attribute Details

#base_durationObject

Returns the value of attribute base_duration.



17
18
19
# File 'lib/musa-dsl/datasets/gdv.rb', line 17

def base_duration
  @base_duration
end

Instance Method Details

#to_gdvd(scale, previous: nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/musa-dsl/datasets/gdv.rb', line 107

def to_gdvd(scale, previous: nil)
  gdvd = {}.extend GDVd
  gdvd.base_duration = @base_duration

  if previous

    if include?(:silence)
      gdvd[:abs_grade] = :silence

    elsif include?(:grade) && !previous.include?(:grade)
      gdvd[:abs_grade] = self[:grade]
      gdvd[:abs_sharps] = self[:sharps]

    elsif include?(:grade) && previous.include?(:grade)
      if self[:grade] != previous[:grade] ||
        (self[:sharps] || 0) != (previous[:sharps] || 0)

        gdvd[:delta_grade] =
            scale[self[:grade]].octave(self[:octave]).wide_grade -
            scale[previous[:grade]].octave(previous[:octave]).wide_grade

        gdvd[:delta_sharps] = (self[:sharps] || 0) - (previous[:sharps] || 0)
      end
    elsif include?(:sharps)
      gdvd[:delta_sharps] = self[:sharps] - (previous[:sharps] || 0)
    end

    if self[:duration] && previous[:duration] && (self[:duration] != previous[:duration])
      gdvd[:delta_duration] = (self[:duration] - previous[:duration])
    end

    if self[:velocity] && previous[:velocity] && (self[:velocity] != previous[:velocity])
      gdvd[:delta_velocity] = self[:velocity] - previous[:velocity]
    end
  else
    gdvd[:abs_grade] = self[:grade] if self[:grade]
    gdvd[:abs_duration] = self[:duration] if self[:duration]
    gdvd[:abs_velocity] = self[:velocity] if self[:velocity]
  end

  (keys - NaturalKeys).each { |k| gdvd[k] = self[k] }

  gdvd
end

#to_neumaObject



68
69
70
71
72
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
98
99
# File 'lib/musa-dsl/datasets/gdv.rb', line 68

def to_neuma
  @base_duration ||= Rational(1, 4)

  attributes = []

  c = 0

  if include?(:silence)
    attributes[c] = :silence
  elsif include?(:grade)
    attributes[c] = self[:grade].to_s
    if include?(:sharps)
      if self[:sharps] > 0
        attributes[c] += '#' * self[:sharps]
      elsif self[:sharps] < 0
        attributes[c] += '_' * self[:sharps]
      end
    end
  end

  attributes[c] = '.' if attributes[c].nil? || attributes[c].empty?

  attributes[c += 1] = 'o' + self[:octave].to_s if self[:octave]
  attributes[c += 1] = (self[:duration] / @base_duration).to_s if self[:duration]
  attributes[c += 1] = velocity_of(self[:velocity]) if self[:velocity]

  (keys - NaturalKeys).each do |k|
    attributes[c += 1] = modificator_string(k, self[k])
  end

  '(' + attributes.join(' ') + ')'
end

#to_pdv(scale) ⇒ Object



23
24
25
26
27
28
29
30
31
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
# File 'lib/musa-dsl/datasets/gdv.rb', line 23

def to_pdv(scale)
  pdv = {}.extend PDV
  pdv.base_duration = @base_duration

  if self[:grade]
    pdv[:pitch] = if self[:silence]
                    :silence
                  else
                    scale[self[:grade]].sharp(self[:sharps] || 0).octave(self[:octave] || 0).pitch
                  end
  end

  if self[:duration]
    pdv[:duration] = self[:duration]
  end

  if self[:note_duration]
    pdv[:note_duration] = self[:note_duration]
  end

  if self[:forward_duration]
    pdv[:forward_duration] = self[:forward_duration]
  end

  if self[:velocity]
    index = if (-5..4).cover?(self[:velocity])
              self[:velocity]
            else
              self[:velocity] < -5 ? -5 : 4
            end
    
    index_min = index.floor
    index_max = index.ceil

    velocity = VELOCITY_MAP[index_min + 5] +
      (VELOCITY_MAP[index_max + 5] - VELOCITY_MAP[index_min + 5]) * (self[:velocity] - index_min)

    pdv[:velocity] = velocity
  end

  (keys - NaturalKeys).each { |k| pdv[k] = self[k] }

  pdv
end