f@0
|
1 /*
|
f@0
|
2 Cross-Modal DAW Prototype - Prototype of a simple Cross-Modal Digital Audio Workstation.
|
f@0
|
3
|
f@0
|
4 Copyright (C) 2015 Queen Mary University of London (http://depic.eecs.qmul.ac.uk/)
|
f@0
|
5
|
f@0
|
6 This program is free software: you can redistribute it and/or modify
|
f@0
|
7 it under the terms of the GNU General Public License as published by
|
f@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
f@0
|
9 (at your option) any later version.
|
f@0
|
10
|
f@0
|
11 This program is distributed in the hope that it will be useful,
|
f@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@0
|
14 GNU General Public License for more details.
|
f@0
|
15
|
f@0
|
16 You should have received a copy of the GNU General Public License
|
f@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@0
|
18 */
|
f@0
|
19 package uk.ac.qmul.eecs.depic.daw;
|
f@0
|
20
|
f@0
|
21 import uk.ac.qmul.eecs.depic.patterns.Range;
|
f@0
|
22
|
f@1
|
23 /**
|
f@1
|
24 *
|
f@1
|
25 * A selection on integer values
|
f@1
|
26 *
|
f@1
|
27 */
|
f@0
|
28 public final class Selection extends Range<Integer>{
|
f@0
|
29 public static Selection ZERO_SELECTION = new Selection(0,1);
|
f@0
|
30 private int scaleFactor;
|
f@0
|
31 private boolean open;
|
f@0
|
32
|
f@0
|
33 /**
|
f@0
|
34 * Constructs a new instance of {@code LoopPoints} from the bound values passed as argument.
|
f@0
|
35 *
|
f@0
|
36 * The lower value is assigned to the start of the loop, whereas the higher value is assigned to the end of the loop.
|
f@0
|
37 * If the two values are equals the loop is assumed to have no end but only a starting point.
|
f@0
|
38 *
|
f@0
|
39 * @param start the start of the range, it must be greater or equal than 0
|
f@0
|
40 * @param end the end of the range, it must be either greater or equal than 0 or equal to {@code NO_POSITION}
|
f@0
|
41 * @param scaleFactor
|
f@0
|
42 *
|
f@0
|
43 * @throws IndexOutOfBoundsException whether {@code start} is a negative
|
f@0
|
44 * value or {@code end} is a negative value different from {@code NO_POSITION}
|
f@0
|
45 * @throws IllegalArgumentException if {@code end} is greater than {@code start}
|
f@0
|
46 */
|
f@0
|
47 public Selection(int start, int end, int scaleFactor) {
|
f@0
|
48 super(start,end);
|
f@0
|
49 if(start < 0 && !equals(ZERO_SELECTION)){
|
f@0
|
50 throw new IndexOutOfBoundsException("bounds value must be a positive integer . start:"+start);
|
f@0
|
51 }
|
f@0
|
52 if(end < 0){
|
f@0
|
53 throw new IndexOutOfBoundsException("bounds value must be a positive integer end:"+end);
|
f@0
|
54 }
|
f@0
|
55 if(start > end ){
|
f@0
|
56 throw new IllegalArgumentException("start cannot be greater than end. start:"+start+" end:"+end);
|
f@0
|
57 }
|
f@0
|
58
|
f@0
|
59 open = false;
|
f@0
|
60 this.scaleFactor = scaleFactor; // FIXME controls on scaleFactor
|
f@0
|
61 }
|
f@0
|
62
|
f@0
|
63 public Selection(int start, int scaleFactor){
|
f@0
|
64 super(start,start);
|
f@0
|
65 if(start < 0){
|
f@0
|
66 throw new IndexOutOfBoundsException("bounds value must be a positive integer . start:"+start);
|
f@0
|
67 }
|
f@0
|
68
|
f@0
|
69 this.scaleFactor = scaleFactor;
|
f@0
|
70 open = true;
|
f@0
|
71 }
|
f@0
|
72
|
f@0
|
73 /**
|
f@0
|
74 * Returns the end value of the loop
|
f@0
|
75 *
|
f@0
|
76 * @return the end point or -1 if the loop has only the start and no end to indicate the final frame.
|
f@0
|
77 * This value is consistent with the {@code Clip interface}
|
f@0
|
78 *
|
f@0
|
79 * @see javax.sound.sampled.Clip#setLoopPoints(int, int)
|
f@0
|
80 */
|
f@0
|
81 @Override
|
f@0
|
82 public final Integer getEnd(){
|
f@0
|
83 if(open)
|
f@0
|
84 return -1;
|
f@0
|
85 else
|
f@0
|
86 return super.getEnd();
|
f@0
|
87 }
|
f@0
|
88
|
f@0
|
89 /**
|
f@0
|
90 * Returns the scale factor of this selection
|
f@0
|
91 *
|
f@0
|
92 * @return the scale factor of this selection
|
f@0
|
93 */
|
f@0
|
94 public int getScaleFactor(){
|
f@0
|
95 return scaleFactor;
|
f@0
|
96 }
|
f@0
|
97
|
f@0
|
98 /**
|
f@0
|
99 * Checks whether the selection defines an open or closed interval.
|
f@0
|
100 *
|
f@0
|
101 * The selection is open is the end is equal to {@code NO_POSITION}.
|
f@0
|
102 *
|
f@0
|
103 * @return {@code true} is the selection is open, {@code false} otherwise
|
f@0
|
104 */
|
f@0
|
105 public boolean isOpen(){
|
f@0
|
106 return open;
|
f@0
|
107 }
|
f@0
|
108
|
f@0
|
109
|
f@0
|
110
|
f@0
|
111 /**
|
f@0
|
112 * Returns {@code true} if {@code p} is contained in the selection ( p is greater than {@code getStart()}
|
f@0
|
113 * and smaller than {@code getEnd()} ).
|
f@0
|
114 *
|
f@0
|
115 * If the selection is open, {@code false} is returned.
|
f@0
|
116 *
|
f@0
|
117 * @param p the integer vale to check for
|
f@0
|
118 *
|
f@0
|
119 * @return @return {@code true} if {@code p} is contained in the selection, {@code false} otherwise
|
f@0
|
120 */
|
f@0
|
121 public boolean contains(int p){
|
f@0
|
122 if(isOpen()){
|
f@0
|
123 return false;
|
f@0
|
124 }else{
|
f@0
|
125 return ((p>=getStart()) && (p<=getEnd()));
|
f@0
|
126 }
|
f@0
|
127
|
f@0
|
128 }
|
f@0
|
129
|
f@0
|
130 @Override
|
f@0
|
131 public String toString(){
|
f@0
|
132 if(open)
|
f@0
|
133 return getClass().getSimpleName()+" ["+getStart()+",inf), scale factor:"+scaleFactor;
|
f@0
|
134 else
|
f@0
|
135 return getClass().getSimpleName()+" ["+getStart()+","+getEnd()+"], scale factor:"+scaleFactor;
|
f@0
|
136 }
|
f@0
|
137
|
f@0
|
138 /**
|
f@0
|
139 * Convert a selection to another selection with a new scale factor.
|
f@0
|
140 *
|
f@0
|
141 * The {@code start} and {@code end} of the selection are changed accordingly.
|
f@0
|
142 *
|
f@0
|
143 * @param s the selection to convert
|
f@0
|
144 * @param newFactor the factor of the converted selection
|
f@0
|
145 * @return {@code s} if {@code newFactor} is already the scale factor of {@code s}, a new selection
|
f@0
|
146 *
|
f@0
|
147 */
|
f@0
|
148 public static Selection convertFactor(Selection s, int newFactor){
|
f@0
|
149 if(s.getScaleFactor() == newFactor)
|
f@0
|
150 return s;
|
f@0
|
151 int newStart = (int)(s.getStart() * Math.pow(2,s.getScaleFactor()-newFactor));
|
f@0
|
152 if(!s.isOpen()){
|
f@0
|
153 int newEnd = (int)(s.getEnd() * Math.pow(2,s.getScaleFactor()-newFactor));
|
f@0
|
154 return new Selection(newStart,newEnd,newFactor);
|
f@0
|
155 }else{
|
f@0
|
156 return new Selection(newStart,newFactor);
|
f@0
|
157 }
|
f@0
|
158 }
|
f@0
|
159
|
f@0
|
160 public static int convertFactor(int position, int fromFactor, int toFactor){
|
f@0
|
161 if(fromFactor == toFactor){
|
f@0
|
162 return position;
|
f@0
|
163 }
|
f@0
|
164
|
f@0
|
165 return (int)(position * Math.pow(2,fromFactor-toFactor));
|
f@0
|
166 }
|
f@0
|
167
|
f@0
|
168 }
|
f@0
|
169
|