Rythmic Input for Virtual Instrument in Game

Ask gaming related questions (AHK v1.1 and older)
PriceCheck
Posts: 3
Joined: 22 Jun 2021, 16:31

Rythmic Input for Virtual Instrument in Game

22 Jun 2021, 17:09

I am new to AHK and tried to hodgepodge this code together myself using what examples I could find online so I'm surprised it works as well as it does, but I'm still having one issue that won't fix itself no matter what I try (though what I know to try is limited). The code uses a single hotkey to press and release a series of keys rhythmically as notes on a virtual instrument. I used the sleep function to represent rests between those notes and for the most part that works, however sometimes the song slows down as it plays making me think the sleep function isn't a good way to space the keystrokes.

Code: Select all

^\::

Sleep 500

( ***** Bar 1 ***** )

(1,2)
Send, {x down}
Send, {x up}
Sleep 450

(3,4)
Send, {x down}
Send, {x up}
Sleep 450

(5,6)
Send, {x down}
Send, {x up}
Sleep 450

(7)
Send, {x down}
Send, {x up}
Sleep 225

(8)
Send, {x down}
Send, {x up}
Sleep 225

(9,10)
Send, {v down}
Send, {v up}
Sleep 450

(11,12)
Send, {v down}
Send, {v up}
Sleep 450

(13,14)
Send, {v down}
Send, {v up}
Sleep 450

(15)
Send, {v down}
Send, {v up}
Sleep 225

(16)
Send, {n down}
Send, {n up}
Sleep 225

(17,18)
Send, {b down}
Send, {b up}
Sleep 450

(19,20)
Send, {b down}
Send, {b up}
Sleep 450

(21,22)
Send, {b down}
Send, {b up}
Sleep 450

(23)
Send, {b down}
Send, {b up}
Sleep 225

(24)
Send, {v down}
Send, {v up}
Sleep 225

(25,26)
Send, {c down}
Send, {c up}
Sleep 450

(27,28)
Send, {c down}
Send, {c up}
Sleep 450

(29,30)
Send, {c down}
Send, {c up}
Sleep 450

(31)
Send, {v down}
Send, {v up}
Sleep 225

(32)
Send, {c down}
Send, {c up}
Sleep 225
This is an example of how I wrote the code and there is nothing more at the beginning or end. Just ten more bars that are written exactly the same way as the first, thirteen hundred lines in total.

On the first playthrough, the song plays the right notes but when it gets a little more complicated it slows down slightly but noticeably. No sleep command throughout the song is shorter than 225 and adjusting the speed to slower intervals doesn't help all that much. On the second playthrough and later more and more errors start occurring; missed notes, delayed notes, etcetera. This requires me to close AHK and re-run the program if I want it to function properly again.

Is there some other way I could execute these commands that would be faster/more reliable/more precise timing wise?

Additionally if you have any ideas on how I could terminate this program mid process using a hotkey that would be great as right now, once it begins I can't stop it. Just have to let it finish.
User avatar
mikeyww
Posts: 26857
Joined: 09 Sep 2014, 18:38

Re: Rythmic Input for Virtual Instrument in Game

22 Jun 2021, 17:31

Does it work in Notepad just to send the letters?
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Rythmic Input for Virtual Instrument in Game

22 Jun 2021, 18:26

To stop the program, for example upon pressing the function key F1:

Code: Select all

F1:: exitapp
I don't know what the number in the parentheses are expected to do, if the purpose is to make comments, you should rather use a semicolumn at the beginning of the line.

I also don't see the advantage of sending a key down followed immediately by a key up, instead of just sending the key. And with setkeydelay, you can specify the time that the key remains pressed, if you just use send x.
PriceCheck
Posts: 3
Joined: 22 Jun 2021, 16:31

Re: Rythmic Input for Virtual Instrument in Game

22 Jun 2021, 18:44

Yes, it works with just a notepad script and it only sends letters. In later bars is sends multiple letters at once to make chords.

Here's an example.

Code: Select all

( ***** Bar 8 ***** )

(1,2)
Send, {x down}{f down}{y down}
Send, {x up}{f up}{y up}
Sleep 450

(3)
Send, {x down}
Send, {x up}
Sleep 225

(4)
Send, {f down}
Send, {f up}
Sleep 225

(5)
Send, {x down}{t down}
Send, {x up}{t up}
Sleep 225

(6)
Send, {r down}
Send, {r up}
Sleep 225

(7)
Send, {v down}{f down}
Send, {v up}{f up}
Sleep 225

(8)
Send, {x down}{g down}{e down}
Send, {x up}{g up}{e up}
Sleep 225

(9)
Send, {v down}{h down}
Send, {v up}{h up}
Sleep 225

(10)
Send, {e down}
Send, {e up}
Sleep 225

(11)
Send, {v down}
Send, {v up}
Sleep 225

(12)
Send, {h down}{e down}
Send, {h up}{e up}
Sleep 225

(13)
Send, {v down}{r down}
Send, {v up}{r up}
Sleep 225

(14)
Send, {t down}
Send, {t up}
Sleep 225

(15)
Send, {v down}{h down}
Send, {v up}{h up}
Sleep 225

(16)
Send, {n down}{r down}
Send, {n up}{r up}
Sleep 225

(17,18)
Send, {b down}{j down}
Send, {b up}{j up}
Sleep 450
User avatar
mikeyww
Posts: 26857
Joined: 09 Sep 2014, 18:38

Re: Rythmic Input for Virtual Instrument in Game

22 Jun 2021, 19:50

It sounds like the issue is not AHK directly, but how your program responds to your script. For example, the sleeps might be too short for the program to keep up with the keys (though you mentioned that longer sleeps did not help). There could be other issues. I'm not sure that there is a way for the reader to debug what is here alone. If you search the forum for the name of your specific music program, there might be solutions or examples of working scripts. AHK documentation provides an alternative to Sleep, but I doubt that it would be of much help here; you could try it anyway. You could also try the SetKeyDelay as braunbaer mentioned, in case you need a longer press duration than the default.

Code: Select all

SetKeyDelay, 30
press("xfy", 450), press("x"  , 225), press("f"  , 225), press("xt" , 225), press("r"  , 225)
press("vf" , 225), press("xge", 225), press("vh" , 225), press("e"  , 225), press("v"  , 225)
press("he" , 225), press("vr" , 225), press("t"  , 225), press("vh" , 225), press("nr" , 225)
press("bj" , 450)

press(keys, wait) {
 key := []
 Loop, Parse, keys
  Send % ("{" A_LoopField " down}", key.InsertAt(1, A_LoopField))
 For k, v in key
  Send {%v% up}
 Sleep, wait
}
PriceCheck
Posts: 3
Joined: 22 Jun 2021, 16:31

Re: Rythmic Input for Virtual Instrument in Game

22 Jun 2021, 20:25

Thank you! You were absolutely right on all points. I rewrote the code per your suggestions and there is no more lagging, rushing, or missed notes. The exitapp function also works perfectly which is a godsend.

I didn't need to extend the notes, and probably should have realized a simple press wouldn't be that complicated.

Here are the changes I made in case anyone is interested.

Beginning

Code: Select all

^\::

sleep 500

; ***** COMPLETE CODE *****

; --------- Bar 1

;1,2
send x
sleep 450

;3,4
send x
sleep 450

;5,6
send x
sleep 450

;7
send x
sleep 225
End

Code: Select all

;25,26,27
send ng
sleep 675

;28
send f
sleep 225

;29,30
send n
sleep 450

;31,32
send s
sleep 450

return
esc::exitapp

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: prototype_zero, Shoobis and 37 guests