Class: DTAS::Tracklist

Inherits:
Object
  • Object
show all
Includes:
Serialize
Defined in:
lib/dtas/tracklist.rb

Overview

the a tracklist object for -player this is inspired by the MPRIS 2.0 TrackList spec

Constant Summary collapse

TL_DEFAULTS =
{
  'pos' => -1,
  'repeat' => false,
  'max' => 20_000,
  'consume' => false,
}
SIVS =
TL_DEFAULTS.keys

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialize

#ivars_to_hash

Constructor Details

#initializeTracklist

Returns a new instance of Tracklist.


61
62
63
64
65
66
67
# File 'lib/dtas/tracklist.rb', line 61

def initialize
  TL_DEFAULTS.each { |k,v| instance_variable_set("@#{k}", v) }
  @list = []
  @goto_off = @goto_pos = nil
  @track_nr = 0
  @shuffle = false
end

Instance Attribute Details

#consumeObject

boolean


15
16
17
# File 'lib/dtas/tracklist.rb', line 15

def consume
  @consume
end

#maxObject

integer


14
15
16
# File 'lib/dtas/tracklist.rb', line 14

def max
  @max
end

#repeatObject

true, false, 1


12
13
14
# File 'lib/dtas/tracklist.rb', line 12

def repeat
  @repeat
end

#shuffleObject

false or shuffled @list


13
14
15
# File 'lib/dtas/tracklist.rb', line 13

def shuffle
  @shuffle
end

Class Method Details

.load(hash) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dtas/tracklist.rb', line 25

def self.load(hash)
  obj = new
  obj.instance_eval do
    list = hash['list'] and @list.replace(list.map { |s| new_track(s) })
    SIVS.each do |k|
      instance_variable_set("@#{k}", hash[k] || TL_DEFAULTS[k])
    end

    # n.b.: we don't check @list.size against max here in case people
    # are migrating

    if hash['shuffle']
      @shuffle = @list.shuffle
      @pos = _idx_of(@shuffle, @list[@pos].track_id) if @pos >= 0
    end
  end
  obj
end

Instance Method Details

#_idx_of(list, track_id) ⇒ Object


193
194
195
# File 'lib/dtas/tracklist.rb', line 193

def _idx_of(list, track_id)
  list.index { |t| t.track_id == track_id }
end

#_update_pos(pos, prev, list) ⇒ Object


95
96
97
98
# File 'lib/dtas/tracklist.rb', line 95

def _update_pos(pos, prev, list)
  old = prev[pos]
  _idx_of(list, old.track_id)
end

#add_track(track, after_track_id = nil, set_as_current = false) ⇒ Object


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/dtas/tracklist.rb', line 146

def add_track(track, after_track_id = nil, set_as_current = false)
  return false if @list.size >= @max

  track = new_track(track)
  if after_track_id
    idx = _idx_of(@list, after_track_id) or
                                raise ArgumentError, 'after_track_id invalid'
    if @shuffle
      _idx_of(@shuffle, after_track_id) or
                                raise ArgumentError, 'after_track_id invalid'
    end
    @list[idx, 1] = [ @list[idx], track ]

    # add into random position if shuffling
    if @shuffle
      idx = rand(@shuffle.size)
      @shuffle[idx, 1] = [ @shuffle[idx], track ]
    end

    if set_as_current
      @pos = idx + 1
    else
      @pos += 1 if @pos > idx
    end
  else # nil = first_track
    @list.unshift(track)

    if @shuffle
      if @shuffle.empty?
        @shuffle << track
        @pos = 0 if set_as_current
      else
        idx = rand(@shuffle.size)
        @shuffle[idx, 1] = [ @shuffle[idx], track ]
        @pos = idx + 1 if set_as_current
      end
    else
      if set_as_current
        @pos = 0
      else
        @pos += 1 if @pos >= 0
      end
    end
  end
  track.track_id
end

#advance_track(repeat_ok = true) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dtas/tracklist.rb', line 118

def advance_track(repeat_ok = true)
  cur = @shuffle || @list
  return if cur.empty?
  prev = cur[@pos] if @consume && @pos >= 0
  # @repeat == 1 for single track repeat
  repeat = repeat_ok ? @repeat : false
  next_pos = @goto_pos || @pos + (repeat == 1 ? 0 : 1)
  next_off = @goto_off # nil by default
  @goto_pos = @goto_off = nil

  if nxt = cur[next_pos]
    @pos = next_pos
    remove_track(prev.track_id) if prev
  else
    remove_track(prev.track_id) if prev
    # reshuffle the tracklist when we've exhausted it
    cur.shuffle! if @shuffle
    return if !repeat || cur.empty?
    next_pos = @pos = 0
    nxt = cur[0]
  end
  [ nxt.to_path, next_off ]
