f@0: /*
f@0: Cross-Modal DAW Prototype - Prototype of a simple Cross-Modal Digital Audio Workstation.
f@0:
f@0: Copyright (C) 2015 Queen Mary University of London (http://depic.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0: package uk.ac.qmul.eecs.depic.daw;
f@0:
f@0: import uk.ac.qmul.eecs.depic.patterns.Range;
f@0:
f@1: /**
f@1: *
f@1: * A selection on integer values
f@1: *
f@1: */
f@0: public final class Selection extends Range{
f@0: public static Selection ZERO_SELECTION = new Selection(0,1);
f@0: private int scaleFactor;
f@0: private boolean open;
f@0:
f@0: /**
f@0: * Constructs a new instance of {@code LoopPoints} from the bound values passed as argument.
f@0: *
f@0: * 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: * If the two values are equals the loop is assumed to have no end but only a starting point.
f@0: *
f@0: * @param start the start of the range, it must be greater or equal than 0
f@0: * @param end the end of the range, it must be either greater or equal than 0 or equal to {@code NO_POSITION}
f@0: * @param scaleFactor
f@0: *
f@0: * @throws IndexOutOfBoundsException whether {@code start} is a negative
f@0: * value or {@code end} is a negative value different from {@code NO_POSITION}
f@0: * @throws IllegalArgumentException if {@code end} is greater than {@code start}
f@0: */
f@0: public Selection(int start, int end, int scaleFactor) {
f@0: super(start,end);
f@0: if(start < 0 && !equals(ZERO_SELECTION)){
f@0: throw new IndexOutOfBoundsException("bounds value must be a positive integer . start:"+start);
f@0: }
f@0: if(end < 0){
f@0: throw new IndexOutOfBoundsException("bounds value must be a positive integer end:"+end);
f@0: }
f@0: if(start > end ){
f@0: throw new IllegalArgumentException("start cannot be greater than end. start:"+start+" end:"+end);
f@0: }
f@0:
f@0: open = false;
f@0: this.scaleFactor = scaleFactor; // FIXME controls on scaleFactor
f@0: }
f@0:
f@0: public Selection(int start, int scaleFactor){
f@0: super(start,start);
f@0: if(start < 0){
f@0: throw new IndexOutOfBoundsException("bounds value must be a positive integer . start:"+start);
f@0: }
f@0:
f@0: this.scaleFactor = scaleFactor;
f@0: open = true;
f@0: }
f@0:
f@0: /**
f@0: * Returns the end value of the loop
f@0: *
f@0: * @return the end point or -1 if the loop has only the start and no end to indicate the final frame.
f@0: * This value is consistent with the {@code Clip interface}
f@0: *
f@0: * @see javax.sound.sampled.Clip#setLoopPoints(int, int)
f@0: */
f@0: @Override
f@0: public final Integer getEnd(){
f@0: if(open)
f@0: return -1;
f@0: else
f@0: return super.getEnd();
f@0: }
f@0:
f@0: /**
f@0: * Returns the scale factor of this selection
f@0: *
f@0: * @return the scale factor of this selection
f@0: */
f@0: public int getScaleFactor(){
f@0: return scaleFactor;
f@0: }
f@0:
f@0: /**
f@0: * Checks whether the selection defines an open or closed interval.
f@0: *
f@0: * The selection is open is the end is equal to {@code NO_POSITION}.
f@0: *
f@0: * @return {@code true} is the selection is open, {@code false} otherwise
f@0: */
f@0: public boolean isOpen(){
f@0: return open;
f@0: }
f@0:
f@0:
f@0:
f@0: /**
f@0: * Returns {@code true} if {@code p} is contained in the selection ( p is greater than {@code getStart()}
f@0: * and smaller than {@code getEnd()} ).
f@0: *
f@0: * If the selection is open, {@code false} is returned.
f@0: *
f@0: * @param p the integer vale to check for
f@0: *
f@0: * @return @return {@code true} if {@code p} is contained in the selection, {@code false} otherwise
f@0: */
f@0: public boolean contains(int p){
f@0: if(isOpen()){
f@0: return false;
f@0: }else{
f@0: return ((p>=getStart()) && (p<=getEnd()));
f@0: }
f@0:
f@0: }
f@0:
f@0: @Override
f@0: public String toString(){
f@0: if(open)
f@0: return getClass().getSimpleName()+" ["+getStart()+",inf), scale factor:"+scaleFactor;
f@0: else
f@0: return getClass().getSimpleName()+" ["+getStart()+","+getEnd()+"], scale factor:"+scaleFactor;
f@0: }
f@0:
f@0: /**
f@0: * Convert a selection to another selection with a new scale factor.
f@0: *
f@0: * The {@code start} and {@code end} of the selection are changed accordingly.
f@0: *
f@0: * @param s the selection to convert
f@0: * @param newFactor the factor of the converted selection
f@0: * @return {@code s} if {@code newFactor} is already the scale factor of {@code s}, a new selection
f@0: *
f@0: */
f@0: public static Selection convertFactor(Selection s, int newFactor){
f@0: if(s.getScaleFactor() == newFactor)
f@0: return s;
f@0: int newStart = (int)(s.getStart() * Math.pow(2,s.getScaleFactor()-newFactor));
f@0: if(!s.isOpen()){
f@0: int newEnd = (int)(s.getEnd() * Math.pow(2,s.getScaleFactor()-newFactor));
f@0: return new Selection(newStart,newEnd,newFactor);
f@0: }else{
f@0: return new Selection(newStart,newFactor);
f@0: }
f@0: }
f@0:
f@0: public static int convertFactor(int position, int fromFactor, int toFactor){
f@0: if(fromFactor == toFactor){
f@0: return position;
f@0: }
f@0:
f@0: return (int)(position * Math.pow(2,fromFactor-toFactor));
f@0: }
f@0:
f@0: }
f@0: