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.v07;
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.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;
36
* TODO To change the template for this generated type comment go to
37
* Window - Preferences - Java - Code Style - Code Templates
39
public class TGInputStream implements TGInputStreamBase{
40
private static final String TG_VERSION = "TG_DEVEL-0.01";
42
private DataInputStream dataInputStream;
43
private TGFactory factory;
44
private String version;
46
public TGInputStream(){
50
public void init(TGFactory factory,InputStream stream) {
51
this.factory = factory;
52
this.dataInputStream = new DataInputStream(stream);
56
public TGFileFormat getFileFormat(){
57
return new TGFileFormat("TuxGuitar","*.tg");
60
public boolean isSupportedVersion(String version){
61
return (version.equals(TG_VERSION));
64
public boolean isSupportedVersion(){
67
return isSupportedVersion(this.version);
75
private void readVersion(){
76
if(this.version == null){
77
this.version = readString();
81
public TGSong readSong()throws TGFileFormatException{
83
if(this.isSupportedVersion()){
84
TGSong song = this.read();
85
this.dataInputStream.close();
88
throw new TGFileFormatException("Unsopported Version");
89
}catch (Throwable throwable) {
90
throw new TGFileFormatException(throwable);
94
private TGSong read(){
95
TGSong song = this.factory.newSong();
98
song.setName(readString());
101
song.setArtist(readString());
104
song.setAlbum(readString());
107
song.setAuthor(readString());
109
//leo la cantidad de pistas
110
int trackCount = readInt();
113
for(int i = 0;i < trackCount;i++){
114
song.addTrack(readTrack(song));
120
private TGTrack readTrack(TGSong song){
121
TGTrack track = this.factory.newTrack();
124
track.setNumber((int)readLong());
127
track.setName(readString());
130
readChannel(track.getChannel());
132
//leo la cantidad de compases
133
int measureCount = readInt();
135
if(song.countMeasureHeaders() == 0){
136
for(int i = 0;i < measureCount;i++){
137
TGMeasureHeader header = this.factory.newHeader();
138
song.addMeasureHeader(header);
143
for(int i = 0;i < measureCount;i++){
144
track.addMeasure(readMeasure(song.getMeasureHeader(i)));
147
//leo la cantidad de cuerdas
148
int stringCount = readInt();
151
for(int i = 0;i < stringCount;i++){
152
track.getStrings().add(readInstrumentString());
156
readColor(track.getColor());
161
private TGMeasure readMeasure(TGMeasureHeader header){
162
TGMeasure measure = this.factory.newMeasure(header);
165
header.setNumber(readInt());
168
header.setStart( (TGDuration.QUARTER_TIME * readLong() / 1000) );
170
//leo la cantidad de notas
171
int noteCount = readInt();
174
TGBeat previous = null;
175
for(int i = 0;i < noteCount;i++){
176
previous = readNote(measure,previous);
179
//leo la cantidad de silencios
180
int silenceCount = readInt();
184
for(int i = 0;i < silenceCount;i++){
185
previous = readSilence(measure,previous);
188
//leo el time signature
189
readTimeSignature(header.getTimeSignature());
192
readTempo(header.getTempo());
195
measure.setClef(readInt());
197
//leo el key signature
198
measure.setKeySignature(readInt());
200
//leo el comienzo de la repeticion
201
header.setRepeatOpen(readBoolean());
203
//leo el numero de repeticiones
204
header.setRepeatClose(readInt());
210
private TGBeat readNote(TGMeasure measure, TGBeat previous){
211
TGBeat beat = previous;
214
int value = readInt();
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);
225
readDuration(beat.getDuration());
227
TGNote note = this.factory.newNote();
229
note.setValue(value);
232
note.setVelocity(readInt());
235
note.setString(readInt());
238
note.setTiedNote(readBoolean());
241
readNoteEffect(note.getEffect());
247
private void readChannel(TGChannel channel){
249
channel.setChannel(readShort());
251
//leo el canal de efectos
252
channel.setEffectChannel(readShort());
255
channel.setInstrument(readShort());
258
channel.setVolume(readShort());
261
channel.setBalance(readShort());
264
channel.setChorus(readShort());
267
channel.setReverb(readShort());
270
channel.setPhaser(readShort());
273
channel.setTremolo(readShort());
276
channel.setSolo(readBoolean());
279
channel.setMute(readBoolean());
282
private TGBeat readSilence(TGMeasure measure, TGBeat previous){
283
TGBeat beat = previous;
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);
293
readDuration(beat.getDuration());
298
private TGString readInstrumentString(){
299
TGString string = this.factory.newString();
302
string.setNumber( readInt() );
305
string.setValue(readInt());
310
private void readTempo(TGTempo tempo){
312
tempo.setValue(readInt());
315
private void readTimeSignature(TGTimeSignature timeSignature){
317
timeSignature.setNumerator(readInt());
320
readDuration(timeSignature.getDenominator());
323
private void readDuration(TGDuration duration){
325
duration.setValue( readInt() );
328
duration.setDotted( readBoolean() );
330
//leo el doble puntillo
331
duration.setDoubleDotted( readBoolean() );
334
readTupleto(duration.getTupleto());
337
private void readTupleto(TGTupleto tupleto){
339
tupleto.setEnters(readInt());
342
tupleto.setTimes(readInt());
345
private void readNoteEffect(TGNoteEffect effect){
347
effect.setVibrato(readBoolean());
351
effect.setBend(readBendEffect());
355
effect.setDeadNote(readBoolean());
358
effect.setSlide(readBoolean());
361
effect.setHammer(readBoolean());
364
private TGEffectBend readBendEffect(){
365
TGEffectBend bend = this.factory.newEffectBend();
367
//leo la cantidad de puntos
368
int count = readInt();
370
for(int i = 0;i < count;i++){
372
int position = readInt();
375
int value = readInt();
378
bend.addPoint(position,((value > 0)?value / 2:value));
383
private void readColor(TGColor color){
385
color.setR(readInt());
386
color.setG(readInt());
387
color.setB(readInt());
390
private short readShort(){
392
return this.dataInputStream.readShort();
393
} catch (IOException e) {
399
private int readInt(){
401
return this.dataInputStream.readInt();
402
} catch (IOException e) {
408
private long readLong(){
410
return this.dataInputStream.readLong();
411
} catch (IOException e) {
417
private String readString(){
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();
425
return String.copyValueOf(chars);
426
} catch (IOException e) {
433
private boolean readBoolean(){
435
return this.dataInputStream.readBoolean();
436
} catch (IOException e) {