class Object

Public Instance Methods

add(ids) click to toggle source
# File bin/dxmms2, line 458
def add(ids)
    ids.each do |z|
        current_pl.add_entry(z).wait
    end
end
current_pl() click to toggle source
# File bin/dxmms2, line 326
def current_pl
    $xc.playlist
end
edit_ids_metadata(ids) click to toggle source
# File bin/dxmms2, line 476
def edit_ids_metadata(ids)
    edit_metadata(ids.map{ |id| $xc.extract_medialib_info(id, "url")[:url] })
end
edit_metadata(urls) click to toggle source
# File bin/dxmms2, line 339
def edit_metadata(urls)
    prefix = "file://"
    plen = prefix.length
    urls = urls.select do |x|
        x.start_with?(prefix)
    end.map do |x|
        x[plen..-1]
    end
    files = urls.map{|x|"\"#{x}\""}.join(" ")
    `picard #{ files }`
end
get_album_collection(id) click to toggle source
# File bin/dxmms2, line 288
def get_album_collection(id)
    album_name = $xc.extract_medialib_info(id, :album)[:album]
    match_collection('album', album_name)
end
get_album_ids(id) click to toggle source
# File bin/dxmms2, line 283
def get_album_ids(id)
    album_coll = get_album_collection(id)
    $xc.coll_query_ids(album_coll).wait.value
end
get_album_info(id, *fields) click to toggle source
# File bin/dxmms2, line 260
def get_album_info(id, *fields)
    get_collection_info(get_album_collection(id), *fields)
end
get_collection_info(coll, *fields) click to toggle source
# File bin/dxmms2, line 264
def get_collection_info(coll, *fields)
    data = $xc.coll_query_info(coll, fields).wait.value
    data.map do |infos|
        res = Hash.new
        fields = fields.map! {|f| f.to_sym }
        fields.each do |field|
            values = infos[field]
            if not values.nil?
                my_value = values
                if field == :url
                    my_value = Xmms::decode_xmms2_url(my_value)
                end
                res[field] = my_value.to_s.force_encoding("utf-8")
            end
        end
        res
    end
end
get_usertags(id) click to toggle source
# File bin/dxmms2, line 198
def get_usertags(id)
    info = $xc.extract_medialib_info(id, :usertags)
    if info.has_key?(:usertags)
        info[:usertags].lines.to_a
    else
        []
    end
