玄箱ジュークボックス

玄箱をジュークボックスにするために、xmms2のapiをいじってみた。
マニュアルはないので、ライブラリのソースを見ながらの作業。
テストコードは動くようになった。だれかwebで動くアプリ作って。

  • メディアライブラリへのインポート
require 'xmmsclient/sync'
require 'pathname'

@ipc = '/tmp/xmms-ipc'

xc = Xmms::Client::Sync.new('Ruby')
xc.connect(@ipc)

def xmms_encode_url(url)
  str = ""
  url.each_char do |c|
    case c
    when c[/[a-zA-Z0-9:\/\-._]/]
      str += c
    when ' '
      str += '+'
    else
      str += '%' + c.unpack('H*').first
    end
  end
  str
end

def xmms_decode_url(str)
  url = ""
  str.split(/((?:%..)+)/).each do |x|
    if x[/^%/]
      url += [x.gsub('%', '')].pack('H*')
    else
      url += x
    end
  end
  url
end

dir = ARGV.shift
unless dir[/^\//]
  dir = Dir.pwd + '/' + dir
end
dir = Pathname.new(dir).cleanpath.to_s

Dir.glob("#{dir}/*").each do |file|
  artist = File.basename(file)
  Dir.glob("#{dir}/#{artist}/*") do |file|
    album = File.basename(file)
    Dir.glob("#{dir}/#{artist}/#{album}/*") do |file|
      _, track, title = File.basename(file).match(/^(?:\d+-)?(\d+)\s(.*).m4a$/).to_a
      next unless track
      puts file
      xc.medialib_add_entry 'file://'+file
      id = xc.medialib_get_id(xmms_encode_url('file://'+file))
      xc.medialib_entry_property_set(id, :artist, artist)
      xc.medialib_entry_property_set(id, :album, album)
      xc.medialib_entry_property_set(id, :track, track)
      xc.medialib_entry_property_set(id, :title, title)
    end
  end
end
  • アーティストを指定して再生
#/bin/env ruby

# memo for xmms system
#  * playlist has real entries   -> playlist as in iTunes
#  * collection has query string -> smart playlist as in iTunes

require 'xmmsclient/sync'

@ipc = '/tmp/xmms-ipc'

xc = Xmms::Client::Sync.new('Ruby')
xc.connect(@ipc)

pl = xc.playlist('Default')
pl.clear

@pattern = 'artist:上原ひろみ'
@coll_name = 'Hiromi Uehara'
@name_space = 'Collections'
@order = ['album', 'track']

c = Xmms::Collection.parse(@pattern)

xc.coll_save(c, @coll_name, @name_space)

xc.coll_query_ids(c, @order).each do |id|
  pl.add_entry(id)
end

pl.load

xc.playback_start