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

« back to all changes in this revision

Viewing changes to TuxGuitar-ptb/src/org/herac/tuxguitar/io/ptb/PTInputStream.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.ptb;
 
2
 
 
3
import java.io.IOException;
 
4
import java.io.InputStream;
 
5
 
 
6
import org.herac.tuxguitar.io.base.TGFileFormat;
 
7
import org.herac.tuxguitar.io.base.TGInputStreamBase;
 
8
import org.herac.tuxguitar.io.ptb.base.PTBar;
 
9
import org.herac.tuxguitar.io.ptb.base.PTBeat;
 
10
import org.herac.tuxguitar.io.ptb.base.PTDirection;
 
11
import org.herac.tuxguitar.io.ptb.base.PTGuitarIn;
 
12
import org.herac.tuxguitar.io.ptb.base.PTNote;
 
13
import org.herac.tuxguitar.io.ptb.base.PTSection;
 
14
import org.herac.tuxguitar.io.ptb.base.PTSong;
 
15
import org.herac.tuxguitar.io.ptb.base.PTSymbol;
 
16
import org.herac.tuxguitar.io.ptb.base.PTTempo;
 
17
import org.herac.tuxguitar.io.ptb.base.PTTrack;
 
18
import org.herac.tuxguitar.io.ptb.base.PTTrackInfo;
 
19
import org.herac.tuxguitar.song.factory.TGFactory;
 
20
import org.herac.tuxguitar.song.models.TGMeasureHeader;
 
21
import org.herac.tuxguitar.song.models.TGSong;
 
