|
1 /* |
|
2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Event processor class for tone sequence REPEAT events |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.microedition.media.tone; |
|
20 |
|
21 import javax.microedition.media.control.ToneControl; |
|
22 |
|
23 /** |
|
24 * Event processor class for tone sequence REPEAT events |
|
25 */ |
|
26 public class RepeatEvent extends Event |
|
27 { |
|
28 |
|
29 // CONSTANTS |
|
30 |
|
31 /* Maximum and minimum tone repeat multiplier values */ |
|
32 public static final int REPEAT_MIN_MULTIPLIER = 2; |
|
33 public static final int REPEAT_MAX_MULTIPLIER = 127; |
|
34 |
|
35 /** |
|
36 * RepeatEvent constructor |
|
37 * @param aSequence tone sequence byte array (input) |
|
38 * @param aMidiSequence midi sequence object where to output midi events. |
|
39 */ |
|
40 |
|
41 RepeatEvent(byte[] aSequence, MidiSequence aMidiSequence) |
|
42 { |
|
43 super(aSequence, aMidiSequence); |
|
44 } |
|
45 |
|
46 /** |
|
47 * Inherited from Event |
|
48 */ |
|
49 public int advance(int aPosition) |
|
50 throws MidiSequenceException |
|
51 { |
|
52 // it is already checked that there is at least two bytes left |
|
53 byte type = iSequence[ aPosition ]; |
|
54 byte data = iSequence[ aPosition + 1 ]; |
|
55 int retVal = doValidate(aPosition); |
|
56 if (retVal == 0) |
|
57 { |
|
58 return 0; |
|
59 } |
|
60 for (int i = 0; i < data; i++) |
|
61 { |
|
62 retVal = ToneEvent.staticAdvance(aPosition + EVENT_SIZE, |
|
63 iSequence, |
|
64 iMidiSequence); |
|
65 // This would go unnoticed if not checked here. |
|
66 if (retVal == 0) |
|
67 { |
|
68 throw new IllegalArgumentException( |
|
69 "Illegal sequence, processing events for playing failed"); |
|
70 } |
|
71 retVal = EVENT_SIZE + EVENT_SIZE; |
|
72 } |
|
73 return retVal; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Child class defined functionality for validate |
|
78 * @param aPosition position in tone sequence array where to validate |
|
79 * @return new position offset related to aPosition in tone sequence |
|
80 * array. Must be positive. |
|
81 */ |
|
82 protected int doValidate(int aPosition) |
|
83 throws IllegalArgumentException |
|
84 { |
|
85 byte type = iSequence[ aPosition ]; |
|
86 byte data = iSequence[ aPosition + 1 ]; |
|
87 int retVal = 0; |
|
88 if (type == ToneControl.REPEAT) |
|
89 { |
|
90 if (data < REPEAT_MIN_MULTIPLIER || data > REPEAT_MAX_MULTIPLIER) |
|
91 { |
|
92 throw new IllegalArgumentException( |
|
93 "Repeat multiplier out of range, valid range is 2 <= multiplier <= 127"); |
|
94 } |
|
95 |
|
96 // Check that there is two more bytes available |
|
97 if (iSequence.length - (aPosition + EVENT_SIZE) < EVENT_SIZE) |
|
98 { |
|
99 throw new IllegalArgumentException( |
|
100 "Illegal ToneControl.REPEAT event, " + |
|
101 "there should be two more bytes available after REPEAT event"); |
|
102 } |
|
103 retVal = EVENT_SIZE + EVENT_SIZE; |
|
104 } |
|
105 return retVal; |
|
106 } |
|
107 |
|
108 /** |
|
109 * Child class defined functionality for checkEventAtNextPosition |
|
110 * @param aPosition position in tone sequence array where to check |
|
111 */ |
|
112 protected void checkEventAtNextPosition(int aPosition) |
|
113 throws IllegalArgumentException |
|
114 { |
|
115 // After this event there can be: |
|
116 // Tone, BlockEnd, PlayBlock, Volume, Repeat or |
|
117 // end of sequence |
|
118 |
|
119 int type = 0; |
|
120 try |
|
121 { |
|
122 type = iSequence[ aPosition ]; |
|
123 } |
|
124 catch (ArrayIndexOutOfBoundsException aioobe) |
|
125 { |
|
126 return; // end of sequence is ok for this event |
|
127 } |
|
128 |
|
129 if (type >= ToneControl.SILENCE || |
|
130 type == ToneControl.BLOCK_END || |
|
131 type == ToneControl.PLAY_BLOCK || |
|
132 type == ToneControl.SET_VOLUME || |
|
133 type == ToneControl.REPEAT) |
|
134 { |
|
135 return; |
|
136 } |
|
137 |
|
138 throw new IllegalArgumentException( |
|
139 "Illegal event found; sequence is corrupted"); |
|
140 } |
|
141 |
|
142 } // end of class |
|
143 |