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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/io/exporter/ASCIITabOutputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.io.exporter;
 
2
 
 
3
import java.io.FileNotFoundException;
 
4
import java.io.FileOutputStream;
 
5
import java.io.PrintStream;
 
6
import java.util.Iterator;
 
7
import java.util.List;
 
8
 
 
9
import org.eclipse.swt.graphics.Point;
 
10
import org.herac.tuxguitar.song.managers.MeasureManager;
 
11
import org.herac.tuxguitar.song.managers.SongManager;
 
12
import org.herac.tuxguitar.song.models.Component;
 
13
import org.herac.tuxguitar.song.models.Duration;
 
14
import org.herac.tuxguitar.song.models.InstrumentString;
 
15
import org.herac.tuxguitar.song.models.Measure;
 
16
import org.herac.tuxguitar.song.models.Note;
 
17
import org.herac.tuxguitar.song.models.Song;
 
18
import org.herac.tuxguitar.song.models.SongTrack;
 
19
 
 
20
public class ASCIITabOutputStream {
 
21
        
 
22
        private static final String[] TONIC_NAMES = new String[]{"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"}; 
 
23
        
 
24
        private static final int MAX_LINE_LENGTH = 80;
 
25
        
 
26
        private SongManager manager;
 
27
        private PrintStream stream;
 
28
        private ASCIIOutputStream out;
 
29
        
 
30
        
 
31
        public ASCIITabOutputStream(PrintStream stream){
 
32
                this.stream = stream;
 
33
        }
 
34
 
 
35
        public ASCIITabOutputStream(String fileName) throws FileNotFoundException{
 
36
                this(new PrintStream( new FileOutputStream(fileName) ));
 
37
                //this(new PrintStream(fileName));
 
38
        }
 
39
        
 
40
        public void writeSong(Song song){
 
41
                this.manager = new SongManager();
 
42
                this.manager.setSong(song);
 
43
                out = new ASCIIOutputStream(this.stream);
 
44
                
 
45
                drawSong();
 
46
                
 
47
                out.flush();
 
48
        }
 
49
        
 
50
        private void drawSong(){
 
51
                Song song = manager.getSong();  
 
52
                
 
53
                //Propiedades de cancion
 
54
                out.drawStringLine("Title: " + song.getName());
 
55
                out.drawStringLine("ALbum: " + song.getAlbum());
 
56
                out.drawStringLine("Author: " + song.getAuthor());
 
57
                out.drawStringLine("Interpret: " + song.getInterpret());
 
58
                
 
59
                Iterator it = song.getTracks().iterator();
 
60
                while(it.hasNext()){
 
61
                        SongTrack track = (SongTrack)it.next();
 
62
                        
 
63
                        out.nextLine();
 
64
                        drawTrack(track);
 
65
                        out.nextLine();         
 
66
                        
 
67
                }                               
 
68
        }
 
69
        
 
70
        private void drawTrack(SongTrack track){
 
71
                //Propiedades de pista
 
72
                out.nextLine();
 
73
                out.drawStringLine("Track " + track.getNumber() + ": " + track.getName());
 
74
                
 
75
                //Obtengo los nombres de la afinacion, y el ancho maximo que ocupa
 
76
                String[] tunning = new String[track.getStrings().size()];
 
77
                int maxTunningLength = 1;               
 
78
                for(int i = 0; i < track.getStrings().size();i++){
 
79
                        InstrumentString string = (InstrumentString)track.getStrings().get(i);
 
80
                        tunning[i] = TONIC_NAMES[(string.getValue() % TONIC_NAMES.length)];
 
81
                        maxTunningLength = Math.max(maxTunningLength,tunning[i].length());
 
82
                }
 
83
                
 
84
                int nextMeasure = 0;            
 
85
                boolean eof = false;
 
86
                while(!eof){                    
 
87
                        out.nextLine();
 
88
                        int index = nextMeasure;
 
89
                        for(int i = 0; i < track.getStrings().size();i++){
 
90
                                InstrumentString string = (InstrumentString)track.getStrings().get(i);
 
91
                        
 
92
                                //Dibujo la afinacion de la cuerda
 
93
                                out.drawTuneSegment(tunning[i],maxTunningLength);
 
94
                                
 
95
                                for(int j = index; j < track.getMeasures().size(); j++){
 
96
                                        Measure measure = (Measure)track.getMeasures().get(j);                          
 
97
                                        drawMeasure(measure,string);
 
98
                                        nextMeasure = (j + 1);
 
99
                                        
 
100
                                        //Calculo si era el ultimo compas
 
101
                                        eof = (manager.getTrackManager().isLastMeasure(track,measure));                                                                         
 
102
                                        
 
103
                                        //Si se supero el ancho maximo, bajo de linea   
 
104
                                        Point position = out.getPosition();
 
105
                                        if(position.x > MAX_LINE_LENGTH){                                               
 
106
                                                break;
 
107
                                        }
 
108
                                }
 
109
                                //Cierro los compases
 
110
                                out.drawBarSegment();
 
111
                                out.nextLine();
 
112
                        }
 
113
                        out.nextLine();
 
114
                }
 
115
                out.nextLine();
 
116
        }
 
117
        
 
118
        private void drawMeasure(Measure measure,InstrumentString string){
 
119
                MeasureManager measureManager = manager.getMeasureManager(); 
 
120
                List components = measureManager.getComponents(measure);
 
121
                measureManager.orderComponents(components);
 
122
                
 
123
                //Abro el compas
 
124
                out.drawBarSegment();
 
125
                out.drawStringSegments(1);
 
126
                Component component = next(components,measure.getStart(),string.getNumber());
 
127
                while(component != null){
 
128
                        int outLength = 0;
 
129
                        //Si es nota y en la misma cuerda, la dibujo
 
130
                        if(component instanceof Note && ((Note)component).getString() == string.getNumber()){
 
131
                                Note note = (Note)component;
 
132
                                outLength = (Integer.toString(note.getValue()).length() - 1);
 
133
                                out.drawNote(note.getValue());
 
134
                        }
 
135
                        //dejo el espacio
 
136
                        else{
 
137
                                out.drawStringSegments(1);      
 
138
                        }
 
139
                        
 
140
                        //Agrego espacios correspondientes hasta el proximo pulso.
 
141
                        out.drawStringSegments(getDurationScaping(component.getDuration()) - outLength);
 
142
                        
 
143
                        component = next(components,component.getStart() + component.getDuration().getTime(),string.getNumber());
 
144
                }
 
145
                
 
146
        }
 
147
        
 
148
        private Component next(List components,long start,int string){
 
149
                Component next = null;
 
150
                for(int i = 0;i < components.size();i++){
 
151
                        Component component = (Component)components.get(i);
 
152
                        
 
153
                        if(component.getStart() >= start && (next == null || component.getStart() < next.getStart())){
 
154
                                next = component;
 
155
                        }else if(component.getStart() >= start && (next == null || component.getStart() == next.getStart())){
 
156
                                if(component instanceof Note && ((Note)component).getString() == string){
 
157
                                        next = component;                                       
 
158
                                }
 
159
                        }                       
 
160
                }
 
161
                return next;
 
162
        }
 
163
        
 
164
        private int getDurationScaping(Duration duration){
 
165
                int spacing = 1;
 
166
 
 
167
                if(duration.getValue() >= Duration.SIXTEENTH){
 
168
                        spacing = 2;
 
169
                }
 
170
                else if(duration.getValue() >= Duration.EIGHTH){
 
171
                        spacing = 3;
 
172
                }
 
173
                else if(duration.getValue() >= Duration.QUARTER){
 
174
                        spacing = 4;
 
175
                }
 
176
                else if(duration.getValue() >= Duration.HALF){
 
177
                        spacing = 5;
 
178
                }               
 
179
                else if(duration.getValue() >= Duration.WHOLE){
 
180
                        spacing = 6;
 
181
                }
 
182
                
 
183
                return spacing;
 
184
        }
 
185
        
 
186
}