MIDI out from keystroke

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
barbaroja9
Posts: 2
Joined: 28 Mar 2021, 22:42

MIDI out from keystroke

Post by barbaroja9 » 29 Mar 2021, 19:37

Hi. Excuse my noobness, but all this is much for me for now. I have succesfully managed to have AHK make life easier by handling some windows shortcuts with the mouse and keyboard. But MIDI has been difficult for me. I have read a lot of posts on the subject but it is still fairly advanced.

What I want: Send midi CCs in a specific midi virtual port (already existent) with keystroke combination "Crtl+Alt+Each of th 12 F keys"

What for: To control a monitoring setup/room/headphone EQ handled by Gigperformer.

The virtual port is called "from AHK midi input"
The CCs are from 0 to 12. Any value goes.

Thanks people!

barbaroja9
Posts: 2
Joined: 28 Mar 2021, 22:42

Re: MIDI out from keystroke

Post by barbaroja9 » 06 Apr 2021, 02:26

Jus tried the "AHK_Generic-Midi-Program-master"

Seems like it could do but code is still fairly complicated for my skill level.

TOTAL
Posts: 52
Joined: 11 Nov 2016, 13:47

Re: MIDI out from keystroke

Post by TOTAL » 04 May 2021, 08:32

Hi

Looking too, so if I find out, I will be back.

It would indeed, help if there were some tutorials just how to set up midi (which I think I have managed to do for an out channel) and how to send a basic midi note and midi cc out. Users could build on this.

TOTAL
Posts: 52
Joined: 11 Nov 2016, 13:47

Re: MIDI out from keystroke

Post by TOTAL » 04 May 2021, 09:04

I have got it :)

You just need to make sure that vDevice variable points to the right MIDI OUT.

Code: Select all

;q:: ;MIDI play notes
hModule := DllCall("kernel32\LoadLibrary", "Str","winmm", "Ptr")

vDeviceID := 1, hMIDIOutputDevice := 0
if DllCall("winmm\midiOutOpen", "Ptr*",hMIDIOutputDevice, "UInt",vDeviceID, "UPtr",0, "UPtr",0, "UInt",0, "UInt")
	return

hDev := hMIDIOutputDevice
vChan := 0

;==============================

AW:= A_IsUnicode? "W": "A", A_CharSize:= A_IsUnicode? 2: 1
hWinMM:= DllCall("kernel32\LoadLibrary" AW, "Str", "Winmm.dll", "Ptr")
NumDevs:= DllCall("Winmm\midiOutGetNumDevs"), sz:= 20+32*A_CharSize
Loop, % NumDevs	{
    VarSetCapacity(DevCaps, sz, 0)
    DllCall("Winmm\midiOutGetDevCaps" AW, "UInt", id:=A_Index-1,   "Ptr", &DevCaps,   "UInt", sz)
    Name:= StrGet(&DevCaps+8)
    DevName%A_Index%:= Name
    DevNames.= id ": " Name "`n"
}
StringTrimRight, DevNames, DevNames, 1
MsgBox % DevNames






^F12::
;C3 and C4 at the same time:
MidiNoteOn(hDev, vChan, 48, 100)
MidiNoteOn(hDev, vChan, 60, 100)
Sleep, 2000
MidiNoteOff(hDev, vChan, 48, 0)
MidiNoteOff(hDev, vChan, 60, 0)
Sleep, 2000


ControlNo := 111
Value := 64
MidiControlChange(hMIDIOutputDevice, vChan, ControlNo, Value)
MidiControlChange(1, Channel, ControlNo, Value)

return

;==============================

;==============================

DllCall("winmm\midiOutClose", "Ptr",hMIDIOutputDevice, "UInt")
DllCall("kernel32\FreeLibrary", "Ptr",hModule)
return

;==================================================

MidiNotePlay(hMIDIOutputDevice, vChannel, vNote, vVelocity, vDuration)
{
	MidiNoteOn(hMIDIOutputDevice, vChannel, vNote, vVelocity)
	Sleep, % vDuration
	MidiNoteOff(hMIDIOutputDevice, vChannel, vNote, 0)
}

MidiNoteOn(hMIDIOutputDevice, vChannel, vNote, vVelocity)
{
	DllCall("winmm\midiOutShortMsg", "Ptr",hMIDIOutputDevice, "UInt",0x90 | vChannel | (vNote << 8) | (vVelocity << 16), "UInt")
}

MidiNoteOff(hMIDIOutputDevice, vChannel, vNote, vVelocity)
{
	DllCall("winmm\midiOutShortMsg", "Ptr",hMIDIOutputDevice, "UInt",0x80 | vChannel | (vNote << 8) | (vVelocity << 16), "UInt")
}

