Class: Xmms::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xmms2_utils.rb

Instance Method Summary collapse

Instance Method Details

#extract_medialib_info(id, *fields) ⇒ Object

returns a hash of the passed in fields with the first-found values for the fields



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/xmms2_utils.rb', line 111

def extract_medialib_info(id, *fields)
    infos = self.medialib_get_info(id).wait.value
    res = Hash.new
    if !infos.nil?
        fields = fields.map! {|f| f.to_sym }
        fields.each do |field|
            values = infos[field]
            if not values.nil?
                my_value = values.first[1] # actual value from the top source [0]
                if field == :url
                    my_value = Xmms::decode_xmms2_url(my_value)
                end
                res[field] = my_value.to_s.force_encoding("utf-8")
            end
        end
    end
    res
end

#shuffle_by(playlist, field) ⇒ Object

Shuffles the playlist by selecting randomly among the tracks as grouped by the given field

shuffle_by is intended to change between different artists/albums/genres more frequently than if all tracks were shuffled based on a uniform distribution. In particular, it works well at preventing one very large group of songs (like A. R. Rahman’s full discography) from dominating your playlist, but leaves many such entries at the end of the playlist.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xmms2_utils.rb', line 77

def shuffle_by(playlist, field)
    pl = playlist.entries.wait.value
    artists = Hash.new
    rnd = Random.new
    playlist.clear.wait
    field = field.to_sym
    pl.each do |id|
        infos = self.medialib_get_info(id).wait.value
        a = infos[field].first[1]
        if artists.has_key?(a)
            artists[a].insert(0,id)
        else
            artists[a] = [id]
        end
    end

    artist_names = artists.keys
    for _ in pl
        artist_idx = (rnd.rand * artist_names.length).to_i
        artist = artist_names[artist_idx]
        songs = artists[artist]
        song_idx = rnd.rand * songs.length
        song = songs[song_idx]
        playlist.add_entry(song).wait
        songs.delete(song)
        if songs.empty?
            artists.delete(artist)
            artist_names.delete(artist)
        end
    end
end