22
 
 
23
public class PTInputStream implements TGInputStreamBase{
 
24
        
 
25
        private static final String PTB_VERSION = "ptab-4";
 
26
        
 
27
        private InputStream stream;
 
28
        private String version;
 
29
        private PTSong song;
 
30
        private PTSongParser parser;
 
31
        
 
32
        public PTInputStream(){
 
33
                super();
 
34
        }
 
35
        
 
36
        public void init(TGFactory factory,InputStream stream){
 
37
                this.version = null;
 
38
                this.stream = stream;
 
39
                this.parser = new PTSongParser(factory);
 
40
        }
 
41
        
 
42
        public TGFileFormat getFileFormat(){
 
43
                return new TGFileFormat("PowerTab","*.ptb");
 
44
        }
 
45
        
 
46
        public boolean isSupportedVersion(String version){
 
47
                return (version.equals(PTB_VERSION));
 
48
        }
 
49
        
 
50
        public boolean isSupportedVersion(){
 
51
                try{
 
52
                        readVersion();
 
53
                        return isSupportedVersion(this.version);
 
54
                }catch(Exception e){
 
55
                        return false;
 
56
                }catch(Error e){
 
57
                        return false;
 
58
                }
 
59
        }
 
60
        
 
61
        private void readVersion(){
 
62
                if(this.version == null){
 
63
                        this.version = (readString(4) + "-" + readShort());
 
64
                }
 
65
        }
 
66
        
 
67
        public TGSong readSong() throws IOException{
 
68
                this.readVersion();
 
69
                if (!isSupportedVersion(this.version)) {
 
70
                        throw new IOException("Unsuported Version");
 
71
                }
 
72
                this.song = new PTSong();
 
73
                this.readSongInfo();
 
74
                this.readDataInstruments(this.song.getTrack1());
 
75
                this.readDataInstruments(this.song.getTrack2());
 
76
                this.close();
 
77
                
 
78
                return this.parser.parseSong(this.song);
 
79
        }
 
80
        
 
81
        private void readSongInfo(){
 
82
                this.song.getInfo().setClassification(readByte());
 
83
                if(this.song.getInfo().getClassification() == 0) {
 
84
                        skip(1);
 
85
                        this.song.getInfo().setName(readString());
 
86
                        this.song.getInfo().setInterpret(readString());
 
87
                        this.song.getInfo().setReleaseType(readByte());
 
88
                        if (this.song.getInfo().getReleaseType() == 0){
 
89
                                this.song.getInfo().setAlbumType(readByte());
 
90
                                this.song.getInfo().setAlbum(readString());
 
91
                                this.song.getInfo().setYear(readShort());
 
92
                                this.song.getInfo().setLiveRecording(readBoolean());
 
93
                        }else if(this.song.getInfo().getReleaseType() == 1){
 
94
                                this.song.getInfo().setAlbum(readString());
 
95
                                this.song.getInfo().setLiveRecording(readBoolean());
 
96
                        }else if(this.song.getInfo().getReleaseType() == 2){
 
97
                                this.song.getInfo().setAlbum(readString());
 
98
                                this.song.getInfo().setDay(readShort());
 
99
                                this.song.getInfo().setMonth(readShort());
 
100
                                this.song.getInfo().setYear(readShort());
 
101
                        }
 
102
                        if (readByte() == 0) {
 
103
                                this.song.getInfo().setAuthor(readString());
 
104
                                this.song.getInfo().setLyricist(readString());
 
105
                        }
 
106
                        this.song.getInfo().setArrenger(readString());
 
107
                        this.song.getInfo().setGuitarTranscriber(readString());
 
108
                        this.song.getInfo().setBassTranscriber(readString());
 
109
                        this.song.getInfo().setCopyright(readString());
 
110
                        this.song.getInfo().setLyrics(readString());
 
111
                        this.song.getInfo().setGuitarInstructions(readString());
 
112
                        this.song.getInfo().setBassInstructions(readString());
 
113
                }else if(this.song.getInfo().getClassification() == 1){
 
114
                        this.song.getInfo().setName(readString());
 
115
                        this.song.getInfo().setAlbum(readString());
 
116
                        this.song.getInfo().setStyle(readShort());
 
117
                        this.song.getInfo().setLevel(readByte());
 
118
                        this.song.getInfo().setAuthor(readString());
 
119
                        this.song.getInfo().setInstructions(readString());
 
120
                        this.song.getInfo().setCopyright(readString());
 
121
                }
 
122
        }
 
123
        
 
124
        private void readDataInstruments(PTTrack track){
 
125
                // Guitar section
 
126
                int itemCount = readHeaderItems();
 
127
                for (int j = 0; j < itemCount; j++) {
 
128
                        readTrackInfo(track);
 
129
                        if (j < itemCount - 1){
 
130
                                readShort();
 
131
                        }
 
132
                }
 
133
                // ChordDiagram section
 
134
                itemCount = readHeaderItems();
 
135
                for (int j = 0; j < itemCount; j++) {
 
136
                        readChord();
 
137
                        if (j < itemCount - 1){
 
138
                                readShort();
 
139
                        }
 
140
                }
 
141
                // FloatingText section
 
142
                itemCount = readHeaderItems();
 
143
                for (int j = 0; j < itemCount; j++) {
 
144
                        readFloattingText();
 
145
                        if (j < itemCount - 1){
 
146
                                readShort();
 
147
                        }
 
148
                }
 
149
                // GuitarIn section
 
150
                itemCount = readHeaderItems();
 
151
                for (int j = 0; j < itemCount; j++) {
 
152
                        readGuitarIn(track);
 
153
                        if (j < itemCount - 1){
 
154
                                readShort();
 
155
                        }
 
156
                }
 
157
                // TempoMarker
 
158
                itemCount = readHeaderItems();
 
159
                for (int j = 0; j < itemCount; j++) {
 
160
                        readTempoMarker(track);
 
161
                        if (j < itemCount - 1){
 
162
                                readShort();
 
163
                        }
 
164
                }
 
165
                // Dynamic section
 
166
                itemCount = readHeaderItems();
 
167
                for (int j = 0; j < itemCount; j++) {
 
168
                        readDynamic();
 
169
                        
 
170
                        if (j < itemCount - 1){
 
171
                                readShort();
 
172
                        }
 
173
                }
 
174
                // SectionSymbol section
 
175
                itemCount = readHeaderItems();
 
176
                for (int j = 0; j < itemCount; j++) {
 
177
                        readSectionSymbol(track);
 
178
                        if (j < itemCount - 1){
 
179
                                readShort();
 
180
                        }
 
181
                }
 
182
                // Section section
 
183
                itemCount = readHeaderItems();
 
184
                for (int j = 0; j < itemCount; j++) {
 
185
                        readSection(track.getSection(j));
 
186
                        if (j < itemCount - 1){
 
187
                                readShort();
 
188
                        }
 
189
                }
 
190
        }
 
191
        
 
192
        private void readTrackInfo(PTTrack track){
 
193
                PTTrackInfo info = new PTTrackInfo();
 
194
                info.setNumber(readByte());
 
195
                info.setName(readString());
 
196
                info.setInstrument((short)readByte());
 
197
                info.setVolume((short)readByte());
 
198
                info.setBalance((short)readByte());
 
199
                info.setReverb((short)readByte());
 
200
                info.setChorus((short)readByte());
 
201
                info.setTremolo((short)readByte());
 
202
                info.setPhaser((short)readByte());
 
203
                
 
204
                readByte();//capo
 
205
                
 
206
                // Tuning
 
207
                readString();//tunningName
 
208
                
 
209
                //bit 7 = Music notation offset sign, bits 6 to 1 = Music notation offset value, bit 0 = display sharps or flats;
 
210
                readByte();  //offset
 
211
                
 
212
                int[] strings = new int[ (readByte() & 0xff) ];
 
213
                for (int i = 0; i < strings.length; i++) {
 
214
                        strings[i] = readByte();
 
215
                }
 
216
                info.setStrings(strings);
 
217
                
 
218
                track.getInfos().add(info);
 
219
        }
 
220
        
 
221
        private void readSection(PTSection section){
 
222
                readInt();//left
 
223
                readInt();//top
 
224
                readInt();//right
 
225
                readInt();//bottom
 
226
                
 
227
                int lastBarData = readByte();
 
228
                
 
229
                readByte();
 
230
                readByte();
 
231
                readByte();
 
232
                readByte();
 
233
                
 
234
                // BarLine
 
235
                readBarLine(section);
 
236
                
 
237
                // Direction section
 
238
                int itemCount = readHeaderItems();
 
239
                for (int j = 0; j < itemCount; j++) {
 
240
                        readDirection(section);
 
241
                        if (j < itemCount - 1){
 
242
                                readShort();
 
243
                        }
 
244
                }
 
245
                // ChordText section
 
246
                itemCount = readHeaderItems();
 
247
                for (int j = 0; j < itemCount; j++) {
 
248
                        readChordText();
 
249
                        if (j < itemCount - 1){
 
250
                                readShort();
 
251
                        }
 
252
                }
 
253
                // RhythmSlash section
 
254
                itemCount = readHeaderItems();
 
255
                for (int j = 0; j < itemCount; j++) {
 
256
                        readRhythmSlash();
 
257
                        if (j < itemCount - 1){
 
258
                                readShort();
 
259
                        }
 
260
                }
 
261
                // Staff
 
262
                section.setStaffs(readHeaderItems());
 
263
                for (int staff = 0; staff < section.getStaffs(); staff++) {
 
264
                        readStaff(staff,section);
 
265
                        if (staff < section.getStaffs() - 1){
 
266
                                readShort();
 
267
                        }
 
268
                }
 
269
                // MusicBar section
 
270
                itemCount = readHeaderItems();
 
271
                for (int j = 0; j < itemCount; j++) {
 
272
                        readBarLine(section);
 
273
                        if (j < itemCount - 1){
 
274
                                readShort();
 
275
                        }
 
276
                }
 
277
                PTBar bar = new PTBar();
 
278
                bar.setRepeatClose(((lastBarData >>> 5) == 4)?(lastBarData - 128):0);
 
279
                section.getPosition(section.getNextPositionNumber()).addComponent(bar);
 
280
        }
 
281
        
 
282
        private void readStaff(int staff,PTSection section){
 
283
                readByte();
 
284
                readByte();
 
285
                readByte();
 
286
                readByte();
 
287
                readByte();
 
288
                int itemCount = readHeaderItems();
 
289
                for (int j = 0; j < itemCount; j++) {
 
290
                        readPosition(staff,0,section);
 
291
                        if (j < itemCount - 1){
 
292
                                readShort();
 
293
                        }
 
294
                }
 
295
                // Position section 
 
296
                itemCount = readHeaderItems();
 
297
                for (int j = 0; j < itemCount; j++) {
 
298
                        readPosition(staff,1,section);
 
299
                        if (j < itemCount - 1){
 
300
                                readShort();
 
301
                        }
 
302
                }
 
303
        }
 
304
        
 
305
        private void readPosition(int staff,int voice,PTSection section){
 
306
                PTBeat beat = new PTBeat(staff,voice);
 
307
                
 
308
                int position = readByte();
 
309
                int beaming = readByte();
 
310
                beaming = ((beaming - 128 < 0)?beaming:beaming - 128);
 
311
                
 
312
                readByte();
 
313
                
 
314
                int data1 = readByte();
 
315
                readByte();
 
316
                int data3 = readByte();
 
317
                int durationValue = readByte();
 
318
                
 
319
                int multiBarRest = 1;
 
320
                int complexCount = readByte();
 
321
                for (int i = 0; i < complexCount; i++) {
 
322
                        int count = readShort();
 
323
                        readByte();
 
324
                        
 
325
                        int type = readByte();
 
326
                        if((type & 0x08) != 0){
 
327
                                multiBarRest = count;
 
328
                        }
 
329
                }
 
330
                
 
331
                int itemCount = readHeaderItems();
 
332
                for (int j = 0; j < itemCount; j++) {
 
333
                        readNote(beat);
 
334
                        if (j < itemCount - 1){
 
335
                                readShort();
 
336
                        }
 
337
                }
 
338
                beat.setMultiBarRest((itemCount == 0)?multiBarRest:1);
 
339
                beat.setVibrato(((data1 & 0x08) != 0) || ((data1 & 0x10) != 0));
 
340
                beat.setGrace((data3 & 0x01) != 0);
 
341
                
 
342
                // Set the duration
 
343
                beat.setDuration(durationValue);
 
344
                beat.setDotted((data1 & 0x01) != 0);
 
345
                beat.setDoubleDotted((data1 & 0x02) != 0);
 
346
                beat.setEnters(((beaming - (beaming % 8)) / 8) + 1);
 
347
                beat.setTimes((beaming % 8) + 1);
 
348
                
 
349
                section.getPosition(position).addComponent(beat);
 
350
        }
 
351
        
 
352
        private void readNote(PTBeat beat){
 
353
                PTNote note = new PTNote();
 
354
                int position = readByte();
 
355
                int simpleData = readShort();
 
356
                int symbolCount = readByte();
 
357
                for (int i = 0; i < symbolCount; i++) {
 
358
                        readByte();
 
359
                        readByte();
 
360
                        int data3 = readByte();
 
361
                        int data4 = readByte();
 
362
                        note.setBend((data4 == 101)?((data3 / 16) + 1):0);
 
363
                        note.setSlide((data4 == 100));
 
364
                }
 
365
                note.setValue(position & 0x1f);
 
366
                note.setString(((position & 0xe0) >> 5) + 1);
 
367
                note.setTied((simpleData & 0x01) != 0);
 
368
                note.setDead((simpleData & 0x02) != 0);
 
369
                beat.addNote(note);
 
370
        }
 
371
        
 
372
        private void readTimeSignature(PTBar bar){
 
373
                int data = readInt();
 
374
                readByte(); //measurePulses
 
375
                
 
376
                bar.setNumerator(((((data >> 24) - ((data >> 24) % 8)) / 8) + 1));
 
377
                bar.setDenominator((int)Math.pow(2,(data >> 24) % 8));
 
378
        }
 
379
        
 
380
        private void readKeySignature(){
 
381
                readByte();
 
382
        }
 
383
        
 
384
        private void readBarLine(PTSection section){
 
385
                PTBar bar = new PTBar();
 
386
                int position = readByte();
 
387
                int type = readByte();
 
388
                
 
389
                //repeat start
 
390
                bar.setRepeatStart(((type >>> 5) == 3));
 
391
                
 
392
                //repeat end
 
393
                bar.setRepeatClose((((type >>> 5) == 4)?(type - 128):0));
 
394
                
 
395
                readKeySignature();
 
396
                readTimeSignature(bar);
 
397
                readRehearsalSign();
 
398
                section.getPosition(position).addComponent(bar);
 
399
        }
 
400
        
 
401
        private void readChord(){
 
402
                readShort(); //chordKey
 
403
                readByte();
 
404
                readShort(); //chordModification
 
405
                readByte();
 
406
                readByte();
 
407
                int stringCount = readByte();
 
408
                for (int j = 0; j < stringCount; j++) {
 
409
                        readByte(); //fret
 
410
                }
 
411
        }
 
412
        
 
413
        private void readFloattingText(){
 
414
                // Floating text
 
415
                readString();
 
416
                // Read mfc rect
 
417
                readInt();//left
 
418
                readInt();//top
 
419
                readInt();//right
 
420
                readInt();//bottom
 
421
                
 
422
                readByte();
 
423
                readFontSetting();
 
424
        }
 
425
        
 
426
        private void readFontSetting(){
 
427
                readString();//fontName
 
428
                readInt();//pointSize   
 
429
                readInt();//weight
 
430
                readBoolean();//italic
 
431
                readBoolean();//underline
 
432
                readBoolean();//strikeout
 
433
                readInt();//color
 
434
        }
 
435
        
 
436
        private void readGuitarIn(PTTrack track){
 
437
                int section = readShort();
 
438
                int staff = readByte();
 
439
                int position = readByte();
 
440
                skip(1);
 
441
                int info = (readByte() & 0xff);
 
442
                track.getSection(section).getPosition(position).addComponent(new PTGuitarIn(staff,info));
 
443
        }
 
444
        
 
445
        private void readTempoMarker(PTTrack track){
 
446
                int section = readShort();
 
447
                int position = readByte();
 
448
                int tempo = readShort();
 
449
                int data = readShort();
 
450
                readString();//description
 
451
                int tripletFeel = TGMeasureHeader.TRIPLET_FEEL_NONE;
 
452
                if((data & 0x01) != 0){
 
453
                        tripletFeel = TGMeasureHeader.TRIPLET_FEEL_EIGHTH;
 
454
                }else if((data & 0x02) != 0){
 
455
                        tripletFeel = TGMeasureHeader.TRIPLET_FEEL_SIXTEENTH;
 
456
                }
 
457
                if(tempo > 0){
 
458
                        track.getSection(section).getPosition(position).addComponent(new PTTempo(tempo,tripletFeel));
 
459
                }
 
460
        }
 
461
        
 
462
        private void readSectionSymbol(PTTrack track){
 
463
                int section = readShort();
 
464
                int position = readByte();
 
465
                int data = readInt();
 
466
                PTSymbol symbol = new PTSymbol();
 
467
                symbol.setEndNumber( (data >> 16) );
 
468
                track.getSection(section).getPosition(position).addComponent(symbol);
 
469
        }
 
470
        
 
471
        private void readDynamic(){
 
472
                readShort();
 
473
                readByte();
 
474
                readByte();
 
475
                readShort();
 
476
        }
 
477
        
 
478
        private void readRehearsalSign(){
 
479
                readByte();
 
480
                readString();
 
481
        }
 
482
        
 
483
        private void readDirection(PTSection section){
 
484
                int position = readByte();
 
485
                int symboleCount = readByte();
 
486
                for (int i = 0; i < symboleCount; i++) {
 
487
                        int data = readShort();
 
488
                        section.getPosition(position).addComponent(new PTDirection( ( data >> 8 ) , ((data & 0xc0) >> 6), (data & 0x1f) ) );
 
489
                }
 
490
        }
 
491
        
 
492
        private void readChordText(){
 
493
                readByte();
 
494
                readShort();
 
495
                readByte();
 
496
                readShort();
 
497
                readByte();
 
498
        }
 
499
        
 
500
        private void readRhythmSlash(){
 
501
                readByte();
 
502
                readByte();
 
503
                readInt();
 
504
        }
 
505
        
 
506
        private int readHeaderItems(){
 
507
                int nbItems = readShort();
 
508
                if (nbItems != 0){
 
509
                        int header = readShort();
 
510
                        if (header == 0xffff) {
 
511
                                if (readShort() != 1) {
 
512
                                        return -1;
 
513
                                }
 
514
                                readString(readShort());
 
515
                        }
 
516
                }
 
517
                return nbItems;
 
518
        }
 
519
        
 
520
        private String readString(){
 
521
                try {
 
522
                        int length = (this.stream.read() & 0xff);
 
523
                        return this.readString(((length < 0xff)?length:readShort()));
 
524
                } catch (IOException e) {
 
525
                        e.printStackTrace();
 
526
                }
 
527
                return null;
 
528
        }
 
529
        
 
530
        private String readString(int length){
 
531
                try {
 
532
                        byte[] bytes = new byte[length];
 
533
                        this.stream.read(bytes);
 
534
                        return new String(bytes);
 
535
                } catch (IOException e) {
 
536
                        e.printStackTrace();
 
537
                }
 
538
                return null;
 
539
        }
 
540
        
 
541
        private int readInt(){
 
542
                try {
 
543
                        byte[] b = new byte[4];
 
544
                        this.stream.read(b);
 
545
                        return ((b[3] & 0xff) << 24) | ((b[2] & 0xff) << 16) | ((b[1] & 0xff) << 8) | (b[0] & 0xff);
 
546
                } catch (IOException e) {
 
547
                        e.printStackTrace();
 
548
                }
 
549
                return 0;
 
550
        }
 
551
        
 
552
        private int readShort(){
 
553
                try {
 
554
                        byte[] b = {0, 0};
 
555
                        this.stream.read(b);
 
556
                        return ((b[1] & 0xff) << 8) | (b[0] & 0xff);
 
557
                } catch (IOException e) {
 
558
                        e.printStackTrace();
 
559
                }
 
560
                return 0;
 
561
        }
 
562
        
 
563
        private boolean readBoolean(){
 
564
                try {
 
565
                        return (this.stream.read() > 0);
 
566
                } catch (IOException e) {
 
567
                        e.printStackTrace();
 
568
                }
 
569
                return false;
 
570
        }
 
571
        
 
572
        private int readByte(){
 
573
                try {
 
574
                        return this.stream.read();
 
575
                } catch (IOException e) {
 
576
                        e.printStackTrace();
 
577
                }
 
578
                return 0;
 
579
        }
 
580
        
 
581
        private void skip(int bytes){
 
582
                try {
 
583
                        this.stream.read(new byte[bytes]);
 
584
                } catch (IOException e) {
 
585
                        e.printStackTrace();
 
586
                }
 
587
        }
 
588
        
 
589
        private void close(){
 
590
                try {
 
591
                        this.stream.close();
 
592
                } catch (IOException e) {
 
593
                        e.printStackTrace();
 
594
                }
 
595
        }
 
596
}