;==================================================

MidiControlChange(hMIDIOutputDevice, Channel, ControlNo, Value)
{
    DllCall("winmm\midiOutShortMsg", "UInt", hMIDIOutputDevice, "UInt", 0xB0 | Channel
                                                          | (ControlNo << 8) | (Value << 16)) ;"Control Change" event
}

Deepmojoman
Posts: 11
Joined: 01 Nov 2022, 13:45

Re: MIDI out from keystroke

Post by Deepmojoman » 01 Dec 2022, 18:43

Hi! I am trying to get my MIDI Controller to send keystrokes. It looks like your code my be able to do that? is that true? I downloaded and ran it, and it immediately showed me a list of all my MIDI devices in a Message Box.

I noticed that you commented that "You just need to make sure that vDevice variable points to the right MIDI OUT". When you say this are you talking about vDeviceID (which you currently have connected to 1)? The two controllers that I am wanting to send keystrokes are hooked up to MIDI port 1, so would I need to change hMIDIOutputDevice from 0 to 1?

I'm just a little confused: I feel that I'm near success, but being a complete Noob at AHK, I really have a VERY limited idea of what's going on, and what I'm doing.

TOTAL
Posts: 52
Joined: 11 Nov 2016, 13:47

Re: MIDI out from keystroke

Post by TOTAL » 01 Dec 2022, 20:07

Hi
I will try to help although I adjust this code rarely enough to forget most. That said, I have been successfully using this code for months to control ableton software, so any effort you put into this successfully pays back in gold.


Note that your situation is inverse - you want midi to send strokes, so you would be looking at inputs rather than outputs. So first, I will answer your question in hope this gives you some generic idea, and then try to guide your steps forward.

1.
When I start the script, here is what is showing:
image.png
image.png (6.91 KiB) Viewed 1983 times
So the relevant variable should be 1 becasue it is the first loopmidi port that the software is attuned to.


Note that if I connect midi devices upon the same windows session, the critical order does not change, i.e. ms gs wavetable synth and the loopmidi ports remain where they were.

Still, if I should restart the computer with some midi device connected, say launchpad, it might come as device 1 pushing loopmidi ports to later positions.
In that case, I would need to change the variable accordingly and restart the AHK script. Then, if it were not connected upon next computer restart, you might need to revert the variable. Conclusion, when windows starts, it is optimal to have the same midi devices connected at all times and only turn more on after windows and AHK have completed their start up.

2.
Regarding your context, you would be looking for something like keystrokes from MIDI in. Fairly sure there are already some ready scripts that come very close to what you are after and linking of a variable to the port number should be identical.

Feel free to ask if you have further questions and if you start a thread, I am happy to join in case I can help further.
Attachments
image.png
image.png (6.91 KiB) Viewed 1983 times

Deepmojoman
Posts: 11
Joined: 01 Nov 2022, 13:45

Re: MIDI out from keystroke

Post by Deepmojoman » 02 Dec 2022, 00:09

Thank you TOTAL for helping me out, I really appreciate it!

So I finally found a solution after searching for it all day: the answer lie in this video: https://www.youtube.com/watch?v=h0IZiYEkvLg&t=318s

RudyB24 actually wrote a script to do exactly what I was looking to do, albeit nominally the script is for an Behringer XTouch 1. But to any looking for the same type of solution, worry not: the script will actually work with any MIDI controller from what I can see. Here is the github link to all the necessary files https://github.com/RudyB24/AutoHotKey_Bome_MIDI_2_Key

Also, don't be deterred by the fact that Rudy speaks of having written with an AHK/Bome solution in mind. You don't have to use Bome at all, if you don't want to.

***********************************************************************************************

What I'm doing currently is I'm running two scripts: one is for the Mouse Clicks:

Code: Select all

CoordMode, Mouse, Client

xx::
Click, ---, ---
Return
[Mod edit: Fixed [code][/code] tags.]

After the CoorMode, Mouse, Client line, the entries are for the actual clicking: the xx represents whatever HotKey I set up to initiate this particular Click, the ---, ---are the two actual Mouse Positon coordinates for this particular Click, which I got from Window Spy (Client coordinates) (these positions are actual buttons in a VST Instrument that, sadly, is not as well appointed with MIDI Control as I feel it should be.)

So after that script is done, then I ran Rudy's script. It opened, and did exactly what he said it would do-like a charm, first opening up with a Message box that searches for my MIDI devices, and gives me a choice of which MIDI device to send from. After choosing the device #, the script will close and then re-open, this time with your chosen MIDI device sending MIDI to AHK! You then edit Rudy's script in the CC section (starting at line 48), entering in whatever Hotkey you want to focus on, and associating that Hotkey with whatever CC your MIDI Controller will be sending, and Voila: it works without a hitch!

