001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sound.midi;
019:
020: import org.apache.harmony.sound.internal.nls.Messages;
021:
022: public class MetaMessage extends MidiMessage {
023: public static final int META = 255;
024:
025: private int dsp; //displacement from begin of array that
026:
027: //return by method getData() from begin
028: //of array that contain data
029:
030: public MetaMessage() {
031: super (new byte[] { -1, 0 });
032: }
033:
034: protected MetaMessage(byte[] data) {
035: super (data);
036: if (data == null) {
037: throw new NullPointerException();
038: }
039: if (super .length > 3) {
040: int n = 3;
041: while ((n <= super .length) && (super .data[n - 1] < 0)) {
042: n++;
043: }
044: dsp = n;
045: }
046: }
047:
048: @Override
049: public Object clone() {
050: return new MetaMessage(this .getMessage());
051: }
052:
053: public byte[] getData() {
054: if ((super .data != null) && (super .length > 3)) {
055: byte[] bt = new byte[super .length - dsp];
056: for (int i = dsp; i < super .length; i++) {
057: bt[i - dsp] = super .data[i];
058: }
059: return bt;
060: }
061: return new byte[0];
062: }
063:
064: public int getType() {
065: if ((super .data != null) && (super .length >= 2)) {
066: return super .data[1] & 0xFF;
067: }
068: return 0;
069: }
070:
071: public void setMessage(int type, byte[] data, int length)
072: throws InvalidMidiDataException {
073: if (type < 0 || type >= 128) {
074: // sound.0A=Invalid meta event with type {0}
075: throw new InvalidMidiDataException(Messages.getString(
076: "sound.0A", type)); //$NON-NLS-1$
077: }
078: if (length < 0 || (data != null && length > data.length)) {
079: // sound.03=length out of bounds: {0}
080: throw new InvalidMidiDataException(Messages.getString(
081: "sound.03", length)); //$NON-NLS-1$
082: }
083: try {
084: if (data == null) {
085: if (length != 0) {
086: throw new NullPointerException();
087: }
088: super .setMessage(new byte[] { -1, (byte) type, 0 }, 3);
089: } else {
090: int div = 128;
091: int n = 1;
092: int ost;
093: int sm = 0;
094: while (length / div != 0) {
095: n++;
096: div *= 128;
097: }
098: int ln = n;
099: byte[] tdata = new byte[length + ln + 2];
100: div = 1;
101: ost = (length / div) % 128;
102: while (n != 0) {
103: tdata[n - 1 + 2] = (byte) (ost + sm);
104: n--;
105: div *= 128;
106: ost = (length / div) % 128;
107: sm = 128;
108: }
109: tdata[0] = -1;
110: tdata[1] = (byte) type;
111: if (length > 0) {
112: for (int i = 0; i < length; i++) {
113: tdata[2 + ln + i] = data[i];
114: }
115: }
116: super .setMessage(tdata, length + 2 + ln);
117: dsp = ln + 2;
118: }
119: } catch (InvalidMidiDataException e) {
120: throw e;
121: }
122: }
123: }
|