~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-tef/src/org/herac/tuxguitar/io/tef/TEInputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.io.tef;
 
2
 
 
3
import java.io.IOException;
 
4
import java.io.InputStream;
 
5
 
 
6
import org.herac.tuxguitar.io.tef.base.TEChord;
 
7
import org.herac.tuxguitar.io.tef.base.TEComponentChord;
 
8
import org.herac.tuxguitar.io.tef.base.TEComponentNote;
 
9
import org.herac.tuxguitar.io.tef.base.TEInfo;
 
10
import org.herac.tuxguitar.io.tef.base.TEPercussion;
 
11
import org.herac.tuxguitar.io.tef.base.TERepeat;
 
12
import org.herac.tuxguitar.io.tef.base.TERhythm;
 
13
import org.herac.tuxguitar.io.tef.base.TESong;
 
14
import org.herac.tuxguitar.io.tef.base.TETempo;
 
15
import org.herac.tuxguitar.io.tef.base.TEText;
 
16
import org.herac.tuxguitar.io.tef.base.TETimeSignature;
 
17
import org.herac.tuxguitar.io.tef.base.TETimeSignatureChange;
 
18
import org.herac.tuxguitar.io.tef.base.TETrack;
 
19
 
 
20
public class TEInputStream {
 
21
        
 
22
        private TESong song;
 
23
        private InputStream stream;
 
24
        
 
25
        public TEInputStream(InputStream stream){
 
26
                this.stream = stream;
 
27
        }
 
28
        
 
29
        public TESong readSong(){
 
30
                this.song = new TESong();
 
31
                
 
32
                this.readInfo();
 
33
                
 
34
                this.song.setMeasures((this.readByte() & 0xff));
 
35
                
 
36
                this.skip(1);
 
37
                this.readTimeSignature();
 
38
                
 
39
                this.skip(15);
 
40
                this.readTempo();
 
41
                
 
42
                this.song.setRepeats( (this.readByte() & 0xff) );
 
43
                
 
44
                this.skip(5);
 
45
                this.song.setTexts((this.readByte() & 0xff));
 
46
                
 
47
                this.skip(5);
 
48
                this.song.setPercussions((this.readByte() & 0xff));
 
49
                this.song.setRhythms((this.readByte() & 0xff));
 
50
                
 
51
                this.song.setChords((this.readByte() & 0xff));
 
52
                
 
53
                this.skip(1);
 
54
                boolean notes = ((this.readByte() & 0xff) > 0);
 
55
                
 
56
                this.skip(1);
 
57
                this.song.setStrings((this.readByte() & 0xff));
 
58
                
 
59
                this.song.setTracks((this.readByte() & 0xff) + 1);
 
60
                
 
61
                this.skip(14);
 
62
                this.readComponents();
 
63
                this.readRepeats();
 
64
                this.readTexts();
 
65
                
 
66
                this.readPercussions();
 
67
                
 
68
                this.readChords();
 
69
                
 
70
                
 
71
                this.readRhythms();
 
72
                this.readNotes(notes);
 
73
                this.readTracks();
 
74
                
 
75
                this.close();
 
76
                
 
77
                return this.song;
 
78
        }
 
79
        
 
80
        private void readInfo(){
 
81
                byte[] info = this.readBytes(200);
 
82
                int offset = 0;
 
83
                String strings[] = new String[3];
 
84
                for(int i = 0; i < strings.length; i ++){
 
85
                        int length = 0;
 
86
                        while( (offset + length) < info.length && info[ (offset + length)  ] != 0 ){
 
87
                                length ++;
 
88
                        }
 
89
                        strings[i] = new String(info,offset,length);
 
90
                        offset += length + 1;
 
91
                }
 
92
                this.song.setInfo(new TEInfo(strings[0],strings[1],strings[2]));
 
93
        }
 
94
        
 
95
        private void readNotes(boolean notes){
 
96
                if(notes){
 
97
                        int length = this.readShort();
 
98
                        this.song.getInfo().setNotes( new String(this.readBytes(length),1,(length -1)) );
 
99
                }
 
100
        }
 
101
        
 
102
        private void readTempo(){
 
103
                int value = this.readShort();
 
104
                this.song.setTempo(new TETempo(value));
 
105
        }
 
106
        
 
107
        private void readTimeSignature(){
 
108
                int numerator = this.readByte();
 
109
                this.skip(1);
 
110
                int denominator = this.readByte();
 
111
                this.song.setTimeSignature(new TETimeSignature(numerator,denominator));
 
112
        }
 
113
        
 
114
        private void readRepeats(){
 
115
                for(int i = 0; i < this.song.getRepeats().length; i ++){
 
116
                        int data1 = this.readByte();
 
117
                        int data2 = this.readByte();
 
118
                        this.song.setRepeat(i,new TERepeat(data1,data2));
 
119
                }
 
120
        }
 
121
        
 
122
        private void readTexts(){
 
123
                for(int i = 0; i < this.song.getTexts().length; i ++){
 
124
                        int length = this.readByte();
 
125
                        byte[] text = this.readBytes(length);
 
126
                        this.song.setText(i,new TEText(new String(text,1,(length -1))));
 
127
                        this.skip(1);
 
128
                }
 
129
        }
 
130
        
 
131
        private void readChords(){
 
132
                for(int i = 0; i < this.song.getChords().length; i ++){
 
133
                        byte[] strings = this.readBytes(14);
 
134
                        byte[] name = this.readBytes(16);
 
135
                        
 
136
                        this.song.setChord(i,new TEChord(strings,new String(name)));
 
137
                        this.skip(2);
 
138
                }
 
139
        }
 
140
        
 
141
        private void readPercussions(){
 
142
                if(this.song.getPercussions().length > 0){
 
143
                        for(int i = 0; i < this.song.getPercussions().length; i ++){
 
144
                                this.skip(96);
 
145
                                String name = new String(this.readBytes(8) );
 
146
                                this.skip(1);
 
147
                                int volume = (this.readByte() & 0xff);
 
148
                                this.song.setPercussion(i,new TEPercussion(name,volume));
 
149
                                this.skip(2);
 
150
                        }
 
151
                        this.skip(this.song.getMeasures());
 
152
                }
 
153
        }
 
154
        
 
155
        private void readRhythms(){
 
156
                if(this.song.getRhythms().length > 0){
 
157
                        for(int i = 0; i < this.song.getRhythms().length; i ++){
 
158
                                this.skip(96);
 
159
                                String name = new String(this.readBytes(8) );
 
160
                                this.skip(1);
 
161
                                int volume = (this.readByte() & 0xff);
 
162
                                int instrument = (this.readByte() & 0xff);
 
163
                                this.song.setRhythm(i,new TERhythm(name,volume,instrument));
 
164
                                this.skip(1);
 
165
                        }
 
166
                        this.skip(this.song.getMeasures());
 
167
                }
 
168
        }
 
169
        
 
170
        private void readTracks(){
 
171
                for(int i = 0; i < this.song.getTracks().length; i ++){
 
172
                        int[] strings = new int[this.readByte()];
 
173
                        
 
174
                        this.skip(5);
 
175
                        int type = this.readByte();
 
176
                        
 
177
                        this.skip(1);
 
178
                        int instrument = this.readByte();
 
179
                        
 
180
                        this.skip(3);
 
181
                        int capo = this.readByte();
 
182
                        
 
183
                        this.skip(1);
 
184
                        
 
185
                        int clefType = this.readByte();
 
186
                        int clefNumber = this.readByte();
 
187
                        
 
188
                        this.skip(1);
 
189
                        
 
190
                        int pan = this.readByte();
 
191
                        int volume = this.readByte();
 
192
                        int flags = this.readByte();
 
193
                        
 
194
                        for(int string = 0; string < strings.length; string ++){
 
195
                                strings[string] = (this.readByte() & 0xff);
 
196
                        }
 
197
                        this.skip(12 - strings.length);
 
198
                        
 
199
                        String name = new String(this.readBytes(16));
 
200
                        
 
201
                        this.song.setTrack(i,new TETrack( (type == 98),instrument,capo, clefType, clefNumber, pan, volume, flags, strings, name));
 
202
                        this.skip(2);
 
203
                }
 
204
        }
 
205
        
 
206
        private void readComponents(){
 
207
                int tsSize = ( (256 * this.song.getTimeSignature().getNumerator()) / this.song.getTimeSignature().getDenominator() );
 
208
                int tsMove = 0;
 
209
                int mIndex = 0;
 
210
                int mData = 0;
 
211
                int count = this.readShort();
 
212
                for(int i = 0; i < count; i ++){
 
213
                        byte[] data = this.readBytes(6);
 
214
                        
 
215
                        int location = ( (data[0] & 0xff) + (256 *  (mData + (data[1] & 0xff))));
 
216
                        if( ( location / ( tsSize * this.song.getStrings() ) ) < mIndex ){
 
217
                                mData += 256;
 
218
                                location = ((data[0] & 0xff) + (256 *  (mData + (data[1] & 0xff))));
 
219
                        }
 
220
                        int position = (location  %  tsSize);
 
221
                        int string = ( (location / tsSize)  % this.song.getStrings()) ;
 
222
                        int measure = ( location / ( tsSize * this.song.getStrings() ) );
 
223
                        
 
224
                        tsMove = (mIndex == measure)?tsMove:0;
 
225
                        position -= tsMove;
 
226
                        
 
227
                        if( ((data[2] & 0xff) & 0x1f) > 0  && ((data[2] & 0xff) & 0x1f) <= 25 ){
 
228
                                int duration = (data[3] & 0xf);
 
229
                                int dynamic =  (data[3] >> 4);
 
230
                                int effect = data[4];
 
231
                                int fret = (((data[2] & 0xff) & 0x1f) - 1);
 
232
                                if((((data[2] & 0xff) >> 5) & 0x01) != 0 ) {
 
233
                                        fret += (data[5] & 0xff);
 
234
                                }
 
235
                                this.song.getComponents().add( new TEComponentNote(position, measure, string ,fret,duration,dynamic,effect ) );
 
236
                        }
 
237
                        else if( ((data[2] & 0xff) & 0x1f) == 27 ){
 
238
                                //TIME SIGNATURE CHANGE
 
239
                                tsMove = (4 * (data[3] & 0xff ) );
 
240
                                int denominator = (int)( ( Math.pow( 2  , (  ( data[2] & 0xff ) >> 5 ) ) / 2 ) );
 
241
                                int numerator = ((( (tsSize / 4) - (data[3] & 0xff) ) * denominator) / 64);
 
242
                                this.song.addTimeSignatureChange(new TETimeSignatureChange(measure, new TETimeSignature(numerator,denominator)));
 
243
                        }
 
244
                        else if( ((data[2] & 0xff) & 0x1f) == 28 && (data[2] & 0x20) != 0){
 
245
                                //SCALE DIAGRAM
 
246
                        }
 
247
                        else if( ((data[2] & 0xff) & 0x1f) == 28){
 
248
                                //CHORD
 
249
                                this.song.getComponents().add( new TEComponentChord(position, measure, string , data[3]) );
 
250
                        }
 
251
                        else if( ((data[2] & 0xff) & 0x1f) == 29 ){
 
252
                                //SCALE DIAGRAM | SPECIAL CHAR | SYNCOPATION CHANGE
 
253
                        }
 
254
                        else if( ((data[2] & 0xff) & 0x1f) == 30 ){
 
255
                                //TEMPO CHANGE | VOICE CHANGE | DRUM EVENT | CRESCENDO ACCENT | REPEAT/ENDING
 
256
                        }
 
257
                        mIndex = measure;
 
258
                }
 
259
        }
 
260
        
 
261
        //-----------------------------------------------------------------------------//
 
262
        //-----------------------------------------------------------------------------//
 
263
        //-----------------------------------------------------------------------------//       
 
264
        protected byte[] readBytes(int length){
 
265
                byte[] bytes = new byte[length];
 
266
                try {
 
267
                        this.stream.read(bytes);
 
268
                } catch (IOException e) {
 
269
                        e.printStackTrace();
 
270
                }
 
271
                return bytes;
 
272
        }
 
273
        
 
274
        protected int readByte(){
 
275
                try {
 
276
                        return this.stream.read();
 
277
                } catch (IOException e) {
 
278
                        e.printStackTrace();
 
279
                }
 
280
                return 0;
 
281
        }
 
282
        
 
283
        protected int readShort(){
 
284
                try {
 
285
                        byte[] b = new byte[2];
 
286
                        this.stream.read(b);
 
287
                        return ((b[1] & 0xff) << 8) | (b[0] & 0xff);
 
288
                } catch (IOException e) {
 
289
                        e.printStackTrace();
 
290
                }
 
291
                return 0;
 
292
        }
 
293
        
 
294
        protected void skip(int count){
 
295
                for(int i = 0; i < count; i++){
 
296
                        readByte();
 
297
                }
 
298
        }
 
299
        
 
300
        protected void close(){
 
301
                try {
 
302
                        this.stream.close();
 
303
                } catch (IOException e) {
 
304
                        e.printStackTrace();
 
305
                }
 
306
        }
 
307
}