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