适用于RPG Maker VXAce,用于改变播放同名BGM、但微调【音调】后,会从头重新播放整首BGM的特性。
#encoding:utf-8
#==============================================================================
# ■ 无缝变调 BGM(BGM_Pitch)
#------------------------------------------------------------------------------
# 在同一首 BGM 正在播放时改变音调(升调 / 降调),音乐从当前播放节点接着
# 放下去:不回到开头,也不会有音量骤降。
#------------------------------------------------------------------------------
# ● 原理
# 1. RGSS3 的 Audio.bgm_play 第 4 个参数 pos 可以指定播放起点,
# Audio.bgm_pos 则能读回当前的播放位置。
# 2. 变调时先读回当前位置,再用新音调从该位置起播,音乐内容便从同一节点
# 继续,而不是回到开头。
# 3. 播放位置是按 ogg 页量化的(约 0.19 秒才跳一格),直接用会“跳拍”,
# 所以本脚本用帧数插值,把位置补足到帧级精度。
# 4. 实测 seek 会向前回退一页,因此续播时补偿一页,避免听到重复的一小段。
# 5. 引擎对“从非零位置起播”有一段 1 秒的淡入(音量从 0 爬满),
# 这是变调瞬间音量变小的原因。本脚本在加载时对已解包的引擎代码做一次
# 内存补丁,把这段淡入的时长 1000ms 改成 0,从而彻底消除音量塌陷。
#------------------------------------------------------------------------------
# ● 使用方法
# 自动生效:任何“对同一首曲子只改音调 / 音量”的播放请求都会变成无缝变调,
# 例如事件里重新指定同一首 BGM 但换了音调。
#
# 脚本中也可以直接调用:
# Audio.bgm_pitch = 120 # 升调,保持当前播放位置
# Audio.bgm_pitch = 80 # 降调
# Audio.bgm_change_pitch(120, 90)# 变调的同时改音量
# Audio.bgm_pitch # 读取当前音调
# Audio.bgm_pitch_changeable? # 当前是否有可无缝变调的 BGM
# Audio.bgm_seek_fade_removed? # 引擎淡入是否已被去除
#------------------------------------------------------------------------------
# ● 注意
# 1. 只有在“同一首 BGM 正在播放”时才会无缝续播;换曲、停止后再播放、
# 或者显式指定了播放起点(第 4 个参数不为 0)时,仍是原版行为。
# 2. 若确实需要同一首曲子从头重播,请先停一次再播放:
# Audio.bgm_stop / RPG::BGM.stop → 再 Audio.bgm_play(...)
# 3. 升调 / 降调在 RGSS3 中同时会改变播放速度(音调越高放得越快),
# 这是 RGSS3 音源本身的特性。
# 4. 引擎补丁只影响“从中间位置起播”的淡入,不改动其他任何行为;
# 若补丁因引擎版本不同而未生效,变调照常工作,只是仍有 1 秒淡入。
#==============================================================================
module BGM_Pitch
#============================================================================
# ■ 引擎补丁:去掉“从中间位置起播”时的 1 秒淡入
#----------------------------------------------------------------------------
# RGSS3 引擎里播放音频的函数有这么一段(32 位机器码):
# 83 7D 14 00 cmp dword ptr [ebp+0x14], 0 ; pos 是否为 0
# 76 10 jbe skip ; pos 为 0 则不淡入
# 68 E8 03 00 00 push 1000 ; set_fade(1000ms)
# 把 push 1000 改成 push 0 即可:引擎内部对 time == 0 的处理是
# “淡入立即完成、音量直接满值”,等于没有淡入。
# 引擎文件带 ASPack 壳,磁盘上无法直接改,因此这里在运行时改内存。
#============================================================================
module EnginePatch
# 补丁点特征码(含 push 1000)
SIG = [0x83,0x7D,0x14,0x00,0x76,0x10,0x68,0xE8,0x03,0x00,0x00].pack("C*")
# 替换用机器码:push 0
PATCH = [0x68,0x00,0x00,0x00,0x00].pack("C*")
# push 1000 在特征码中的偏移
SIG_OFF = 6
# 已知构建(RGSS300.dll 3.0.0.1 / RGSS301.dll 3.0.1.1)中的特征码偏移
KNOWN = 0x11A575
# 可能被加载的引擎文件名
DLL_NAME = ["RGSS300.dll", "RGSS301.dll"]
# 扫描时每次读取的字节数
CHUNK = 0x10000
# PAGE_EXECUTE_READWRITE
PAGE_RWX = 0x40
#--------------------------------------------------------------------------
# ● 常量
#--------------------------------------------------------------------------
@installed = false
@status = nil
#--------------------------------------------------------------------------
# ● 公开属性
#--------------------------------------------------------------------------
class << self
attr_reader :status
#------------------------------------------------------------------------
# ● 补丁是否已生效
#------------------------------------------------------------------------
def installed?
install
@installed
end
#------------------------------------------------------------------------
# ● 执行补丁(只会真正执行一次)
#------------------------------------------------------------------------
def install
return @status unless @status.nil?
begin
if win32.nil?
return (@status = "Win32API 不可用,未打补丁")
end
base = module_base
if base.nil? || base == 0
return (@status = "未找到引擎 DLL")
end
addr = find_signature(base)
if addr.nil?
return (@status = "特征码未匹配,未打补丁")
end
target = addr + SIG_OFF
if read_mem(target, PATCH.length) == PATCH
@installed = true
return (@status = "淡入已被去除")
end
len = PATCH.length
old = [0].pack("V")
win32[:protect].call(target, len, PAGE_RWX, old)
win32[:write].call(target, PATCH, len)
win32[:protect].call(target, len, old.unpack("V")[0], [0].pack("V"))
@installed = (read_mem(target, len) == PATCH)
@status = @installed ? "淡入已被去除" : "写入失败,未打补丁"
rescue Exception => e
@status = "补丁异常:" + e.class.to_s
end
@status
end
#------------------------------------------------------------------------
# ● 初始化 Win32API(不可用时返回 nil)
#------------------------------------------------------------------------
def win32
return @win32 unless @win32.nil?
return nil unless defined?(Win32API)
@win32 = {
:module => Win32API.new("kernel32", "GetModuleHandleA", "p", "l"),
:protect => Win32API.new("kernel32", "VirtualProtect", "pllp", "l"),
:write => Win32API.new("kernel32", "RtlMoveMemory", "ppl", "v")
}
@win32
rescue Exception
nil
end
#------------------------------------------------------------------------
# ● 取得引擎模块的基址
#------------------------------------------------------------------------
def module_base
api = win32
return nil if api.nil?
DLL_NAME.each do |name|
base = api[:module].call(name)
return base if base && base != 0
end
nil
end
#------------------------------------------------------------------------
# ● 生成二进制串(Ruby 1.9 里字符串比较会受编码影响,必须统一成 BINARY)
#------------------------------------------------------------------------
def bin(str)
str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
str
end
#------------------------------------------------------------------------
# ● 读取内存
#------------------------------------------------------------------------
def read_mem(addr, len)
return nil if addr.nil? || len.nil? || len <= 0
buf = bin("\0" * len)
win32[:write].call(buf, addr, len)
buf
rescue Exception
nil
end
#------------------------------------------------------------------------
# ● 定位特征码:先试已知偏移,再按 PE 节表扫描可执行节
#------------------------------------------------------------------------
def find_signature(base)
return nil if base.nil?
head = read_mem(base + KNOWN, SIG.length)
return base + KNOWN if head == SIG
scan_image(base)
end
#------------------------------------------------------------------------
# ● 按节表扫描
#------------------------------------------------------------------------
def scan_image(base)
hdr = read_mem(base, 0x2000)
return nil if hdr.nil? || hdr.length < 0x40
return nil if hdr[0, 2] != "MZ"
lfanew = hdr[0x3C, 4].unpack("V")[0]
return nil if lfanew.nil? || lfanew <= 0 || lfanew + 24 > hdr.length
nsec = hdr[lfanew + 6, 2].unpack("v")[0]
optsz = hdr[lfanew + 20, 2].unpack("v")[0]
start = lfanew + 24 + optsz
nsec.times do |i|
off = start + i * 40
break if off + 40 > hdr.length
vsize, vaddr = hdr[off + 8, 8].unpack("VV")
chars = hdr[off + 36, 4].unpack("V")[0]
next if vsize.nil? || vsize <= 0
next if (chars & 0x20000000) == 0 # 只看可执行节
addr = scan_range(base + vaddr, vsize)
return addr unless addr.nil?
end
nil
end
#------------------------------------------------------------------------
# ● 在指定范围内逐块搜索特征码
#------------------------------------------------------------------------
def scan_range(addr, size)
keep = SIG.length - 1
tail = bin("")
pos = 0
while pos < size
len = size - pos
len = CHUNK if len > CHUNK
buf = tail + read_mem(addr + pos, len).to_s
idx = buf.index(SIG)
return addr + pos - tail.length + idx unless idx.nil?
tail = buf.length > keep ? buf[buf.length - keep, keep] : buf
pos += len
end
nil
rescue Exception
nil
end
end
end
end
module BGM_Pitch
#--------------------------------------------------------------------------
# ● 常量
#--------------------------------------------------------------------------
DEFAULT_VOLUME = 100 # 默认音量
DEFAULT_PITCH = 100 # 默认音调
# 位置量化的“一页”长度(音调 100、44.1kHz 时实测约 33076)
QUANTUM = 33076.0
# 音调 100 时播放位置每帧前进的单位数(44100 * 4 / 60)
UNITS_PER_FRAME = 2940.0
# 判断是否为同一文件时要忽略的扩展名
AUDIO_EXT = [".ogg", ".mp3", ".wav", ".mid", ".midi", ".m4a", ".wma"]
#--------------------------------------------------------------------------
# ● 定义实例变量
#--------------------------------------------------------------------------
@file = nil # 正在播放的 BGM 文件名
@volume = DEFAULT_VOLUME # 当前音量
@pitch = DEFAULT_PITCH # 当前音调
@playing = false # 是否正在播放
@seen_pos = 0.0 # 最近一次读到的播放位置
@seen_frame = 0 # 读到该位置时的帧数
@upf = UNITS_PER_FRAME# 每帧前进的单位数(会自动校准)
@length = {} # 文件名 => 曲长(播放位置的单位)
@verify = nil # 变调后的校验信息
#--------------------------------------------------------------------------
# ● 公开属性
#--------------------------------------------------------------------------
class << self
attr_reader :file
attr_reader :volume
attr_reader :pitch
#------------------------------------------------------------------------
# ● 判定是否正在播放(可无缝变调)
#------------------------------------------------------------------------
def playing?
@playing && !@file.nil?
end
#------------------------------------------------------------------------
# ● 由 Audio.bgm_play 调用
# 返回 true 表示已经由本模块处理,调用方不要再播放
#------------------------------------------------------------------------
def request_play(filename, volume, pitch, pos)
volume = DEFAULT_VOLUME if volume.nil?
pitch = DEFAULT_PITCH if pitch.nil?
pos = 0 if pos.nil?
return false unless playing?
return false unless same_file?(filename, @file)
return false unless pos.to_i == 0
return false if pitch.to_i == @pitch && volume.to_i == @volume
change_pitch(pitch, volume)
rescue
false # 出现意外时按原版方式播放,保证不会没声音
end
#------------------------------------------------------------------------
# ● 记录一次普通的起播
#------------------------------------------------------------------------
def on_play(filename, volume, pitch, pos)
volume = DEFAULT_VOLUME if volume.nil?
pitch = DEFAULT_PITCH if pitch.nil?
pos = 0 if pos.nil?
@file = filename
@volume = volume.to_i
@pitch = pitch.to_i
@playing = true
@seen_pos = pos.to_f
@seen_frame = frame_count()
@upf = UNITS_PER_FRAME * @pitch / 100.0
@verify = nil
nil
end
#------------------------------------------------------------------------
# ● 停止 / 淡出:清空状态,之后再播放就按原版行为从头开始
#------------------------------------------------------------------------
def stop
@playing = false
@file = nil
@verify = nil
nil
end
#------------------------------------------------------------------------
# ● 无缝变调
# pitch : 新音调(50 ~ 150 之外 RGSS3 也接受,但会明显变声)
# volume : 新音量,省略则保持不变
# 返回 true 表示成功切换
#------------------------------------------------------------------------
def change_pitch(pitch, volume = nil)
return false unless playing?
pitch = pitch.to_i
volume = (volume.nil? ? @volume : volume.to_i)
return false if pitch == @pitch && volume == @volume
old_pitch = (@pitch == 0 ? DEFAULT_PITCH : @pitch)
# 先算出当前位置,再补偿 seek 的回退,最后防止越过曲尾
pos = estimate_position + QUANTUM
pos = wrap_position(@file, pos)
# 一次调用完成切换:不先 stop,减少中断时间
play_raw(@file, volume, pitch, pos)
@volume = volume
@pitch = pitch
@upf = @upf * pitch / old_pitch.to_f
@seen_pos = pos
@seen_frame = frame_count()
# 起点比较靠后时做一次校验,防止 seek 越过曲尾导致 BGM 被停止
@verify = [frame_count(), pos] if pos > QUANTUM * 4
true
end
#------------------------------------------------------------------------
# ● 估算当前播放位置(单位:RGSS3 的 bgm_pos 单位)
#------------------------------------------------------------------------
def estimate_position
return 0.0 unless playing?
frame = frame_count()
pos = read_pos
if !pos.nil? && pos > 0
if pos < @seen_pos
# 位置回退:曲子循环回到开头(顺便记住曲长)
if @seen_pos > QUANTUM * 10 && pos < QUANTUM * 3
@length[@file] = @seen_pos + QUANTUM
end
@seen_pos = pos
@seen_frame = frame
elsif pos > @seen_pos
# 位置前进:顺便校准每帧前进量
frames = frame - @seen_frame
if frames > 0 && @seen_pos > 0
rate = (pos - @seen_pos) / frames.to_f
@upf = rate if rate > 0 && rate < 100000
end
@seen_pos = pos
@seen_frame = frame
end
end
extra = (frame - @seen_frame).to_f * @upf
extra = 0.0 if extra < 0
extra = QUANTUM if extra > QUANTUM # 位置停滞时不要无限外推
@seen_pos + extra
end
#------------------------------------------------------------------------
# ● 把位置限制在曲子范围内(已知曲长时)
#------------------------------------------------------------------------
def wrap_position(filename, pos)
length = @length[filename]
return pos if length.nil? || length <= QUANTUM * 4
limit = length - QUANTUM
return pos if pos < limit
pos - (pos / limit).to_i * limit
end
#------------------------------------------------------------------------
# ● 每帧调用:变调后的校验
#------------------------------------------------------------------------
def tick
return if @verify.nil?
frame = frame_count()
return if frame - @verify[0] < 20
pos = read_pos
# 指定了明显靠后的起点,位置却仍然归零:说明 seek 越过了曲尾,
# BGM 已被停止,此时从头恢复播放,避免彻底静音。
if !pos.nil? && pos < QUANTUM
play_raw(@file, @volume, @pitch, 0)
@seen_pos = 0.0
@seen_frame = frame
end
@verify = nil
nil
end
#------------------------------------------------------------------------
# ● 读取当前帧数
#------------------------------------------------------------------------
def frame_count
Graphics.frame_count
rescue
0
end
#------------------------------------------------------------------------
# ● 读取播放位置
#------------------------------------------------------------------------
def read_pos
return nil unless Audio.respond_to?(:bgm_pos)
Audio.bgm_pos.to_f
rescue
nil
end
#------------------------------------------------------------------------
# ● 判定文件名是否相同(忽略扩展名差异)
#------------------------------------------------------------------------
def same_file?(a, b)
return false if a.nil? || b.nil?
return true if a == b
normalize_path(a) == normalize_path(b)
end
#------------------------------------------------------------------------
# ● 去掉扩展名的路径
#------------------------------------------------------------------------
def normalize_path(path)
name = path.to_s
ext = File.extname(name).downcase
AUDIO_EXT.include?(ext) ? name[0, name.length - ext.length] : name
end
#------------------------------------------------------------------------
# ● 直接调用原版 bgm_play(绕过本模块的自动变调判断)
#------------------------------------------------------------------------
def play_raw(filename, volume, pitch, pos)
Audio.bgm_play_without_seamless(filename, volume, pitch, pos)
end
end
end
#==============================================================================
# ■ Audio
#==============================================================================
module Audio
class << self
#------------------------------------------------------------------------
# ● 保存原版方法
#------------------------------------------------------------------------
alias_method :bgm_play_without_seamless, :bgm_play unless method_defined?(:bgm_play_without_seamless)
alias_method :bgm_stop_without_seamless, :bgm_stop unless method_defined?(:bgm_stop_without_seamless)
alias_method :bgm_fade_without_seamless, :bgm_fade unless method_defined?(:bgm_fade_without_seamless)
#------------------------------------------------------------------------
# ● 播放 BGM
# 同一首曲子只改音调 / 音量时,改为无缝变调,不从头重播
#------------------------------------------------------------------------
def bgm_play(filename, volume = 100, pitch = 100, pos = 0)
return if BGM_Pitch.request_play(filename, volume, pitch, pos)
bgm_play_without_seamless(filename, volume, pitch, pos)
BGM_Pitch.on_play(filename, volume, pitch, pos)
nil
end
#------------------------------------------------------------------------
# ● 停止 BGM
#------------------------------------------------------------------------
def bgm_stop
BGM_Pitch.stop
bgm_stop_without_seamless
end
#------------------------------------------------------------------------
# ● 淡出 BGM
#------------------------------------------------------------------------
def bgm_fade(time)
BGM_Pitch.stop
bgm_fade_without_seamless(time)
end
#------------------------------------------------------------------------
# ● 读取当前音调
#------------------------------------------------------------------------
def bgm_pitch
BGM_Pitch.pitch
end
#------------------------------------------------------------------------
# ● 无缝变调(升调 / 降调)
#------------------------------------------------------------------------
def bgm_pitch=(pitch)
BGM_Pitch.change_pitch(pitch)
end
#------------------------------------------------------------------------
# ● 无缝变调,可同时改音量
#------------------------------------------------------------------------
def bgm_change_pitch(pitch, volume = nil)
BGM_Pitch.change_pitch(pitch, volume)
end
#------------------------------------------------------------------------
# ● 当前是否有可以无缝变调的 BGM
#------------------------------------------------------------------------
def bgm_pitch_changeable?
BGM_Pitch.playing?
end
#------------------------------------------------------------------------
# ● 引擎的 seek 淡入是否已被去除
#------------------------------------------------------------------------
def bgm_seek_fade_removed?
BGM_Pitch::EnginePatch.installed?
end
end
end
#==============================================================================
# ■ Graphics
#==============================================================================
module Graphics
class << self
alias_method :update_without_bgm_pitch, :update unless method_defined?(:update_without_bgm_pitch)
#------------------------------------------------------------------------
# ● 刷新画面
#------------------------------------------------------------------------
def update
BGM_Pitch.tick
update_without_bgm_pitch
end
end
end
#==============================================================================
# ■ Game_System
#==============================================================================
class Game_System
#--------------------------------------------------------------------------
# ● 无缝变调
#--------------------------------------------------------------------------
def bgm_change_pitch(pitch, volume = nil)
BGM_Pitch.change_pitch(pitch, volume)
end
end
#==============================================================================
# ■ 启动时打补丁
#==============================================================================
BGM_Pitch::EnginePatch.install
大dick老师还是太强大了