Jump to content

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

MIDI input library


  • Please log in to reply
90 replies to this topic
genmce
  • Members
  • 144 posts
  • Last active: May 21 2015 03:09 PM
  • Joined: 10 Jan 2009
Midi2KeyStroke v 0.1

This is a stripped down version of generic midi app version 0.1
Removed all midi out.
This only works with midi input.
It has a list box to select midi port, the gui could/should be cleaned up and buttons re sized.
It will write midi input port to ini file.
I do not believe it will work with ahk - L unicode. Dll call must be changed for that to show ports properly....

In fact, whole file needs a good clean up. Sorry, no more time for this.

All in one file.

Shows 2 examples of midi note on to keypress(s) conversion.

It does NOT do all that you asked for, I must leave something for you to do.
Also - I just commented out some of the midi out parts, they could be removed totally... up to you.

/* 
 Midi2KeyStroke v. 0.1  by Genmce 6.4.11
 
 Below is a hack of Generic midi program v. - 0.1 which is a hack of orbiks winmm.dll call
 
 Removed midi out selection from generic midi program v 0.1
 
  - select input 
  - open and close selected midi ports
  - write port to ini file
  - send keypress(s) based on midi input note number.
  
  Currently set to midi note numbers 57 and 59.
  - script will create an inifile first time it loads.
  - that file can be deleted if needed via tray menu
  - uses contributions by so many different people. 
  - post your creations back to this thread or the midi I/O thread - that way we can all build on it.
  
  Notes - All midi in lib stuff is included here in one file, no external dlls, besides winmm.dll required.
  Based on orbiks winmm.dll call and others' stuff.
 
  comments need to be cleaned up
  
*/
  
#Persistent
#SingleInstance
SendMode Input              ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

if A_OSVersion in WIN_NT4,WIN_95,WIN_98,WIN_ME  ; if not xp or 2000 quit - probably not needed but eh...
{
    MsgBox This script requires Windows 2000/XP or later.
    ExitApp
}

; =============== 
  version = Midi2KeyStroke.01   ; file name and version
; =============== 

readini()
gosub, MidiPortRefresh          ; used to refresh the input and output port lists - see label below 
port_test(numports)             ;  test the ports - check for valid ports?
gosub, midiin_go                ; opens the midi input port listening routine
;gosub, midiout                 ; opens the midi out port   
gosub, midiMon                  ; see below - a monitor gui - for learning mostly - you will probably comment this out eventually.

; =============== end of auto execute section =====================

return


;*************************************************
;*      MIDI input MESSAGE PARSE and filtering
;*************************************************
/* 
  PARSE - LAST MIDI MESSAGE RECEIVED - 
  - manipulate midi input message - 
  - send it to the output port
  Edit the section below to process your midi data the way you want.
  couple of examples provided.
  You will need to use if elese or ifequal ... etc to determine which midi data you want to change and which to pass or to block.
  These if statements can be nested (see below)
*/

MidiMsgDetect(hInput, midiMsg, wMsg) ; Midi input section calls this function each time a midi message is received. Then the midi message is broken up into parts for manipulation.  See http://www.midi.org/techspecs/midimessages.php (decimal values).
  { ; * this function goes on for a while watch for the end brace...
    global statusbyte, chan, note, cc, byte1, byte2
    
    statusbyte 	:=  midiMsg & 0xFF			; EXTRACT THE STATUS BYTE (WHAT KIND OF MIDI MESSAGE IS IT?)
    chan 		:= (statusbyte & 0x0f) + 1	; WHAT MIDI CHANNEL IS THE MESSAGE ON?
    byte1 		:= (midiMsg >> 8) & 0xFF	; THIS IS DATA1 VALUE = NOTE NUMBER OR CC NUMBER
    byte2 		:= (midiMsg >> 16) & 0xFF	; DATA2 VALUE IS NOTE VELEOCITY OR CC VALUE
            
      
      ;midi monitor - just a monitor showing all input
      GuiControl,12:, MidiMsOut, MidiMon:%statusbyte% %chan% %byte1% %byte2% ; MidiMs (green text in gui) in midimon gui below - this is for display you will probably comment this out evenually.
    
  ; After there ok to edit  - watch your " { } " - this is all part of the above MidiMsgDetect function - 

  ;*************************************************
  ;*     Midi filters and keypresses to send
  ;*************************************************
    ; Is midi input a Note On/off message?
  if Statusbyte between 144 and 152 ;or (statusbyte between 128 and 143) ; note on message?  You logic may vary...
	{
			
	if (byte1 = 57) ; test if note message is note number 57 with note on status byte
		{
			GuiControl,12:, MidiMs, convert1:%statusbyte% %chan% %byte1% %byte2%  ; for display of filtered note passing to gui -testing only

            ;*************************************************
            ;*    Send some keypresses - try testing with NOTEPAD with focus.
            ;*************************************************
            send {tab 2}{1}         ; send two tabs then the number 1
              sleep, 1000           ; wait 1 seccond - sleep timers only shown for reference. May be needed  for response.
            send {enter}{tab 3}{2}  ; send enter, then 3 tabs followed by the number 2
              sleep, 2000
            send {enter} {tab 4}{3} ; send enter, then tab 4 times followed by the number 3       
        }

    if (byte1 = 59) ; test for note number 59
		{
			GuiControl,12:, MidiMs, convert2:%statusbyte% %chan% %byte1% %byte2%  ; for display testing only

            send {tab 2}{4} 
              sleep, 1000 
            send {enter}{tab 3}{5} 
              sleep, 2000
            send {enter} {tab 4}{6}     
           
        }  
    } ; end of note on detection
  
;
;*************************************************
;*   this is for cc detection statusbyte           
;*************************************************
  ; Is midi input a CC?
  if statusbyte between 176 and 192 ; check status byte for cc 
    {
        GuiControl,12:, MidiMsOut, cc:%statusbyte% %chan% %cc% %byte2% ; just to update the gui
        
        ; do something here, if you want it to do anything with the cc except display it.
        
    }    
 
  /* 
  ;Here is an example of a cc on midi chan 1, operation that presses numlock or capslock depending on which cc (data1) is input.
  ; replace the cc statement above with this block

  ifequal, statusbyte, 176 ; is data input a cc message, midi ch 1?
     {
         ifegual, byte1, 80 ; byte1 for cc is the cc# (is this cc, cc#80?)
            {
                 send {NumLock} ; if it was cc80 then send a numlock keypress
             }
           
         ifequal, byte1, 90 ; is this cc, cc#90 ?
             {
                 send {CapsLock} ; if it was cc90 then send a capslock
             }
      }
  */

  ;*************************************************
  ;*           Is midi input a Program Change?
  ;*************************************************;
  if statusbyte between 192 and 208  ; make this a between statement
      {
          ;GuiControl,12:, MidiMsOut, ProgC:%statusbyte% %chan% %byte1% %byte2% 
          ; need something for it to do here, could be converting to a cc or a note or changing the value of the pc
          ; however, at this point the only thing that happens is the gui change, not midi is output here.
          ; you may want to make a SendPc: label below
      }

  } ; end of MidiMsgDetect funciton


