< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/SoftEnvelopeGenerator.java

Print this page




   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  * AHDSR control signal envelope generator.
  29  *
  30  * @author Karl Helgason
  31  */
  32 public final class SoftEnvelopeGenerator implements SoftProcess {
  33 
  34     public static final int EG_OFF = 0;
  35     public static final int EG_DELAY = 1;
  36     public static final int EG_ATTACK = 2;
  37     public static final int EG_HOLD = 3;
  38     public static final int EG_DECAY = 4;
  39     public static final int EG_SUSTAIN = 5;
  40     public static final int EG_RELEASE = 6;
  41     public static final int EG_SHUTDOWN = 7;
  42     public static final int EG_END = 8;
  43     int max_count = 10;
  44     int used_count = 0;
  45     private final int[] stage = new int[max_count];
  46     private final int[] stage_ix = new int[max_count];
  47     private final double[] stage_v = new double[max_count];
  48     private final int[] stage_count = new int[max_count];
  49     private final double[][] on = new double[max_count][1];
  50     private final double[][] active = new double[max_count][1];
  51     private final double[][] out = new double[max_count][1];
  52     private final double[][] delay = new double[max_count][1];
  53     private final double[][] attack = new double[max_count][1];
  54     private final double[][] hold = new double[max_count][1];
  55     private final double[][] decay = new double[max_count][1];
  56     private final double[][] sustain = new double[max_count][1];
  57     private final double[][] release = new double[max_count][1];
  58     private final double[][] shutdown = new double[max_count][1];
  59     private final double[][] release2 = new double[max_count][1];
  60     private final double[][] attack2 = new double[max_count][1];
  61     private final double[][] decay2 = new double[max_count][1];
  62     private double control_time = 0;
  63 

  64     public void reset() {
  65         for (int i = 0; i < used_count; i++) {
  66             stage[i] = 0;
  67             on[i][0] = 0;
  68             out[i][0] = 0;
  69             delay[i][0] = 0;
  70             attack[i][0] = 0;
  71             hold[i][0] = 0;
  72             decay[i][0] = 0;
  73             sustain[i][0] = 0;
  74             release[i][0] = 0;
  75             shutdown[i][0] = 0;
  76             attack2[i][0] = 0;
  77             decay2[i][0] = 0;
  78             release2[i][0] = 0;
  79         }
  80         used_count = 0;
  81     }
  82 

  83     public void init(SoftSynthesizer synth) {
  84         control_time = 1.0 / synth.getControlRate();
  85         processControlLogic();
  86     }
  87 

  88     public double[] get(int instance, String name) {
  89         if (instance >= used_count)
  90             used_count = instance + 1;
  91         if (name == null)
  92             return out[instance];
  93         if (name.equals("on"))
  94             return on[instance];
  95         if (name.equals("active"))
  96             return active[instance];
  97         if (name.equals("delay"))
  98             return delay[instance];
  99         if (name.equals("attack"))
 100             return attack[instance];
 101         if (name.equals("hold"))
 102             return hold[instance];
 103         if (name.equals("decay"))
 104             return decay[instance];
 105         if (name.equals("sustain"))
 106             return sustain[instance];
 107         if (name.equals("release"))
 108             return release[instance];
 109         if (name.equals("shutdown"))
 110             return shutdown[instance];
 111         if (name.equals("attack2"))
 112             return attack2[instance];
 113         if (name.equals("decay2"))
 114             return decay2[instance];
 115         if (name.equals("release2"))
 116             return release2[instance];
 117 
 118         return null;
 119     }
 120 

 121     @SuppressWarnings("fallthrough")
 122     public void processControlLogic() {
 123         for (int i = 0; i < used_count; i++) {
 124 
 125             if (stage[i] == EG_END)
 126                 continue;
 127 
 128             if ((stage[i] > EG_OFF) && (stage[i] < EG_RELEASE)) {
 129                 if (on[i][0] < 0.5) {
 130                     if (on[i][0] < -0.5) {
 131                         stage_count[i] = (int)(Math.pow(2,
 132                                 this.shutdown[i][0] / 1200.0) / control_time);
 133                         if (stage_count[i] < 0)
 134                             stage_count[i] = 0;
 135                         stage_v[i] = out[i][0];
 136                         stage_ix[i] = 0;
 137                         stage[i] = EG_SHUTDOWN;
 138                     } else {
 139                         if ((release2[i][0] < 0.000001) && release[i][0] < 0
 140                                 && Double.isInfinite(release[i][0])) {


 278                         }
 279                     }
 280 
 281                 }
 282                 break;
 283             case EG_SHUTDOWN:
 284                 stage_ix[i]++;
 285                 if (stage_ix[i] >= stage_count[i]) {
 286                     out[i][0] = 0;
 287                     active[i][0] = 0;
 288                     stage[i] = EG_END;
 289                 } else {
 290                     double m = ((double)stage_ix[i]) / ((double)stage_count[i]);
 291                     out[i][0] = (1 - m) * stage_v[i];
 292                 }
 293                 break;
 294             default:
 295                 break;
 296             }
 297         }
 298 
 299     }
 300 }


   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 
  26 package com.sun.media.sound;
  27 
  28 /**
  29  * AHDSR control signal envelope generator.
  30  *
  31  * @author Karl Helgason
  32  */
  33 public final class SoftEnvelopeGenerator implements SoftProcess {
  34 
  35     public static final int EG_OFF = 0;
  36     public static final int EG_DELAY = 1;
  37     public static final int EG_ATTACK = 2;
  38     public static final int EG_HOLD = 3;
  39     public static final int EG_DECAY = 4;
  40     public static final int EG_SUSTAIN = 5;
  41     public static final int EG_RELEASE = 6;
  42     public static final int EG_SHUTDOWN = 7;
  43     public static final int EG_END = 8;
  44     int max_count = 10;
  45     int used_count = 0;
  46     private final int[] stage = new int[max_count];
  47     private final int[] stage_ix = new int[max_count];
  48     private final double[] stage_v = new double[max_count];
  49     private final int[] stage_count = new int[max_count];
  50     private final double[][] on = new double[max_count][1];
  51     private final double[][] active = new double[max_count][1];
  52     private final double[][] out = new double[max_count][1];
  53     private final double[][] delay = new double[max_count][1];
  54     private final double[][] attack = new double[max_count][1];
  55     private final double[][] hold = new double[max_count][1];
  56     private final double[][] decay = new double[max_count][1];
  57     private final double[][] sustain = new double[max_count][1];
  58     private final double[][] release = new double[max_count][1];
  59     private final double[][] shutdown = new double[max_count][1];
  60     private final double[][] release2 = new double[max_count][1];
  61     private final double[][] attack2 = new double[max_count][1];
  62     private final double[][] decay2 = new double[max_count][1];
  63     private double control_time = 0;
  64 
  65     @Override
  66     public void reset() {
  67         for (int i = 0; i < used_count; i++) {
  68             stage[i] = 0;
  69             on[i][0] = 0;
  70             out[i][0] = 0;
  71             delay[i][0] = 0;
  72             attack[i][0] = 0;
  73             hold[i][0] = 0;
  74             decay[i][0] = 0;
  75             sustain[i][0] = 0;
  76             release[i][0] = 0;
  77             shutdown[i][0] = 0;
  78             attack2[i][0] = 0;
  79             decay2[i][0] = 0;
  80             release2[i][0] = 0;
  81         }
  82         used_count = 0;
  83     }
  84 
  85     @Override
  86     public void init(SoftSynthesizer synth) {
  87         control_time = 1.0 / synth.getControlRate();
  88         processControlLogic();
  89     }
  90 
  91     @Override
  92     public double[] get(int instance, String name) {
  93         if (instance >= used_count)
  94             used_count = instance + 1;
  95         if (name == null)
  96             return out[instance];
  97         if (name.equals("on"))
  98             return on[instance];
  99         if (name.equals("active"))
 100             return active[instance];
 101         if (name.equals("delay"))
 102             return delay[instance];
 103         if (name.equals("attack"))
 104             return attack[instance];
 105         if (name.equals("hold"))
 106             return hold[instance];
 107         if (name.equals("decay"))
 108             return decay[instance];
 109         if (name.equals("sustain"))
 110             return sustain[instance];
 111         if (name.equals("release"))
 112             return release[instance];
 113         if (name.equals("shutdown"))
 114             return shutdown[instance];
 115         if (name.equals("attack2"))
 116             return attack2[instance];
 117         if (name.equals("decay2"))
 118             return decay2[instance];
 119         if (name.equals("release2"))
 120             return release2[instance];
 121 
 122         return null;
 123     }
 124 
 125     @Override
 126     @SuppressWarnings("fallthrough")
 127     public void processControlLogic() {
 128         for (int i = 0; i < used_count; i++) {
 129 
 130             if (stage[i] == EG_END)
 131                 continue;
 132 
 133             if ((stage[i] > EG_OFF) && (stage[i] < EG_RELEASE)) {
 134                 if (on[i][0] < 0.5) {
 135                     if (on[i][0] < -0.5) {
 136                         stage_count[i] = (int)(Math.pow(2,
 137                                 this.shutdown[i][0] / 1200.0) / control_time);
 138                         if (stage_count[i] < 0)
 139                             stage_count[i] = 0;
 140                         stage_v[i] = out[i][0];
 141                         stage_ix[i] = 0;
 142                         stage[i] = EG_SHUTDOWN;
 143                     } else {
 144                         if ((release2[i][0] < 0.000001) && release[i][0] < 0
 145                                 && Double.isInfinite(release[i][0])) {


 283                         }
 284                     }
 285 
 286                 }
 287                 break;
 288             case EG_SHUTDOWN:
 289                 stage_ix[i]++;
 290                 if (stage_ix[i] >= stage_count[i]) {
 291                     out[i][0] = 0;
 292                     active[i][0] = 0;
 293                     stage[i] = EG_END;
 294                 } else {
 295                     double m = ((double)stage_ix[i]) / ((double)stage_count[i]);
 296                     out[i][0] = (1 - m) * stage_v[i];
 297                 }
 298                 break;
 299             default:
 300                 break;
 301             }
 302         }

 303     }
 304 }
< prev index next >