Jump to content

OSC output from WatchOut (I solved it!)


autonic
 Share

Recommended Posts

If anyone is interested I found a fairly simple way to send OSC commands from WatchOut 6.6.

Use the attached google sheet to input the target OSC adress (and any neccessary arguments) and the script will format the string into UDP-messages that are recognized as valid OSC commands. Just create a String Output, set it to UDP network data and set the target IP and adress. Then use this form to generate valid OSC-commands and paste them in the string field.

In case the google sheet doesnt export/import the custom function I'll paste the javascript code for the function here:

function stringToHex(str){

  string = str.split(':')
  const typeTags = [',']
  var oscAddress = ''
  const arguments = []

  for (let i = 0; i <= string.length-1; i++) {

    // encode osc address
    if (i == 0) {

      oscAddress = addPadding(hexEncodeString(string[i]))

    // handle optional arguments
    } else if (i > 0 && !isEmpty(string[i])) {

      // determine argument type
      if (isNaN(string[i])) { 
        type = 's' 
        arguments.push(addPadding(hexEncodeString(string[i]) + '$00'))
      } else if (isInteger(parseInt(string[i]))) {
        type = 'i'
        arguments.push(addPadding(hexEncodeInt(parseInt(string[i]))))
      } else if (isFloat(parseFloat(string[i]))) {
        type = 'f'
        arguments.push(addPadding(hexEncodeFloat(string[i])))
      }

      // push osc type tag array
      typeTags.push(type)

    }

  }

  // return output
  if (string.length == 1) return oscAddress + addPadding(hexEncodeString(typeTags.join('')))
  if (string.length > 1) return oscAddress + addPadding(hexEncodeString(typeTags.join(''))) + arguments.join('')
  return 'incorrect string input format'

}

function hexEncodeString(string) {
  try{
    hex = unescape(encodeURIComponent(string))
    .split('').map(function(v){
      return v.charCodeAt(0).toString(16)
    }).join('$')
  }
  catch(e){
    hex = string[i]
    console.log('invalid text input: ' + string)
    return 'error'
  }
  return '$' + hex
}

function hexEncodeInt(s) {
  var a = s.toString(16);
  if ((a.length % 2) > 0) {
      a = "0" + a;
  }
  hexString = a.padStart(8, '0');
  return '$' + addItemEvery(hexString, '$', 2)
}

function hexEncodeFloat(number) {
  const getHex = i => ('00' + i.toString(16)).slice(-2);

  var view = new DataView(new ArrayBuffer(4)),
      result;

  view.setFloat32(0, number);

  result = Array
      .apply(null, { length: 4 })
      .map((_, i) => getHex(view.getUint8(i)))
      .join('');

  return '$' + addItemEvery(result, '$', 2)

}

function addItemEvery (str, item, every){
  for(let i = 0; i < str.length; i++){
    if(!(i % (every + 1))){
      str = str.substring(0, i) + item + str.substring(i);
    }
   }
  return str.substring(1);
}

function addPadding(byteString) {

    addressLength = byteString.split('$').length - 1

    // string is divisible by 4
    if(addressLength % 4 === 0) {
      byteString = byteString

    // string is not divisible by 4, needs padding
    } else {
      var padding = ''
      divisibleBy = '' + addressLength / 4
      decimals = divisibleBy.split('.')
      if (decimals[1] == '25') padding = '$00$00$00'
      if (decimals[1] == '5') padding = '$00$00'
      if (decimals[1] == '75') padding = '$00'
      byteString = byteString + padding
    }

  return byteString

}

function isFloat(n) {
    return n === +n && n !== (n|0);
}

function isInteger(n) {
    return n % 1 === 0;
}

function isEmpty(str) {
    return (!str || /^\s*$/.test(str));
}

 

OSC to UDP-string converter.xlsx

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...