From 5a764293fc3738fd7b60dc981c97986f8bbd37fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurij=20Podgor=C5=A1ek?= Date: Tue, 3 Sep 2024 15:03:08 +0200 Subject: [PATCH] Some fixes, formatting --- SLIPDecoder.sc | 265 ++++++++++++++++++++++--------------------------- 1 file changed, 118 insertions(+), 147 deletions(-) diff --git a/SLIPDecoder.sc b/SLIPDecoder.sc index be237c7..7fc5724 100644 --- a/SLIPDecoder.sc +++ b/SLIPDecoder.sc @@ -23,12 +23,10 @@ SLIPDecoder { // Buffer for OSC bundles/messages const bufferSize = 4096; - var <>deviceName, <>rate, <>prepend, deviceName, <>rate, <>prepend, trace = false, = 0x20) && (x <= 0x7E), { ^x.asAscii }, - { ^x }) + { ^x } + ); } init { - "init".postln; - dump(this); - deviceName.postln; - rate.postln; port = SerialPort(deviceName, rate); - trace = false; - stop = false; - "init done".postln; - } - - trace { |val| - trace = val; - val; } traceMsg { |...msg| - if (trace, { "> ".post; msg.join(' ').postln; }); + if ((trace), { "> ".post; msg.join(' ').postln; }); } // Function for decoding the properly-SLIP-decoded message. decode { |data| - // Is it a bundle? (read header from first 8 bytes) var header = data.at((0..7)); - if (header == oscBundleHeader, + // Is it a bundle? (read header from first 8 bytes) + if ((header == oscBundleHeader), { // OSC bundle - { - var timetag, nextMsgLen, nextMsgStart, nextMsgEnd, bundlePart; + var nextMsgLen, nextMsgStart, nextMsgEnd, bundlePart; + this.traceMsg("BUNDLE"); - this.traceMsg("BUNDLE"); + // Next 8 bytes are a time tag (or null) + /* @TODO handle timetags! + var timetag = data.at((8..15)); + */ - // Next 8 bytes are a time tag (or null) - /* @TODO handle timetags! - timetag = data.at((8..15)); - */ + // First message starts after the time tag + nextMsgStart = 16; - // First message starts after the time tag - nextMsgStart = 16; + // Loop for each message + while({ nextMsgStart < data.size }, { + // Further 4 bytes hold the next message length + nextMsgLen = this.readInt32(data.at((nextMsgStart..(nextMsgStart + 3)))); + nextMsgStart = nextMsgStart + 4; + nextMsgEnd = min(nextMsgStart + nextMsgLen - 1, data.size - 1); + bundlePart = data.at((nextMsgStart..nextMsgEnd)); - // Loop for each message - while({ nextMsgStart < data.size }, - { - // Further 4 bytes hold the next message length - nextMsgLen = this.readInt32(data.at((nextMsgStart..(nextMsgStart + 3)))); - nextMsgStart = nextMsgStart + 4; - nextMsgEnd = nextMsgStart + nextMsgLen - 1; - bundlePart = data.at((nextMsgStart..nextMsgEnd)); + // Recursively decode; each bundle part can be another bundle + this.decode(bundlePart); - // Recursively decode; each bundle part can be another bundle - this.decode(bundlePart); - - nextMsgStart = nextMsgEnd + 1; - } - ); - }, + nextMsgStart = nextMsgEnd + 1; + }); + }, { // OSC message - { - var nextString, index, address, type, args = []; + var nextString, index, address, type, args = []; - // Message Address - nextString = this.readNextString(data); - address = nextString[0]; - data = data[(nextString[1]..(data.size - 1))]; + // Message Address + nextString = this.readNextString(data); + address = nextString[0]; + data = data[(nextString[1]..(data.size - 1))]; - nextString = this.readNextString(data); - type = nextString[0]; - data = data[(nextString[1]..(data.size - 1))]; + nextString = this.readNextString(data); + type = nextString[0]; + data = data[(nextString[1]..(data.size - 1))]; - type.do({ |t| - t.switch ( - $i, { - args = args.add(this.readInt32(data)); - data = data[(4..(data.size - 1))]; - }, - $f, { - args = args.add(this.readFloat32(data)); - data = data[(4..(data.size - 1))]; - }, - $s, { - nextString = this.readNextString(data); - args = args.add(nextString[0]); - data = data[(nextString[1]..(data.size - 1))]; - } - /* @TODO implement strings, bytearrays */ - ); - }); - this.traceMsg("OSC", prepend ++ address, args); + type.do({ |t| + t.switch ( + $i, { + args = args.add(this.readInt32(data)); + data = data[(4..(data.size - 1))]; + }, + $f, { + args = args.add(this.readFloat32(data)); + data = data[(4..(data.size - 1))]; + }, + $s, { + nextString = this.readNextString(data); + args = args.add(nextString[0]); + data = data[(nextString[1]..(data.size - 1))]; + } + /* @TODO implement strings, bytearrays */ + ); + }); + this.traceMsg("OSC", prepend ++ address, args); - // Send OSC message to the engine - NetAddr.localAddr.sendMsg(prepend ++ address, *args); - } - ); + // Send OSC message to the engine + NetAddr.localAddr.sendMsg(prepend ++ address, *args); + }); } start { - this.traceMsg("Starting..."); - dump(this); - // TODO fix restart - if ((port == nil), {this.init;}); - if (port.isOpen.not, {this.init;}); + this.traceMsg("Starting..."); + if ((port == nil), {this.init;}); + if (port.isOpen.not, {this.init;}); - this.traceMsg("opened"); + this.traceMsg("opened"); + running = true; reader = fork { var serialByte, buffer, firstRead; - firstRead = true; + firstRead = true; - // Skip data before the first END character - while({stop.not && firstRead}, { + // Skip data before the first END character + while({running && firstRead}, { serialByte = port.read; - //this.traceMsg("Read byte"); - //this.traceMsg(serialByte.asAscii); + //this.traceMsg("Read byte"); + //this.traceMsg(serialByte.asAscii); - if (serialByte == slipEND, { - buffer = Int8Array(maxSize: bufferSize); + if (serialByte == slipEND, { + buffer = Int8Array(maxSize: bufferSize); firstRead = false; }, { - //this.traceMsg("Skip...") - }); - }); + this.traceMsg("Skip...") + }); + }); - // Start reading data - this.traceMsg("First read!"); - while({stop.not}, { + // Start reading data + this.traceMsg("First read!"); + while({running}, { serialByte = port.read; - //this.traceMsg("Checking", serialByte.asInteger, serialByte.asAscii); - serialByte.switch( - // on END, decode buffer - slipEND, { - //this.traceMsg("SLIP END, decoding "); - //this.traceMsg(buffer); - //3.wait; - if (buffer.isEmpty.not, { - this.traceMsg("decode!", buffer); - this.decode(buffer); - buffer = Int8Array(maxSize: bufferSize); - }); - }, - slipESC, { - serialByte = port.read; - serialByte.switch( - slipESC_END, { - //this.traceMsg("SLIP ESC and ESC_END"); - buffer.add(slipEND) - }, - slipESC_ESC, { - //this.traceMsg("SLIP ESC and ESC_ESC"); - buffer.add(slipESC) - }, - {"SLIP encoding error.".error } - ); - }, - { - // Otherwise just add the byte - //this.traceMsg(buffer); - buffer.add(serialByte); - }); - }); - }; + //this.traceMsg("Checking", serialByte.asInteger, serialByte.asAscii); + serialByte.switch( + // on END, decode buffer + slipEND, { + //this.traceMsg("SLIP END, decoding "); + //this.traceMsg(buffer); + //3.wait; + if (buffer.isEmpty.not, { + this.traceMsg("decode!", buffer); + this.decode(buffer); + buffer = Int8Array(maxSize: bufferSize); + }); + }, + slipESC, { + serialByte = port.read; + serialByte.switch( + slipESC_END, { buffer.add(slipEND) }, + slipESC_ESC, { buffer.add(slipESC) }, + {"SLIP encoding error.".error } + ); + }, { + // Otherwise just add the byte + //this.traceMsg(buffer); + buffer.add(serialByte); + }); + }); + }; } stop { - this.traceMsg("Stopping..."); - stop = true; - port.close; + this.traceMsg("Stopping..."); + running = false; + port.close; + this.reader.free; } }