annotate src/uk/ac/qmul/eecs/depic/daw/Selection.java @ 0:3074a84ef81e

first import
author Fiore Martin <f.martin@qmul.ac.uk>
date Wed, 26 Aug 2015 16:16:53 +0100
parents
children 629262395647
rev   line source
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@0 23
f@0 24 public final class Selection extends Range<Integer>{
f@0 25 public static Selection ZERO_SELECTION = new Selection(0,1);
f@0 26 private int scaleFactor;
f@0 27 private boolean open;
f@0 28
f@0 29 /**
f@0 30 * Constructs a new instance of {@code LoopPoints} from the bound values passed as argument.
f@0 31 *
f@0 32 * 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 33 * If the two values are equals the loop is assumed to have no end but only a starting point.
f@0 34 *
f@0 35 * @param start the start of the range, it must be greater or equal than 0
f@0 36 * @param end the end of the range, it must be either greater or equal than 0 or equal to {@code NO_POSITION}
f@0 37 * @param scaleFactor
f@0 38 *
f@0 39 * @throws IndexOutOfBoundsException whether {@code start} is a negative
f@0 40 * value or {@code end} is a negative value different from {@code NO_POSITION}
f@0 41 * @throws IllegalArgumentException if {@code end} is greater than {@code start}
f@0 42 */
f@0 43 public Selection(int start, int end, int scaleFactor) {
f@0 44 super(start,end);
f@0 45 if(start < 0 && !equals(ZERO_SELECTION)){
f@0 46 throw new IndexOutOfBoundsException("bounds value must be a positive integer . start:"+start);
f@0 47 }
f@0 48 if(end < 0){
f@0 49 throw new IndexOutOfBoundsException("bounds value must be a positive integer end:"+end);
f@0 50 }
f@0 51 if(start > end ){
f@0 52 throw new IllegalArgumentException("start cannot be greater than end. start:"+start+" end:"+end);
f@0 53 }
f@0 54
f@0 55 open = false;
f@0 56 this.scaleFactor = scaleFactor; // FIXME controls on scaleFactor
f@0 57 }
f@0 58
f@0 59 public Selection(int start, int scaleFactor){
f@0 60 super(start,start);
f@0 61 if(start < 0){
f@0 62 throw new IndexOutOfBoundsException("bounds value must be a positive integer . start:"+start);
f@0 63 }
f@0 64
f@0 65 this.scaleFactor = scaleFactor;
f@0 66 open = true;
f@0 67 }
f@0 68
f@0 69 /**
f@0 70 * Returns the end value of the loop
f@0 71 *
f@0 72 * @return the end point or -1 if the loop has only the start and no end to indicate the final frame.
f@0 73 * This value is consistent with the {@code Clip interface}
f@0 74 *
f@0 75 * @see javax.sound.sampled.Clip#setLoopPoints(int, int)
f@0 76 */
f@0 77 @Override
f@0 78 public final Integer getEnd(){
f@0 79 if(open)
f@0 80 return -1;
f@0 81 else
f@0 82 return super.getEnd();
f@0 83 }
f@0 84
f@0 85 /**
f@0 86 * Returns the scale factor of this selection
f@0 87 *
f@0 88 * @return the scale factor of this selection
f@0 89 */
f@0 90 public int getScaleFactor(){
f@0 91 return scaleFactor;
f@0 92 }
f@0 93
f@0 94 /**
f@0 95 * Checks whether the selection defines an open or closed interval.
f@0 96 *
f@0 97 * The selection is open is the end is equal to {@code NO_POSITION}.
f@0 98 *
f@0 99 * @return {@code true} is the selection is open, {@code false} otherwise
f@0 100 */
f@0 101 public boolean isOpen(){
f@0 102 return open;
f@0 103 }
f@0 104
f@0 105
f@0 106
f@0 107 /**
f@0 108 * Returns {@code true} if {@code p} is contained in the selection ( p is greater than {@code getStart()}
f@0 109 * and smaller than {@code getEnd()} ).
f@0 110 *
f@0 111 * If the selection is open, {@code false} is returned.
f@0 112 *
f@0 113 * @param p the integer vale to check for
f@0 114 *
f@0 115 * @return @return {@code true} if {@code p} is contained in the selection, {@code false} otherwise
f@0 116 */
f@0 117 public boolean contains(int p){
f@0 118 if(isOpen()){
f@0 119 return false;
f@0 120 }else{
f@0 121 return ((p>=getStart()) && (p<=getEnd()));
f@0 122 }
f@0 123
f@0 124 }
f@0 125
f@0 126 @Override
f@0 127 public String toString(){
f@0 128 if(open)
f@0 129 return getClass().getSimpleName()+" ["+getStart()+",inf), scale factor:"+scaleFactor;
f@0 130 else
f@0 131 return getClass().getSimpleName()+" ["+getStart()+","+getEnd()+"], scale factor:"+scaleFactor;
f@0 132 }
f@0 133
f@0 134 /**
f@0 135 * Convert a selection to another selection with a new scale factor.
f@0 136 *
f@0 137 * The {@code start} and {@code end} of the selection are changed accordingly.
f@0 138 *
f@0 139 * @param s the selection to convert
f@0 140 * @param newFactor the factor of the converted selection
f@0 141 * @return {@code s} if {@code newFactor} is already the scale factor of {@code s}, a new selection
f@0 142 *
f@0 143 */
f@0 144 public static Selection convertFactor(Selection s, int newFactor){
f@0 145 if(s.getScaleFactor() == newFactor)
f@0 146 return s;
f@0 147 int newStart = (int)(s.getStart() * Math.pow(2,s.getScaleFactor()-newFactor));
f@0 148 if(!s.isOpen()){
f@0 149 int newEnd = (int)(s.getEnd() * Math.pow(2,s.getScaleFactor()-newFactor));
f@0 150 return new Selection(newStart,newEnd,newFactor);
f@0 151 }else{
f@0 152 return new Selection(newStart,newFactor);
f@0 153 }
f@0 154 }
f@0 155
f@0 156 public static int convertFactor(int position, int fromFactor, int toFactor){
f@0 157 if(fromFactor == toFactor){
f@0 158 return position;
f@0 159 }
f@0 160
f@0 161 return (int)(position * Math.pow(2,fromFactor-toFactor));
f@0 162 }
f@0 163
f@0 164 }
f@0 165