end
list_ids(entries, prompt, start=0, nentries=nil, commands=nil) click to toggle source
# File bin/dxmms2, line 99
def list_ids(entries, prompt, start=0, nentries=nil, commands=nil)
    # XXX: Create a list object to pass to user-supplied commands
    #      list object allows for setting list start, page size, list content,
    #      whether to redisplay the list, and maybe other things
    morestring,endstring,startstring,backstring =
        %w[more end start back].map{|w| $COMMAND_SIG + w}
    if commands.nil?
        commands = Hash.new
    end

    c1 = Hash.new
    commands.each do |k,v|
        c1[$COMMAND_SIG + k] = v
    end
    listing=1
    nentries = (nentries.nil? ? $LIST_ENTRIES_PER_PAGE : nentries)
    pos = nil
    list_start = start
    while ( listing == 1 ) do
        start_clamped = false
        end_clamped = false
        items = Array.new

        #clamps start
        (list_start <= 0) and (list_start = 0 ; start_clamped = true)

        list_end = list_start + nentries

        #clamps end
        (list_end > entries.length) and (list_end = entries.length ; end_clamped = true)


        nw = list_end.to_s.length
        i = list_start

        items += xmms2_ids_to_display_list_strings(entries[list_start..list_end]).map do |string|
            string = "#{i.to_s.rjust(nw)}. #{string}"
            i += 1
            string
        end

        if not start_clamped
            items << backstring
            items << startstring
        end

        if not end_clamped
            items << endstring
            items << morestring
        end

        c1.keys.each do |d|
            items << d
        end

        choice = my_dmenu(items, prompt, items.length).gsub(/^\s+|\s+$/, "")
        pos = choice[/^-?\d+|#{$COMMAND_SIG}[a-zA-Z0-9\-_.]+/]

        case pos
        when backstring
            list_start -= nentries
        when morestring
            list_start += nentries
        when endstring
            list_start = entries.length - nentries
        when startstring
            list_start = 0
        when *(c1.keys)
            f = c1[pos]
            f.call(entries)
            pos = nil
            listing = 0
        else
            listing = 0
        end
    end

    if pos.nil?
        return nil
    end

    pos = pos.to_i
    if pos < 0
        entries.length + pos
    else
        pos
    end
end
match_collection(field, pattern, base_coll=nil) click to toggle source
# File bin/dxmms2, line 293
def match_collection(field, pattern, base_coll=nil)
    coll = Xmms::Collection.new(Xmms::Collection::TYPE_MATCH)
    if !coll.nil?
        coll.operands << base_coll
    else
        coll.operands << Xmms::Collection.universe
    end
    coll.attributes["field"] = field
    coll.attributes["value"] = pattern
    coll
end
my_dmenu(entries, prompt='dxmms2', height=entries.count, width=$SCREEN_WIDTH) click to toggle source
# File bin/dxmms2, line 67
def my_dmenu (entries, prompt='dxmms2', height=entries.count, width=$SCREEN_WIDTH)
    height = [height, 20].min
    Dmenu::dmenu(entries, prompt, height, width,
          $FG_COLOR,
          $BG_COLOR,
          $SEL_FG_COLOR,
          $SEL_BG_COLOR,
          $FONT,
          $LINE_HEIGHT)
end
new_playlist(title, ids) click to toggle source
# File bin/dxmms2, line 464
def new_playlist(title, ids)
    pl_coll = Xmms::Collection.new(Xmms::Collection::TYPE_IDLIST)
    pl_coll.idlist = ids
    if !title.empty?
        $xc.coll_save(pl_coll, title, Xmms::Collection::NS_PLAYLISTS).wait
    end
end
new_playlist_prompt(ids) click to toggle source
# File bin/dxmms2, line 472
def new_playlist_prompt(ids)
    new_playlist(my_dmenu([], "Playlist name"), ids)
end
pl_list(start=nil,nentries=nil,prompt="Track: ",plname=nil) click to toggle source
# File bin/dxmms2, line 90
def pl_list(start=nil,nentries=nil,prompt="Track: ",plname=nil)
    pl = plname.nil? ? $xc.playlist : $xc.playlist(plname)
    entries = pl.entries.wait.value
    current = pl.current_pos.wait.value
    cur = current.nil? ? 0 : current[:position]
    start = start.nil? ? cur : start
    list_ids(entries,prompt,start,nentries)
end
play_new_track(xc, pos=false) click to toggle source
# File bin/dxmms2, line 330
def play_new_track(xc, pos=false)
    if pos
        xc.playlist_set_next(pos).wait
    end
    xc.playback_tickle.wait
    xc.playback_stop.wait
    xc.playback_start.wait
end
playlists_prompt(&call_back) click to toggle source
# File bin/dxmms2, line 78
def playlists_prompt(&call_back)
    playlists = $xc.playlist_list.wait.value
    playlists = playlists.delete_if {|s| s.start_with?("_")}
    selected = my_dmenu(playlists)
    if !call_back.nil?
        if !selected.empty?
            call_back.call(selected)
        end
    end
    selected
end
set_tags(id, tags) click to toggle source
# File bin/dxmms2, line 207
def set_tags(id, tags)
    $xc.medialib_entry_property_set(id, :usertags, tags.map{|x| x.chomp}.uniq.join("\n")).wait
end
tag_menu(ids, previous_tags) click to toggle source
# File bin/dxmms2, line 221
def tag_menu(ids, previous_tags)
    tag_file = File.join(Xmms::userconfdir, 'usertags')
    all_tags = File.new(tag_file,File::CREAT).to_a

    previous_tags = previous_tags.map{|x| x.chomp}

    tag_list = previous_tags.map{|k| "!" + k} + all_tags
    tag_list.map!{|x| x.chomp}

    tag = my_dmenu(tag_list, "Add/delete a tag")
    if (!tag.empty?)
        Thread.new do
            if (tag[0] == "!")
                ids.each do |id|
                    untag_track(id, tag[1..-1])
                end
            else
                ids.each do |id|
                    tag_track(id, tag)
                end
            end
        end


        if (tag[0] != "!")
            all_tags << tag + "\n"
            all_tags.uniq!
        end
        File.open(tag_file, "w") do |file|
            all_tags.each do |t|
                file.write(t)
            end
        end
        tag
    else
        nil
    end
end
tag_track(id, tag) click to toggle source
# File bin/dxmms2, line 211
def tag_track(id, tag)
    tags = get_usertags(id) << tag.chomp
    set_tags(id, tags)
end
time_string_to_ms(str) click to toggle source
# File bin/dxmms2, line 57
def time_string_to_ms(str)
    res = 0
    s = 1
    for n in str.split(":").reverse do
        s = 60 * s
        res = res + s * (n.to_i)
    end
    return res.to_i
end
track_appreciation_factor(playtime, track_duration) click to toggle source

The idea of this is that if I like a song around the middle of the track, I think it's a really good song and it's probably new since I had to listen halfway through to decide I liked it. I also consider that there may be an over-eagerness in my reaction early in a track, so I penalize that. Lastly, for me, if I like the track near the end, I'm only sort-of listening to it or only like it enough to appreciate it as a whole, but can't say much for it in parts.

This should only be used additively with past `likes'

# File bin/dxmms2, line 313
def track_appreciation_factor(playtime, track_duration)
    k = [0, [playtime, track_duration].min].max.to_f / track_duration - 0.5
    l = 1.0 - k.abs
    l
end
untag_track(id, tag) click to toggle source
# File bin/dxmms2, line 216
def untag_track(id, tag)
    tags = get_usertags(id).reject{|x|x==tag}
    set_tags(id, tags)
end
xmms2_ids_to_display_list_strings(ids, fields=%w[artist title url duration]) click to toggle source
# File bin/dxmms2, line 188
def xmms2_ids_to_display_list_strings(ids, fields=%w[artist title url duration])
    ids.map do |id|
        my_info = $xc.extract_medialib_info(id, *fields)
        artist_part = my_info[:artist]
        some_title = (my_info[:title] or File.basename(my_info[:url]))
        duration = my_info[:duration].to_i.ms_to_time_string
        "#{artist_part}|||#{some_title} [#{duration}]"
    end
end