KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > midi > xmidi > Utils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.midi.xmidi;
17
18 /**
19  * The MIDI file parsing parts of this class are based on code from the XMidi project, written
20  * by Peter Arthur Loeb (http://www.palserv.com/XMidi/) and used with permission.
21  * The warranty disclaimer of the MIT license (http://www.opensource.org/licenses/mit-license.html)
22  * applies to Peter Arthur Loeb's code.
23  *
24  * @author <a HREF="mailto:mark.leicester@energyintellect.com">Mark Leicester</a>
25  * @author <a HREF="mailto:peter@palserv.com">Peter Loeb</a>
26  */

27
28 import org.apache.cocoon.ProcessingException;
29
30 public final class Utils
31 {
32     public static final String JavaDoc VERSION = "1.2";
33
34   /**
35   convert hex string to byte array
36   */

37   static public byte[] hexToBa(String JavaDoc h, int len) throws ProcessingException
38   {
39     int l = h.length();
40     int cnt = 0;
41     int bs = 0;
42     int bc = 0;
43     byte[] ba = new byte[len];
44     for (int i = 0; i < l; i++)
45     {
46       String JavaDoc s = h.substring(i, i + 1);
47       int x = " \n\t\r".indexOf(s);
48       if (x != -1)
49       {
50         continue;
51       }
52       int tmp = "0123456789ABCDEF".indexOf(s.toUpperCase());
53       if (bc == 0)
54       {
55         bs = tmp;
56       }
57       else
58         if (bc == 1)
59         {
60           bs <<= 4;
61           bs |= tmp;
62         }
63         else
64         {
65           throw new ProcessingException("hexToBa: internal error");
66         }
67       bc++;
68       if (bc >= 2)
69       {
70         ba[cnt] = (byte) bs;
71         cnt++;
72         bc = 0;
73       }
74     }
75     if (bc != 0)
76     {
77       throw new ProcessingException("un-even number of hex digits");
78     }
79     if (cnt != len)
80     {
81       throw new ProcessingException(
82         "hexToBa: count (" + cnt + ") != length (" + len + ")");
83     }
84     return ba;
85   }
86
87   /**
88   convert byte array to string (not hex)
89   */

90   public static String JavaDoc baToString(byte[] b, int strt, int end)
91   {
92     int l = end - strt + 1;
93     char[] c = new char[l];
94     for (int j = 0; j < l; j++)
95     {
96       c[j] = (char) b[j];
97     }
98     return new String JavaDoc(c);
99   }
100
101   /**
102   convert byte array to hex string
103   */

104   public static String JavaDoc baToHex(byte[] b, int strt, int end)
105     throws ProcessingException
106   {
107     int l = end - strt + 1;
108     if (b.length < end)
109     {
110       throw new ProcessingException(
111         "baToHex: length error; b.length="
112           + b.length
113           + ", strt="
114           + strt
115           + ", end="
116           + end
117           + ", l="
118           + l);
119     }
120     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
121     for (int i = 0; i < l; i++)
122     {
123       int t = getUnsignedByte(b[strt + i]);
124       int a = t / 16;
125       int aa = t % 16;
126       sb.append("0123456789ABCDEF".substring(a, a + 1));
127       sb.append("0123456789ABCDEF".substring(aa, aa + 1));
128     }
129     return sb.toString();
130   }
131
132   /**
133   convert int to byte array of length c
134   */

135   static public byte[] intToBa(int n, int c) throws ProcessingException
136   {
137     byte[] b = new byte[c];
138     int t = n;
139     for (int i = 0; i < c; i++)
140     {
141       int j = c - i - 1;
142       int k = t % 256;
143       b[j] = (byte) k;
144       t /= 256;
145     }
146     if (t > 0)
147     {
148       throw new ProcessingException(
149         "intToBa: t is " + t + ", n = " + n + ", c = " + c);
150     }
151     return b;
152   }
153
154   /**
155   convert byte array to int
156   */

157   static public int baToInt(byte[] b, int strt, int end)
158     throws ProcessingException
159   {
160     if (end > b.length - 1)
161     {
162       throw new ProcessingException(
163         "baToInt: strt = "
164           + strt
165           + ", end = "
166           + end
167           + ", b.length = "
168           + b.length);
169     }
170     int l = end - strt + 1;
171     int i = 0;
172     for (int j = 0; j < l; j++)
173     {
174       int p = strt + l - j - 1;
175       // get int value of unsigned byte into k
176
if (p > b.length - 1)
177       {
178         throw new ProcessingException(
179           "baToInt: p = "
180             + p
181             + ", strt = "
182             + strt
183             + ", end = "
184             + end
185             + ", l = "
186             + l
187             + ", j = "
188             + j
189             + ", i = "
190             + i);
191       }
192       int k = getUnsignedByte(b[p]);
193       int n = pow(256, j);
194       i += n * k;
195     }
196     return i;
197   }
198
199   /**
200   convert byte (unsigned) to int
201   */

202   static public int getUnsignedByte(byte b)
203   {
204     int t = 0;
205     if ((b & 128) == 128)
206     {
207       t = 1;
208     }
209     b &= 127;
210     int k = b;
211     k += t * 128;
212     return k;
213   }
214
215   /**
216   convert delta time to byte array
217   a delta time is expressed as a
218   byte array, length unknown (4 or less)
219   */

220   static public ByteLen deltaToInt(byte[] b, int offset)
221     throws ProcessingException
222   {
223     /*
224     - capture up to four bytes including first with hi-ord
225         bit off
226     - turn off hi-ord bits
227     - accumulate 4 groups of 7 into 28 bit number with
228         hi-ord 4 bits zero.
229     */

230     int j = 0;
231     byte[] ba = new byte[4];
232     boolean jFlag = true;
233     while (jFlag)
234     {
235       if ((j + offset) > b.length)
236       {
237         throw new ProcessingException(
238           "deltaToInt: length error; j = "
239             + j
240             + ", offset = "
241             + offset
242             + ", b.length = "
243             + b.length);
244       }
245       ba[j] = b[j + offset];
246       if (ba[j] >= 0)
247       {
248         jFlag = false;
249       }
250       else
251       {
252         ba[j] &= 127;
253       }
254       /*this.getLogger().debug(
255         "deltaToInt: j = " + j + ", ba = " + baToInt(ba, 0, j));*/

256       j++;
257     }
258     int s = 0;
259     for (int i = 0; i < j; i++)
260     {
261       int k = j - i - 1;
262       int p = pow(128, i);
263       int m = ba[k];
264       s += m * p;
265       /*this.getLogger().debug(
266         "deltaToInt: in loop: s = "
267             + s
268             + ", i = "
269             + i
270             + ", j = "
271             + j
272             + ", k = "
273             + k
274             + ", p = "
275             + p
276             + ", m = "
277             + m);*/

278     }
279     /*this.getLogger().debug("deltaToInt: s = " + s);*/
280     ByteLen bl = new ByteLen(intToBa(s, 4), j);
281     return bl;
282   }
283
284   /**
285   compute b to the e power (b ** e)
286   */

287   static public int pow(int b, int e)
288   {
289     int a = 1;
290     for (int i = 0; i < e; i++)
291     {
292       a *= b;
293     }
294     return a;
295   }
296
297   public static int getPW(byte[] dta, int offset) throws ProcessingException
298   {
299     int hi = baToInt(dta, offset, offset);
300     int lo = baToInt(dta, offset + 1, offset + 1);
301     hi <<= 7;
302     lo |= hi;
303     return lo;
304   }
305
306   public static int getNextHiOrd(byte[] dta, int offset)
307     throws ProcessingException
308   {
309     int ol = 0;
310     boolean tflag = true;
311     for (int o = offset + 1; o < dta.length - 1; o++)
312     {
313       if (tflag)
314       {
315         int x = baToInt(dta, o, o);
316         if ((x & 128) == 128)
317         {
318           tflag = false;
319           ol = o;
320         }
321       }
322     }
323     if (tflag)
324     {
325       ol = offset + dta.length;
326     }
327     return ol;
328   }
329
330   /**
331   convert byte array to delta time
332   a delta time is expressed as a
333   byte array, length unknown (4 or less)
334   */

335   public static byte[] intToDelta(byte[] t) throws ProcessingException
336   {
337     /*
338     from fullword binary value to variable (midi) value:
339     - split number into 5 bit groups of 4 7 7 7 7
340     - just deal with the four groups of 7 (ignore the 4)
341     - put each group of 7 into a byte
342     - case 1: whole full word is zero.
343         return one byte of zero.
344     - case 2: some non-zero bit (after first four bits)
345         - work from left to right
346         - throw away bytes which are zero until non-zero byte
347             encountered
348         - turn on hi-ord bit on all but last byte
349     */

350     int i1 = baToInt(t, 0, t.length - 1);
351     //this.getLogger().debug("intToDelta: i1 = " + i1 + ", t = " + XMidiUtils.baToHex(t, 0, t.length - 1));
352
if (i1 == 0)
353     {
354       byte[] b = new byte[1];
355       b[0] = 0;
356       return b;
357     }
358     int i2 = i1;
359     byte[] b1 = new byte[4];
360     for (int i = 0; i < 4; i++)
361     {
362       int j = 4 - i - 1;
363       int k = i2 % 128;
364       i2 /= 128;
365       b1[j] = (byte) k;
366     }
367     int j = -1;
368     boolean bFlag = false;
369     for (int i = 0; i < 4; i++)
370     {
371       if (bFlag)
372       {
373         continue;
374       }
375       if (b1[i] != 0)
376       {
377         j = i;
378         bFlag = true;
379       }
380     }
381     int k = 4 - j;
382     byte[] b2 = new byte[k];
383     for (int i = 0; i < k; i++)
384     {
385       b2[i] = b1[i + j];
386       if (i != (k - 1))
387       {
388         b2[i] |= 128;
389       }
390     }
391     return b2;
392   }
393
394     public static int stringToInt(String JavaDoc s)
395   {
396     Integer JavaDoc i = new Integer JavaDoc(s);
397     int r = i.intValue();
398     return r;
399   }
400
401 }
402
Popular Tags