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;
9
import java.io.DataInputStream;
11
import java.io.FileInputStream;
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
14
import java.util.ArrayList;
15
import java.util.List;
17
import org.herac.tuxguitar.song.models.Component;
18
import org.herac.tuxguitar.song.models.Duration;
19
import org.herac.tuxguitar.song.models.InstrumentString;
20
import org.herac.tuxguitar.song.models.Lyric;
21
import org.herac.tuxguitar.song.models.Marker;
22
import org.herac.tuxguitar.song.models.Measure;
23
import org.herac.tuxguitar.song.models.MeasureHeader;
24
import org.herac.tuxguitar.song.models.Note;
25
import org.herac.tuxguitar.song.models.NoteEffect;
26
import org.herac.tuxguitar.song.models.RGBColor;
27
import org.herac.tuxguitar.song.models.Silence;
28
import org.herac.tuxguitar.song.models.Song;
29
import org.herac.tuxguitar.song.models.SongChannel;
30
import org.herac.tuxguitar.song.models.SongTrack;
31
import org.herac.tuxguitar.song.models.Tempo;
32
import org.herac.tuxguitar.song.models.TimeSignature;
33
import org.herac.tuxguitar.song.models.Tupleto;
34
import org.herac.tuxguitar.song.models.VelocityValues;
35
import org.herac.tuxguitar.song.models.effects.BendEffect;
36
import org.herac.tuxguitar.song.models.effects.GraceEffect;
37
import org.herac.tuxguitar.song.models.effects.HarmonicEffect;
38
import org.herac.tuxguitar.song.models.effects.TremoloBarEffect;
39
import org.herac.tuxguitar.song.models.effects.TremoloPickingEffect;
40
import org.herac.tuxguitar.song.models.effects.TrillEffect;
45
* TODO To change the template for this generated type comment go to
46
* Window - Preferences - Java - Code Style - Code Templates
48
public class TGInputStream extends TGStream{
50
private DataInputStream dataInputStream;
52
private String version;
54
public TGInputStream(FileInputStream file) throws FileNotFoundException {
55
this.dataInputStream = new DataInputStream(file);
58
public TGInputStream(String fileName) throws FileNotFoundException {
59
this(new FileInputStream(new File(fileName)));
62
public boolean isSupportedVersion(String version){
63
return (version.equals(TG_VERSION));
66
public boolean isSupportedVersion(){
69
return isSupportedVersion(version);
80
Song song = this.readSong();
81
this.dataInputStream.close();
83
} catch (IOException e) {
89
private void readVersion(){
90
if(this.version == null){
91
this.version = readString();
95
private Song readSong(){
97
String name = readString();
100
String interpret = readString();
103
String album = readString();
106
String author = readString();
108
//leo la cantidad de measure headers
109
int headerCount = readShort();
112
long headerStart = 1000;
113
MeasureHeader lastHeader = null;
114
List headers = new ArrayList(headerCount);
115
for(int i = 0;i < headerCount;i++){
116
MeasureHeader header = readMeasureHeader(i + 1,headerStart,lastHeader);
118
headerStart += header.getLength();
122
//leo la cantidad de pistas
123
int trackCount = readByte();
126
List tracks = new ArrayList(trackCount);
127
for(int i = 0;i < trackCount;i++){
128
tracks.add(readTrack(i + 1,headers));
131
return new Song(name,interpret,album,author,tracks,headers,Song.MAX_VOLUME);
134
private SongTrack readTrack(int number,List headers){
136
int header = readHeader();
139
String name = readString();
142
SongChannel channel = readChannel();
144
//leo la cantidad de compases
145
int measureCount = headers.size();
148
Measure lastMeasure = null;
149
List measures = new ArrayList(measureCount);
150
for(int i = 0;i < measureCount;i++){
151
Measure measure = readMeasure((MeasureHeader)headers.get(i),lastMeasure);
152
measures.add(measure);
153
lastMeasure = measure;
156
//leo la cantidad de cuerdas
157
int stringCount = readByte();
160
List strings = new ArrayList(stringCount);
161
for(int i = 0;i < stringCount;i++){
162
strings.add(readInstrumentString(i + 1));
166
int offset = (SongTrack.MIN_OFFSET + readByte());
169
RGBColor color = readRGBColor();
172
Lyric lyrics = new Lyric();
173
if(((header & TRACK_LYRICS) != 0)){
174
lyrics = readLyrics();
177
return new SongTrack(number,name,channel,measures,strings,offset,color,lyrics);
180
private MeasureHeader readMeasureHeader(int number,long start,MeasureHeader lastMeasureHeader){
181
int header = readHeader();
183
//leo el time signature
184
TimeSignature timeSignature = null;
185
if(((header & MEASURE_HEADER_TIMESIGNATURE) != 0)){
186
timeSignature = readTimeSignature();
188
timeSignature = (TimeSignature)lastMeasureHeader.getTimeSignature().clone();
193
if(((header & MEASURE_HEADER_TEMPO) != 0)){
196
tempo = (Tempo)lastMeasureHeader.getTempo().clone();
199
//leo el comienzo de la repeticion
200
boolean repeatStart = ((header & MEASURE_HEADER_OPEN_REPEAT) != 0);
202
//leo el numero de repeticiones
203
int numberOfRepetitions = 0;
204
if(((header & MEASURE_HEADER_CLOSE_REPEAT) != 0)){
205
numberOfRepetitions = readShort();
209
Marker marker = null;
210
if(((header & MEASURE_HEADER_MARKER) != 0)){
211
marker = readMarker(number);
214
int tripletFeel = ((lastMeasureHeader != null)?lastMeasureHeader.getTripletFeel():MeasureHeader.TRIPLET_FEEL_NONE);
215
if(((header & MEASURE_HEADER_TRIPLET_FEEL) != 0)){
216
tripletFeel = readByte();
219
return new MeasureHeader(number,start,timeSignature,tempo,marker,tripletFeel,repeatStart,numberOfRepetitions);
223
private Measure readMeasure(MeasureHeader measureHeader,Measure lastMeasure){
224
int header = readHeader();
226
List notes = new ArrayList();
227
List silences = new ArrayList();
229
//leo la cantidad de componentes
230
Component lastComponent = null;
231
int componentCount = readShort();
232
for(int i = 0;i < componentCount;i++){
233
lastComponent = readComponent(measureHeader,notes,silences,lastComponent);
238
if(((header & MEASURE_CLEF) != 0)){
241
clef = lastMeasure.getClef();
244
//leo el key signature
245
int keySignature = 0;
246
if(((header & MEASURE_KEYSIGNATURE) != 0)){
247
keySignature = readByte();
249
keySignature = lastMeasure.getKeySignature();
252
return new Measure(measureHeader,notes,silences,clef,keySignature);
256
private SongChannel readChannel(){
257
int header = readHeader();
260
short channel = (short)readByte();
262
//leo el canal de efectos
263
short effectChannel = (short)readByte();
266
short instrument = (short)readByte();
269
short volume = (short)readByte();
272
short balance = (short)readByte();
275
short chorus = (short)readByte();
278
short reverb = (short)readByte();
281
short phaser = (short)readByte();
284
short tremolo = (short)readByte();
287
boolean solo = ((header & CHANNEL_SOLO) != 0);
290
boolean mute = ((header & CHANNEL_MUTE) != 0);
292
return new SongChannel(channel,effectChannel,instrument,volume,balance,chorus,reverb,phaser,tremolo,solo,mute);
295
private Component readComponent(MeasureHeader measure,List notes,List silences,Component lastComponent){
296
int header = readHeader();
300
if(lastComponent == null){
301
start = measure.getStart();
302
}else if(((header & COMPONENT_NEXT_BEAT) != 0)){
303
start = (lastComponent.getStart() + lastComponent.getDuration().getTime());
305
start = lastComponent.getStart();
309
Duration duration = null;
310
if(((header & COMPONENT_NEXT_DURATION) != 0)){
311
duration = readDuration();
313
duration = (Duration)lastComponent.getDuration().clone();
315
if(((header & COMPONENT_NOTE) != 0)){
317
int value = readByte();
320
int string = readByte();
323
boolean tiedNote = ((header & COMPONENT_TIEDNOTE) != 0);
326
int velocity = ((lastComponent instanceof Note)?((Note)lastComponent).getVelocity():VelocityValues.DEFAULT);
327
if(((header & COMPONENT_VELOCITY) != 0)){
328
velocity = readByte();
332
NoteEffect effect = new NoteEffect();
333
if(((header & COMPONENT_EFFECT) != 0)){
334
effect = readNoteEffect();
337
Note note = new Note(value,start,duration,velocity,string,tiedNote,effect);
340
}else if(((header & COMPONENT_SILENCE) != 1)){
341
Silence silence = new Silence(start,duration);
342
silences.add(silence);
348
private InstrumentString readInstrumentString(int number){
350
int value = readByte();
352
return new InstrumentString(number,value);
355
private Tempo readTempo(){
357
int value = readShort();
359
return new Tempo(value);
363
private TimeSignature readTimeSignature(){
365
int numerator = readByte();
368
Duration denominator = readDuration();
370
return new TimeSignature(numerator,denominator);
373
private Duration readDuration(){
374
int header = readHeader();
375
boolean dotted = ((header & DURATION_DOTTED) != 0);
376
boolean doubleDotted = ((header & DURATION_DOUBLE_DOTTED) != 0);
379
int value = readByte();
382
Tupleto tupleto = Duration.NO_TUPLETO;
383
if(((header & DURATION_TUPLETO) != 0)){
384
tupleto = readTupleto();
386
return new Duration(value,dotted,doubleDotted,tupleto);
389
private Tupleto readTupleto(){
391
int enters = readByte();
394
int times = readByte();
396
return new Tupleto(enters,times);
399
private NoteEffect readNoteEffect(){
400
NoteEffect effect = new NoteEffect();
402
int header = readHeader(3);
405
if(((header & EFFECT_BEND) != 0)){
406
effect.setBend(readBendEffect());
410
if(((header & EFFECT_TREMOLO_BAR) != 0)){
411
effect.setTremoloBar(readTremoloBarEffect());
415
if(((header & EFFECT_HARMONIC) != 0)){
416
effect.setHarmonic(readHarmonicEffect());
420
if(((header & EFFECT_GRACE) != 0)){
421
effect.setGrace(readGraceEffect());
425
if(((header & EFFECT_TRILL) != 0)){
426
effect.setTrill(readTrillEffect());
429
//leo el tremolo picking
430
if(((header & EFFECT_TREMOLO_PICKING) != 0)){
431
effect.setTremoloPicking(readTremoloPickingEffect());
435
effect.setVibrato(((header & EFFECT_VIBRATO) != 0));
438
effect.setDeadNote(((header & EFFECT_DEAD) != 0));
441
effect.setSlide(((header & EFFECT_SLIDE) != 0));
444
effect.setHammer(((header & EFFECT_HAMMER) != 0));
447
effect.setGhostNote(((header & EFFECT_GHOST) != 0));
450
effect.setAccentuatedNote(((header & EFFECT_ACCENTUATED) != 0));
452
//heavy accentuated note
453
effect.setHeavyAccentuatedNote(((header & EFFECT_HEAVY_ACCENTUATED) != 0));
456
effect.setPalmMute(((header & EFFECT_PALM_MUTE) != 0));
459
effect.setStaccato(((header & EFFECT_STACCATO) != 0));
462
effect.setTapping(((header & EFFECT_TAPPING) != 0));
465
effect.setSlapping(((header & EFFECT_SLAPPING) != 0));
468
effect.setPopping(((header & EFFECT_POPPING) != 0));
471
effect.setFadeIn(((header & EFFECT_FADE_IN) != 0));
476
private BendEffect readBendEffect(){
477
BendEffect bend = new BendEffect();
479
//leo la cantidad de puntos
480
int count = readByte();
482
for(int i = 0;i < count;i++){
484
int position = readByte();
487
int value = readByte();
490
bend.addPoint(position,value);
495
private TremoloBarEffect readTremoloBarEffect(){
496
TremoloBarEffect tremoloBar = new TremoloBarEffect();
498
//leo la cantidad de puntos
499
int count = readByte();
501
for(int i = 0;i < count;i++){
503
int position = readByte();
506
int value = (readByte() - TremoloBarEffect.MAX_VALUE_LENGTH);
509
tremoloBar.addPoint(position,value);
514
private HarmonicEffect readHarmonicEffect(){
516
int type = readByte();
520
if(type == HarmonicEffect.TYPE_ARTIFICIAL){
521
data = (HarmonicEffect.MIN_ARTIFICIAL_OFFSET + readByte());
522
}else if(type == HarmonicEffect.TYPE_TAPPED){
526
return new HarmonicEffect(type,data);
529
private GraceEffect readGraceEffect(){
530
int header = readHeader();
532
boolean dead = ((header & GRACE_FLAG_DEAD) != 0) ;
534
boolean onBeat = ((header & GRACE_FLAG_ON_BEAT) != 0) ;
537
int fret = readByte();
540
int duration = readByte();
543
int dynamic = readByte();
546
int transition = readByte();
548
return new GraceEffect(fret,duration,dynamic,transition,onBeat,dead);
551
private TremoloPickingEffect readTremoloPickingEffect(){
553
int value = readByte();
555
return new TremoloPickingEffect(value);
558
private TrillEffect readTrillEffect(){
560
int fret = readByte();
563
int value = readByte();
565
return new TrillEffect(fret,value);
571
private Marker readMarker(int measure){
573
String title = readString();
576
RGBColor color = readRGBColor();
578
return new Marker(measure,title,color);
581
private RGBColor readRGBColor(){
587
return new RGBColor(r,g,b);
591
private Lyric readLyrics(){
592
//leo el compas de comienzo
593
int from = readShort();
596
String lyrics = readString();
598
return new Lyric(from,lyrics);
601
private int readByte(){
603
return this.dataInputStream.read();
604
} catch (IOException e) {
611
private int readHeader(){
613
return this.dataInputStream.read();
614
} catch (IOException e) {
620
private int readHeader(int bCount){
622
for(int i = bCount; i > 0; i --){
623
header += ( readHeader() << ( (8 * i) - 8 ) );
628
private short readShort(){
630
return this.dataInputStream.readShort();
631
} catch (IOException e) {
637
private String readString(){
639
int length = this.dataInputStream.read();
640
char[] chars = new char[length];
641
for(int i = 0;i < chars.length; i++){
642
chars[i] = this.dataInputStream.readChar();
644
return String.copyValueOf(chars);
645
} catch (IOException e) {