Class: DTAS::Tracklist
- Inherits:
-
Object
- Object
- DTAS::Tracklist
- Includes:
- Serialize
- Defined in:
- lib/dtas/tracklist.rb
Overview
this is inspired by the MPRIS 2.0 TrackList spec
Constant Summary collapse
- SIVS =
%w(list pos repeat)
- TL_DEFAULTS =
{ "list" => [], "pos" => -1, "repeat" => false, }
Instance Attribute Summary collapse
-
#repeat ⇒ Object
true, false, 1.
Class Method Summary collapse
Instance Method Summary collapse
-
#_track_id_map ⇒ Object
caching this probably isn’t worth it.
- #add_track(track, after_track_id = nil, set_as_current = false) ⇒ Object
- #advance_track(repeat_ok = true) ⇒ 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.
- #previous! ⇒ Object
- #remove_track(track_id) ⇒ Object
- #reset ⇒ Object
- #size ⇒ Object
- #to_hsh ⇒ Object
- #tracks ⇒ Object
Methods included from Serialize
Constructor Details
#initialize ⇒ Tracklist
Returns a new instance of Tracklist.
32 33 34 35 36 |
# File 'lib/dtas/tracklist.rb', line 32 def initialize TL_DEFAULTS.each { |k,v| instance_variable_set("@#{k}", v) } @list = [] @goto_off = @goto_pos = nil end |
Instance Attribute Details
#repeat ⇒ Object
true, false, 1
9 10 11 |
# File 'lib/dtas/tracklist.rb', line 9 def repeat @repeat end |
Class Method Details
.load(hash) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/dtas/tracklist.rb', line 18 def self.load(hash) obj = new obj.instance_eval do list = hash["list"] and @list.replace(list) @pos = hash["pos"] || -1 @repeat = hash["repeat"] || false end obj end |
Instance Method Details
#_track_id_map ⇒ Object
caching this probably isn’t worth it. a tracklist is usually a few tens of tracks, maybe a hundred at most.
49 50 51 52 53 |
# File 'lib/dtas/tracklist.rb', line 49 def _track_id_map by_track_id = {} @list.each_with_index { |t,i| by_track_id[t.object_id] = i } by_track_id end |
#add_track(track, after_track_id = nil, set_as_current = false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dtas/tracklist.rb', line 88 def add_track(track, after_track_id = nil, set_as_current = false) if after_track_id by_track_id = _track_id_map idx = by_track_id[after_track_id] or raise ArgumentError, "after_track_id invalid" @list[idx, 1] = [ @list[idx], track ] if set_as_current @pos = idx + 1 else @pos += 1 if @pos > idx end else # nil = first_track @list.unshift(track) if set_as_current @pos = 0 else @pos += 1 if @pos >= 0 end end track.object_id end |
#advance_track(repeat_ok = true) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dtas/tracklist.rb', line 68 def advance_track(repeat_ok = true) return if @list.empty? # @repeat == 1 for single track repeat next_pos = @goto_pos || @pos + (@repeat == 1 ? 0 : 1) next_off = @goto_off # nil by default @goto_pos = @goto_off = nil if @list[next_pos] @pos = next_pos elsif @repeat && repeat_ok next_pos = @pos = 0 else return end [ @list[next_pos], next_off ] end |
#cur_track ⇒ Object
84 85 86 |
# File 'lib/dtas/tracklist.rb', line 84 def cur_track @pos >= 0 ? @list[@pos] : nil end |
#get_tracks(track_ids) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/dtas/tracklist.rb', line 55 def get_tracks(track_ids) by_track_id = _track_id_map track_ids.map do |track_id| idx = by_track_id[track_id] # dtas-mpris fills in the metadata, we just return a path [ track_id, idx ? @list[idx] : nil ] end end |
#go_to(track_id, offset_hhmmss = nil) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/dtas/tracklist.rb', line 123 def go_to(track_id, offset_hhmmss = nil) by_track_id = _track_id_map if idx = by_track_id[track_id] @goto_off = offset_hhmmss return @list[@goto_pos = idx] end @goto_pos = nil # noop if track_id is invalid end |
#previous! ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/dtas/tracklist.rb', line 133 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
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/dtas/tracklist.rb', line 110 def remove_track(track_id) by_track_id = _track_id_map idx = by_track_id.delete(track_id) or return false @list[idx] = nil @list.compact! len = @list.size if @pos >= len @pos = len == 0 ? TL_DEFAULTS["pos"] : len end @goto_pos = @goto_pos = nil # TODO: reposition? true end |
#reset ⇒ Object
38 39 40 41 |
# File 'lib/dtas/tracklist.rb', line 38 def reset @goto_off = @goto_pos = nil @pos = TL_DEFAULTS["pos"] end |
#size ⇒ Object
43 44 45 |
# File 'lib/dtas/tracklist.rb', line 43 def size @list.size end |
#to_hsh ⇒ Object
28 29 30 |
# File 'lib/dtas/tracklist.rb', line 28 def to_hsh ivars_to_hash(SIVS).delete_if { |k,v| TL_DEFAULTS[k] == v } end |
#tracks ⇒ Object
64 65 66 |
# File 'lib/dtas/tracklist.rb', line 64 def tracks @list.map(&:object_id) end |