end

#clearObject


214
215
216
217
218
# File 'lib/dtas/tracklist.rb', line 214

def clear
  @list.clear
  @shuffle.clear if @shuffle
  reset
end

#cur_trackObject


142
143
144
# File 'lib/dtas/tracklist.rb', line 142

def cur_track
  @pos >= 0 ? (@shuffle || @list)[@pos] : nil
end

#get_tracks(track_ids) ⇒ Object


85
86
87
88
89
90
91
92
93
# File 'lib/dtas/tracklist.rb', line 85

def get_tracks(track_ids)
  want = {}
  track_ids.each { |i| want[i] = i }
  rv = []
  @list.each do |t|
    i = want[t.track_id] and rv << [ i, t.to_path ]
  end
  rv
end

#go_to(track_id, offset_hhmmss = nil) ⇒ Object


220
221
222
223
224
225
226
227
228
# File 'lib/dtas/tracklist.rb', line 220

def go_to(track_id, offset_hhmmss = nil)
  list = @shuffle || @list
  if idx = _idx_of(list, track_id)
    @goto_off = offset_hhmmss
    return list[@goto_pos = idx].to_path
  end
  @goto_pos = nil
  # noop if track_id is invalid
end

#new_track(path) ⇒ Object


69
70
71
72
73
74
75
76
77
# File 'lib/dtas/tracklist.rb', line 69

def new_track(path)
  n = @track_nr += 1

  # nobody needs a billion tracks in their tracklist, right?
  # avoid promoting to Bignum on 32-bit
  @track_nr = n = 1 if n >= 0x3fffffff

  DTAS::Track.new(n, path)
end

#previous!Object


230
231
232
233
234
235
236
237
238
# File 'lib/dtas/tracklist.rb', line 230

def previous!
  return if @list.empty?
  prev_idx = @pos - 1
  if prev_idx < 0
    # stop playback if nothing to go back to.
    prev_idx = @repeat ? @list.size - 1 : @list.size
  end
  @goto_pos = prev_idx
end

#remove_track(track_id) ⇒ Object


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dtas/tracklist.rb', line 197

def remove_track(track_id)
  idx = _idx_of(@list, track_id) or return false
  track = @list.delete_at(idx)
  if @shuffle
    idx = _idx_of(@shuffle, track_id) or return false
    @shuffle.delete_at(idx)
  end
  len = @list.size
  if @pos >= len
    @pos = len - 1
  elsif idx <= @pos
    @pos -= 1
  end
  @goto_pos = @goto_off = nil # TODO: reposition?
  track.to_path
end

#resetObject


79
80
81
82
83
# File 'lib/dtas/tracklist.rb', line 79

def reset
  @goto_off = @goto_pos = nil
  @pos = TL_DEFAULTS["pos"]
  @shuffle.shuffle! if @shuffle
end

#swap(a_id, b_id) ⇒ Object


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/dtas/tracklist.rb', line 240

def swap(a_id, b_id)
  ok = { a_id => a_idx = [], b_id => b_idx = [] }
  @list.each_with_index do |t,i|
    ary = ok.delete(t.track_id) or next
    ary[0] = i
    break if ok.empty?
  end
  a_idx = a_idx[0] or return
  b_idx = b_idx[0] or return
  @list[a_idx], @list[b_idx] = @list[b_idx], @list[a_idx]
  unless @shuffle
    [ :@goto_pos, :@pos ].each do |v|
      case instance_variable_get(v)
      when a_idx then instance_variable_set(v, b_idx)
      when b_idx then instance_variable_set(v, a_idx)
      end
    end
  end
  true
end

#to_hsh(full_list = true) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dtas/tracklist.rb', line 44

def to_hsh(full_list = true)
  h = ivars_to_hash(SIVS)
  h.delete_if { |k,v| TL_DEFAULTS[k] == v }
  unless @list.empty?
    if full_list
      h['list'] = @list.map(&:to_path)
    else
      h['size'] = @list.size
    end
  end
  if @shuffle
    h['shuffle'] = true
    h['pos'] = _idx_of(@list, @shuffle[@pos].track_id) if @pos >= 0
  end
  h
end

#tracksObject


114
115
116
# File 'lib/dtas/tracklist.rb', line 114

def tracks
  @list.map(&:track_id)
end