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