;*************************************************
;*          midi monitor gui - this could be on the tray menu for use when needed.
;*************************************************;

midiMon: ;just a simple gui window for a midi monitor of sorts.

  Gui, 12: +LastFound +AlwaysOnTop +Caption +ToolWindow  ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
  Gui,12: Color, white ; %CustomColor% ;blue ;
  Gui,12: Font, s15 ; Set a large font size (32-point).

  gui, 12: add, text, w250 vMidiMsOut cblue, XXXXX YYYYY
  Gui,12: Add, Text, w250 vMidiMs cgreen, XXXXX YYYYY  ; XX & YY serve to auto-size the window.;gui, 12: add, text, w250 vMidiMsOutSend cred, XXXXX YYYYY
  Gui,12: Show, xcenter y0 w500 NoActivate, %version% Midi Monitor - thingy  ; NoActivate avoids deactivating the currently active window.

; ===============  end monitor gui
;****************************************************************************************************************
;******************************************** midi "under the hood" *********************************************
/* 
    This part is meant to take care of the "under the hood" midi input and output selection and save selection to an ini file.
    Hopefully it simplifies usage for others out here trying to do things with midi and ahk.
    
    * use it as an include.
    
    The code here was taken/modified from the work by TomB/Lazslo on Midi Output
        http://www.autohotkey.com/forum/viewtopic.php?t=18711&highlight=midi+output
    
    Orbik's Midi input thread 
        http://www.autohotkey.com/forum/topic30715.html
        This method does NOT use the midi_in.dll, it makes direct calls to the winmm.dll
        
    Many different people took part in the creation of this file.
    
    ; Last edited 6/17/2010 11:30 AM by genmce

*/

MidiPortRefresh: ; get the list of ports

 MIlist := MidiInsList(NumPorts) 
	Loop Parse, MIlist, | 
		{
		}
	TheChoice := MidiInDevice + 1

/* ; midi out port stuff
MOlist := MidiOutsList(NumPorts2) 
   Loop Parse, MOlist, | 
		{
		}
	TheChoice2 := MidiOutDevice + 1
*/
return

;-----------------------------------------------------------------

ReadIni() ; also set up the tray Menu
  {
    Menu, tray, add, MidiSet            ; set midi ports tray item
    Menu, tray, add, ResetAll           ; Delete the ini file for testing --------------------------------

    global MidiInDevice, version ; MidiOutDevice   ;version var is set at the beginning.
    IfExist, %version%io.ini
      {
        IniRead, MidiInDevice, %version%io.ini, Settings, MidiInDevice , %MidiInDevice%     ; read the midi In port from ini file
        ;IniRead, MidiOutDevice, %version%io.ini, Settings, MidiOutDevice , %MidiOutDevice%  ; read the midi out port from ini file
      }
    Else ; no ini exists and this is either the first run or reset settings.
      {
        MsgBox, 1, No ini file found, Select midi ports?
        IfMsgBox, Cancel
          ExitApp
        IfMsgBox, yes
          gosub, midiset
        ;WriteIni()
      }
  }

;CALLED TO UPDATE INI WHENEVER SAVED PARAMETERS CHANGE
WriteIni()
  {
    global MidiInDevice, version ;MidiOutDevice
   
    IfNotExist, %version%.ini ; if no ini 
      FileAppend,, %version%.ini ; make one with the following entries.
    IniWrite, %MidiInDevice%, %version%io.ini, Settings, MidiInDevice
    ;IniWrite, %MidiOutDevice%, %version%io.ini, Settings, MidiOutDevice
  }

;------------ port testing to make sure selected midi port is valid --------------------------------

