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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/song/models/Duration.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
/*
 
2
 * Created on 29-nov-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.song.models;
 
8
 
 
9
import java.io.Serializable;
 
10
/**
 
11
 * @author julian
 
12
 *
 
13
 * TODO To change the template for this generated type comment go to
 
14
 * Window - Preferences - Java - Code Style - Code Templates
 
15
 */
 
16
public class Duration implements Serializable{    
 
17
    /**
 
18
     * tiempo por defecto de la Negra.
 
19
     */
 
20
    public static final long QUARTER_TIME = 1000;    
 
21
    /**
 
22
     * Redonda
 
23
     */
 
24
    public static final int WHOLE = 1;
 
25
    
 
26
    /**
 
27
     * Blanca.
 
28
     */
 
29
    public static final int HALF = 2;
 
30
    
 
31
    /**
 
32
     * Negra.
 
33
     */
 
34
    public static final int QUARTER = 4;
 
35
    
 
36
    /**
 
37
     * Corchea.
 
38
     */
 
39
    public static final int EIGHTH = 8;
 
40
    
 
41
    /**
 
42
     * Semi-Corchea.
 
43
     */
 
44
    public static final int SIXTEENTH = 16;
 
45
    
 
46
    /**
 
47
     * Fusa.
 
48
     */
 
49
    public static final int THIRTY_SECOND = 32;
 
50
    
 
51
    /**
 
52
     * Semi-Fusa.
 
53
     */
 
54
    public static final int SIXTY_FOURTH = 64;    
 
55
           
 
56
    /**
 
57
     * Sin tupleto
 
58
     */    
 
59
    public static final Tupleto NO_TUPLETO = new Tupleto(1,1);
 
60
    /**
 
61
     * Valor
 
62
     */
 
63
    private int value;
 
64
    /**
 
65
     * Puntillo
 
66
     */
 
67
    private boolean dotted;
 
68
    /**
 
69
     * Doble Puntillo
 
70
     */
 
71
    private boolean doubleDotted;    
 
72
    /**
 
73
     * Tupleto
 
74
     */
 
75
    private Tupleto tupleto;
 
76
    
 
77
    public Duration(int value,boolean dotted,boolean doubleDotted,Tupleto tupleto){
 
78
        this.value = value;    
 
79
        this.dotted = dotted;
 
80
        this.doubleDotted = doubleDotted;
 
81
        this.tupleto = tupleto;
 
82
    }
 
83
    
 
84
    public Duration(int value){
 
85
        this(value,false,false,new Tupleto(1,1));
 
86
    }
 
87
    
 
88
    public int getValue() {
 
89
        return value;
 
90
    }
 
91
    public void setValue(int value) {
 
92
        this.value = value;
 
93
    }
 
94
    
 
95
    public boolean isDotted() {
 
96
        return dotted;
 
97
    }
 
98
    public void setDotted(boolean dotted) {
 
99
        this.dotted = dotted;
 
100
    }
 
101
            
 
102
    public boolean isDoubleDotted() {
 
103
        return doubleDotted;
 
104
    }
 
105
    
 
106
    public void setDoubleDotted(boolean doubleDotted) {
 
107
        this.doubleDotted = doubleDotted;
 
108
    }
 
109
    
 
110
    public Tupleto getTupleto(){
 
111
        return this.tupleto;
 
112
    }
 
113
    
 
114
    public long getTime(){
 
115
        long time = 0;
 
116
        switch(this.value){
 
117
                case WHOLE:
 
118
                    time = QUARTER_TIME * 4;
 
119
                    break;
 
120
                case HALF:
 
121
                    time = QUARTER_TIME * 2;
 
122
                    break;                                  
 
123
                case QUARTER:
 
124
                    time = QUARTER_TIME;
 
125
                    break;                                  
 
126
                case EIGHTH:
 
127
                    time = QUARTER_TIME / 2;
 
128
                    break;                                  
 
129
                case SIXTEENTH:
 
130
                    time = QUARTER_TIME / 4;
 
131
                    break;                                  
 
132
                case THIRTY_SECOND:
 
133
                    time = QUARTER_TIME / 8;
 
134
                    break;                                  
 
135
                case SIXTY_FOURTH:
 
136
                    time = QUARTER_TIME / 16;
 
137
                    break;                                  
 
138
        }
 
139
        if(this.dotted){
 
140
            time += time / 2;
 
141
        }else if(this.doubleDotted){
 
142
            time += ((time / 4) * 3);
 
143
        }
 
144
        
 
145
        return this.tupleto.convertTime(time);
 
146
    }
 
147
    
 
148
    public static Duration fromTime(long time){
 
149
        return fromTime(time,new Duration(Duration.SIXTY_FOURTH,false,false,new Tupleto(3,2)));
 
150
    }
 
151
    
 
152
    public static Duration fromTime(long time,Duration minDuration){
 
153
        //Duration duration =  new Duration(Duration.SIXTY_FOURTH,false,false,new Tupleto(3,2));
 
154
        Duration duration = (Duration)minDuration.clone();
 
155
        
 
156
        int marginTime = 10;
 
157
        
 
158
        Duration tempDuration = new Duration(Duration.WHOLE);
 
159
        tempDuration.setDotted(true);
 
160
        
 
161
        boolean finish = false;        
 
162
        while(!finish){
 
163
            long currentDurationTime = tempDuration.getTime();
 
164
            if(currentDurationTime - marginTime <= time){
 
165
                if(currentDurationTime > duration.getTime()){
 
166
                        duration = (Duration)tempDuration.clone();
 
167
                }
 
168
             
 
169
            }
 
170
            if(tempDuration.isDotted()){                
 
171
                tempDuration.setDotted(false);
 
172
            }else if(tempDuration.getTupleto().isEqual(Duration.NO_TUPLETO)){
 
173
                tempDuration.getTupleto().setEnters(3);
 
174
                tempDuration.getTupleto().setTimes(2);
 
175
            }else{
 
176
                tempDuration.setValue(tempDuration.getValue() * 2);
 
177
                tempDuration.setDotted(true);            
 
178
                tempDuration.getTupleto().setEnters(1);
 
179
                tempDuration.getTupleto().setTimes(1);
 
180
            }
 
181
            if(tempDuration.getValue() > Duration.SIXTY_FOURTH){
 
182
                finish = true;
 
183
            }
 
184
        }        
 
185
        return duration;
 
186
        
 
187
        
 
188
        /*
 
189
        while(!finish){
 
190
            long currentDurationTime = duration.getTime();
 
191
            if(currentDurationTime - marginTime <= time){
 
192
                return duration;
 
193
            }else{
 
194
                if(duration.isDotted()){                
 
195
                        duration.setDotted(false);
 
196
                }else{
 
197
                        duration.setValue(duration.getValue() * 2);
 
198
                        duration.setDotted(true);                       
 
199
                }
 
200
            }
 
201
            if(duration.getValue() > Duration.SIXTY_FOURTH){
 
202
                finish = true;
 
203
            }
 
204
        }
 
205
        return null;
 
206
        */
 
207
    }
 
208
    
 
209
    public int log2(){
 
210
        return (int)(Math.log(getValue() ) / Math.log(2.0));
 
211
    }
 
212
    
 
213
    public boolean isEqual(Duration d){
 
214
        return (getValue() == d.getValue() && isDotted() == d.isDotted() && isDoubleDotted() == d.isDoubleDotted() && getTupleto().isEqual(d.getTupleto()));
 
215
    }
 
216
    
 
217
    public Object clone(){
 
218
        return new Duration(this.value,this.dotted,this.doubleDotted,(Tupleto)this.tupleto.clone());
 
219
    }
 
220
}