模拟摇杆调制轮无响应求助:仅弯音轮功能正常
问题:摇杆垂直轴模拟MIDI调制轮无响应,弯音轮功能正常
我编写了一段脚本,意图让模拟摇杆的垂直轴模拟MIDI调制轮,水平轴模拟弯音轮,但测试时仅弯音轮功能正常,调制轮完全无反应。已尝试更换不同轴,问题依旧。
脚本代码
// buttons (bitmask), numbuttons (valid bits in buttons) // systime (time in seconds) // axis(x), numaxis (number actually supported) // pov(x), numpov, pov(x) > 360 if not pointing // event(msg1,msg2,msg3) to send events // settings joystick_modwheel_axis = 2; joystick_pitchblend_axis = 1; midi_channel = 0; // 0-15 modwheel_cc = 1; // 0-127 pitchblend_cc = 2; // 0-127 modwheel_val = floor(abs(axis(joystick_modwheel_axis) * 127)); pitchblend_val = floor((axis(joystick_pitchblend_axis) * 8192) + 8192); modwheel_val != modwheel_val_old ? ( event($xb0 | midi_channel, modwheel_cc, modwheel_val); modwheel_val_old = modwheel_val; ); pitchblend_val != pitchblend_val_old ? ( event($xe0 | midi_channel, pitchblend_val & 0x7F, pitchblend_val >> 7); pitchblend_val_old = pitchblend_val; );
功能示意图

问题排查与修复方案
1. 未初始化状态变量
代码中modwheel_val_old和pitchblend_val_old没有初始值,第一次执行时,modwheel_val != modwheel_val_old的判断会因modwheel_val_old未定义而失效,导致调制轮事件无法触发,后续状态更新也会异常。
修复: 在脚本开头添加变量初始化:
modwheel_val_old = -1; pitchblend_val_old = -1;
2. 轴索引与值范围验证
- 确认
axis(x)的索引规则:部分平台轴索引从0开始,joystick_modwheel_axis = 2可能指向不存在的轴。可添加调试代码输出轴值:print("Mod Wheel Axis Value: " .. axis(joystick_modwheel_axis)); - 原代码用
abs()处理轴值,会丢失摇杆的方向信息(垂直轴通常是-1到1,对应调制轮0-127的范围),更合理的映射应该是把-1→0,0→64,1→127:modwheel_val = floor((axis(joystick_modwheel_axis) * 63.5) + 63.5); // 限制值在0-127之间 modwheel_val = math.max(0, math.min(127, modwheel_val));
3. 脚本执行逻辑确认
确保脚本设置为持续运行,而非仅在按键触发时执行——部分平台默认只在输入事件(如按键按下)时运行脚本,摇杆轴的平滑变化可能无法触发脚本执行。
完整修复后的脚本
// buttons (bitmask), numbuttons (valid bits in buttons) // systime (time in seconds) // axis(x), numaxis (number actually supported) // pov(x), numpov, pov(x) > 360 if not pointing // event(msg1,msg2,msg3) to send events // 初始化状态变量,确保首次判断生效 modwheel_val_old = -1; pitchblend_val_old = -1; // settings joystick_modwheel_axis = 2; joystick_pitchblend_axis = 1; midi_channel = 0; // 0-15 modwheel_cc = 1; // 0-127 pitchblend_cc = 2; // 0-127 // 调制轮值映射:摇杆-1~1 → MIDI 0~127(中心64) modwheel_val = floor((axis(joystick_modwheel_axis) * 63.5) + 63.5); modwheel_val = math.max(0, math.min(127, modwheel_val)); // 弯音轮值映射:摇杆-1~1 → MIDI 0~16383 pitchblend_val = floor((axis(joystick_pitchblend_axis) * 8192) + 8192); pitchblend_val = math.max(0, math.min(16383, pitchblend_val)); // 发送调制轮控制变更事件 if modwheel_val != modwheel_val_old then event($xb0 | midi_channel, modwheel_cc, modwheel_val); modwheel_val_old = modwheel_val; end // 发送弯音轮事件 if pitchblend_val != pitchblend_val_old then event($xe0 | midi_channel, pitchblend_val & 0x7F, pitchblend_val >> 7); pitchblend_val_old = pitchblend_val; end
内容的提问来源于stack exchange,提问作者Peter Tremblay
相关产品推荐
相关产品推荐

