#==============================================================================
# ■ CacheAudio
#------------------------------------------------------------------------------
# 本模組載入音效,建立並保存音頻物件。為使播放暢順並節省内存,本模組可把指定
# 音效預先讀入,使得程序在需求已存在的音效時能快速讀取。
#==============================================================================
$mciSendStringW = Win32API.new("winmm","mciSendStringW",['P','P','I','L'],'L')
$mciGetErrorStringW = Win32API.new("winmm","mciGetErrorStringW",['L','P','I'],'L')
$: << Dir.getwd
module CacheAudio
#--------------------------------------------------------------------------
# ● 載入音檔。
# filename : Str.文件名
# *device : Str.標識
#--------------------------------------------------------------------------
def self.open(filename,device=filename)
return $mciSendStringW.call("open #{filename} alias #{device}".to_unc,0,0,0)
end
#--------------------------------------------------------------------------
# ● 結束音檔。
# device : Str.標識
#--------------------------------------------------------------------------
def self.close(device)
error = $mciSendStringW.call("close #{device}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 播放音檔。
# device : Str.標識
#--------------------------------------------------------------------------
def self.play(device)
error = $mciSendStringW.call("play #{device}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 播放音檔。
# device : Str.標識
# *from : Int.位置
#--------------------------------------------------------------------------
def self.playfrom(device,from=0)
error = $mciSendStringW.call("play #{device} from #{from}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 停止播放。
# device : Str.標識
#--------------------------------------------------------------------------
def self.stop(device)
error = $mciSendStringW.call("stop #{device}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 暫停播放。
# device : Str.標識
#--------------------------------------------------------------------------
def self.pause(device)
error = $mciSendStringW.call("pause #{device}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 恢復播放。
# device : Str.標識
#--------------------------------------------------------------------------
def self.resume(device)
error = $mciSendStringW.call("resume #{device}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 移動游標。
# device : Str.標識
# *position : Int.位置
#--------------------------------------------------------------------------
def self.seek(device,position=0)
position = "start" if position == 0
position = "end" if position == 0 - 1
error = $mciSendStringW.call("seek #{device} to #{position}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 設置音量。
# device : Str.標識
# volune : Int.音量
#--------------------------------------------------------------------------
def self.setvolume(device,volume)
volume = [[volume,0].max,1000].min
error = $mciSendStringW.call("setaudio #{device} volume to #{volume}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 獲取音量。
# device : Str.標識
#--------------------------------------------------------------------------
def self.getvolume(device)
buf = " " * 256
error = $mciSendStringW.call("status #{device} volume".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
return buf.strip.to_utf
end
#--------------------------------------------------------------------------
# ● 漸變音量
# device : Str.標識
# time : Int.漸變時間
# to : Int.最後音量
#--------------------------------------------------------------------------
def self.gradual(device,time,to)
Thread.new {
begin
unless time <= 0
volume = getvolume(device).to_f
change = (to - getvolume(device)).to_f / time / 10
for t in 0...time * 10
setvolume(device,volume.to_i)
volume += change
sleep 0.08
end
end
setvolume(device,to)
rescue Exception => ex
p "淡入或淡出時發生了異常:",ex
end
}
end
#--------------------------------------------------------------------------
# ● 設置播放速度。
# device : Str.標識
# speed : Int.播放速度
#--------------------------------------------------------------------------
def self.setspeed(device,speed)
speed = [[speed,500].max,1500].min
error = $mciSendStringW.call("set #{device} speed #{speed}".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
end
#--------------------------------------------------------------------------
# ● 獲取播放速度。
# device : Str.標識
#--------------------------------------------------------------------------
def self.getspeed(device)
buf = " " * 256
error = $mciSendStringW.call("status #{device} speed".to_unc,0,0,0)
raise CacheAudioException.new(error) if error != 0
return buf.strip.to_utf
end
end
#==============================================================================
# ■ CacheAudioException
#------------------------------------------------------------------------------
# 音頻貯藏所模塊異常類。
#==============================================================================
class CacheAudioException < Exception
#--------------------------------------------------------------------------
# ● 初始化异常类对象
# code : 错误代码
#--------------------------------------------------------------------------
def initialize(code)
return super if code.class == String
@msg = " " * 512
$mciGetErrorStringW.call(code,@msg,256)
@msg = "錯誤代碼︰#{code} #{@msg.to_utf}"
end
#--------------------------------------------------------------------------
# ● 返回异常对象的字符串描述
#--------------------------------------------------------------------------
def to_s
return @msg if @msg
return super
end
end