OSC extension for Autohotkey, a DLL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Ludwig Frühschütz 3b36d8b689 Added OSC send message functions. Also fixed oscpack address string size check. 3 years ago
OSC2AHK Added OSC send message functions. Also fixed oscpack address string size check. 3 years ago
examples Added string example 3 years ago
oscpack Added OSC send message functions. Also fixed oscpack address string size check. 3 years ago
.gitignore Initial commit 3 years ago
LICENSE Initial commit 3 years ago
OSC2AHK.sln Added oscpack library and (very) basic dll integration 3 years ago
README.md Readme: Updated string retrievement 3 years ago
msgtest.ahk Fixed heap corruption in string retrievement, also avoid runtime error if string doesnt fit buffer 3 years ago

README.md

OSC2AHK

This is a DLL extension for AutoHotkey that enables support for OSC (Open Sound Control).

Work in progress

Currently this project is unfinished, there may be missing functionality and errors may occur!

  • OSC address wildcards are not supported yet
  • Documentation could be improved

Installation

Just download the latest version of OSC2AHK.dll from the releases page and place it in the same directory as your AutoHotkey scripts. Maybe you also want to check out the provided usage examples.

Usage

General concept

After the DLL is loaded and a network port is opened with the open() function, it receives all incoming OSC messages. To specify the messages that should be passed to the AutoHotkey (AHK) script, "listeners" are specified and created by the 'addListener()' function. When a OSC message is received that matches a existing listener, a "system message" is sent to the AHK script. This happens via the Windows message queue and the system message ID is used to tell the script which OSC address was received. Therefore with addListener() a ID has to be passed to the DLL which then uses this ID to post the system message.

Functions

Load DLL

First the DLL should be loaded from within the AHK script by calling

DllCall("LoadLibrary", "Str", "OSC2AHK.dll", "Ptr")

(Assuming the OSC2AHK.dll file is in the same directory as the calling script.)

Open network port

int open(HWND targetWindowHandle, unsigned int port);
//targetWindowHandle: Handle to the calling window to which messages on received OSC data is sent
//port: Network port to open

This opens the network port and enables the DLL to receive OSC messages.

Example AHK snippet:

Gui +LastFound
hWnd := WinExist()
DllCall("OSC2AHK.dll\open", UInt, hWnd, UInt, 7002)

Close network port

If you want to close the network port before termination of the script you may use:

int close(unsigned int clearListeners = 1);
//clearListeners: If this is not zero, all the listeners will also be removed (for future open() call). May be omitted, then defaults to 1 (remove listeners).

Example AHK call without clearing the listners:

DllCall("OSC2AHK.dll\close", UInt, 0)

Add listener

int addListener(LPCSTR address, unsigned int messageID, unsigned int dataType = OSC_TYPE_ALL);
//address: OSC address to listen for
//messageID: ID for the system message that is sent to the AHK script when this listener receives something.
//dataType: Specifies the OSC datatype to listen for. See below. May be omitted, then it defaults to all datatypes.

This adds a new listener for a OSC address pattern, only OSC messages with a existing listener will be passed to the calling script. The (system) message ID may be in the range 0x0400 (the value of WM_USER) through 0x7FFF for general purpose use but others can be used for special purposes as well (maybe see Microsoft's page about messages). Only if the address and the datatype of a incoming OSC message match the ones of the listeners, a message is posted to the AHK script. Note that the datatype can be one of the following values or the sum of multiple ones.

#define OSC_TYPE_NONE		1
#define OSC_TYPE_INT		2
#define OSC_TYPE_FLOAT		4
#define OSC_TYPE_STRING		8
#define OSC_TYPE_BLOB		16
#define OSC_TYPE_INT64		32
#define OSC_TYPE_FLOAT64	64
#define OSC_TYPE_BOOL		128
#define OSC_TYPE_CHAR		256
#define OSC_TYPE_TIMETAG	512
#define OSC_TYPE_COLOR		1024
#define OSC_TYPE_ALL		UINT_MAX //=0xffffffff

Add a listener from the AHK script:

; just for convenience, you also could use the "magic numbers" directly
global oscTypeInt := 2
global oscTypeFloat := 4
; Tell the DLL to post a system message with ID 0x1001 when a OSC message with address "/test1" and type Integer or Float is received.
DllCall("OSC2AHK.dll\addListener", AStr, "/test1", UInt, 0x1001, UInt, oscTypeInt+oscTypeFloat)
; tell AHK to invoke the function msghandlerTest1 when a windows message with ID 0x1001 is received
OnMessage(0x1001, "msghandlerTest1")

Then, there has to be the handler function in the AHK script:

msghandlerTest1(oscType, data, msgID, hwnd) 
{
    ; Check which datatype we received
    if (oscType = oscTypeInt) 
    {
        ; Integers can be used "as is"
        msgbox,Got Integer: %data%
    }
    if (oscType = oscTypeFloat) 
    {
        ; Floats have to be "reinterpreted"
        VarSetCapacity(buf, 4, 0)
        NumPut(data, buf)
        theFloat := NumGet(buf, "Float")
        msgbox,Got Float: %theFloat%
    }
}

Note that you may add many listeners, for any combination of the same/different OSC addresses, datatypes and system message IDs. Also, in AutoHotkey you may add multiple callback functions for the same system message ID or multiple system message IDs to the same callback function. This creates a big flexibility but to keep it less clutterd, such multiple routing paths should be avoided where possible.

Remove listener

Of course, listeners also can be removed in a similar way:

int removeListener(LPCSTR address);

Example AHK call:

DllCall("OSC2AHK.dll\removeListener", AStr, "/test1")

Get string data

As string data carried by OSC messages may be of varying length and cannot directly be passed by system messages. Therefore, the string is buffered in the DLL and the data variable of the callback function receives a unique identifier to the stored string. This string then can be retrieved later on from the AHK script by calling this function.

char* getStringData(char* targetString, unsigned int targetSize, unsigned int StringID);

This function writes the stored string to the targetString and also removes it from the buffer inside the DLL.

A callback functin in AHK could look like this (also look at the string example):

msghandlerString(oscType, data, msgID, hwnd) 
{
    VarSetCapacity(theString, 20)
    theString := DllCall("OSC2AHK.dll\getStringData", str, theString, UInt, 20, UInt, data, AStr)

    msgbox,Got string: %theString%
}

Credits

This DLL uses the oscpack OSC implementation by Ross Bencina.