1 /*
   2  * Copyright (c) 2007, 2014, 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  * 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])) {
 141                             out[i][0] = 0;
 142                             active[i][0] = 0;
 143                             stage[i] = EG_END;
 144                             continue;
 145                         }
 146 
 147                         stage_count[i] = (int)(Math.pow(2,
 148                                 this.release[i][0] / 1200.0) / control_time);
 149                         stage_count[i]
 150                                 += (int)(this.release2[i][0]/(control_time * 1000));
 151                         if (stage_count[i] < 0)
 152                             stage_count[i] = 0;
 153                         // stage_v[i] = out[i][0];
 154                         stage_ix[i] = 0;
 155 
 156                         double m = 1 - out[i][0];
 157                         stage_ix[i] = (int)(stage_count[i] * m);
 158 
 159                         stage[i] = EG_RELEASE;
 160                     }
 161                 }
 162             }
 163 
 164             switch (stage[i]) {
 165             case EG_OFF:
 166                 active[i][0] = 1;
 167                 if (on[i][0] < 0.5)
 168                     break;
 169                 stage[i] = EG_DELAY;
 170                 stage_ix[i] = (int)(Math.pow(2,
 171                         this.delay[i][0] / 1200.0) / control_time);
 172                 if (stage_ix[i] < 0)
 173                     stage_ix[i] = 0;
 174                 // Fallthrough
 175             case EG_DELAY:
 176                 if (stage_ix[i] == 0) {
 177                     double attack = this.attack[i][0];
 178                     double attack2 = this.attack2[i][0];
 179 
 180                     if (attack2 < 0.000001
 181                             && (attack < 0 && Double.isInfinite(attack))) {
 182                         out[i][0] = 1;
 183                         stage[i] = EG_HOLD;
 184                         stage_count[i] = (int)(Math.pow(2,
 185                                 this.hold[i][0] / 1200.0) / control_time);
 186                         stage_ix[i] = 0;
 187                     } else {
 188                         stage[i] = EG_ATTACK;
 189                         stage_count[i] = (int)(Math.pow(2,
 190                                 attack / 1200.0) / control_time);
 191                         stage_count[i] += (int)(attack2 / (control_time * 1000));
 192                         if (stage_count[i] < 0)
 193                             stage_count[i] = 0;
 194                         stage_ix[i] = 0;
 195                     }
 196                 } else
 197                     stage_ix[i]--;
 198                 break;
 199             case EG_ATTACK:
 200                 stage_ix[i]++;
 201                 if (stage_ix[i] >= stage_count[i]) {
 202                     out[i][0] = 1;
 203                     stage[i] = EG_HOLD;
 204                 } else {
 205                     // CONVEX attack
 206                     double a = ((double)stage_ix[i]) / ((double)stage_count[i]);
 207                     a = 1 + ((40.0 / 96.0) / Math.log(10)) * Math.log(a);
 208                     if (a < 0)
 209                         a = 0;
 210                     else if (a > 1)
 211                         a = 1;
 212                     out[i][0] = a;
 213                 }
 214                 break;
 215             case EG_HOLD:
 216                 stage_ix[i]++;
 217                 if (stage_ix[i] >= stage_count[i]) {
 218                     stage[i] = EG_DECAY;
 219                     stage_count[i] = (int)(Math.pow(2,
 220                             this.decay[i][0] / 1200.0) / control_time);
 221                     stage_count[i] += (int)(this.decay2[i][0]/(control_time*1000));
 222                     if (stage_count[i] < 0)
 223                         stage_count[i] = 0;
 224                     stage_ix[i] = 0;
 225                 }
 226                 break;
 227             case EG_DECAY:
 228                 stage_ix[i]++;
 229                 double sustain = this.sustain[i][0] * (1.0 / 1000.0);
 230                 if (stage_ix[i] >= stage_count[i]) {
 231                     out[i][0] = sustain;
 232                     stage[i] = EG_SUSTAIN;
 233                     if (sustain < 0.001) {
 234                         out[i][0] = 0;
 235                         active[i][0] = 0;
 236                         stage[i] = EG_END;
 237                     }
 238                 } else {
 239                     double m = ((double)stage_ix[i]) / ((double)stage_count[i]);
 240                     out[i][0] = (1 - m) + sustain * m;
 241                 }
 242                 break;
 243             case EG_SUSTAIN:
 244                 break;
 245             case EG_RELEASE:
 246                 stage_ix[i]++;
 247                 if (stage_ix[i] >= stage_count[i]) {
 248                     out[i][0] = 0;
 249                     active[i][0] = 0;
 250                     stage[i] = EG_END;
 251                 } else {
 252                     double m = ((double)stage_ix[i]) / ((double)stage_count[i]);
 253                     out[i][0] = (1 - m); // *stage_v[i];
 254 
 255                     if (on[i][0] < -0.5) {
 256                         stage_count[i] = (int)(Math.pow(2,
 257                                 this.shutdown[i][0] / 1200.0) / control_time);
 258                         if (stage_count[i] < 0)
 259                             stage_count[i] = 0;
 260                         stage_v[i] = out[i][0];
 261                         stage_ix[i] = 0;
 262                         stage[i] = EG_SHUTDOWN;
 263                     }
 264 
 265                     // re-damping
 266                     if (on[i][0] > 0.5) {
 267                         sustain = this.sustain[i][0] * (1.0 / 1000.0);
 268                         if (out[i][0] > sustain) {
 269                             stage[i] = EG_DECAY;
 270                             stage_count[i] = (int)(Math.pow(2,
 271                                     this.decay[i][0] / 1200.0) / control_time);
 272                             stage_count[i] +=
 273                                     (int)(this.decay2[i][0]/(control_time*1000));
 274                             if (stage_count[i] < 0)
 275                                 stage_count[i] = 0;
 276                             m = (out[i][0] - 1) / (sustain - 1);
 277                             stage_ix[i] = (int) (stage_count[i] * m);
 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 }