Two things:
[indent=]1. Thus far, I can see no way for AHK to receive CCs from more than one MIDI Controller. Do you think this is at all possible?
2. I want to give credit where credit is due: Rudy himself amply states that his script was actually more of a cobbled together revision of a body of work already written by Genmce, who posted his initial solution to MIDI to AHK back in 2020, under the title: AHK_Midi2Keypress on Github (https://github.com/genmce/AHK_Midi2Keypress). Thanks so much, Genmce!!! Thanks again Rudy.[/indent]

Deepmojoman
Posts: 11
Joined: 01 Nov 2022, 13:45

Re: MIDI out from keystroke

Post by Deepmojoman » 02 Dec 2022, 14:18

Update: I found a way to-assumedly-run many different MIDI Devices in a MIDI to AHK context!

If you open up Rudy's AutoHotKey_Bome_MIDI_2_Key you will see, on line 19, the entry: version = XTouchONe2DaVinciResolve. The code is using this entry as a prompt, telling it-upon being opened for the first time-to determine the MIDI port to listen from. The user will cycle through Message boxes (at a certain point being greeted with a full-fledged MIDI Monitor to help in giving the script the correct directions) after which, the script creates a two line .ini file that documents the MIDI Device and its MIDI Channel.

So, in order for you, the user, to be able to use multiple MIDI devices, all you have to do is to make multiple versions of the script, giving each copy it's own distinct name. Then, at line 19 of each script version, replace the words XTouchOne2DaVinciResolve with another distinct name (I'm thinking it could be literally any name you want: Melvin; Socrates-whatever. I opted to call the script whatever MIDI Device it was going to be interfacing with). Then just save the script and run it. The script will go through the aforementioned beginning routine, wherein you will direct the script to the appropriate MIDI Device you want it associated with, and Voila 2.0-it's done!

I have already got two distinct MIDI Controllers successfully sending the same MIDI CCs to AHK, which then sends the keystrokes to Cubase, which presses the buttons I want pressed. I am seeing no reason why I couldn't use the same procedure to enable other MIDI Controllers in my studio to do the same thing, if I so desired. So it looks like this is a solution.

Hope that helps anyone out there!

Epoch
Posts: 41
Joined: 04 Jun 2021, 11:09

Re: MIDI out from keystroke

Post by Epoch » 02 Feb 2023, 20:56

[/quote]
TOTAL wrote:
01 Dec 2022, 20:07
Feel free to ask if you have further questions and if you start a thread, I am happy to join in case I can help further.
Deepmojoman wrote: So I finally found a solution after searching for it all day: the answer lie in this video: https://www.youtube.com/watch?v=h0IZiYEkvLg&t=318s

RudyB24 actually wrote a script to do exactly what I was looking to do, albeit nominally the script is for an Behringer XTouch 1. But to any looking for the same type of solution, worry not: the script will actually work with any MIDI controller from what I can see. Here is the github link to all the necessary files https://github.com/RudyB24/AutoHotKey_Bome_MIDI_2_Key

Also, don't be deterred by the fact that Rudy speaks of having written with an AHK/Bome solution in mind. You don't have to use Bome at all, if you don't want to.
So guys, would you say that AutoHotKey_Bome_MIDI_2_Key is the most efficient / best way to combine AHK and Midi? I've tried using it to translate a midi message from my midi controller to a keystroke (which does other useful stuff on my DAW, using another AHK script) and while I didn't have much trouble setting that up, once I fire up my DAW it becomes clear the midi controller input can be used either by the script or DAW, so I can't use it. I suppose one could get around this using additional software like MIDIOX or even better, Bome Midi Translator, but then there's no point. I've got BMT and I can simply use that for the midi-keyboard translation on its own, the whole point was doing it (along with other midi stuff) with Autohotkey only. Any ideas?

Deepmojoman
Posts: 11
Joined: 01 Nov 2022, 13:45

Re: MIDI out from keystroke

Post by Deepmojoman » 03 Feb 2023, 01:36

once I fire up my DAW it becomes clear the midi controller input can be used either by the script or DAW, so I can't use it
You’re going to have to clarify what you mean by this, in order for me to fully help, but: I have been able to map several keystrokes (for anything from opening up programs, to actuating functions) to multiple MIDI controllers at this point. Note: I have been able to use these controllers indiscriminately between my DAW (Cubase), other programs and Windows.

