• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code receiving OSC messages

Y

yoyowan

Guest
I am trying to implement OSC receive in my game with the help of this excellent work from hryx here :

https://github.com/hryx/osc.gml

I am struggling to make it work and can not see messages I am sending from my phone...
First I set the same WIFI network for both computer and phone. OSC host adress on phone is 192.168.1.37 and send port (outgoing) set to 8001

Code:
Create event //
osc_init("192.168.1.37",9000,8001); // so receive port is 8001

Async Networking event //
var messages = osc_receive();
for (i = 0; i < array_length_1d(messages); i++) {
    show_message(string(messages));
}

I do not see any message... any help ?
 
Last edited by a moderator:
Y

yoyowan

Guest
Here is the script for osc_receive from hryx :

Code:
/* osc.gml
 * by hryx 2015
 * Public Domain
 *
 * OSC event callback.
 * Parses incoming OSC packets and returns an array: Address pattern and args.
 * An integer (false or a negative number) is returned upon error.
 * Each OSC message will be a list containing an address pattern and
 * optionally argument values. Values are translated to native GML types:
 * string, real (blob not supported).
 * Currently, #bundle messages are not supported.
 * This script must be called in an object's Networking Event through the GUI.
 */

// Information from the Networking event
var server = async_load[? "id"];
var type = async_load[? "type"];
var buf = async_load[? "buffer"];
var size = async_load[? "size"];

// Only interested in payloads on the server socket
if (server != osc_listen_socket) {
    return -1;
}
if (type != network_type_data) {
    return -1;
}

// Local ****
var i;
var j;
var addrpattern;
var typetagstr;
var typetags;
var arguments;
var buf_tmp = buffer_create(4, buffer_fixed, 4);
var messages;

//// Parse the packet ////

// Address pattern
buffer_seek(buf, buffer_seek_start, 0);
addrpattern = buffer_read(buf, buffer_string);
if (string_length(addrpattern) < 1) {
    return false;
}

// Type tags
while (buffer_tell(buf) % 4 != 0) {
    buffer_seek(buf, buffer_seek_relative, 1);
}
typetagstr = buffer_read(buf, buffer_string);
if (string_char_at(typetagstr, 1) != ",") {
    return false;
}
i = 1;
while (true) {
    i++;
    var ch = string_char_at(typetagstr, i);
    if (ch == "") {
        break;
    }
    switch (ch) {
        case "i":
        case "f":
        case "s":
            typetags[i - 2] = ch;
            break;
        default:
            return false;
    }
}

// Arguments
while (buffer_tell(buf) % 4 != 0) {
    buffer_seek(buf, buffer_seek_relative, 1);
}
for (i = 0; i < array_length_1d(typetags); i++) {
    // Don't go past the end of the buffer
    var pos = buffer_tell(buf);
    if (pos >= size) {
        return false;
    }
    // Grab next argument
    tag = typetags;
    switch (tag) {
        case "s":
            arguments = buffer_read(buf, buffer_string);
            break;
        case "i":
        case "f":
            // We must swap to big-endian for OSC
            buffer_seek(buf_tmp, buffer_seek_start, 0);
            var k = 0;
            var j = 3;
            for (j = 3; j >= 0; j -= 1) {
                var byte = buffer_peek(buf, pos + j, buffer_u8);
                buffer_poke(buf_tmp, k, buffer_u8, byte);
                k++;
            }
            if (tag == "i") {
                arguments = buffer_read(buf_tmp, buffer_s32);
            }
            else if (tag == "f") {
                arguments = buffer_read(buf_tmp, buffer_f32);
            }
            buffer_seek(buf, buffer_seek_relative, 4);
            break;
        default:
            return false; // Unreachable
    }
    // Seek to end of 4-bytes padding
    while (buffer_tell(buf) % 4 != 0) {
        buffer_seek(buf, buffer_seek_relative, 1);
    }
}

// That's it
messages[0] = addrpattern;
for (i = 0; i < array_length_1d(arguments); i++) {
    messages[i + 1] = arguments;
}

return messages;
 
Last edited by a moderator:
Y

yoyowan

Guest
and osc_init :

Code:
/* osc.gml
 * by hryx 2015
 * Public Domain
 *
 * Initialize OSC settings.
 * This must be called before any other OSC functions.
 * Usage:
 *   osc_init("127.0.0.1", 8140, 8142);
 */

// Validate args
if (argument_count < 3) {
    show_error("osc_init(): Not enough arguments.", true);
}
osc_send_url = argument[0];
osc_send_port = argument[1];
osc_listen_port = argument[2];
if (!is_string(osc_send_url)) {
    show_error("osc_init(): Expected URL (string) at argument 0.", true);
}
else if (!is_real(osc_send_port)) {
    show_error("osc_init(): Expected send port (number) at argument 1.", true);
}
else if (!is_real(osc_listen_port)) {
    show_error("osc_init(): Expected listen port (number) at argument 2.", true);
}

// Create network objects
osc_send_buf = buffer_create(512, buffer_grow, 1);
osc_send_socket = network_create_socket(network_socket_udp);
osc_listen_socket = network_create_server_raw(network_socket_udp, osc_listen_port, 1);

// Set init flag
osc_is_setup = true;
 
Last edited by a moderator:
Top