1 /*
   2  * Copyright (c) 2007, 2013, 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 import java.util.Arrays;
  29 
  30 /**
  31  * Connection blocks are used to connect source variable
  32  * to a destination variable.
  33  * For example Note On velocity can be connected to output gain.
  34  * In DLS this is called articulator and in SoundFonts (SF2) a modulator.
  35  *
  36  * @author Karl Helgason
  37  */
  38 public final class ModelConnectionBlock {
  39 
  40     //
  41     //   source1 * source2 * scale -> destination
  42     //
  43     private static final ModelSource[] no_sources = new ModelSource[0];
  44     private ModelSource[] sources = no_sources;
  45     private double scale = 1;
  46     private ModelDestination destination;
  47 
  48     public ModelConnectionBlock() {
  49     }
  50 
  51     public ModelConnectionBlock(double scale, ModelDestination destination) {
  52         this.scale = scale;
  53         this.destination = destination;
  54     }
  55 
  56     public ModelConnectionBlock(ModelSource source,
  57             ModelDestination destination) {
  58         if (source != null) {
  59             this.sources = new ModelSource[1];
  60             this.sources[0] = source;
  61         }
  62         this.destination = destination;
  63     }
  64 
  65     public ModelConnectionBlock(ModelSource source, double scale,
  66             ModelDestination destination) {
  67         if (source != null) {
  68             this.sources = new ModelSource[1];
  69             this.sources[0] = source;
  70         }
  71         this.scale = scale;
  72         this.destination = destination;
  73     }
  74 
  75     public ModelConnectionBlock(ModelSource source, ModelSource control,
  76             ModelDestination destination) {
  77         if (source != null) {
  78             if (control == null) {
  79                 this.sources = new ModelSource[1];
  80                 this.sources[0] = source;
  81             } else {
  82                 this.sources = new ModelSource[2];
  83                 this.sources[0] = source;
  84                 this.sources[1] = control;
  85             }
  86         }
  87         this.destination = destination;
  88     }
  89 
  90     public ModelConnectionBlock(ModelSource source, ModelSource control,
  91             double scale, ModelDestination destination) {
  92         if (source != null) {
  93             if (control == null) {
  94                 this.sources = new ModelSource[1];
  95                 this.sources[0] = source;
  96             } else {
  97                 this.sources = new ModelSource[2];
  98                 this.sources[0] = source;
  99                 this.sources[1] = control;
 100             }
 101         }
 102         this.scale = scale;
 103         this.destination = destination;
 104     }
 105 
 106     public ModelDestination getDestination() {
 107         return destination;
 108     }
 109 
 110     public void setDestination(ModelDestination destination) {
 111         this.destination = destination;
 112     }
 113 
 114     public double getScale() {
 115         return scale;
 116     }
 117 
 118     public void setScale(double scale) {
 119         this.scale = scale;
 120     }
 121 
 122     public ModelSource[] getSources() {
 123         return Arrays.copyOf(sources, sources.length);
 124     }
 125 
 126     public void setSources(ModelSource[] source) {
 127         this.sources = source == null ? no_sources : Arrays.copyOf(source, source.length);
 128     }
 129 
 130     public void addSource(ModelSource source) {
 131         ModelSource[] oldsources = sources;
 132         sources = new ModelSource[oldsources.length + 1];
 133         System.arraycopy(oldsources, 0, sources, 0, oldsources.length);
 134         sources[sources.length - 1] = source;
 135     }
 136 }