#!ruby # RubyからVox.dllを使うラッパクラス vox.rb # # 動作確認環境 # Windows10 x64 21H1 # Ruby 1.8.7 p330 x86 (i386-mswin32) # Ruby 1.9.3 p551 x86 (i386-mingw32) # Ruby 2.3.3 p222 x86 (i386-mingw32) # Ruby 2.6.8 p205 x86 (i386-mingw32) # Ruby 2.7.4 p191 x86 (i386-mingw32) # Ruby 3.0.2 p107 x86 (i386-mingw32) # v = Vox.new("test.ogg") # v.play # 再生 # v.pause # 停止 # v.fade(10000, 0, 300) # 3秒かけて最大から無音へフェードアウト # v.setLoop(9999) # ループ回数設定 # v.setVolume(10000) # 音量設定 # ttime = v.getTotalTime # 曲の全長取得(msec) # ctime = v.getCurrentTime # 曲の現在再生位置を取得(msec) # playing = v.getStatus # 再生状態を調べる(0:停止中 1:再生中) # v.seek(1000) # 再生位置を変更(msec) # v.delete # メモリ開放 if RUBY_VERSION.to_f <= 1.9 then # Ruby 1.8, 1.9 require "Win32API" class Vox VoxLoad = Win32API.new("vox", "VoxLoad", "P", "I") VoxPlay = Win32API.new("vox", "VoxPlay", "I", "I") VoxPause = Win32API.new("vox", "VoxPause", "I", "I") VoxSetLoop = Win32API.new("vox", "VoxSetLoop", "II", "I") VoxSetVolume = Win32API.new("vox", "VoxSetVolume", "II", "I") VoxFade = Win32API.new("vox", "VoxFade", "IIII", "I") VoxDelete = Win32API.new("vox", "VoxDelete", "I", "I") VoxGetTotalTime = Win32API.new("vox", "VoxGetTotalTime", "I", "I") VoxGetCurrentTime = Win32API.new("vox", "VoxGetCurrentTime", "I", "I") VoxSeek = Win32API.new("vox", "VoxSeek", "II", "I") VoxGetStatus = Win32API.new("vox", "VoxGetStatus", "I", "I") # Load ogg file def initialize(filename) if filename.class != String then raise "File name is invalid. (#{filename}) - initialize" end @id = VoxLoad.call(filename) if @id == -1 then raise "Failed to load file. (#{filename}) - initialize" end end # Play def play if VoxPlay.call(@id) == 0 then raise 'Failed to play - play' end self end # Pause def pause if VoxPause.call(@id) == 0 then raise 'Failed to pause - pause' end self end # Set loop count def setLoop(count) if VoxSetLoop.call(@id, count) == 0 then raise 'Failed to set loop count - setLoop' end self end # Set volume(0 - 10000) def setVolume(volume) if VoxSetVolume.call(@id, volume) == 0 then raise 'Failed to set volume - setVolume' end self end # Fade(start volume、end volume、time(mSec) ) def fade(startvolume, endvolume, fadetime) if VoxFade.call(@id, startvolume, endvolume, fadetime) == 0 then raise 'Failed fade - fade' end self end # Memory release def delete if VoxDelete.call(@id) == 0 then raise 'Failed to memory release - delete' end self end # Get total time of the song (mSec) def getTotalTime totaltime = VoxGetTotalTime.call(@id) if totaltime == -1 then raise 'Failed to get total time - getTotalTime' end return totaltime end # Get current time of the song (mSec) def getCurrentTime currenttime = VoxGetCurrentTime.call(@id) if currenttime== -1 then raise 'Failed to get current time - getCurrentTime' end return currenttime end # Change current time (msec) def seek(time) if VoxSeek.call(@id, time) == 0 then raise 'Failed to change current time - seek' end self end # Check playback status (0: paused / 1: playing) def getStatus status = VoxGetStatus.call(@id) if status == -1 then raise 'Failed to get status - getStatus' end return status end end else # Ruby 2.0 or later require 'fiddle/import' require 'fiddle/types' module VOXDLL voxdllpath = format("%s/%s", File.expand_path('..', __FILE__), 'Vox.dll') extend Fiddle::Importer dlload voxdllpath include Fiddle::Win32Types extern 'int VoxLoad(char*)' extern 'int VoxPlay(int)' extern 'int VoxPause(int)' extern 'int VoxRelease(int)' extern 'int VoxSetLoop(int, int)' extern 'int VoxCheckDevice(int)' extern 'int VoxSetVolume(int, int)' extern 'int VoxGetVolume(int)' extern 'int VoxFade(int, int, int, int)' extern 'int VoxDelete(int)' extern 'int VoxGetTotalTime(int)' extern 'int VoxGetCurrentTime(int)' extern 'int VoxSeek(int, int)' extern 'int VoxGetStatus(int)' end class Vox # Load ogg file def initialize(filename) if filename.class != String then raise "File name is invalid. (#{filename}) - initialize" end @id = VOXDLL.VoxLoad(filename) if @id == -1 then raise "Failed to load file. (#{filename}) - initialize" end end # Play def play if VOXDLL.VoxPlay(@id) == 0 then raise 'Failed to play - play' end self end # Pause def pause if VOXDLL.VoxPause(@id) == 0 then raise 'Failed to pause - pause' end self end # Set loop count def setLoop(count) if VOXDLL.VoxSetLoop(@id, count) == 0 then raise 'Failed to set loop count - setLoop' end self end # Set volume(0 - 10000) def setVolume(volume) if VOXDLL.VoxSetVolume(@id, volume) == 0 then raise 'Failed to set volume - setVolume' end self end # Fade(start volume、end volume、time(mSec) ) def fade(startvolume, endvolume, fadetime) if VOXDLL.VoxFade(@id, startvolume, endvolume, fadetime) == 0 then raise 'Failed fade - fade' end self end # Memory release def delete if VOXDLL.VoxDelete(@id) == 0 then raise 'Failed to memory release - delete' end self end # Get total time of the song (mSec) def getTotalTime totaltime = VOXDLL.VoxGetTotalTime(@id) if totaltime == -1 then raise 'Failed to get total time - getTotalTime' end return totaltime end # Get current time of the song (mSec) def getCurrentTime currenttime = VOXDLL.VoxGetCurrentTime(@id) if currenttime== -1 then raise 'Failed to get current time - getCurrentTime' end return currenttime end # Change current time (msec) def seek(time) if VOXDLL.VoxSeek(@id, time) == 0 then raise 'Failed to change current time - seek' end self end # Check playback status (0: paused / 1: playing) def getStatus status = VOXDLL.VoxGetStatus(@id) if status == -1 then raise 'Failed to get status - getStatus' end return status end end end