Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

MIDI as an input for AutoHotkey?


  • Please log in to reply
3 replies to this topic

Poll: Do you think this is a useful and useable technique? (3 member(s) have cast votes)

Do you think this is a useful and useable technique?

  1. Yes! Very much so! I will use this right away! (1 votes [33.33%])

    Percentage of vote: 33.33%

  2. Looks good, I might use it (0 votes [0.00%])

    Percentage of vote: 0.00%

  3. Nice, don't know if I will use it though (2 votes [66.67%])

    Percentage of vote: 66.67%

  4. Hmm, doubt if I will ever use this (0 votes [0.00%])

    Percentage of vote: 0.00%

  5. This sucks bigtime, I would never use such crap! (0 votes [0.00%])

    Percentage of vote: 0.00%

Vote Guests cannot vote
daonlyfreez
  • Members
  • 995 posts
  • Last active: Jan 23 2013 08:16 AM
  • Joined: 16 Mar 2005
Edit: Obsolete! Use Midi input library DLL by orbik instead...
___________________________________________________________
The keyboard and the mouse are definately not the only input devices one could use to control the computer. One possibility is the use of MIDI. MIDI is the language modern music instruments use to intercommunicate. This could be a keypress on a keyboard, a slider/knob being changed, a pedal pushed or numerous other events.

This is an attempt to capture the occuring MIDI events, so we can act on them...

MIDI as Input for AutoHotkey

Download all necessary files here

Posted Image

Download all necessary files here

; MIDIasInput4ahk.ahk
;
; --- Script to capture MIDI input
;
; Most modern musical instruments (like synthesizers, keyboards or drummachines)
; communicate with each other and the sequencer/computer by using MIDI events
;
; This script is a (feeble) attempt to capture input from MIDI in 'semi-realtime',
; so it will be useable to control other events
;
; ---
;
; You'll need midimon (command line MIDI-in monitor). You can get it from here:
;
; http://www.sreal.com:8000/~div/midi-utilities-for-windows/
;
; You will also need a device to generate MIDI events.
;
; If you don't own hardware, you can use the following program:
;
; Virtual MIDI Keyboard:
; http://www.xs4all.nl/~vanbeekw/vmk.html
;
; You will probably need a Virtual MIDI driver too if you don't have hardware:
;
; MIDI Yoke
; http://www.midiox.com/myoke.htm
;
; Make sure your MIDI controller (e.g. keyboard) is sending on port 1 if you
; don't get any activity. Check also the MIDI Yoke settings.
;
;
; ************ Edit if necessary ************
;
; --- Set to -1 for maximum scriptspeed:
SetBatchLines = -1
;
; --- Set to milliseconds between checks:
waittime = 10
;
; ************ Probably no need to edit below here... ************
;
detecthiddenwindows, on
indatafile = %a_scriptdir%\midiin.txt
midimonwintitle = %comspec% - "%a_scriptdir%\midimon.exe"
;
; --- Check for running instance of midimon. If running, it is stopped.
; --- Next it's output file is erased, so we can start off with a fresh file
IfWinExist, %midimonwintitle%
{
	gosub, _deleteInputfile	
}
else
{
	FileDelete, %indatafile%
	gosub, _startMidimon
}
;
; --- Prepare
filegetsize, oldfilesize, %indatafile%
gosub, _linecount
oldlinecount = %linecount%
thetext =
;
; <--- Start of our Gui definitions ***
Gui, +AlwaysOnTop
Gui, Add, ListBox, vListBoxTrigger x2 y2 w436 h380
Gui, Add, Button, x2 y376 w86 h20, Start fetching
Gui, Add, Button, x90 y376 w86 h20, Stop fetching
Gui, Add, Button, x180 y376 w86 h20, Delete inputfile
Gui, Add, Button, x300 y376 w86 h20, Clear listbox
Gui, Show, center h400 w440, MIDIasInput4ahk
Return
;
ButtonClearlistbox:
GuiControl,, ListBoxTrigger, |
Return
;
ButtonStartfetching:
GuiControl,, ListBoxTrigger, |Fetching of MIDI data started...
stopscan = 0
gosub, _scan
Return
;
ButtonStopfetching:
GuiControl,, ListBoxTrigger, |Fetching of MIDI data stopped...
stopscan = 1
gosub, _scan
Return
;
ButtonDeleteinputfile:
gosub, _deleteInputfile
Return
;
GuiClose:
Gosub, _stopMidimon
ExitApp
;
; *** End of our Gui definitions --->
;
; <--- Start of our routines ***
;
; --- This is the routine that scans for received midi-events
_scan:
loop
{
	; --- If triggered with this variable true, stop the scan
	ifequal, stopscan, 1, break
	; --- Get the filesize of the datafile
	filegetsize, newfilesize, %indatafile%
	; --- Compare with previous size, only if they are not equal, something changed
	ifnotequal, newfilesize, %oldfilesize%
	{
		; --- Get the number of lines of the datafile
		gosub, _linecount
    	newlinecount = %linecount%
		; --- Compare with previous, only if they are not equal, something changed
		ifnotequal, newlinecount, %oldlinecount%
		{
			; --- Get the number of added lines
			addedlinescount = %newlinecount%
			addedlinescount -= %oldlinecount% 
			anewlinenr = %oldlinecount%
			; --- Get all added lines		
			loop, %addedlinescount%
			{	
				filereadline, anewline, %indatafile%, %anewlinenr%
				thetext = %thetext%%anewline%
				anewlinenr += 1
			}
			; --- Cleanup
			GuiControl,, ListBoxTrigger, |
			GuiControl,, ListBoxTrigger, %thetext%
			anewlinenr =
			thetext =
			oldfilesize = %newfilesize%
			oldlinecount = %newlinecount%
	}
}
sleep, %waittime%
}
return

_linecount:
prevbatchlines = %a_batchlines%
SetBatchLines = -1  ; Maximum speed. 
Loop, Read, %indatafile% 
    linecount = %A_Index%
SetBatchLines = %prevbatchlines%
return
;
; --- This will start midimon. If already running, it will be closed first
_startMidimon:
IfWinExist, %midimonwintitle%
{
	WinClose, %midimonwintitle%
	WinWaitClose, %midimonwintitle%
}
Run, %comspec% /k "%a_scriptdir%\midimon.exe" >midiin.txt, %a_scriptdir%, hide UseErrorLevel
if ErrorLevel = ERROR
{
	MsgBox, Error!, Midimon could not be launched!`n`nMake sure it exists in the same folder as the script
	Exit
}
Return
;
; --- This will stop midimon
_stopMidimon:
IfWinExist, %midimonwintitle%
{
	WinClose, %midimonwintitle%
	WinWaitClose, %midimonwintitle%
}
Return
;
; --- This will delete the file midimon generates.
;
; It is necessary to clean up every now and then, because midimon will
; append to file endlessly. To be able to delete the file, midimon has
; to be stopped first.
;
_deleteInputfile:
gosub, _stopMidimon
FileDelete, %indatafile%
if errorlevel != 0
	msgbox, 0, The file could not be deleted! Errorlevel = %errorlevel%
gosub, _startMidimon
Return
;
; *** End of our routines --->

Download all necessary files here

Any comments are welcome 8)
Posted Image mirror 1mirror 2mirror 3ahk4.me • PM or Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Great script and I like your detailed description. I suspect this will be of interest to anyone who has midi devices on hand.

jmon44
  • Guests
  • Last active:
  • Joined: --
Curious, where did you get midimon from? I've located several different versions out there.

jmon44
  • Guests
  • Last active:
  • Joined: --
Sorry, stupid question. Derp.