jdk/src/share/classes/com/sun/media/sound/ModelStandardIndexedDirector.java
changeset 6502 13b20559a04a
child 6503 600ab3437bb8
equal deleted inserted replaced
6501:684810d882b3 6502:13b20559a04a
       
     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     {
       
    62         if ((x >= 0) && (x < 128) && (y >= 0) && (y < 128)) {
       
    63             int xt = trantables[0][x];
       
    64             int yt = trantables[1][y];
       
    65             if (xt != -1 && yt != -1) {
       
    66                 return mat[xt + yt * counters[0]];
       
    67             }
       
    68         }
       
    69         return null;
       
    70     }
       
    71 
       
    72     private void buildindex() {
       
    73         trantables = new byte[2][129];
       
    74         counters = new int[trantables.length];
       
    75         for (ModelPerformer performer : performers) {
       
    76             trantables[0][performer.getKeyFrom()] = 1;
       
    77             trantables[0][performer.getKeyTo() + 1] = 1;
       
    78             trantables[1][performer.getVelFrom()] = 1;
       
    79             trantables[1][performer.getVelTo() + 1] = 1;
       
    80         }
       
    81         for (int d = 0; d < trantables.length; d++) {
       
    82             byte[] trantable = trantables[d];
       
    83             int transize = trantable.length;
       
    84             for (int i = transize - 1; i >= 0; i--) {
       
    85                 if (trantable[i] == 1) {
       
    86                     trantable[i] = -1;
       
    87                     break;
       
    88                 }
       
    89                 trantable[i] = -1;
       
    90             }
       
    91             int counter = -1;
       
    92             for (int i = 0; i < transize; i++) {
       
    93                 if (trantable[i] != 0) {
       
    94                     counter++;
       
    95                     if (trantable[i] == -1)
       
    96                         break;
       
    97                 }
       
    98                 trantable[i] = (byte) counter;
       
    99             }
       
   100             counters[d] = counter;
       
   101         }
       
   102         mat = new int[counters[0] * counters[1]][];
       
   103         int ix = 0;
       
   104         for (ModelPerformer performer : performers) {
       
   105             int x_from = trantables[0][performer.getKeyFrom()];
       
   106             int x_to = trantables[0][performer.getKeyTo() + 1];
       
   107             int y_from = trantables[1][performer.getVelFrom()];
       
   108             int y_to = trantables[1][performer.getVelTo() + 1];
       
   109             if (x_to == -1)
       
   110                 x_to = counters[0];
       
   111             if (y_to == -1)
       
   112                 y_to = counters[1];
       
   113             for (int y = y_from; y < y_to; y++) {
       
   114                 int i = x_from + y * counters[0];
       
   115                 for (int x = x_from; x < x_to; x++) {
       
   116                     int[] mprev = mat[i];
       
   117                     if (mprev == null) {
       
   118                         mat[i] = new int[] { ix };
       
   119                     } else {
       
   120                         int[] mnew = new int[mprev.length + 1];
       
   121                         mnew[mnew.length - 1] = ix;
       
   122                         for (int k = 0; k < mprev.length; k++)
       
   123                             mnew[k] = mprev[k];
       
   124                         mat[i] = mnew;
       
   125                     }
       
   126                     i++;
       
   127                 }
       
   128             }
       
   129             ix++;
       
   130         }
       
   131     }
       
   132 
       
   133     public void close() {
       
   134     }
       
   135 
       
   136     public void noteOff(int noteNumber, int velocity) {
       
   137         if (!noteOffUsed)
       
   138             return;
       
   139         int[] plist = lookupIndex(noteNumber, velocity);
       
   140         if(plist == null) return;
       
   141         for (int i : plist) {
       
   142             ModelPerformer p = performers[i];
       
   143             if (p.isReleaseTriggered()) {
       
   144                 player.play(i, null);
       
   145             }
       
   146         }
       
   147     }
       
   148 
       
   149     public void noteOn(int noteNumber, int velocity) {
       
   150         if (!noteOnUsed)
       
   151             return;
       
   152         int[] plist = lookupIndex(noteNumber, velocity);
       
   153         if(plist == null) return;
       
   154         for (int i : plist) {
       
   155             ModelPerformer p = performers[i];
       
   156             if (!p.isReleaseTriggered()) {
       
   157                 player.play(i, null);
       
   158             }
       
   159         }
       
   160     }
       
   161 }