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

« back to all changes in this revision

Viewing changes to TuxGuitar-ascii/src/org/herac/tuxguitar/io/ascii/ASCIITabOutputStream.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.ascii;
 
2
 
 
3
import java.io.FileNotFoundException;
 
4
import java.io.FileOutputStream;
 
5
import java.io.OutputStream;
 
6
import java.io.PrintStream;
 
7
import java.util.Iterator;
 
8
 
 
9
import org.herac.tuxguitar.song.managers.TGSongManager;
 
10
import org.herac.tuxguitar.song.models.TGBeat;
 
11
import org.herac.tuxguitar.song.models.TGDuration;
 
12
import org.herac.tuxguitar.song.models.TGMeasure;
 
13
import org.herac.tuxguitar.song.models.TGNote;
 
14
import org.herac.tuxguitar.song.models.TGSong;
 
15
import org.herac.tuxguitar.song.models.TGString;
 
16
import org.herac.tuxguitar.song.models.TGTrack;
 
17
 
 
18
public class ASCIITabOutputStream {
 
19
        
 
20
        private static final String[] TONIC_NAMES = new String[]{"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
 
21
        
 
22
        private static final int MAX_LINE_LENGTH = 80;
 
23
        
 
24
        private TGSongManager manager;
 
25
        private PrintStream stream;
 
26
        private ASCIIOutputStream out;
 
27
        
 
28
        public ASCIITabOutputStream(PrintStream stream){
 
29
                this.stream = stream;
 
30
        }
 
31
        
 
32
        public ASCIITabOutputStream(OutputStream stream){
 
33
                this(new PrintStream(stream));
 
34
        }
 
35
        
 
36
        public ASCIITabOutputStream(String fileName) throws FileNotFoundException{
 
37
                this(new FileOutputStream(fileName));
 
38
        }
 
39
        
 
40
        public void writeSong(TGSong song){
 
41
                this.manager = new TGSongManager();
 
42
                this.manager.setSong(song);
 
43
                this.out = new ASCIIOutputStream(this.stream);
 
44
                this.drawSong();
 
45
                this.out.flush();
 
46
                this.out.close();
 
47
        }
 
48
        
 
49
        private void drawSong(){
 
50
                TGSong song = this.manager.getSong();
 
51
                
 
52
                //Propiedades de cancion
 
53
                this.out.drawStringLine("Title: " + song.getName());
 
54
                this.out.drawStringLine("Artist: " + song.getArtist());
 
55
                this.out.drawStringLine("Album: " + song.getAlbum());
 
56
                this.out.drawStringLine("Author: " + song.getAuthor());
 
57
                
 
58
                Iterator it = song.getTracks();
 
59
                while(it.hasNext()){
 
60
                        TGTrack track = (TGTrack)it.next();
 
61
                        this.out.nextLine();
 
62
                        drawTrack(track);
 
63
                        this.out.nextLine();
 
64
                }
 
65
        }
 
66
        
 
67
        private void drawTrack(TGTrack track){
 
68
                //Propiedades de pista
 
69
                this.out.nextLine();
 
70
                this.out.drawStringLine("Track " + track.getNumber() + ": " + track.getName());
 
71
                
 
72
                //Obtengo los nombres de la afinacion, y el ancho maximo que ocupa
 
73
                String[] tuning = new String[track.getStrings().size()];
 
74
                int maxTuningLength = 1;
 
75
                for(int i = 0; i < track.getStrings().size();i++){
 
76
                        TGString string = (TGString)track.getStrings().get(i);
 
77
                        tuning[i] = TONIC_NAMES[(string.getValue() % TONIC_NAMES.length)];
 
78
                        maxTuningLength = Math.max(maxTuningLength,tuning[i].length());
 
79
                }
 
80
                
 
81
                int nextMeasure = 0;
 
82
                boolean eof = false;
 
83
                while(!eof){
 
84
                        this.out.nextLine();
 
85
                        int index = nextMeasure;
 
86
                        for(int i = 0; i < track.getStrings().size();i++){
 
87
                                TGString string = (TGString)track.getStrings().get(i);
 
88
                                
 
89
                                //Dibujo la afinacion de la cuerda
 
90
                                this.out.drawTuneSegment(tuning[i],maxTuningLength);
 
91
                                int measureCount = track.countMeasures();
 
92
                                for(int j = index; j < measureCount; j++){
 
93
                                        TGMeasure measure = track.getMeasure(j);
 
94
                                        drawMeasure(measure,string);
 
95
                                        nextMeasure = (j + 1);
 
96
                                        
 
97
                                        //Calculo si era el ultimo compas
 
98
                                        eof = (this.manager.getTrackManager().isLastMeasure(measure));
 
99
                                        
 
100
                                        //Si se supero el ancho maximo, bajo de linea
 
101
                                        if(this.out.getPosX() > MAX_LINE_LENGTH){
 
102
                                                break;
 
103
                                        }
 
104
                                }
 
105
                                //Cierro los compases
 
106
                                this.out.drawBarSegment();
 
107
                                this.out.nextLine();
 
108
                        }
 
109
                        this.out.nextLine();
 
110
                }
 
111
                this.out.nextLine();
 
112
        }
 
113
        
 
114
        private void drawMeasure(TGMeasure measure,TGString string){
 
115
                //Abro el compas
 
116
                this.out.drawBarSegment();
 
117
                this.out.drawStringSegments(1);
 
118
                TGBeat beat = this.manager.getMeasureManager().getFirstBeat( measure.getBeats() );
 
119
                while(beat != null){
 
120
                        int outLength = 0;
 
121
                        
 
122
                        //Si hay una nota en la misma cuerda, la dibujo
 
123
                        TGNote note = this.manager.getMeasureManager().getNote(beat, string.getNumber());
 
124
                        if(note != null){
 
125
                                outLength = (Integer.toString(note.getValue()).length() - 1);
 
126
                                this.out.drawNote(note.getValue());
 
127
                        }
 
128
                        //dejo el espacio
 
129
                        else{
 
130
                                this.out.drawStringSegments(1);
 
131
                        }
 
132
                        
 
133
                        //Agrego espacios correspondientes hasta el proximo pulso.
 
134
                        this.out.drawStringSegments(getDurationScaping(beat.getDuration()) - outLength);
 
135
                        
 
136
                        beat = this.manager.getMeasureManager().getNextBeat( measure.getBeats() , beat);
 
137
                }
 
138
                
 
139
        }
 
140
        
 
141
        private int getDurationScaping(TGDuration duration){
 
142
                int spacing = 1;
 
143
                
 
144
                if(duration.getValue() >= TGDuration.SIXTEENTH){
 
145
                        spacing = 2;
 
146
                }
 
147
                else if(duration.getValue() >= TGDuration.EIGHTH){
 
148
                        spacing = 3;
 
149
                }
 
150
                else if(duration.getValue() >= TGDuration.QUARTER){
 
151
                        spacing = 4;
 
152
                }
 
153
                else if(duration.getValue() >= TGDuration.HALF){
 
154
                        spacing = 5;
 
155
                }
 
156
                else if(duration.getValue() >= TGDuration.WHOLE){
 
157
                        spacing = 6;
 
158
                }
 
159
                return spacing;
 
160
        }
 
161
        
 
162
}