class PocketMiku::Device

Public Class Methods

new(device) click to toggle source

Args

device
  • String

    MIDIデバイスファイル名

  • IO,StringIO

    出力するIOオブジェクト

Exception

PocketMiku::ArgumentError

ファイル device が見つからず、ストリームとしても利用できない場合
# File lib/pocket_miku/device.rb, line 12
def initialize(device)
  @io = case device
        when IO,StringIO
          device
        when String
          open(device, 'w')
        else
          raise PocketMiku::ArgumentError, "device should give IO or String. but give `#{device.class}'"
        end
  if block_given?
    begin
      self.sing(&Proc.new)
    ensure
      @io.close
    end
  end
end

Public Instance Methods

+@(score)
Alias for: play
-@(track, key)
Alias for: stop
close() click to toggle source
# File lib/pocket_miku/device.rb, line 89
def close
  #stop
  @io.close
end
closed?() click to toggle source
# File lib/pocket_miku/device.rb, line 94
def closed?
  @io.closed?
end
play(score) click to toggle source

score をこのデバイスで再生する

Array

score

PocketMiku::Score 楽譜情報

Exception

PocketMiku::InvalidByteError

packetの中に、1byte(0..255)に収まらない数値がある場合
# File lib/pocket_miku/device.rb, line 51
def play(score)
  score.map{|note|[note, note.to_s.freeze]}.each do |note, packet|
    case note
    when RestNote
      sleep Rational(60.to_f, score.tempo) * Rational(note.length.to_f, Note4)
    when Note
      @io << packet
      @io.flush
      sleep Rational(60.to_f, score.tempo) * Rational(note.length.to_f, Note4)
      stop 0, note.key
      @io.flush
    end
  end
end
Also aliased as: +@
send(packet) click to toggle source

ポケットミクに直接MIDIパケットを送る

Args

packet
  • Array

    バイト値配列をpack(“C*”)して送る

  • Integer

    対応するキャラクタを送る

Exception

PocketMiku::InvalidByteError

packetの中に、1byte(0..255)に収まらない数値がある場合

Return

self

# File lib/pocket_miku/device.rb, line 40
def send(packet)
  @io << PocketMiku::PacketFactory.pack(packet)
  @io.flush
end
sing(score=nil) click to toggle source

ブロックをこのインスタンスのコンテキストで実行してから、ポケットミクで再生する

Array

score

PocketMiku::Score 楽譜情報

Return

ブロックの評価結果

# File lib/pocket_miku/device.rb, line 84
def sing(score=nil)
  score = PocketMiku::Score.new(&Proc.new) if block_given?
  play score
end
stop(track, key) click to toggle source

特定のトラックとキーのサウンドを再生停止する

Args

track

トラック番号

key

音程

Exception

PocketMiku::InvalidByteError

packetの中に、1byte(0..255)に収まらない数値がある場合
# File lib/pocket_miku/device.rb, line 74
def stop(track, key)
  send([0x80 + track, key, 0])
end
Also aliased as: -@