Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]
    1   /*
    2    * Copyright (c) 2007, 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   import java.util.ArrayList;
   28   import java.util.List;
   29   import javax.sound.midi.Patch;
   30   
   31   /**
   32    * A simple instrument that is made of other ModelInstrument, ModelPerformer
   33    * objects.
   34    *
   35    * @author Karl Helgason
   36    */
   37   public class SimpleInstrument extends ModelInstrument {
   38   
   39       private static class SimpleInstrumentPart {
   40           ModelPerformer[] performers;
   41           int keyFrom;
   42           int keyTo;
   43           int velFrom;
   44           int velTo;
   45           int exclusiveClass;
   46       }
   47       protected int preset = 0;
   48       protected int bank = 0;
   49       protected boolean percussion = false;
   50       protected String name = "";
   51       protected List<SimpleInstrumentPart> parts
   52               = new ArrayList<SimpleInstrumentPart>();
   53   
   54       public SimpleInstrument() {
   55           super(null, null, null, null);
   56       }
   57   
   58       public void clear() {
   59           parts.clear();
   60       }
   61   
   62       public void add(ModelPerformer[] performers, int keyFrom, int keyTo,
   63               int velFrom, int velTo, int exclusiveClass) {
   64           SimpleInstrumentPart part = new SimpleInstrumentPart();
   65           part.performers = performers;
   66           part.keyFrom = keyFrom;
   67           part.keyTo = keyTo;
   68           part.velFrom = velFrom;
   69           part.velTo = velTo;
   70           part.exclusiveClass = exclusiveClass;
   71           parts.add(part);
   72       }
   73   
   74       public void add(ModelPerformer[] performers, int keyFrom, int keyTo,
   75               int velFrom, int velTo) {
   76           add(performers, keyFrom, keyTo, velFrom, velTo, -1);
   77       }
   78   
   79       public void add(ModelPerformer[] performers, int keyFrom, int keyTo) {
   80           add(performers, keyFrom, keyTo, 0, 127, -1);
   81       }
   82   
   83       public void add(ModelPerformer[] performers) {
   84           add(performers, 0, 127, 0, 127, -1);
   85       }
   86   
   87       public void add(ModelPerformer performer, int keyFrom, int keyTo,
   88               int velFrom, int velTo, int exclusiveClass) {
   89           add(new ModelPerformer[]{performer}, keyFrom, keyTo, velFrom, velTo,
   90                   exclusiveClass);
   91       }
   92   
   93       public void add(ModelPerformer performer, int keyFrom, int keyTo,
   94               int velFrom, int velTo) {
   95           add(new ModelPerformer[]{performer}, keyFrom, keyTo, velFrom, velTo);
   96       }
   97   
   98       public void add(ModelPerformer performer, int keyFrom, int keyTo) {
   99           add(new ModelPerformer[]{performer}, keyFrom, keyTo);
  100       }
  101   
  102       public void add(ModelPerformer performer) {
  103           add(new ModelPerformer[]{performer});
  104       }
  105   
  106       public void add(ModelInstrument ins, int keyFrom, int keyTo, int velFrom,
  107               int velTo, int exclusiveClass) {
  108           add(ins.getPerformers(), keyFrom, keyTo, velFrom, velTo, exclusiveClass);
  109       }
  110   
  111       public void add(ModelInstrument ins, int keyFrom, int keyTo, int velFrom,
  112               int velTo) {
  113           add(ins.getPerformers(), keyFrom, keyTo, velFrom, velTo);
  114       }
  115   
  116       public void add(ModelInstrument ins, int keyFrom, int keyTo) {
  117           add(ins.getPerformers(), keyFrom, keyTo);
  118       }
  119   
  120       public void add(ModelInstrument ins) {
  121           add(ins.getPerformers());
  122       }
  123   
  124       public ModelPerformer[] getPerformers() {
  125   
  126           int percount = 0;
  127           for (SimpleInstrumentPart part : parts)
  128               if (part.performers != null)
  129                   percount += part.performers.length;
  130   
  131           ModelPerformer[] performers = new ModelPerformer[percount];
  132           int px = 0;
  133           for (SimpleInstrumentPart part : parts) {
  134               if (part.performers != null) {
  135                   for (ModelPerformer mperfm : part.performers) {
  136                       ModelPerformer performer = new ModelPerformer();
  137                       performer.setName(getName());
  138                       performers[px++] = performer;
  139   
  140                       performer.setDefaultConnectionsEnabled(
  141                               mperfm.isDefaultConnectionsEnabled());
  142                       performer.setKeyFrom(mperfm.getKeyFrom());
  143                       performer.setKeyTo(mperfm.getKeyTo());
  144                       performer.setVelFrom(mperfm.getVelFrom());
  145                       performer.setVelTo(mperfm.getVelTo());
  146                       performer.setExclusiveClass(mperfm.getExclusiveClass());
  147                       performer.setSelfNonExclusive(mperfm.isSelfNonExclusive());
  148                       performer.setReleaseTriggered(mperfm.isReleaseTriggered());
  149                       if (part.exclusiveClass != -1)
  150                           performer.setExclusiveClass(part.exclusiveClass);
  151                       if (part.keyFrom > performer.getKeyFrom())
  152                           performer.setKeyFrom(part.keyFrom);
  153                       if (part.keyTo < performer.getKeyTo())
  154                           performer.setKeyTo(part.keyTo);
  155                       if (part.velFrom > performer.getVelFrom())
  156                           performer.setVelFrom(part.velFrom);
  157                       if (part.velTo < performer.getVelTo())
  158                           performer.setVelTo(part.velTo);
  159                       performer.getOscillators().addAll(mperfm.getOscillators());
  160                       performer.getConnectionBlocks().addAll(
  161                               mperfm.getConnectionBlocks());
  162                   }
  163               }
  164           }
  165   
  166           return performers;
  167       }
  168   
  169       public Object getData() {
  170           return null;
  171       }
  172   
  173       public String getName() {
  174           return this.name;
  175       }
  176   
  177       public void setName(String name) {
  178           this.name = name;
  179       }
  180   
  181       public ModelPatch getPatch() {
  182           return new ModelPatch(bank, preset, percussion);
  183       }
  184   
  185       public void setPatch(Patch patch) {
  186           if (patch instanceof ModelPatch && ((ModelPatch)patch).isPercussion()) {
  187               percussion = true;
  188               bank = patch.getBank();
  189               preset = patch.getProgram();
  190           } else {
  191               percussion = false;
  192               bank = patch.getBank();
  193               preset = patch.getProgram();
  194           }
  195       }
  196   }

Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]