1
package org.herac.tuxguitar.io.tef;
3
import java.io.IOException;
4
import java.io.InputStream;
6
import org.herac.tuxguitar.io.tef.base.TEChord;
7
import org.herac.tuxguitar.io.tef.base.TEComponentChord;
8
import org.herac.tuxguitar.io.tef.base.TEComponentNote;
9
import org.herac.tuxguitar.io.tef.base.TEInfo;
10
import org.herac.tuxguitar.io.tef.base.TEPercussion;
11
import org.herac.tuxguitar.io.tef.base.TERepeat;
12
import org.herac.tuxguitar.io.tef.base.TERhythm;
13
import org.herac.tuxguitar.io.tef.base.TESong;
14
import org.herac.tuxguitar.io.tef.base.TETempo;
15
import org.herac.tuxguitar.io.tef.base.TEText;
16
import org.herac.tuxguitar.io.tef.base.TETimeSignature;
17
import org.herac.tuxguitar.io.tef.base.TETimeSignatureChange;
18
import org.herac.tuxguitar.io.tef.base.TETrack;
20
public class TEInputStream {
23
private InputStream stream;
25
public TEInputStream(InputStream stream){
29
public TESong readSong(){
30
this.song = new TESong();
34
this.song.setMeasures((this.readByte() & 0xff));
37
this.readTimeSignature();
42
this.song.setRepeats( (this.readByte() & 0xff) );
45
this.song.setTexts((this.readByte() & 0xff));
48
this.song.setPercussions((this.readByte() & 0xff));
49
this.song.setRhythms((this.readByte() & 0xff));
51
this.song.setChords((this.readByte() & 0xff));
54
boolean notes = ((this.readByte() & 0xff) > 0);
57
this.song.setStrings((this.readByte() & 0xff));
59
this.song.setTracks((this.readByte() & 0xff) + 1);
62
this.readComponents();
66
this.readPercussions();
72
this.readNotes(notes);
80
private void readInfo(){
81
byte[] info = this.readBytes(200);
83
String strings[] = new String[3];
84
for(int i = 0; i < strings.length; i ++){
86
while( (offset + length) < info.length && info[ (offset + length) ] != 0 ){
89
strings[i] = new String(info,offset,length);
92
this.song.setInfo(new TEInfo(strings[0],strings[1],strings[2]));
95
private void readNotes(boolean notes){
97
int length = this.readShort();
98
this.song.getInfo().setNotes( new String(this.readBytes(length),1,(length -1)) );
102
private void readTempo(){
103
int value = this.readShort();
104
this.song.setTempo(new TETempo(value));
107
private void readTimeSignature(){
108
int numerator = this.readByte();
110
int denominator = this.readByte();
111
this.song.setTimeSignature(new TETimeSignature(numerator,denominator));
114
private void readRepeats(){
115
for(int i = 0; i < this.song.getRepeats().length; i ++){
116
int data1 = this.readByte();
117
int data2 = this.readByte();
118
this.song.setRepeat(i,new TERepeat(data1,data2));
122
private void readTexts(){
123
for(int i = 0; i < this.song.getTexts().length; i ++){
124
int length = this.readByte();
125
byte[] text = this.readBytes(length);
126
this.song.setText(i,new TEText(new String(text,1,(length -1))));
131
private void readChords(){
132
for(int i = 0; i < this.song.getChords().length; i ++){
133
byte[] strings = this.readBytes(14);
134
byte[] name = this.readBytes(16);
136
this.song.setChord(i,new TEChord(strings,new String(name)));
141
private void readPercussions(){
142
if(this.song.getPercussions().length > 0){
143
for(int i = 0; i < this.song.getPercussions().length; i ++){
145
String name = new String(this.readBytes(8) );
147
int volume = (this.readByte() & 0xff);
148
this.song.setPercussion(i,new TEPercussion(name,volume));
151
this.skip(this.song.getMeasures());
155
private void readRhythms(){
156
if(this.song.getRhythms().length > 0){
157
for(int i = 0; i < this.song.getRhythms().length; i ++){
159
String name = new String(this.readBytes(8) );
161
int volume = (this.readByte() & 0xff);
162
int instrument = (this.readByte() & 0xff);
163
this.song.setRhythm(i,new TERhythm(name,volume,instrument));
166
this.skip(this.song.getMeasures());
170
private void readTracks(){
171
for(int i = 0; i < this.song.getTracks().length; i ++){
172
int[] strings = new int[this.readByte()];
175
int type = this.readByte();
178
int instrument = this.readByte();
181
int capo = this.readByte();
185
int clefType = this.readByte();
186
int clefNumber = this.readByte();
190
int pan = this.readByte();
191
int volume = this.readByte();
192
int flags = this.readByte();
194
for(int string = 0; string < strings.length; string ++){
195
strings[string] = (this.readByte() & 0xff);
197
this.skip(12 - strings.length);
199
String name = new String(this.readBytes(16));
201
this.song.setTrack(i,new TETrack( (type == 98),instrument,capo, clefType, clefNumber, pan, volume, flags, strings, name));
206
private void readComponents(){
207
int tsSize = ( (256 * this.song.getTimeSignature().getNumerator()) / this.song.getTimeSignature().getDenominator() );
211
int count = this.readShort();
212
for(int i = 0; i < count; i ++){
213
byte[] data = this.readBytes(6);
215
int location = ( (data[0] & 0xff) + (256 * (mData + (data[1] & 0xff))));
216
if( ( location / ( tsSize * this.song.getStrings() ) ) < mIndex ){
218
location = ((data[0] & 0xff) + (256 * (mData + (data[1] & 0xff))));
220
int position = (location % tsSize);
221
int string = ( (location / tsSize) % this.song.getStrings()) ;
222
int measure = ( location / ( tsSize * this.song.getStrings() ) );
224
tsMove = (mIndex == measure)?tsMove:0;
227
if( ((data[2] & 0xff) & 0x1f) > 0 && ((data[2] & 0xff) & 0x1f) <= 25 ){
228
int duration = (data[3] & 0xf);
229
int dynamic = (data[3] >> 4);
230
int effect = data[4];
231
int fret = (((data[2] & 0xff) & 0x1f) - 1);
232
if((((data[2] & 0xff) >> 5) & 0x01) != 0 ) {
233
fret += (data[5] & 0xff);
235
this.song.getComponents().add( new TEComponentNote(position, measure, string ,fret,duration,dynamic,effect ) );
237
else if( ((data[2] & 0xff) & 0x1f) == 27 ){
238
//TIME SIGNATURE CHANGE
239
tsMove = (4 * (data[3] & 0xff ) );
240
int denominator = (int)( ( Math.pow( 2 , ( ( data[2] & 0xff ) >> 5 ) ) / 2 ) );
241
int numerator = ((( (tsSize / 4) - (data[3] & 0xff) ) * denominator) / 64);
242
this.song.addTimeSignatureChange(new TETimeSignatureChange(measure, new TETimeSignature(numerator,denominator)));
244
else if( ((data[2] & 0xff) & 0x1f) == 28 && (data[2] & 0x20) != 0){
247
else if( ((data[2] & 0xff) & 0x1f) == 28){
249
this.song.getComponents().add( new TEComponentChord(position, measure, string , data[3]) );
251
else if( ((data[2] & 0xff) & 0x1f) == 29 ){
252
//SCALE DIAGRAM | SPECIAL CHAR | SYNCOPATION CHANGE
254
else if( ((data[2] & 0xff) & 0x1f) == 30 ){
255
//TEMPO CHANGE | VOICE CHANGE | DRUM EVENT | CRESCENDO ACCENT | REPEAT/ENDING
261
//-----------------------------------------------------------------------------//
262
//-----------------------------------------------------------------------------//
263
//-----------------------------------------------------------------------------//
264
protected byte[] readBytes(int length){
265
byte[] bytes = new byte[length];
267
this.stream.read(bytes);
268
} catch (IOException e) {
274
protected int readByte(){
276
return this.stream.read();
277
} catch (IOException e) {
283
protected int readShort(){
285
byte[] b = new byte[2];
287
return ((b[1] & 0xff) << 8) | (b[0] & 0xff);
288
} catch (IOException e) {
294
protected void skip(int count){
295
for(int i = 0; i < count; i++){
300
protected void close(){
303
} catch (IOException e) {