Android Open Source - MachinaMidiJacketSDK O S C Parser






From Project

Back to project page MachinaMidiJacketSDK.

License

The source code is released under:

GNU General Public License

If you think the Android project MachinaMidiJacketSDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//
//  OSCParser.java
//  JacketSDK//w w  w.j  a v a 2s  .com
//
//  Created by Henry Serrano on 6/20/14.
//  Copyright (c) 2014 Machina Wearable Technology SAPI de CV.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
package com.machina.jacketsdk;

import java.util.ArrayList;

import android.util.Log;

public class OSCParser {
  public static OSCMessage parseOSCMessage(byte[] buffer){
    OSCMessage message = new OSCMessage();
    message.messageExists = false;
    
    if((buffer.length % 4) == 0){
      Integer comaIndex = findComaIndex(buffer);
      
      if(comaIndex % 4 == 0){
        message.address = parseAddress(buffer, comaIndex);
        
        if(message.address != null){
          //Log.d("Parsed Address", message.address);
          
          message.arguments = parseArguments(buffer, comaIndex);
          
          if(message.arguments.size() > 0){
            //Log.d("Message Arguments", message.arguments.toString());
            
            message.messageExists = true;
          }
        }
      }
    }
    
    return message;
  }
  
  private static Integer findComaIndex(byte[] buffer){
    Integer comaIndex = 0;
    for(int i = 0; i < buffer.length; i++){
      if(buffer[i] == ','){
        comaIndex = i;
        break;
      }
    }
    return comaIndex;
  }
  
  private static Integer findZeroAfterIndex(byte[] buffer, Integer startIndex){
    Integer zeroIndex = 0;
    //look for zeros
    for(int i = startIndex; i < buffer.length; i++){
      if(buffer[i] == 0){
        zeroIndex = i;
        break;
      }
    }
    return zeroIndex;
  }
  
  private static String parseAddress(byte[] buffer, Integer comaIndex){
    Integer zeroIndex = findZeroAfterIndex(buffer, 0);
    if(zeroIndex >= comaIndex){
      return null;
    }else{
      byte[] addressBuffer = new byte[zeroIndex];
      copyBuffer(buffer, addressBuffer, 0, zeroIndex);
    
      return new String(addressBuffer);
    }
  }
  
  private static ArrayList<Object> parseArguments(byte[] buffer, Integer comaIndex){
    Integer ArgumentsTypesIndex = findZeroAfterIndex(buffer, comaIndex);
    
    char[] argumentsTypes = new char[ArgumentsTypesIndex - comaIndex];
    
    for(int i = comaIndex; i < ArgumentsTypesIndex; i++){
      argumentsTypes[i - comaIndex] = (char)buffer[i];
    }
    
    Integer EndOfArgumentsTypesIndex = ArgumentsTypesIndex;
    if(EndOfArgumentsTypesIndex % 4 == 0){
      EndOfArgumentsTypesIndex += 4;
    }else{
      while(EndOfArgumentsTypesIndex % 4 != 0){
        EndOfArgumentsTypesIndex++;
      }
    }
    
    ArrayList<Object> arguments = new ArrayList<Object>();
    
    for(int i = 0; i < argumentsTypes.length; i++){
      switch(argumentsTypes[i]){
        case 'i':
          int number = 0;
          
          number = (buffer[EndOfArgumentsTypesIndex++] << 24);
          number |= (buffer[EndOfArgumentsTypesIndex++] << 16);
          number |= (buffer[EndOfArgumentsTypesIndex++] << 8);
          number |= buffer[EndOfArgumentsTypesIndex++];
          
          Number auxNumber = number;
          arguments.add(auxNumber);
          break;
        case 'f':
          break;
        case 's':
          break;
        case 'b':
          break;
      }
    }
    
    return arguments;
  }
  
  private static void copyBuffer(byte[] mainBuffer, byte[] toCopyBuffer, Integer start, Integer length){
    for(int i = start; i < length; i++){
      toCopyBuffer[i] = mainBuffer[i];
    }
  }
}




Java Source Code List

com.machina.jacketsdk.JacketData.java
com.machina.jacketsdk.JacketOSCServerListener.java
com.machina.jacketsdk.JacketOSCServer.java
com.machina.jacketsdk.OSCMessage.java
com.machina.jacketsdk.OSCParser.java