Class: Musa::Datasets::Score

Inherits:
Object
  • Object
show all
Includes:
Enumerable, AbsD, Queriable, Render, ToMXML
Defined in:
lib/musa-dsl/datasets/score.rb,
lib/musa-dsl/datasets/score/render.rb,
lib/musa-dsl/datasets/score/queriable.rb,
lib/musa-dsl/datasets/score/to-mxml/to-mxml.rb

Defined Under Namespace

Modules: Queriable, Render, ToMXML

Constant Summary collapse

NaturalKeys =
NaturalKeys.freeze

Instance Method Summary collapse

Methods included from Render

#render

Methods included from ToMXML

#to_mxml

Methods included from AbsD

#forward_duration, is_compatible?, #note_duration, to_AbsD

Methods included from E

#valid?, #validate!

Constructor Details

#initialize(hash = nil) ⇒ Score

Returns a new instance of Score.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/musa-dsl/datasets/score.rb', line 22

def initialize(hash = nil)
  raise ArgumentError, "'hash' parameter should be a Hash with time and events information" unless hash.nil? || hash.is_a?(Hash)

  @score = {}
  @indexer = []

  if hash
    hash.sort.each do |k, v|
      raise ArgumentError, "'hash' values for time #{k} should be an Array of events" unless v.is_a?(Array)

      v.each do |vv|
        at(k, add: vv)
      end
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
47
48
# File 'lib/musa-dsl/datasets/score.rb', line 44

def [](key)
  if NaturalKeys.include?(key) && self.respond_to?(key)
    self.send(key)
  end
end

#at(time, add: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/musa-dsl/datasets/score.rb', line 58

def at(time, add: nil)
  time = time.rationalize

  if add
    raise ArgumentError, "#{add} is not a Abs dataset" unless add&.is_a?(Musa::Datasets::Abs)

    slot = @score[time] ||= [].extend(QueryableByTimeSlot)

    slot << add

    @indexer << { start: time,
                  finish: time + add.duration.rationalize,
                  dataset: add }

    nil
  else
    @score[time] ||= [].extend(QueryableByTimeSlot)
  end
end

#between(closed_interval_start, open_interval_finish) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/musa-dsl/datasets/score.rb', line 94

def between(closed_interval_start, open_interval_finish)
  @indexer
    .select { |i| i[:start] < open_interval_finish && i[:finish] > closed_interval_start ||
                  closed_interval_start == open_interval_finish &&
                      i[:start] == i[:finish] &&
                      i[:start] == closed_interval_start }
    .sort_by { |i| i[:start] }
    .collect { |i| { start: i[:start],
                     finish: i[:finish],
                     start_in_interval: i[:start] > closed_interval_start ? i[:start] : closed_interval_start,
                     finish_in_interval: i[:finish] < open_interval_finish ? i[:finish] : open_interval_finish,
                     dataset: i[:dataset] } }.extend(QueryableByDataset)
end

#changes_between(closed_interval_start, open_interval_finish) ⇒ Object

TODO hay que implementar un effective_start y effective_finish con el inicio/fin dentro del bar, no absoluto



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
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/musa-dsl/datasets/score.rb', line 110

def changes_between(closed_interval_start, open_interval_finish)
  (
    #
    # we have a start event if the element
    # begins between queried interval start (included) and interval finish (excluded)
    #
    @indexer
      .select { |i| i[:start] >= closed_interval_start && i[:start] < open_interval_finish }
      .collect { |i| i.clone.merge({ change: :start, time: i[:start] }) } +

    #
    # we have a finish event if the element interval finishes
    # between queried interval start (excluded) and queried interval finish (included) or
    # element interval finishes exactly on queried interval start
    # but the element interval started before queried interval start
    # (the element is not an instant)
    #
    @indexer
      .select { |i| ( i[:finish] > closed_interval_start ||
                      i[:finish] == closed_interval_start && i[:finish] == i[:start])   &&
                    ( i[:finish] < open_interval_finish ||
                      i[:finish] == open_interval_finish && i[:start] < open_interval_finish) }
      .collect { |i| i.clone.merge({ change: :finish, time: i[:finish] }) } +

    #
    # when the queried interval has no duration (it's an instant) we have a start and a finish event
    # if the element also is an instant exactly coincident with the queried interval
    #
    @indexer
      .select { |i| ( closed_interval_start == open_interval_finish &&
                      i[:start] == closed_interval_start &&
                      i[:finish] == open_interval_finish) }
      .collect { |i| [i.clone.merge({ change: :start, time: i[:start] }),
                      i.clone.merge({ change: :finish, time: i[:finish] })] }
      .flatten(1)
  )
    .sort_by { |i| [ i[:time],
                     i[:start] < i[:finish] && i[:change] == :finish ? 0 : 1] }
    .collect { |i| { change: i[:change],
                     time: i[:time],
                     start: i[:start],
                     finish: i[:finish],
                     start_in_interval: i[:start] > closed_interval_start ? i[:start] : closed_interval_start,
                     finish_in_interval: i[:finish] < open_interval_finish ? i[:finish] : open_interval_finish,
                     time_in_interval: if i[:time] < closed_interval_start
                                         closed_interval_start
                                       elsif i[:time] > open_interval_finish
                                         open_interval_finish
                                       else
                                         i[:time]
                                       end,
                     dataset: i[:dataset] } }.extend(QueryableByDataset)
end

#durationObject



54
55
56
# File 'lib/musa-dsl/datasets/score.rb', line 54

def duration
  (finish || 1r) - 1r
end

#each(&block) ⇒ Object



86
87
88
# File 'lib/musa-dsl/datasets/score.rb', line 86

def each(&block)
  @score.sort.each(&block)
end

#finishObject



50
51
52
# File 'lib/musa-dsl/datasets/score.rb', line 50

def finish
  @indexer.collect { |i| i[:finish] }.max
end

#inspectObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/musa-dsl/datasets/score.rb', line 186

def inspect
  s = StringIO.new

  first_level1 = true

  s.write "Musa::Datasets::Score.new({\n"

  @score.each do |k, v|
    s.write "#{ ", \n" unless first_level1 }  #{ k.inspect } => [\n"
    first_level1 = false
    first_level2 = true

    v.each do |vv|
      s.write "#{ ", \n" unless first_level2 }\t#{ vv }"
      first_level2 = false
    end

    s.write  " ]"
  end
  s.write "\n})"

  s.string
end

#positionsObject



82
83
84
# File 'lib/musa-dsl/datasets/score.rb', line 82

def positions
  @score.keys.sort
end

#resetObject



39
40
41
42
# File 'lib/musa-dsl/datasets/score.rb', line 39

def reset
  @score.clear
  @indexer.clear
end

#sizeObject



78
79
80
# File 'lib/musa-dsl/datasets/score.rb', line 78

def size
  @score.keys.size
end

#subsetObject

Raises:

  • (ArgumentError)


172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/musa-dsl/datasets/score.rb', line 172

def subset
  raise ArgumentError, "subset needs a block with the inclusion condition on the dataset" unless block_given?

  filtered_score = Score.new

  @score.each_pair do |time, datasets|
    datasets.each do |dataset|
      filtered_score.at time, add: dataset if yield(dataset)
    end
  end

  filtered_score
end

#to_hObject



90
91
92
# File 'lib/musa-dsl/datasets/score.rb', line 90

def to_h
  @score.sort.to_h
end

#values_of(attribute) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/musa-dsl/datasets/score.rb', line 164

def values_of(attribute)
  values = Set[]
  @score.each_value do |slot|
    slot.each { |dataset| values << dataset[attribute] }
  end
  values
end