At first glance it’s sounding like you’re not realizing the need for separating functions through Channels. Just as an example of what I mean: all on one Controller, I can have, say, the command Control+D be for opening up a Mixer Window in Cubase, but also use that same Control+D command open up Photoshop in Windows (and can use them both-both working independently-while Cubase is open). How I do that is to each command to its own button, which itself has its own MIDI Channel. Then I write this spec into the script and: Voila. You have complete control of whatever you like

Please give a little bit more in depth of an explanation of what you’re problem is and I’ll try to assist.

Epoch
Posts: 41
Joined: 04 Jun 2021, 11:09

Re: MIDI out from keystroke

Post by Epoch » 03 Feb 2023, 19:16

Deepmojoman wrote:
once I fire up my DAW it becomes clear the midi controller input can be used either by the script or DAW, so I can't use it
You’re going to have to clarify what you mean by this, in order for me to fully help, but: I have been able to map several keystrokes (for anything from opening up programs, to actuating functions) to multiple MIDI controllers at this point. Note: I have been able to use these controllers indiscriminately between my DAW (Cubase), other programs and Windows.

At first glance it’s sounding like you’re not realizing the need for separating functions through Channels. Just as an example of what I mean: all on one Controller, I can have, say, the command Control+D be for opening up a Mixer Window in Cubase, but also use that same Control+D command open up Photoshop in Windows (and can use them both-both working independently-while Cubase is open). How I do that is to each command to its own button, which itself has its own MIDI Channel. Then I write this spec into the script and: Voila. You have complete control of whatever you like

Please give a little bit more in depth of an explanation of what you’re problem is and I’ll try to assist.
Thanks a lot for the reply, greatly appreciated. Alright, I am going to explain in detail then.

I've got a Nektar Panorama midi controller. My DAW (Studio One) uses these two midi inputs/outputs. Great DAW intergration btw, AFAIK their integration for Cubase is up there too.

Image

MIDIIN/OUT2 (Panorama 6) is for the controller part of the device (knobs, faders, buttons etc.) which is also the part I am currently trying to work with, assigning a specific hotkey combination to a specific Midi CC and value.
There's also another input/output simply called PANORAMA P6 which as you see responds to the keyboard part of the device. There are also two more IN/OUT pairs 3 and 4, you can also set those in the IN/OUT settings on Studio One and the AHK script sees them too, but doing so doesn't seem to do anything and I don't think we need them. Quite positive the controller merely uses the two I've mentioned.
So it's MIDIIN2 (Panorama 6) and PANORAMA P6 we are interested in. Selecting the MIDIIN2 (Panorama 6) on MIdiSet settings results in receiving midi data on the script window and the button I want to translate sends data1=106 and data2=127.
As a test, I edit the XTouchOne_2_DaVinciResolve.ahk script, adding

Code: Select all

if (data1=106 and data2=127)
 Send, 5
and comment all the other "if data" statements I don't need. Using the notepad I can see my button sending "5" indeed. All good so far. However, once I open Studio One, during initializing, I get an error message that PANORAMA P6 is not found. Likewise, if I open Studio One first and THEN the script, selecting the MIDIIN2 (Panorama 6) on MIdiSet settings now returns an error message:
Error, midiInOpen Returned 7
Error, midiInOpen Returned 5
Right Click on the Tray Icon - Left click on MidiSet to select valid midi_in port.


It seems to me that a midi port can be used by one thing at a time (in my case Studio One or the AHK script) and to be honest, I thought this is pretty much normal and the expected behaviour, at least on Windows (which we both use), AFAIK on MacOs it's not like that.
And I had the impression that this is one of the main reasons to use programs like BMT, MIDIOX etc. to create virtual midi ports. But from what you say and the freedom you are describing I am clearly missing something... Not sure what Midi Channels have to do with it if the controller can''t be seen at all when the AHK occupies its ports but to be honest the last time I was bothered with Midi Channels was more than a decade ago when using some multi-timbral instrument... I saw Midi Channels are represented by the StatusByte in the script and I've noticed these are 176 for the controller and 144 for the keyboard. After what you said, I went and edited the script again:

Code: Select all

;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; ADD YOUR OWN INSTRUCTIONS HERE USING: if (data1=... [and/or data2=...) Send ...
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if statusbyte between 176 and 191
and changed 176 to 140 in order to include 144 as well...but it didn't make a difference. Am I supposed to do something else with those two StatusByte numbers? Add/remove something else from the script or even not using XTouchOne_2_DaVinciResolve.ahk at all? (As explained, I am treating it as a working template, editing it with my own stuff). As you can clearly see I am quite confused so I really appreciate your assistance!

Post Reply

Return to “Ask for Help (v1)”