~ubuntu-branches/ubuntu/lucid/tuxguitar/lucid-security

« back to all changes in this revision

Viewing changes to TuxGuitar-compat/src/org/herac/tuxguitar/io/tg/v07/TGInputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080619003030-agens2gvd5m4dacu
New upstream release (Closes: #481728) also (LP: #176979, #212207)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 16-dic-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.io.tg.v07;
 
8
 
 
9
import java.io.DataInputStream;
 
10
import java.io.IOException;
 
11
import java.io.InputStream;
 
12
 
 
13
import org.herac.tuxguitar.io.base.TGFileFormat;
 
14
import org.herac.tuxguitar.io.base.TGFileFormatException;
 
15
import org.herac.tuxguitar.io.base.TGInputStreamBase;
 
16
import org.herac.tuxguitar.song.factory.TGFactory;
 
17
import org.herac.tuxguitar.song.models.TGBeat;
 
18
import org.herac.tuxguitar.song.models.TGChannel;
 
19
import org.herac.tuxguitar.song.models.TGColor;
 
20
import org.herac.tuxguitar.song.models.TGDuration;
 
21
import org.herac.tuxguitar.song.models.TGMeasure;
 
22
import org.herac.tuxguitar.song.models.TGMeasureHeader;
 
23
import org.herac.tuxguitar.song.models.TGNote;
 
24
import org.herac.tuxguitar.song.models.TGNoteEffect;
 
25
import org.herac.tuxguitar.song.models.TGSong;
 
26
import org.herac.tuxguitar.song.models.TGString;
 
27
import org.herac.tuxguitar.song.models.TGTempo;
 
28
import org.herac.tuxguitar.song.models.TGTimeSignature;
 
29
import org.herac.tuxguitar.song.models.TGTrack;
 
30
import org.herac.tuxguitar.song.models.TGTupleto;
 
31
import org.herac.tuxguitar.song.models.effects.TGEffectBend;
 
32
 
 
33
/**
 
34
 * @author julian
 
35
 *
 
36
 * TODO To change the template for this generated type comment go to
 
37
 * Window - Preferences - Java - Code Style - Code Templates
 
38
 */
 
39
public class TGInputStream implements TGInputStreamBase{
 
40
        private static final String TG_VERSION = "TG_DEVEL-0.01";
 
41
        
 
42
        private DataInputStream dataInputStream;
 
43
        private TGFactory factory;
 
44
        private String version;
 
45
        
 
46
        public TGInputStream(){
 
47
                super();
 
48
        }
 
49
        
 
50
        public void init(TGFactory factory,InputStream stream) {
 
51
                this.factory = factory;
 
52
                this.dataInputStream = new DataInputStream(stream);
 
53
                this.version = null;
 
54
        }
 
55
        
 
56
        public TGFileFormat getFileFormat(){
 
57
                return new TGFileFormat("TuxGuitar","*.tg");
 
58
        }
 
59
        
 
60
        public boolean isSupportedVersion(String version){
 
61
                return (version.equals(TG_VERSION));
 
62
        }
 
63
        
 
64
        public boolean isSupportedVersion(){
 
65
                try{
 
66
                        readVersion();
 
67
                        return isSupportedVersion(this.version);
 
68
                }catch(Exception e){
 
69
                        return false;
 
70
                }catch(Error e){
 
71
                        return false;
 
72
                }
 
73
        }
 
74
        
 
75
        private void readVersion(){
 
76
                if(this.version == null){
 
77
                        this.version = readString();
 
78
                }
 
79
        }
 
80
        
 
81
        public TGSong readSong()throws TGFileFormatException{
 
82
                try {
 
83
                        if(this.isSupportedVersion()){
 
84
                                TGSong song = this.read();
 
85
                                this.dataInputStream.close();
 
86
                                return song;
 
87
                        }
 
88
                        throw new TGFileFormatException("Unsopported Version");
 
89
                }catch (Throwable throwable) {
 
90
                        throw new TGFileFormatException(throwable);
 
91
                }
 
92
        }
 
93
        
 
94
        private TGSong read(){
 
95
                TGSong song = this.factory.newSong();
 
96
                
 
97
                //leo el nombre
 
98
                song.setName(readString());
 
99
                
 
100
                //leo el artista
 
101
                song.setArtist(readString());
 
102
                
 
103
                //leo el album
 
104
                song.setAlbum(readString());
 
105
                
 
106
                //leo el autor
 
107
                song.setAuthor(readString());
 
108
                
 
109
                //leo la cantidad de pistas
 
110
                int trackCount = readInt();
 
111
                
 
112
                //leo las pistas
 
113
                for(int i = 0;i < trackCount;i++){
 
114
                        song.addTrack(readTrack(song));
 
115
                }
 
116
                
 
117
                return song;
 
118
        }
 
119
        
 
120
        private TGTrack readTrack(TGSong song){
 
121
                TGTrack track = this.factory.newTrack();
 
122
                
 
123
                //leo el numero
 
124
                track.setNumber((int)readLong());
 
125
                
 
126
                //leo el nombre
 
127
                track.setName(readString());
 
128
                
 
129
                //leo el canal
 
130
                readChannel(track.getChannel());
 
131
                
 
132
                //leo la cantidad de compases
 
133
                int measureCount = readInt();
 
134
                
 
135
                if(song.countMeasureHeaders() == 0){
 
136
                        for(int i = 0;i < measureCount;i++){
 
137
                                TGMeasureHeader header = this.factory.newHeader();
 
138
                                song.addMeasureHeader(header);
 
139
                        }
 
140
                }
 
141
                
 
142
                //leo los compases
 
143
                for(int i = 0;i < measureCount;i++){
 
144
                        track.addMeasure(readMeasure(song.getMeasureHeader(i)));
 
145
                }
 
146
                
 
147
                //leo la cantidad de cuerdas
 
148
                int stringCount = readInt();
 
149
                
 
150
                //leo las cuerdas
 
151
                for(int i = 0;i < stringCount;i++){
 
152
                        track.getStrings().add(readInstrumentString());
 
153
                }
 
154
                
 
155
                //leo el color
 
156
                readColor(track.getColor());
 
157
                
 
158
                return track;
 
159
        }
 
160
        
 
161
        private TGMeasure readMeasure(TGMeasureHeader header){
 
162
                TGMeasure measure = this.factory.newMeasure(header);
 
163
                
 
164
                //leo el number
 
165
                header.setNumber(readInt());
 
166
                
 
167
                //leo el start
 
168
                header.setStart( (TGDuration.QUARTER_TIME * readLong() / 1000) );
 
169
                
 
170
                //leo la cantidad de notas
 
171
                int noteCount = readInt();
 
172
                
 
173
                //leo las notas
 
174
                TGBeat previous = null;
 
175
                for(int i = 0;i < noteCount;i++){
 
176
                        previous = readNote(measure,previous);
 
177
                }
 
178
                
 
179
                //leo la cantidad de silencios
 
180
                int silenceCount = readInt();
 
181
                
 
182
                //leo los silencios
 
183
                previous = null;
 
184
                for(int i = 0;i < silenceCount;i++){
 
185
                        previous = readSilence(measure,previous);
 
186
                }
 
187
                
 
188
                //leo el time signature
 
189
                readTimeSignature(header.getTimeSignature());
 
190
                
 
191
                //leo el tempo
 
192
                readTempo(header.getTempo());
 
193
                
 
194
                //leo la clave
 
195
                measure.setClef(readInt());
 
196
                
 
197
                //leo el key signature
 
198
                measure.setKeySignature(readInt());
 
199
                
 
200
                //leo el comienzo de la repeticion
 
201
                header.setRepeatOpen(readBoolean());
 
202
                
 
203
                //leo el numero de repeticiones
 
204
                header.setRepeatClose(readInt());
 
205
                
 
206
                return measure;
 
207
                
 
208
        }
 
209
        
 
210
        private TGBeat readNote(TGMeasure measure, TGBeat previous){
 
211
                TGBeat beat = previous;
 
212
                
 
213
                //leo el valor
 
214
                int value = readInt();
 
215
                
 
216
                //leo el start
 
217
                long start = (TGDuration.QUARTER_TIME * readLong() / 1000);
 
218
                if(beat == null || beat.getStart() != start){
 
219
                        beat = this.factory.newBeat();
 
220
                        beat.setStart(start);
 
221
                        measure.addBeat(beat);
 
222
                }
 
223
                
 
224
                //leo la duracion
 
225
                readDuration(beat.getDuration());
 
226
                
 
227
                TGNote note = this.factory.newNote();
 
228
                
 
229
                note.setValue(value);
 
230
                
 
231
                //leo el velocity
 
232
                note.setVelocity(readInt());
 
233
                
 
234
                //leo la cuerda
 
235
                note.setString(readInt());
 
236
                
 
237
                //leo la ligadura
 
238
                note.setTiedNote(readBoolean());
 
239
                
 
240
                //leo los efectos
 
241
                readNoteEffect(note.getEffect());
 
242
                
 
243
                beat.addNote(note);
 
244
                return beat;
 
245
        }
 
246
        
 
247
        private void readChannel(TGChannel channel){
 
248
                //leo el canal
 
249
                channel.setChannel(readShort());
 
250
                
 
251
                //leo el canal de efectos
 
252
                channel.setEffectChannel(readShort());
 
253
                
 
254
                //leo el instrumento
 
255
                channel.setInstrument(readShort());
 
256
                
 
257
                //leo el volumen
 
258
                channel.setVolume(readShort());
 
259
                
 
260
                //leo el balance
 
261
                channel.setBalance(readShort());
 
262
                
 
263
                //leo el chorus
 
264
                channel.setChorus(readShort());
 
265
                
 
266
                //leo el reverb
 
267
                channel.setReverb(readShort());
 
268
                
 
269
                //leo el phaser
 
270
                channel.setPhaser(readShort());
 
271
                
 
272
                //leo el tremolo
 
273
                channel.setTremolo(readShort());
 
274
                
 
275
                //leo el solo
 
276
                channel.setSolo(readBoolean());
 
277
                
 
278
                //leo el mute
 
279
                channel.setMute(readBoolean());
 
280
        }
 
281
        
 
282
        private TGBeat readSilence(TGMeasure measure, TGBeat previous){
 
283
                TGBeat beat = previous;
 
284
                
 
285
                //leo el start
 
286
                long start = (TGDuration.QUARTER_TIME * readLong() / 1000);
 
287
                if(beat == null || beat.getStart() != start){
 
288
                        beat = this.factory.newBeat();
 
289
                        beat.setStart(start);
 
290
                        measure.addBeat(beat);
 
291
                }
 
292
                //leo la duracion
 
293
                readDuration(beat.getDuration());
 
294
                
 
295
                return beat;
 
296
        }
 
297
        
 
298
        private TGString readInstrumentString(){
 
299
                TGString string = this.factory.newString();
 
300
                
 
301
                //leo el numero
 
302
                string.setNumber( readInt() );
 
303
                
 
304
                //leo el valor
 
305
                string.setValue(readInt());
 
306
                
 
307
                return string;
 
308
        }
 
309
        
 
310
        private void readTempo(TGTempo tempo){
 
311
                //leo el valor
 
312
                tempo.setValue(readInt());
 
313
        }
 
314
        
 
315
        private void readTimeSignature(TGTimeSignature timeSignature){
 
316
                //leo el numerador
 
317
                timeSignature.setNumerator(readInt());
 
318
                
 
319
                //leo el denominador
 
320
                readDuration(timeSignature.getDenominator());
 
321
        }
 
322
        
 
323
        private void readDuration(TGDuration duration){
 
324
                //leo el valor
 
325
                duration.setValue( readInt() );
 
326
                
 
327
                //leo el puntillo
 
328
                duration.setDotted( readBoolean() );
 
329
                
 
330
                //leo el doble puntillo
 
331
                duration.setDoubleDotted( readBoolean() );
 
332
                
 
333
                //leo el tupleto
 
334
                readTupleto(duration.getTupleto());
 
335
        }
 
336
        
 
337
        private void readTupleto(TGTupleto tupleto){
 
338
                //leo los enters
 
339
                tupleto.setEnters(readInt());
 
340
                
 
341
                //leo los tiempos
 
342
                tupleto.setTimes(readInt());
 
343
        }
 
344
        
 
345
        private void readNoteEffect(TGNoteEffect effect){
 
346
                //leo el vibrato
 
347
                effect.setVibrato(readBoolean());
 
348
                
 
349
                //leo el bend
 
350
                if(readBoolean()){
 
351
                        effect.setBend(readBendEffect());
 
352
                }
 
353
                
 
354
                //leo la nota muerta
 
355
                effect.setDeadNote(readBoolean());
 
356
                
 
357
                //leo el slide
 
358
                effect.setSlide(readBoolean());
 
359
                
 
360
                //leo el hammer
 
361
                effect.setHammer(readBoolean());
 
362
        }
 
363
        
 
364
        private TGEffectBend readBendEffect(){
 
365
                TGEffectBend bend = this.factory.newEffectBend();
 
366
                
 
367
                //leo la cantidad de puntos
 
368
                int count = readInt();
 
369
                
 
370
                for(int i = 0;i < count;i++){
 
371
                        //leo la posicion
 
372
                        int position = readInt();
 
373
                        
 
374
                        //leo el valor
 
375
                        int value = readInt();
 
376
                        
 
377
                        //agrego el punto
 
378
                        bend.addPoint(position,((value > 0)?value / 2:value));
 
379
                }
 
380
                return bend;
 
381
        }
 
382
        
 
383
        private void readColor(TGColor color){
 
384
                //escribo el RGB
 
385
                color.setR(readInt());
 
386
                color.setG(readInt());
 
387
                color.setB(readInt());
 
388
        }
 
389
        
 
390
        private short readShort(){
 
391
                try {
 
392
                        return this.dataInputStream.readShort();
 
393
                } catch (IOException e) {
 
394
                        e.printStackTrace();
 
395
                }
 
396
                return 0;
 
397
        }
 
398
        
 
399
        private int readInt(){
 
400
                try {
 
401
                        return this.dataInputStream.readInt();
 
402
                } catch (IOException e) {
 
403
                        e.printStackTrace();
 
404
                }
 
405
                return 0;
 
406
        }
 
407
        
 
408
        private long readLong(){
 
409
                try {
 
410
                        return this.dataInputStream.readLong();
 
411
                } catch (IOException e) {
 
412
                        e.printStackTrace();
 
413
                }
 
414
                return 0;
 
415
        }
 
416
        
 
417
        private String readString(){
 
418
                try {
 
419
                        int length = this.dataInputStream.read();
 
420
                        char[] chars = new char[length];
 
421
                        for(int i = 0;i < chars.length; i++){
 
422
                                chars[i] = this.dataInputStream.readChar();
 
423
                        }
 
424
                        
 
425
                        return String.copyValueOf(chars);
 
426
                } catch (IOException e) {
 
427
                        e.printStackTrace();
 
428
                }
 
429
                return null;
 
430
                
 
431
        }
 
432
        
 
433
        private boolean readBoolean(){
 
434
                try {
 
435
                        return this.dataInputStream.readBoolean();
 
436
                } catch (IOException e) {
 
437
                        e.printStackTrace();
 
438
                }
 
439
                return false;
 
440
        }
 
441
}