port_test(numports) ; confirm selected ports exist ; CLEAN THIS UP STILL ,numports2

  {
    global midiInDevice, midiok ;midiOutDevice
    
    ; ----- In port selection test based on numports
    If MidiInDevice not Between 0 and %numports% 
      {
        MidiIn := 0 ; this var is just to show if there is an error - set if the ports are valid = 1, invalid = 0
            ;MsgBox, 0, , midi in port Error ; (this is left only for testing)
        If (MidiInDevice = "")              ; if there is no midi in device 
            MidiInerr = Midi In Port EMPTY. ; set this var = error message
            ;MsgBox, 0, , midi in port EMPTY
        If (midiInDevice > %numports%)          ; if greater than the number of ports on the system.
            MidiInnerr = Midi In Port Invalid.  ; set this error message
            ;MsgBox, 0, , midi in port out of range
      }
    Else
      {
        MidiIn := 1 ; setting var to non-error state or valid
      }
   /*
   ; ----- out port selection test based on numports2
    If  MidiOutDevice not Between 0 and %numports2%
      {
        MidiOut := 0 ; set var to 0 as Error state.
        If (MidiOutDevice = "")                 ; if blank
            MidiOuterr = Midi Out Port EMPTY.   ; set this error message
            ;MsgBox, 0, , midi o port EMPTY
        If (midiOutDevice > %numports2%)             ; if greater than number of availble ports  
            MidiOuterr = Midi Out Port Out Invalid.  ; set this error message   
            ;MsgBox, 0, , midi out port out of range
      }
    */
    /*
    Else
      {
        MidiOut := 1 ;set var to 1 as valid state.
      }
      ; ---- test to see if ports valid, if either invalid load the gui to select.
      ;midicheck(MCUin,MCUout)
      */
    If (%MidiIn% = 0)
      {
        MsgBox, 49, Midi Port Error!,%MidiInerr%`nLaunch Midi Port Selection!
        IfMsgBox, Cancel
          ExitApp
        midiok = 0 ; Not sure if this is really needed now....
        Gosub, MidiSet ;Gui, show Midi Port Selection
      }
    Else
      {
        midiok = 1
        Return ; DO NOTHING - PERHAPS DO THE NOT TEST INSTEAD ABOVE.
      }
  }
Return

; ------------------ end of port testing ---------------------------


MidiSet: ; midi port selection gui
 
 ; ------------- MIDI INPUT SELECTION -----------------------
  ;Gui, Destroy
  ;Gosub, Suspendit
  Gui, 6: Destroy
  Gui, 2: Destroy
  Gui, 3: Destroy
  Gui, 4: Destroy
  ;Gui, 5: Destroy
  Gui, 4: +LastFound +AlwaysOnTop   +Caption +ToolWindow ;-SysMenu
  Gui, 4: Font, s12
  Gui, 4: add, text, x10 y10 w300 cmaroon, Select Midi Ports. ; Text title
  Gui, 4: Font, s8
  Gui, 4: Add, Text, x10 y+10 w175 Center , Midi In Port  ;Just text label
  Gui, 4: font, s8
  ; midi ins list box
  Gui, 4: Add, ListBox, x10 w200 h100  Choose%TheChoice% vMidiInPort gDoneInChange AltSubmit, %MiList% ; --- midi in listing of ports
    ;Gui,  Add, DropDownList, x10 w200 h120 Choose%TheChoice% vMidiInPort gDoneInChange altsubmit, %MiList%  ; ( you may prefer this style, may need tweak)

  ; --------------- MidiOutSet ---------------------
 ; Gui, 4: Add, TEXT,  x220 y40 w175 Center, Midi Out Port ; gDoneOutChange
  ; midi outlist box
  ;Gui, 4: Add, ListBox, x220 y62 w200 h100 Choose%TheChoice2% vMidiOutPort gDoneOutChange AltSubmit, %MoList% ; --- midi out listing
  ;Gui,  Add, DropDownList, x220 y97 w200 h120 Choose%TheChoice2% vMidiOutPort gDoneOutChange altsubmit , %MoList%
  Gui, 4: add, Button, x10 w205 gSet_Done, Done - Reload script.
  Gui, 4: add, Button, xp+205 w205 gCancel, Cancel
  ;gui, 4: add, checkbox, x10 y+10 vNotShown gDontShow, Do Not Show at startup.
  ;IfEqual, NotShown, 1
  ;guicontrol, 4:, NotShown, 1
  Gui, 4: show , , %version% Midi Port Selection ; main window title and command to show it.

Return

;-----------------gui done change stuff - see label in both gui listbox line

DoneInChange:
  Gui, 4: Submit, NoHide
  Gui, 4: Flash
  If %MidiInPort%
      UDPort:= MidiInPort - 1, MidiInDevice:= UDPort ; probably a much better way do this, I took this from JimF's qwmidi without out editing much.... it does work same with doneoutchange below.
  GuiControl, 4:, UDPort, %MidiIndevice%
  WriteIni()
  ;MsgBox, 32, , midi in device = %MidiInDevice%`nmidiinport = %MidiInPort%`nport = %port%`ndevice= %device% `n UDPort = %UDport% ; only for testing
Return

Set_Done: ;  aka reload program, called from midi selection gui
  Gui, 3: Destroy
  Gui, 4: Destroy
   sleep, 100
  Reload
Return

Cancel:
  Gui, Destroy
  Gui, 2: Destroy
  Gui, 3: Destroy
  Gui, 4: Destroy
  Gui, 5: Destroy
Return

; ********************** Midi output detection
/*
MidiOut: ; Function to load new settings from midi out menu item
  OpenCloseMidiAPI()
  h_midiout := midiOutOpen(MidiOutDevice) ; OUTPUT PORT 1 SEE BELOW FOR PORT 2
return
*/

ResetAll: ; for development only, leaving this in for a program reset if needed by user
  MsgBox, 33, %version% - Reset All?, This will delete ALL settings`, and restart this program!
  IfMsgBox, OK
    {
      FileDelete, %version%.ini   ; delete the ini file to reset ports, probably a better way to do this ...
      Reload                        ; restart the app.
    }
  IfMsgBox, Cancel
Return

GuiClose: ; on x exit app
  Suspend, Permit ; allow Exit to work Paused. I just added this yesterday 3.16.09 Can now quit when Paused.
 
  MsgBox, 4, Exit %version%, Exit %version%? ; 
  IfMsgBox No
      Return
  Else IfMsgBox Yes
      ;midiOutClose(h_midiout)
    
  Gui, 6: Destroy
  Gui, 2: Destroy
  Gui, 3: Destroy
  Gui, 4: Destroy
  Gui, 5: Destroy
  gui, 7: destroy
 ;gui, 
 Sleep 100
  ;winclose, Midi_in_2 ;close the midi in 2 ahk file
 ExitApp


;############################################## MIDI LIB from orbik and lazslo#############
;-------- orbiks midi input code --------------
; Set up midi input and callback_window based on the ini file above.
; This code copied from ahk forum Orbik's post on midi input

; nothing below here to edit.

; =============== midi in =====================

Midiin_go:
DeviceID := MidiInDevice      ; midiindevice from IniRead above assigned to deviceid
CALLBACK_WINDOW := 0x10000    ; from orbiks code for midi input

Gui, +LastFound 	; set up the window for midi data to arrive.
hWnd := WinExist()	;MsgBox, 32, , line 176 - mcu-input  is := %MidiInDevice% , 3 ; this is just a test to show midi device selection

hMidiIn =
VarSetCapacity(hMidiIn, 4, 0)
  result := DllCall("winmm.dll\midiInOpen", UInt,&hMidiIn, UInt,DeviceID, UInt,hWnd, UInt,0, UInt,CALLBACK_WINDOW, "UInt")
    If result
      {
        MsgBox, Error, midiInOpen Returned %result%`n
        ;GoSub, sub_exit
      }

hMidiIn := NumGet(hMidiIn) ; because midiInOpen writes the value in 32 bit binary Number, AHK stores it as a string
  result := DllCall("winmm.dll\midiInStart", UInt,hMidiIn)
    If result
      {
        MsgBox, Error, midiInStart Returned %result%`nRight Click on the Tray Icon - Left click on MidiSet to select valid midi_in port.
        ;GoSub, sub_exit
      }

OpenCloseMidiAPI()
  
  ; ----- the OnMessage listeners ----

      ; #define MM_MIM_OPEN 0x3C1 /* MIDI input */
      ; #define MM_MIM_CLOSE 0x3C2
      ; #define MM_MIM_DATA 0x3C3
      ; #define MM_MIM_LONGDATA 0x3C4
      ; #define MM_MIM_ERROR 0x3C5
      ; #define MM_MIM_LONGERROR 0x3C6

    OnMessage(0x3C1, "MidiMsgDetect")  ; calling the function MidiMsgDetect in get_midi_in.ahk
    OnMessage(0x3C2, "MidiMsgDetect")  
    OnMessage(0x3C3, "MidiMsgDetect")
    OnMessage(0x3C4, "MidiMsgDetect")
    OnMessage(0x3C5, "MidiMsgDetect")
    OnMessage(0x3C6, "MidiMsgDetect")

Return

;--- MIDI INS LIST FUNCTIONS - port handling -----

MidiInsList(ByRef NumPorts)
  { ; Returns a "|"-separated list of midi output devices
    local List, MidiInCaps, PortName, result
    VarSetCapacity(MidiInCaps, 50, 0)
    VarSetCapacity(PortName, 32)                       ; PortNameSize 32

    NumPorts := DllCall("winmm.dll\midiInGetNumDevs") ; #midi output devices on system, First device ID = 0

    Loop %NumPorts%
      {
        result := DllCall("winmm.dll\midiInGetDevCapsA", UInt,A_Index-1, UInt,&MidiInCaps, UInt,50, UInt)
        If (result OR ErrorLevel) {
            List .= "|-Error-"
            Continue
          }
        DllCall("RtlMoveMemory", Str,PortName, UInt,&MidiInCaps+8, UInt,32) ; PortNameOffset 8, PortNameSize 32
        List .= "|" PortName
      }
    Return SubStr(List,2)
  }

MidiInGetNumDevs() { ; Get number of midi output devices on system, first device has an ID of 0
    Return DllCall("winmm.dll\midiInGetNumDevs")
  }
MidiInNameGet(uDeviceID = 0) { ; Get name of a midiOut device for a given ID

    ;MIDIOUTCAPS struct
    ;    WORD      wMid;
    ;    WORD      wPid;
    ;    MMVERSION vDriverVersion;
    ;    CHAR      szPname[MAXPNAMELEN];
    ;    WORD      wTechnology;
    ;    WORD      wVoices;
    ;    WORD      wNotes;
    ;    WORD      wChannelMask;
    ;    DWORD     dwSupport;

    VarSetCapacity(MidiInCaps, 50, 0)  ; allows for szPname to be 32 bytes
    OffsettoPortName := 8, PortNameSize := 32
    result := DllCall("winmm.dll\midiInGetDevCapsA", UInt,uDeviceID, UInt,&MidiInCaps, UInt,50, UInt)

    If (result OR ErrorLevel) {
        MsgBox Error %result% (ErrorLevel = %ErrorLevel%) in retrieving the name of midi Input %uDeviceID%
        Return -1
      }

    VarSetCapacity(PortName, PortNameSize)
    DllCall("RtlMoveMemory", Str,PortName, Uint,&MidiInCaps+OffsettoPortName, Uint,PortNameSize)
    Return PortName
  }

MidiInsEnumerate() { ; Returns number of midi output devices, creates global array MidiOutPortName with their names
    local NumPorts, PortID
    MidiInPortName =
    NumPorts := MidiInGetNumDevs()

    Loop %NumPorts% {
        PortID := A_Index -1
        MidiInPortName%PortID% := MidiInNameGet(PortID)
      }
    Return NumPorts
  }

OpenCloseMidiAPI() {  ; at the beginning to load, at the end to unload winmm.dll
    static hModule
    If hModule
        DllCall("FreeLibrary", UInt,hModule), hModule := ""
    If (0 = hModule := DllCall("LoadLibrary",Str,"winmm.dll")) {
        MsgBox Cannot load libray winmm.dll
        Exit
      }
  }

KeyMce/GenMce - mackie emulator for pc keyboard/Convert your controller to mackie.
Midi I/O - Want to play with midi/ahk? links dead.. pm me


BlobVanDam
  • Members
  • 6 posts
  • Last active: Jun 04 2011 06:33 PM
  • Joined: 30 May 2011
Thank you very much! I really do appreciate you taking the time to do this, and doing it so quickly.

And don't worry, this is perfect. I didn't expect you to write the whole thing for me. This is very clearly commented enough for me to manage the rest, and I've dealt with raw midi input before. I just couldn't do it on my own when when all I had was the complete midi input/output package.

I've already written a couple of my own keypresses, so that's already 90% of what I need sorted, so I'm happy.

Thank you again. You've made my day. :D

genmce
  • Members
  • 144 posts
  • Last active: May 21 2015 03:09 PM
  • Joined: 10 Jan 2009
No problem.
I am curious what you are doing, specifically?
I hope you will consider posting your finished script back here.

KeyMce/GenMce - mackie emulator for pc keyboard/Convert your controller to mackie.
Midi I/O - Want to play with midi/ahk? links dead.. pm me


Kumpan
  • Members
  • 7 posts
  • Last active: Feb 19 2012 08:47 PM
  • Joined: 08 Dec 2010
Hmmm, curious...i want to send a cc# value 50 when i press a pad on my akai mpd (note on) and send cc# value 0 when releasing the pad (note off).
It works so far but for some reasons it sometimes doesn't send a note off...
If i play the mpd directly into my daw, it always send a correct note off when releasing the pad.

; ==============================================	   
; =============== NOTE MESSAGES  =============== 
; ==============================================
if statusbyte between 128 and 159 ; see range of values for notemsg var defined in autoexec section. 
{ 
	; ================== NOTE ON =======================
	if statusbyte between 144 and 159 ; detect if note message is "note on" (key is held down)
	{
		ifEqual, byte1, 0 ; if the note number coming in is note #0
		{
			cc_num = 7 				
			CCIntVal = 50
			midiOutShortMsg(h_midiout, (Channel+175), CC_num, CCIntVal)
				; =============== needed for output gui display only
				stb := "CC"
				statusbyte := 176
				chan = %channel%
				byte1 = %cc_num%			
				byte2 = %CCIntVal%			
				gosub, ShowMidiOutMessage   
				; =============== end 		
		}


	}
	; ================== NOTE OFF =======================
  
	if statusbyte between 128 and 143 ; detect if note message is "note off" (key is released)
	{

		ifEqual, byte1, 0
		{
			cc_num = 7 				
			CCIntVal = 0
			midiOutShortMsg(h_midiout, (Channel+175), CC_num, CCIntVal) 	
				; =============== needed for output gui display only
				stb := "CC"
				statusbyte := 176
				chan = %channel%
				byte1 = %cc_num%			
				byte2 = %CCIntVal%			
				gosub, ShowMidiOutMessage   
				; =============== end 		
		}		

		
	}
} ; end of note block
  
; ========================================================	   
; =============== CONTROL CHANGE MESSAGES  =============== 
; ========================================================  
;gosub, sendcc
if statusbyte between 176 and 191 ; check status byte for cc 176-191 is the range for CC messages ; 
{
	
	if (byte2 > 0)		; pressed
	{
		ifEqual, byte1, 14
		  {
			Send {Space}
		  }
	}
	
	ifEqual, byte2, 0 	; released
	{
		; actually does nothing whenever a cc sends 0
	}	
	
} ; end of control change block



EDIT:
ok, i am just on the way and noticed that with lpd8 pads everything is working...
Must have something to do with the pads from mpd32 which seem to send some midi commands each time your press the pad harder or softer (without releasing it).
I don't have it with me so i will delve into this again at home.

Kumpan
  • Members
  • 7 posts
  • Last active: Feb 19 2012 08:47 PM
  • Joined: 08 Dec 2010
Something else: Can anybody tell me how to send an MMC message with midiOutShortMsg?

Something like this:

The general form of an MMC message (that is sent to, or generated by, an MMC device) is:

0xF0 0x7F <deviceID> 0x06 <command> 0xF7

The third byte is the Device ID.
The fifth byte is the <command>. It is one of the following values:

0x01 Stop
0x02 Play
0x03 Deferred Play
0x04 Fast Forward
0x05 Rewind
0x06 Record Strobe (Punch In)
0x07 Record Exit (Punch out)
0x09 Pause

aaronbewza
  • Members
  • 466 posts
  • Last active: Feb 05 2013 08:40 AM
  • Joined: 20 Feb 2011
orbik said "In theory you could make the script play sounds on midi input but what's the point of that?"

I've created a collection of Kontakt Instruments. Kontakt is a sampler that plays sampled instruments and also lets us create
them with audio files. For example, I tuned my tattoo machine to the note "C" and I recorded that note. Then I assigned that
recording to the actual MIDI keyboard's "C" key. I can also add multiple recordings of the "C" note at different velocities and
place them in the corresponding velocity ranges for that note. So, if I press the key softly it instantly plays the soft sample.
If I hammer the key it plays the louder samples.

This script of yours suddenly has my deep interest, as I have this entire collection of the different key's samples for a range
of custom instruments, including a guitar, recorder, harmonica, a blade of grass, mouth drums etc etc. I hope I can figure out
how to make these instruments using AutoHotkey and your script! It would mean that I could freely share these instruments
and people would not have to use a third-party program like Kontakt to play them!

I will hammer away at this until I run into trouble, then I might have some questions. Thank you very much for
making this possible :)
Aaron

burt777
  • Members
  • 3 posts
  • Last active: Jul 22 2015 02:44 PM
  • Joined: 26 Aug 2012
Posted Image

Sweet! I used this to create a script that controls the PatchMix DSP of my E-Mu 1212m sound card and my WinAmp Volume, through my Akai LDP-8 drumpad.

The E-mu is a matter of opening the mixer (if it isn't already), clicking on the field that allows you to type the value in dB, and send the data. I tried dragging the faders with the mouse, but that got really messy. Controller 5 and 6 control the left-most and second fader. What's still missing is a smooth way to close the mixer when you're done and return the mouse to its original position, but i on't want to close the mixer after every change (because turning the knob gives dozens of changes in a second!)

WinAmp can be controlled with CLAMP, which is a command line tool. (controller 7).

Controlling the windows mixer (controller 8) is the easiest part, and works most smootly, because autohotkey has a builtin function SoundSet.

The coolest part is that you can turn a cheap interface into a powerful tool. I still have 4 knobs and 8 pads left. My mixer just broke down, but at least i have 8 assignable volume knobs! (-:

Thanks OP!

#SingleInstance force

SendMode Input 
SetWorkingDir %A_ScriptDir% 

OnExit, sub_exit 
if (midi_in_Open(1)) 
    ExitApp 

;--------------------  Midi "hotkey" mappings  ----------------------- 

; listenCC(1, "wheel1")
; listenCC(2, "wheel2")
; listenCC(3, "wheel3")
; listenCC(4, "wheel4")
listenCC(5, "wheel5")
listenCC(6, "wheel6")
listenCC(7, "wheel7")
listenCC(8, "wheel8")

return   
;----------------------End of auto execute section-------------------- 

sub_exit: 
    ; midi_in_Close()  ; i commented this out because it caused AHK to hang. I hope i'm not breaking other stuff!!
    ExitApp 

;-------------------------Miscellaneous hotkeys----------------------- 
; Esc::ExitApp 

;-------------------------Midi "hotkey" functions---------------------


wheel5(a, b, c) {
    static nextRun := 0
    if a_tickCount > %nextRun%
    {
        CCvalue5 := getCC(5, 1)
        updateDSPMixer(71, 447, CCvalue5)
        nextRun := a_tickCount + 40
    }
    return
}

wheel6(a, b, c) {
    static nextRun := 0
    if a_tickCount > %nextRun%
    {
        CCvalue5 := getCC(6, 1)
        updateDSPMixer(155, 447, CCvalue5)
        nextRun := a_tickCount + 40
    }
    return
}

wheel7(a, b, c) {
    static nextRun := 0
    if a_tickCount > %nextRun%
    {
        CCvalue7 := getCC(7, 1) * 2
        run, Clamp.exe /volset %CCvalue7%, , Hide
        nextRun := a_tickCount + 40
    }
    return
}

wheel8(a, b, c) {
    static nextRun := 0
    if a_tickCount > %nextRun%
    {
        CCvalue := getCC(8, 1) / 1.1
        
        SoundSet, CCvalue, , 
        
        nextRun := a_tickCount + 40
    }
    return
}




updateDSPMixer(faderX, faderY, CCvalue) {
    Static faderTopY := 10
    Static faderBottomY := -80
    
    IfWinExist, PatchMix DSP 
    {
       IfWinActive, PatchMix DSP 
        {
          ; Movement starts here:
          faderVal := Ceil(faderBottomY + ((faderTopY - faderBottomY) / 127 * CCvalue))
          MouseGetPos, xpos, ypos
          mouseClick, Left, faderX, faderY
          send %faderVal%{enter}
          ; MouseMove, xpos, ypos
          ToolTip, %CCvalue% -> %faderVal%, 0, 0, myStatus
      } Else 
        {
            WinActivate   ; no need to specify title again, since it will automatically use the "last found" window.
            ToolTip, Trying to Activate the patchmix, 0, 0,  myStatus
        }      
    } Else
    {
        ToolTip, Running the patchmix, 0, 0,  myStatus
        RunWait, C:\Program Files\Creative Professional\E-MU PatchMix DSP\EmuPMixDSP.exe
    }
    return    

}

;-------------------------  Midi input library  ---------------------- 
#include midi_in_lib.ahk


aaronbewza
  • Members
  • 466 posts
  • Last active: Feb 05 2013 08:40 AM
  • Joined: 20 Feb 2011
Ok... I've built my first sampled instrument. "Aaron's Tattoo Machine Instrument".
I tuned my tattoo machine to different pitches by adjusting both the armature bar settings and the machine's voltage.
I used a pitch-shifter to fill in the required notes in between, and built three samples for each note's velocities: soft, medium, and loud.
I figured out how to stop playing the sample upon key release... the sound of the tattoo machine's pedal is used
as the sound played on its release.

There's a problem... polyphony. There needs to be a way to play more than one sound at a time, at the same time.
That ability would open up an entire world of musical instrument production for me! I'm using "SoundPlay" which
refers to %byte1% to choose the sample to play (for example, with middle C... %byte1% is 60 so the sample 60_soft.wav is played).

I'm going to post on the ask-for-help thread about multiple instances of SoundPlay, or for another way to play multiple sounds simultaneously.
Thanks very much for figuring this out!

burt777
  • Members
  • 3 posts
  • Last active: Jul 22 2015 02:44 PM
  • Joined: 26 Aug 2012
The script i posted above (the one to control different software mixers with the Akai midi drumpad), turns out to be a work in progress. First of all, i took the liberty of changing the first two functions of the midi_in_lib.ahk library. You can "ask" for a midi port by number, but since midi/usb devices are pretty much in random order, i wanted to (also) specify a default midi port by name (my drumpad is called "USB Audio Device", which is annoyingly generic, but it works better than a number).

Here's the functions, i'm not sure if this is the best way to go about it, but it works for me. Suggestions are welcome:

midi_in_Open(defaultDevID = -1)
{
    global
      if ((midi_in_hModule := DllCall("LoadLibrary", Str,A_ScriptDir . "\midi_in.dll")) == 0)
    {
        MsgBox Cannot load library midi_in.dll"
        return 1
    }

    if (strLen(defaultDevID) < 3) {
        if (defaultDevID >= DllCall("midi_in.dll\getNumDevs"))
            defaultDevID := -1
    }

    midi_in_MakeTrayMenu(defaultDevID)
    midi_in_OpenDevice(midi_in_lastSelectedMenuItem)
    return 0
}

midi_in_MakeTrayMenu(defaultDevID)
{
    numDevs := DllCall("midi_in.dll\getNumDevs")
    global midi_in_lastSelectedMenuItem

    Menu devNameMenu, Add, No input, sub_menu_openinput
    Menu devNameMenu, Add ; separator 
    if (defaultDevID < 0)
        midi_in_lastSelectedMenuItem := "No Input"

    loop %numDevs%
    {
        devID := A_Index-1
        if ((devName := DllCall("midi_in.dll\getDevName", Int,devID, Str)) == 0)
        {
            MsgBox, Error in creating midi input device list
            return 1
        }
        Menu devNameMenu, Add, %devName%, sub_menu_openinput
        if (devID == defaultDevID || defaultDevID == devName)
        {
            Menu devNameMenu, Check, %devName%
            midi_in_lastSelectedMenuItem := devID
        }
    }
    Menu TRAY, Add, MIDI-in device, :devNameMenu
}

Also, i've changed the way the midi changes are handled. A midi event is being sent for every cc number passed, so if you turn the wheel quickly, you're generating 128 events in less than a second. For clamp and soundset (the functionality i use for resp. WinAmp and the sound mixer, with wheel 7 and 8) is fast enough to cope with this. But the E-MU PatchMix DSP method under wheel 5 and 6, was too slow. I had a protection against it (the "if a_tickCount > %nextRun%"), but that could possibly leave out the last few events, which are most important. Also, i wanted to close the Mixer window a second after the last event. This is the new version. Again: suggestions are welcome:

#SingleInstance force

; SendMode Input 
; SetWorkingDir %A_ScriptDir% 

OnExit, sub_exit 
if (midi_in_Open("USB Audio Device")) 
    ExitApp 

;--------------------  Midi "hotkey" mappings  ----------------------- 

; listenCC(1, "wheel1")
; listenCC(2, "wheel2")
; listenCC(3, "wheel3")
; listenCC(4, "wheel4")
listenCC(5, "wheel5")
listenCC(6, "wheel6")
listenCC(7, "wheel7")
listenCC(8, "wheel8")

return   
;----------------------End of auto execute section-------------------- 

sub_exit: 
    ; midi_in_Close() 
    ExitApp 

;-------------------------Midi "hotkey" functions---------------------


Wheel5(a, b, c) {
    Global ToggleWheel5, Wheel5LastUpdate
    If (!ToggleWheel5) {
        SetTimer, Wheel5Update, 50 
        ToggleWheel5 := true
    }
    Wheel5LastUpdate := A_TickCount
    Return
}

Wheel5Update:
    ; Update The Fader if nessecary
    CCvalue5 := getCC(5, 1)
    If (CCvalue5 != CCvalue5Old)
      updateDSPMixer(71, 447, CCvalue5)

    CCvalue5Old := CCvalue5
    ; Close the w
    If (Wheel5LastUpdate < (A_TickCount - 600))
    {
        IfWinActive, PatchMix DSP 
        Send, !{F4}
        SetTimer, Wheel5Update, Off
        ToggleWheel5 := false
    }  

    ; Shut off the timer
    If (!ToggleWheel5) 
    {
        SetTimer, Wheel5Update, Off
    }

Return





Wheel6(a, b, c) {
    Global ToggleWheel6, Wheel6LastUpdate
    If (!ToggleWheel6) {
        SetTimer, Wheel6Update, 50 
        ToggleWheel6 := true
    }
    Wheel6LastUpdate := A_TickCount
    Return
}

Wheel6Update:
    ; Update The Fader if nessecary
    CCvalue6 := getCC(6, 1)
    If (CCvalue6 != CCvalue6Old)
      updateDSPMixer(155, 447, CCvalue6)

    CCvalue6Old := CCvalue6
    ; Close the w
    If (Wheel6LastUpdate < (A_TickCount - 600))
    {
        IfWinActive, PatchMix DSP 
        Send, !{F4}
        SetTimer, Wheel6Update, Off
        ToggleWheel6 := false
    }  

    ; Shut off the timer
    If (!ToggleWheel6) 
    {
        SetTimer, Wheel6Update, Off
    }

Return







/*
wheel5(a, b, c) {
    static nextRun := 0
    if a_tickCount > %nextRun%
    {
        CCvalue5 := getCC(5, 1)
        updateDSPMixer(71, 447, CCvalue5)
        nextRun := a_tickCount + 40
    }
    return
}

wheel6(a, b, c) {
    static nextRun := 0
    if a_tickCount > %nextRun%
    {
        CCvalue5 := getCC(6, 1)
        updateDSPMixer(155, 447, CCvalue5)
        nextRun := a_tickCount + 40
    }
    return
}
*/


wheel7(a, b, c) {
    CCvalue7 := getCC(7, 1) * 2
    run, D:\Albums\Clamp.exe /volset %CCvalue7%, , Hide
    return
}

wheel8(a, b, c) {
    CCvalue := getCC(8, 1)         
    SoundSet, CCvalue, , 
    return
}




updateDSPMixer(faderX, faderY, CCvalue) {
    Static faderTopY := 10
    Static faderBottomY := -80
    IfWinExist, PatchMix DSP 
    {
       IfWinActive, PatchMix DSP 
        {
          ; Movement starts here:
          faderVal := Ceil(faderBottomY + ((faderTopY - faderBottomY) / 127 * CCvalue))
          ; MouseGetPos, MouseBeforeDSPMixerX, MouseBeforeDSPMixerY, WindowBeforeDSPMixer
          mouseClick, Left, faderX, faderY
          send %faderVal%{enter}
          ; ToolTip, %CCvalue% -> %faderVal%, 0, 0, myStatus
      } Else 
        {
            WinActivate   ; no need to specify title again, since it will automatically use the "last found" window.
            ; ToolTip, Trying to Activate the patchmix, 0, 0,  myStatus
        }      
    } Else
    {
        ; ToolTip, Running the patchmix, 0, 0,  myStatus
        RunWait, C:\Program Files\Creative Professional\E-MU PatchMix DSP\EmuPMixDSP.exe
    }
    Return    

}

; Midi input library   
#Include midi_in_lib.ahk



intermayer
  • Members
  • 1 posts
  • Last active: Jan 19 2013 11:04 AM
  • Joined: 19 Jan 2013

@ all: Thank you so much for your contribution in this thread!

I'm heavily using AHK for two or three years now, and thanks to this thread I was able to control sliders in the picture editor GIMP with faders on a cheap and simple Korg nanoKONTROL2.

GIMP allows assigning hotkeys to actions, and w/ a modification of genmce's script I've been able to get the job done. Thus changing the brush tip, brush size, brush opacity and mode, level opacity and mode and lots more with a fader feels much more ergonimic than moving the stylus on the graphic tablet from the picture to controls and back.

 

Again: thank you so much.



Rualark
  • Members
  • 2 posts
  • Last active: Sep 15 2013 06:59 AM
  • Joined: 10 Sep 2013

Hi All!

 

This is a very interesting topic for me.

I am working on converting CC moves of a knob into keypresses.

 

Unfortunately, I receive "Cannot load library midi_in.dll" message here. I checked A_ScriptDir and it is pointing to the folder with dll correctly. Can anybody help?

midi_in_Open(defaultDevID = -1)
{
	global
        MsgBox %A_ScriptDir%
	midi_in_hModule := DllCall("LoadLibrary", "str", A_ScriptDir . "\midi_in.dll")
        MsgBox %midi_in_hModule%
	if (midi_in_hModule == 0)
	{ 
		MsgBox Cannot load library midi_in.dll"
		return 1
	}
	if (defaultDevID >= DllCall("midi_in.dll\getNumDevs"))
		defaultDevID := -1
	
	midi_in_MakeTrayMenu(defaultDevID)
	if (defaultDevID >= 0)
		midi_in_OpenDevice(defaultDevID)
	return 0
}


It seems to me that the problem is that DLL is 32-bit and my system is Win7 x64. Can anybody please compile a 64 bit version?



micahstubbs
  • Members
  • 8 posts
  • Last active: Feb 07 2015 08:18 PM
  • Joined: 07 May 2012

Hi Rualark, I'm running 64 bit Windows 7 and have the same problem.  When I run MidInDllTest.ahk I see the error Cannot load library midi_in.dll



micahstubbs
  • Members
  • 8 posts
  • Last active: Feb 07 2015 08:18 PM
  • Joined: 07 May 2012

going to try to compile a 64 bit dll

 

When I open midi_in.vcproj in Visual Studio 2012, and accept some upgrade warnings, I get this Migration Report:

 

FBZvCr7.png



micahstubbs
  • Members
  • 8 posts
  • Last active: Feb 07 2015 08:18 PM
  • Joined: 07 May 2012

following this guide How to: Configure Visual C++ Projects to Target 64-Bit Platforms

 

https://msdn.microso...y/9yb4317s.aspx



micahstubbs
  • Members
  • 8 posts
  • Last active: Feb 07 2015 08:18 PM
  • Joined: 07 May 2012

I encountered these errors in Visual Studio 2012 while building the 64 bit midi_in.dll

 

v8ya8L6.png