Class: DTAS::Tracklist
- Inherits:
-
Object
- Object
- DTAS::Tracklist
- 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, }
- SIVS =
TL_DEFAULTS.keys
Instance Attribute Summary collapse
-
#max ⇒ Object
integer.
-
#repeat ⇒ Object
true, false, 1.
-
#shuffle ⇒ Object
false or shuffled @list.
Class Method Summary collapse
Instance Method Summary collapse
- #_idx_of(list, track_id) ⇒ Object
- #_update_pos(pos, prev, list) ⇒ Object
- #add_track(track, after_track_id = nil, set_as_current = false) ⇒ Object
- #advance_track(repeat_ok = true) ⇒ Object
- #clear ⇒ Object
- #cur_track ⇒ Object
- #get_tracks(track_ids) ⇒ Object
- #go_to(track_id, offset_hhmmss = nil) ⇒ Object
-
#initialize ⇒ Tracklist
constructor
A new instance of Tracklist.
- #new_track(path) ⇒ Object
- #previous! ⇒ Object
- #remove_track(track_id) ⇒ Object
- #reset ⇒ Object
- #swap(a_id, b_id) ⇒ Object
- #to_hsh(full_list = true) ⇒ Object
- #tracks ⇒ Object
Methods included from Serialize
Constructor Details
#initialize ⇒ Tracklist
Returns a new instance of Tracklist.
58 59 60 61 62 63 64 |
# File 'lib/dtas/tracklist.rb', line 58 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
#max ⇒ Object
integer
13 14 15 |
# File 'lib/dtas/tracklist.rb', line 13 def max @max end |
#repeat ⇒ Object
true, false, 1
11 12 13 |
# File 'lib/dtas/tracklist.rb', line 11 def repeat @repeat end |
#shuffle ⇒ Object
false or shuffled @list
12 13 14 |
# File 'lib/dtas/tracklist.rb', line 12 def shuffle @shuffle end |
Class Method Details
.load(hash) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dtas/tracklist.rb', line 22 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
184 185 186 |
# File 'lib/dtas/tracklist.rb', line 184 def _idx_of(list, track_id) list.index { |t| t.track_id == track_id } end |
#_update_pos(pos, prev, list) ⇒ Object
92 93 94 95 |
# File 'lib/dtas/tracklist.rb', line 92 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
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dtas/tracklist.rb', line 137 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dtas/tracklist.rb', line 115 def advance_track(repeat_ok = true) cur = @shuffle || @list return if cur.empty? # @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 cur[next_pos] @pos = next_pos elsif repeat next_pos = @pos = 0 else return end [ cur[next_pos].to_path, next_off ] end |
#clear ⇒ Object
203 204 205 206 207 |
# File 'lib/dtas/tracklist.rb', line 203 def clear @list.clear @shuffle.clear if @shuffle reset end |
#cur_track ⇒ Object
133 134 135 |
# File 'lib/dtas/tracklist.rb', line 133 def cur_track @pos >= 0 ? (@shuffle || @list)[@pos] : nil end |
#get_tracks(track_ids) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/dtas/tracklist.rb', line 82 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
209 210 211 212 213 214 215 216 217 |
# File 'lib/dtas/tracklist.rb', line 209 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
66 67 68 69 70 71 72 73 74 |
# File 'lib/dtas/tracklist.rb', line 66 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
219 220 221 222 223 224 225 226 227 |
# File 'lib/dtas/tracklist.rb', line 219 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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/dtas/tracklist.rb', line 188 def remove_track(track_id) idx = _idx_of(@list, track_id) or return false if @shuffle si = _idx_of(@shuffle, track_id) or return false @shuffle.delete_at(si) end track = @list.delete_at(idx) len = @list.size if @pos >= len @pos = len == 0 ? TL_DEFAULTS["pos"] : len end @goto_pos = @goto_pos = nil # TODO: reposition? track.to_path end |
#reset ⇒ Object
76 77 78 79 80 |
# File 'lib/dtas/tracklist.rb', line 76 def reset @goto_off = @goto_pos = nil @pos = TL_DEFAULTS["pos"] @shuffle.shuffle! if @shuffle end |
#swap(a_id, b_id) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/dtas/tracklist.rb', line 229 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dtas/tracklist.rb', line 41 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 |
#tracks ⇒ Object
111 112 113 |
# File 'lib/dtas/tracklist.rb', line 111 def tracks @list.map(&:track_id) end |