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 package com.sun.media.sound;
  26 
  27 import javax.sound.midi.Instrument;
  28 import javax.sound.midi.MidiChannel;
  29 import javax.sound.midi.Patch;
  30 import javax.sound.midi.Soundbank;
  31 import javax.sound.sampled.AudioFormat;
  32 
  33 /**
  34  * The model instrument class.
  35  *
  36  * <p>The main methods to override are:<br>
  37  * getPerformer, getDirector, getChannelMixer.
  38  *
  39  * <p>Performers are used to define what voices which will
  40  * playback when using the instrument.<br>
  41  *
  42  * ChannelMixer is used to add channel-wide processing
  43  * on voices output or to define non-voice oriented instruments.<br>
  44  *
  45  * Director is used to change how the synthesizer
  46  * chooses what performers to play on midi events.
  47  *
  48  * @author Karl Helgason
  49  */
  50 public abstract class ModelInstrument extends Instrument {
  51 
  52     protected ModelInstrument(Soundbank soundbank, Patch patch, String name,
  53             Class<?> dataClass) {
  54         super(soundbank, patch, name, dataClass);
  55     }
  56 
  57     public ModelDirector getDirector(ModelPerformer[] performers,
  58             MidiChannel channel, ModelDirectedPlayer player) {
  59         return new ModelStandardIndexedDirector(performers, player);
  60     }
  61 
  62     public ModelPerformer[] getPerformers() {
  63         return new ModelPerformer[0];
  64     }
  65 
  66     public ModelChannelMixer getChannelMixer(MidiChannel channel,
  67             AudioFormat format) {
  68         return null;
  69     }
  70 
  71     // Get General MIDI 2 Alias patch for this instrument.
  72     public final Patch getPatchAlias() {
  73         Patch patch = getPatch();
  74         int program = patch.getProgram();
  75         int bank = patch.getBank();
  76         if (bank != 0)
  77             return patch;
  78         boolean percussion = false;
  79         if (getPatch() instanceof ModelPatch)
  80             percussion = ((ModelPatch)getPatch()).isPercussion();
  81         if (percussion)
  82             return new Patch(0x78 << 7, program);
  83         else
  84             return new Patch(0x79 << 7, program);
  85     }
  86 
  87     // Return name of all the keys.
  88     // This information is generated from ModelPerformer.getName()
  89     // returned from getPerformers().
  90     public final String[] getKeys() {
  91         String[] keys = new String[128];
  92         for (ModelPerformer performer : getPerformers()) {
  93             for (int k = performer.getKeyFrom(); k <= performer.getKeyTo(); k++) {
  94                 if (k >= 0 && k < 128 && keys[k] == null) {
  95                     String name = performer.getName();
  96                     if (name == null)
  97                         name = "untitled";
  98                     keys[k] = name;
  99                 }
 100             }
 101         }
 102         return keys;
 103     }
 104 
 105     // Return what channels this instrument will probably response
 106     // on General MIDI synthesizer.
 107     public final boolean[] getChannels() {
 108         boolean percussion = false;
 109         if (getPatch() instanceof ModelPatch)
 110             percussion = ((ModelPatch)getPatch()).isPercussion();
 111 
 112         // Check if instrument is percussion.
 113         if (percussion) {
 114             boolean[] ch = new boolean[16];
 115             for (int i = 0; i < ch.length; i++)
 116                 ch[i] = false;
 117             ch[9] = true;
 118             return ch;
 119         }
 120 
 121         // Check if instrument uses General MIDI 2 default banks.
 122         int bank = getPatch().getBank();
 123         if (bank >> 7 == 0x78 || bank >> 7 == 0x79) {
 124             boolean[] ch = new boolean[16];
 125             for (int i = 0; i < ch.length; i++)
 126                 ch[i] = true;
 127             return ch;
 128         }
 129 
 130         boolean[] ch = new boolean[16];
 131         for (int i = 0; i < ch.length; i++)
 132             ch[i] = true;
 133         ch[9] = false;
 134         return ch;
 135     }
 136 }