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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/song/models/TGDuration.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
/*
 
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 org.herac.tuxguitar.song.factory.TGFactory;
 
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 abstract class TGDuration {
 
17
        /**
 
18
         * tiempo por defecto de la Negra.
 
19
         */
 
20
        public static final long QUARTER_TIME = 960;
 
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
         * Valor.
 
57
         */
 
58
        private int value;
 
59
        /**
 
60
         * Puntillo.
 
61
         */
 
62
        private boolean dotted;
 
63
        /**
 
64
         * Doble Puntillo.
 
65
         */
 
66
        private boolean doubleDotted;
 
67
        /**
 
68
         * Tupleto.
 
69
         */
 
70
        private TGTupleto tupleto;
 
71
        
 
72
        public TGDuration(TGFactory factory){
 
73
                this.value = QUARTER;
 
74
                this.dotted = false;
 
75
                this.doubleDotted = false;
 
76
                this.tupleto = factory.newTupleto();
 
77
        }
 
78
        
 
79
        public int getValue() {
 
80
                return this.value;
 
81
        }
 
82
        
 
83
        public void setValue(int value) {
 
84
                this.value = value;
 
85
        }
 
86
        
 
87
        public boolean isDotted() {
 
88
                return this.dotted;
 
89
        }
 
90
        
 
91
        public void setDotted(boolean dotted) {
 
92
                this.dotted = dotted;
 
93
        }
 
94
        
 
95
        public boolean isDoubleDotted() {
 
96
                return this.doubleDotted;
 
97
        }
 
98
        
 
99
        public void setDoubleDotted(boolean doubleDotted) {
 
100
                this.doubleDotted = doubleDotted;
 
101
        }
 
102
        
 
103
        public TGTupleto getTupleto(){
 
104
                return this.tupleto;
 
105
        }
 
106
        
 
107
        public long getTime(){
 
108
                long time = (long)( QUARTER_TIME * ( 4.0f / this.value ) ) ;
 
109
                if(this.dotted){
 
110
                        time += time / 2;
 
111
                }else if(this.doubleDotted){
 
112
                        time += ((time / 4) * 3);
 
113
                }
 
114
                return this.tupleto.convertTime(time);
 
115
        }
 
116
        
 
117
        public static TGDuration fromTime(TGFactory factory,long time){
 
118
                TGDuration duration = factory.newDuration();
 
119
                duration.setValue(TGDuration.SIXTY_FOURTH);
 
120
                duration.setDotted(false);
 
121
                duration.setDoubleDotted(false);
 
122
                duration.getTupleto().setEnters(3);
 
123
                duration.getTupleto().setTimes(2);
 
124
                return fromTime(factory,time,duration);
 
125
        }
 
126
        
 
127
        public static TGDuration fromTime(TGFactory factory,long time,TGDuration minDuration){
 
128
                return fromTime(factory, time, minDuration, 10);
 
129
        }
 
130
        
 
131
        public static TGDuration fromTime(TGFactory factory,long time,TGDuration minimum, int diff){
 
132
                TGDuration duration = minimum.clone(factory);
 
133
                TGDuration tmpDuration = factory.newDuration();
 
134
                tmpDuration.setValue(TGDuration.WHOLE);
 
135
                tmpDuration.setDotted(true);
 
136
                boolean finish = false;
 
137
                while(!finish){
 
138
                        long tmpTime = tmpDuration.getTime();
 
139
                        if(tmpTime - diff <= time){
 
140
                                //if(tmpTime > duration.getTime()){
 
141
                                if(Math.abs( tmpTime - time ) < Math.abs( duration.getTime() - time ) ){
 
142
                                        duration = tmpDuration.clone(factory);
 
143
                                }
 
144
                        }
 
145
                        if(tmpDuration.isDotted()){
 
146
                                tmpDuration.setDotted(false);
 
147
                        }else if(tmpDuration.getTupleto().isEqual(TGTupleto.NORMAL)){
 
148
                                tmpDuration.getTupleto().setEnters(3);
 
149
                                tmpDuration.getTupleto().setTimes(2);
 
150
                        }else{
 
151
                                tmpDuration.setValue(tmpDuration.getValue() * 2);
 
152
                                tmpDuration.setDotted(true);
 
153
                                tmpDuration.getTupleto().setEnters(1);
 
154
                                tmpDuration.getTupleto().setTimes(1);
 
155
                        }
 
156
                        if(tmpDuration.getValue() > TGDuration.SIXTY_FOURTH){
 
157
                                finish = true;
 
158
                        }
 
159
                }
 
160
                return duration;
 
161
        }
 
162
        /*
 
163
        public int log2(){
 
164
                return (int)((Math.log(getValue() ) / Math.log(2.0)) + 0.5f );
 
165
        }
 
166
        */
 
167
        public int getIndex(){
 
168
                int index = 0;
 
169
                int value = this.value;
 
170
                while( ( value = ( value >> 1 ) ) > 0 ){
 
171
                        index ++;
 
172
                }
 
173
                return index;
 
174
        }
 
175
        
 
176
        public boolean isEqual(TGDuration d){
 
177
                return (getValue() == d.getValue() && isDotted() == d.isDotted() && isDoubleDotted() == d.isDoubleDotted() && getTupleto().isEqual(d.getTupleto()));
 
178
        }
 
179
        
 
180
        public TGDuration clone(TGFactory factory){
 
181
                TGDuration duration = factory.newDuration();
 
182
                copy(duration);
 
183
                return duration;
 
184
        }
 
185
        
 
186
        public void copy(TGDuration duration){
 
187
                duration.setValue(getValue());
 
188
                duration.setDotted(isDotted());
 
189
                duration.setDoubleDotted(isDoubleDotted());
 
190
                getTupleto().copy(duration.getTupleto());
 
191
        }
 
192
        
 
193
}