Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]
    1   /*
    2    * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   package com.sun.media.sound;
   26   
   27   /**
   28    * A standard indexed director who chooses performers
   29    * by there keyfrom,keyto,velfrom,velto properties.
   30    *
   31    * @author Karl Helgason
   32    */
   33   public class ModelStandardIndexedDirector implements ModelDirector {
   34   
   35       ModelPerformer[] performers;
   36       ModelDirectedPlayer player;
   37       boolean noteOnUsed = false;
   38       boolean noteOffUsed = false;
   39   
   40       // Variables needed for index
   41       byte[][] trantables;
   42       int[] counters;
   43       int[][] mat;
   44   
   45       public ModelStandardIndexedDirector(ModelPerformer[] performers,
   46               ModelDirectedPlayer player) {
   47           this.performers = performers;
   48           this.player = player;
   49           for (int i = 0; i < performers.length; i++) {
   50               ModelPerformer p = performers[i];
   51               if (p.isReleaseTriggered()) {
   52                   noteOffUsed = true;
   53               } else {
   54                   noteOnUsed = true;
   55               }
   56           }
   57           buildindex();
   58       }
   59   
   60       private int[] lookupIndex(int x, int y) {
   61           if ((x >= 0) && (x < 128) && (y >= 0) && (y < 128)) {
   62               int xt = trantables[0][x];
   63               int yt = trantables[1][y];
   64               if (xt != -1 && yt != -1) {
   65                   return mat[xt + yt * counters[0]];
   66               }
   67           }
   68           return null;
   69       }
   70   
   71       private int restrict(int value) {
   72           if(value < 0) return 0;
   73           if(value > 127) return 127;
   74           return value;
   75       }
   76   
   77       private void buildindex() {
   78           trantables = new byte[2][129];
   79           counters = new int[trantables.length];
   80           for (ModelPerformer performer : performers) {
   81               int keyFrom = performer.getKeyFrom();
   82               int keyTo = performer.getKeyTo();
   83               int velFrom = performer.getVelFrom();
   84               int velTo = performer.getVelTo();
   85               if (keyFrom > keyTo) continue;
   86               if (velFrom > velTo) continue;
   87               keyFrom = restrict(keyFrom);
   88               keyTo = restrict(keyTo);
   89               velFrom = restrict(velFrom);
   90               velTo = restrict(velTo);
   91               trantables[0][keyFrom] = 1;
   92               trantables[0][keyTo + 1] = 1;
   93               trantables[1][velFrom] = 1;
   94               trantables[1][velTo + 1] = 1;
   95           }
   96           for (int d = 0; d < trantables.length; d++) {
   97               byte[] trantable = trantables[d];
   98               int transize = trantable.length;
   99               for (int i = transize - 1; i >= 0; i--) {
  100                   if (trantable[i] == 1) {
  101                       trantable[i] = -1;
  102                       break;
  103                   }
  104                   trantable[i] = -1;
  105               }
  106               int counter = -1;
  107               for (int i = 0; i < transize; i++) {
  108                   if (trantable[i] != 0) {
  109                       counter++;
  110                       if (trantable[i] == -1)
  111                           break;
  112                   }
  113                   trantable[i] = (byte) counter;
  114               }
  115               counters[d] = counter;
  116           }
  117           mat = new int[counters[0] * counters[1]][];
  118           int ix = 0;
  119           for (ModelPerformer performer : performers) {
  120               int keyFrom = performer.getKeyFrom();
  121               int keyTo = performer.getKeyTo();
  122               int velFrom = performer.getVelFrom();
  123               int velTo = performer.getVelTo();
  124               if (keyFrom > keyTo) continue;
  125               if (velFrom > velTo) continue;
  126               keyFrom = restrict(keyFrom);
  127               keyTo = restrict(keyTo);
  128               velFrom = restrict(velFrom);
  129               velTo = restrict(velTo);
  130               int x_from = trantables[0][keyFrom];
  131               int x_to = trantables[0][keyTo + 1];
  132               int y_from = trantables[1][velFrom];
  133               int y_to = trantables[1][velTo + 1];
  134               if (x_to == -1)
  135                   x_to = counters[0];
  136               if (y_to == -1)
  137                   y_to = counters[1];
  138               for (int y = y_from; y < y_to; y++) {
  139                   int i = x_from + y * counters[0];
  140                   for (int x = x_from; x < x_to; x++) {
  141                       int[] mprev = mat[i];
  142                       if (mprev == null) {
  143                           mat[i] = new int[] { ix };
  144                       } else {
  145                           int[] mnew = new int[mprev.length + 1];
  146                           mnew[mnew.length - 1] = ix;
  147                           for (int k = 0; k < mprev.length; k++)
  148                               mnew[k] = mprev[k];
  149                           mat[i] = mnew;
  150                       }
  151                       i++;
  152                   }
  153               }
  154               ix++;
  155           }
  156       }
  157   
  158       public void close() {
  159       }
  160   
  161       public void noteOff(int noteNumber, int velocity) {
  162           if (!noteOffUsed)
  163               return;
  164           int[] plist = lookupIndex(noteNumber, velocity);
  165           if(plist == null) return;
  166           for (int i : plist) {
  167               ModelPerformer p = performers[i];
  168               if (p.isReleaseTriggered()) {
  169                   player.play(i, null);
  170               }
  171           }
  172       }
  173   
  174       public void noteOn(int noteNumber, int velocity) {
  175           if (!noteOnUsed)
  176               return;
  177           int[] plist = lookupIndex(noteNumber, velocity);
  178           if(plist == null) return;
  179           for (int i : plist) {
  180               ModelPerformer p = performers[i];
  181               if (!p.isReleaseTriggered()) {
  182                   player.play(i, null);
  183               }
  184           }
  185       }
  186   }

Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]