Example #1
0
  /**
   * Returns the index with which to start parsing the next part of the string, once this method is
   * done with its part
   */
  private int parseVelocity(String s, int index, NoteContext context) {
    // Don't compute note velocity for a rest
    if (context.isRest) {
      return index;
    }

    // Process velocity attributes, if they exist
    while (index < s.length()) {
      int startPoint = index + 1;
      int endPoint = startPoint;

      char velocityChar = s.charAt(index);
      int lengthOfByte = 0;
      if ((velocityChar == '+') || (velocityChar == '_') || (velocityChar == ' ')) break;
      logger.info("Identified Velocity character " + velocityChar);
      boolean byteDone = false;
      while (!byteDone && (index + lengthOfByte + 1 < s.length())) {
        char possibleByteChar = s.charAt(index + lengthOfByte + 1);
        if ((possibleByteChar >= '0') && (possibleByteChar <= '9')) {
          lengthOfByte++;
        } else {
          byteDone = true;
        }
      }
      endPoint = index + lengthOfByte + 1;

      if (startPoint == endPoint) {
        return endPoint;
      }

      byte velocityNumber = Byte.parseByte(s.substring(startPoint, endPoint));

      // Or maybe a bracketed string was passed in, instead of a byte
      String velocityString = null;
      if ((index + 1 < s.length()) && (s.charAt(index + 1) == '[')) {
        endPoint = s.indexOf(']', startPoint) + 1;
        velocityString = s.substring(startPoint, endPoint);
      }

      switch (velocityChar) {
        case 'A':
          if (velocityString == null) {
            context.noteOnVelocity = velocityNumber;
          } else {
            context.noteOnVelocityValueAsString = velocityString;
          }
          context.hasNoteOnVelocity = true;
          break;
        case 'D':
          if (velocityString == null) {
            context.noteOffVelocity = velocityNumber;
          } else {
            context.noteOffVelocityValueAsString = velocityString;
          }
          context.hasNoteOffVelocity = true;
          break;
        default:
          throw new ParserException(
              StaccatoMessages.VELOCITY_CHARACTER_NOT_RECOGNIZED,
              s.substring(startPoint, endPoint));
      }
      index = endPoint;
    }

    if (context.hasNoteOnVelocity) {
      logger.info("Attack velocity = " + context.noteOnVelocity);
    }
    if (context.hasNoteOffVelocity) {
      logger.info("Decay velocity = " + context.noteOffVelocity);
    }

    return index;
  }