2
* Created on 16-dic-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.io.tg.v09;
9
import java.io.DataInputStream;
10
import java.io.IOException;
11
import java.io.InputStream;
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.TGLyric;
22
import org.herac.tuxguitar.song.models.TGMarker;
23
import org.herac.tuxguitar.song.models.TGMeasure;
24
import org.herac.tuxguitar.song.models.TGMeasureHeader;
25
import org.herac.tuxguitar.song.models.TGNote;
26
import org.herac.tuxguitar.song.models.TGNoteEffect;
27
import org.herac.tuxguitar.song.models.TGSong;
28
import org.herac.tuxguitar.song.models.TGString;
29
import org.herac.tuxguitar.song.models.TGTempo;
30
import org.herac.tuxguitar.song.models.TGTimeSignature;
31
import org.herac.tuxguitar.song.models.TGTrack;
32
import org.herac.tuxguitar.song.models.TGTupleto;
33
import org.herac.tuxguitar.song.models.TGVelocities;
34
import org.herac.tuxguitar.song.models.effects.TGEffectBend;
35
import org.herac.tuxguitar.song.models.effects.TGEffectGrace;
36
import org.herac.tuxguitar.song.models.effects.TGEffectHarmonic;
37
import org.herac.tuxguitar.song.models.effects.TGEffectTremoloBar;
38
import org.herac.tuxguitar.song.models.effects.TGEffectTremoloPicking;
39
import org.herac.tuxguitar.song.models.effects.TGEffectTrill;
44
* TODO To change the template for this generated type comment go to
45
* Window - Preferences - Java - Code Style - Code Templates
47
public class TGInputStream extends TGStream implements TGInputStreamBase{
49
private DataInputStream dataInputStream;
50
private String version;
51
private TGFactory factory;
55
public TGInputStream() {
59
public void init(TGFactory factory,InputStream stream) {
60
this.factory = factory;
61
this.dataInputStream = new DataInputStream(stream);
65
public TGFileFormat getFileFormat(){
66
return new TGFileFormat("TuxGuitar","*.tg");
69
public boolean isSupportedVersion(String version){
70
return (version.equals(TG_VERSION));
73
public boolean isSupportedVersion(){
76
return isSupportedVersion(this.version);
77
}catch(Throwable throwable){
82
private void readVersion(){
83
if(this.version == null){
84
this.version = readString();
88
public TGSong readSong() throws TGFileFormatException{
90
if(this.isSupportedVersion()){
91
TGSong song = this.read();
92
this.dataInputStream.close();
95
throw new TGFileFormatException("Unsopported Version");
96
} catch (Throwable throwable) {
97
throw new TGFileFormatException(throwable);
101
private TGSong read(){
102
TGSong song = this.factory.newSong();
105
song.setName(readString());
108
song.setArtist(readString());
111
song.setAlbum(readString());
114
song.setAuthor(readString());
116
//leo la cantidad de measure headers
117
int headerCount = readShort();
120
long headerStart = TGDuration.QUARTER_TIME;
121
TGMeasureHeader lastHeader = null;
122
for(int i = 0;i < headerCount;i++){
123
TGMeasureHeader header = readMeasureHeader(i + 1,headerStart,lastHeader);
124
song.addMeasureHeader(header);
125
headerStart += header.getLength();
129
//leo la cantidad de pistas
130
int trackCount = readByte();
133
for(int i = 0;i < trackCount;i++){
134
song.addTrack(readTrack(i + 1,song) );
140
private TGTrack readTrack(int number,TGSong song){
142
int header = readHeader();
144
TGTrack track = this.factory.newTrack();
146
track.setNumber(number);
149
track.setName( readString() );
152
readChannel(track.getChannel());
154
//leo la cantidad de compases
155
int measureCount = song.countMeasureHeaders();
158
TGMeasure lastMeasure = null;
159
for(int i = 0;i < measureCount;i++){
160
TGMeasure measure = readMeasure(song.getMeasureHeader(i),lastMeasure);
161
track.addMeasure(measure);
162
lastMeasure = measure;
165
//leo la cantidad de cuerdas
166
int stringCount = readByte();
169
for(int i = 0;i < stringCount;i++){
170
track.getStrings().add(readInstrumentString(i + 1));
174
track.setOffset( (TGTrack.MIN_OFFSET + readByte()) );
177
readColor(track.getColor());
180
if(((header & TRACK_LYRICS) != 0)){
181
readLyrics(track.getLyrics());
187
private TGMeasureHeader readMeasureHeader(int number,long start,TGMeasureHeader lastMeasureHeader){
188
int header = readHeader();
190
TGMeasureHeader measureHeader = this.factory.newHeader();
191
measureHeader.setNumber(number);
192
measureHeader.setStart(start);
194
//leo el time signature
195
if(((header & MEASURE_HEADER_TIMESIGNATURE) != 0)){
196
readTimeSignature(measureHeader.getTimeSignature());
197
}else if(lastMeasureHeader != null){
198
lastMeasureHeader.getTimeSignature().copy(measureHeader.getTimeSignature());
202
if(((header & MEASURE_HEADER_TEMPO) != 0)){
203
readTempo(measureHeader.getTempo());
204
}else if(lastMeasureHeader != null){
205
lastMeasureHeader.getTempo().copy(measureHeader.getTempo());
208
//leo el comienzo de la repeticion
209
measureHeader.setRepeatOpen((header & MEASURE_HEADER_OPEN_REPEAT) != 0);
211
//leo el numero de repeticiones
212
if(((header & MEASURE_HEADER_CLOSE_REPEAT) != 0)){
213
measureHeader.setRepeatClose(readShort());
217
if(((header & MEASURE_HEADER_MARKER) != 0)){
218
measureHeader.setMarker(readMarker(number));
221
measureHeader.setTripletFeel ((lastMeasureHeader != null)?lastMeasureHeader.getTripletFeel():TGMeasureHeader.TRIPLET_FEEL_NONE);
222
if(((header & MEASURE_HEADER_TRIPLET_FEEL) != 0)){
223
measureHeader.setTripletFeel( readByte() );
226
return measureHeader;
229
private TGMeasure readMeasure(TGMeasureHeader measureHeader,TGMeasure lastMeasure){
230
this.velocity = TGVelocities.DEFAULT;
232
int header = readHeader();
234
TGMeasure measure = this.factory.newMeasure(measureHeader);
236
//leo la cantidad de componentes
237
TGBeat previous = null;
238
int componentCount = readShort();
239
for(int i = 0;i < componentCount;i++){
240
previous = readComponent(measure,previous);
244
measure.setClef( (lastMeasure == null)?TGMeasure.CLEF_TREBLE:lastMeasure.getClef());
245
if(((header & MEASURE_CLEF) != 0)){
246
measure.setClef( readByte() );
249
//leo el key signature
250
measure.setKeySignature((lastMeasure == null)?0:lastMeasure.getKeySignature());
251
if(((header & MEASURE_KEYSIGNATURE) != 0)){
252
measure.setKeySignature(readByte());
259
private void readChannel(TGChannel channel){
260
int header = readHeader();
263
channel.setChannel( (short)readByte() );
265
//leo el canal de efectos
266
channel.setEffectChannel( (short)readByte() );
269
channel.setInstrument( (short)readByte() );
272
channel.setVolume( (short)readByte() );
275
channel.setBalance( (short)readByte() );
278
channel.setChorus( (short)readByte() );
281
channel.setReverb( (short)readByte() );
284
channel.setPhaser( (short)readByte() );
287
channel.setTremolo( (short)readByte() );
290
channel.setSolo( ((header & CHANNEL_SOLO) != 0) );
293
channel.setMute( ((header & CHANNEL_MUTE) != 0) );
296
private TGBeat readComponent(TGMeasure measure,TGBeat previous){
297
TGBeat beat = previous;
299
int header = readHeader();
303
beat = this.factory.newBeat();
304
beat.setStart(measure.getStart());
305
measure.addBeat(beat);
306
}else if(((header & COMPONENT_NEXT_BEAT) != 0)){
307
beat = this.factory.newBeat();
308
beat.setStart(previous.getStart() + previous.getDuration().getTime());
309
measure.addBeat(beat);
313
if(((header & COMPONENT_NEXT_DURATION) != 0)){
314
readDuration(beat.getDuration());
315
}else if(previous != null && !previous.equals(beat)){
316
previous.getDuration().copy( beat.getDuration() );
319
if(((header & COMPONENT_NOTE) != 0)){
320
TGNote note = this.factory.newNote();
323
note.setValue(readByte());
326
note.setString(readByte());
329
note.setTiedNote((header & COMPONENT_TIEDNOTE) != 0);
332
if(((header & COMPONENT_VELOCITY) != 0)){
333
this.velocity = readByte();
335
note.setVelocity(this.velocity);
338
if(((header & COMPONENT_EFFECT) != 0)){
339
readNoteEffect(note.getEffect());
346
private TGString readInstrumentString(int number){
347
TGString string = this.factory.newString();
349
string.setNumber(number);
352
string.setValue( readByte() );
357
private void readTempo(TGTempo tempo){
359
tempo.setValue(readShort());
362
private void readTimeSignature(TGTimeSignature timeSignature){
364
timeSignature.setNumerator(readByte());
367
readDuration(timeSignature.getDenominator());
370
private void readDuration(TGDuration duration){
371
int header = readHeader();
374
duration.setDotted((header & DURATION_DOTTED) != 0);
376
//leo el doble puntillo
377
duration.setDoubleDotted((header & DURATION_DOUBLE_DOTTED) != 0);
380
duration.setValue(readByte());
383
if(((header & DURATION_TUPLETO) != 0)){
384
readTupleto(duration.getTupleto());
388
private void readTupleto(TGTupleto tupleto){
390
tupleto.setEnters(readByte());
393
tupleto.setTimes(readByte());
396
private void readNoteEffect(TGNoteEffect effect){
397
int header = readHeader(3);
400
if(((header & EFFECT_BEND) != 0)){
401
effect.setBend(readBendEffect());
405
if(((header & EFFECT_TREMOLO_BAR) != 0)){
406
effect.setTremoloBar(readTremoloBarEffect());
410
if(((header & EFFECT_HARMONIC) != 0)){
411
effect.setHarmonic(readHarmonicEffect());
415
if(((header & EFFECT_GRACE) != 0)){
416
effect.setGrace(readGraceEffect());
420
if(((header & EFFECT_TRILL) != 0)){
421
effect.setTrill(readTrillEffect());
424
//leo el tremolo picking
425
if(((header & EFFECT_TREMOLO_PICKING) != 0)){
426
effect.setTremoloPicking(readTremoloPickingEffect());
430
effect.setVibrato(((header & EFFECT_VIBRATO) != 0));
433
effect.setDeadNote(((header & EFFECT_DEAD) != 0));
436
effect.setSlide(((header & EFFECT_SLIDE) != 0));
439
effect.setHammer(((header & EFFECT_HAMMER) != 0));
442
effect.setGhostNote(((header & EFFECT_GHOST) != 0));
445
effect.setAccentuatedNote(((header & EFFECT_ACCENTUATED) != 0));
447
//heavy accentuated note
448
effect.setHeavyAccentuatedNote(((header & EFFECT_HEAVY_ACCENTUATED) != 0));
451
effect.setPalmMute(((header & EFFECT_PALM_MUTE) != 0));
454
effect.setStaccato(((header & EFFECT_STACCATO) != 0));
457
effect.setTapping(((header & EFFECT_TAPPING) != 0));
460
effect.setSlapping(((header & EFFECT_SLAPPING) != 0));
463
effect.setPopping(((header & EFFECT_POPPING) != 0));
466
effect.setFadeIn(((header & EFFECT_FADE_IN) != 0));
469
private TGEffectBend readBendEffect(){
470
TGEffectBend bend = this.factory.newEffectBend();
472
//leo la cantidad de puntos
473
int count = readByte();
475
for(int i = 0;i < count;i++){
477
int position = readByte();
480
int value = readByte();
483
bend.addPoint(position,value);
488
private TGEffectTremoloBar readTremoloBarEffect(){
489
TGEffectTremoloBar tremoloBar = this.factory.newEffectTremoloBar();
491
//leo la cantidad de puntos
492
int count = readByte();
494
for(int i = 0;i < count;i++){
496
int position = readByte();
499
int value = (readByte() - TGEffectTremoloBar.MAX_VALUE_LENGTH);
502
tremoloBar.addPoint(position,value);
507
private TGEffectHarmonic readHarmonicEffect(){
508
TGEffectHarmonic harmonic = this.factory.newEffectHarmonic();
511
harmonic.setType(readByte());
514
if(harmonic.getType() == TGEffectHarmonic.TYPE_ARTIFICIAL){
515
harmonic.setData(TGEffectHarmonic.MIN_ARTIFICIAL_OFFSET + readByte());
516
}else if(harmonic.getType() == TGEffectHarmonic.TYPE_TAPPED){
517
harmonic.setData(readByte());
522
private TGEffectGrace readGraceEffect(){
523
TGEffectGrace grace = this.factory.newEffectGrace();
525
int header = readHeader();
527
grace.setDead(((header & GRACE_FLAG_DEAD) != 0));
529
grace.setOnBeat(((header & GRACE_FLAG_ON_BEAT) != 0));
532
grace.setFret(readByte());
535
grace.setDuration(readByte());
538
grace.setDynamic(readByte());
541
grace.setTransition(readByte());
546
private TGEffectTremoloPicking readTremoloPickingEffect(){
547
TGEffectTremoloPicking tremoloPicking = this.factory.newEffectTremoloPicking();
550
tremoloPicking.getDuration().setValue(readByte());
552
return tremoloPicking;
555
private TGEffectTrill readTrillEffect(){
556
TGEffectTrill trill = this.factory.newEffectTrill();
559
trill.setFret(readByte());
562
trill.getDuration().setValue(readByte());
567
private TGMarker readMarker(int measure){
568
TGMarker marker = this.factory.newMarker();
570
marker.setMeasure(measure);
573
marker.setTitle(readString());
576
readColor(marker.getColor());
581
private void readColor(TGColor color){
583
color.setR(readShort());
584
color.setG(readShort());
585
color.setB(readShort());
588
private void readLyrics(TGLyric lyrics){
589
//leo el compas de comienzo
590
lyrics.setFrom(readShort());
593
lyrics.setLyrics(readString());
596
private int readByte(){
598
return this.dataInputStream.read();
599
} catch (IOException e) {
605
private int readHeader(){
607
return this.dataInputStream.read();
608
} catch (IOException e) {
614
private int readHeader(int bCount){
616
for(int i = bCount; i > 0; i --){
617
header += ( readHeader() << ( (8 * i) - 8 ) );
622
private short readShort(){
624
return this.dataInputStream.readShort();
625
} catch (IOException e) {
631
private String readString(){
633
int length = this.dataInputStream.read();
634
char[] chars = new char[length];
635
for(int i = 0;i < chars.length; i++){
636
chars[i] = this.dataInputStream.readChar();
638
return String.copyValueOf(chars);
639
} catch (IOException e) {