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 
  26 package com.sun.media.sound;
  27 
  28 import java.io.IOException;
  29 
  30 import javax.sound.midi.Instrument;
  31 import javax.sound.midi.MidiChannel;
  32 import javax.sound.midi.Patch;
  33 import javax.sound.midi.Soundbank;
  34 import javax.sound.midi.SoundbankResource;
  35 import javax.sound.midi.VoiceStatus;
  36 
  37 /**
  38  * A abstract class used to simplify creating custom ModelOscillator.
  39  *
  40  * @author Karl Helgason
  41  */
  42 public abstract class ModelAbstractOscillator
  43         implements ModelOscillator, ModelOscillatorStream, Soundbank {
  44 
  45     protected float pitch = 6000;
  46     protected float samplerate;
  47     protected MidiChannel channel;
  48     protected VoiceStatus voice;
  49     protected int noteNumber;
  50     protected int velocity;
  51     protected boolean on = false;
  52 
  53     public void init() {
  54     }
  55 
  56     @Override
  57     public void close() throws IOException {
  58     }
  59 
  60     @Override
  61     public void noteOff(int velocity) {
  62         on = false;
  63     }
  64 
  65     @Override
  66     public void noteOn(MidiChannel channel, VoiceStatus voice, int noteNumber,
  67                        int velocity) {
  68         this.channel = channel;
  69         this.voice = voice;
  70         this.noteNumber = noteNumber;
  71         this.velocity = velocity;
  72         on = true;
  73     }
  74 
  75     @Override
  76     public int read(float[][] buffer, int offset, int len) throws IOException {
  77         return -1;
  78     }
  79 
  80     public MidiChannel getChannel() {
  81         return channel;
  82     }
  83 
  84     public VoiceStatus getVoice() {
  85         return voice;
  86     }
  87 
  88     public int getNoteNumber() {
  89         return noteNumber;
  90     }
  91 
  92     public int getVelocity() {
  93         return velocity;
  94     }
  95 
  96     public boolean isOn() {
  97         return on;
  98     }
  99 
 100     @Override
 101     public void setPitch(float pitch) {
 102         this.pitch = pitch;
 103     }
 104 
 105     public float getPitch() {
 106         return pitch;
 107     }
 108 
 109     public void setSampleRate(float samplerate) {
 110         this.samplerate = samplerate;
 111     }
 112 
 113     public float getSampleRate() {
 114         return samplerate;
 115     }
 116 
 117     @Override
 118     public float getAttenuation() {
 119         return 0;
 120     }
 121 
 122     @Override
 123     public int getChannels() {
 124         return 1;
 125     }
 126 
 127     @Override
 128     public String getName() {
 129         return getClass().getName();
 130     }
 131 
 132     public Patch getPatch() {
 133         return new Patch(0, 0);
 134     }
 135 
 136     @Override
 137     public ModelOscillatorStream open(float samplerate) {
 138         ModelAbstractOscillator oscs;
 139         try {
 140             oscs = this.getClass().getDeclaredConstructor().newInstance();
 141         } catch (ReflectiveOperationException e) {
 142             throw new IllegalArgumentException(e);
 143         }
 144         oscs.setSampleRate(samplerate);
 145         oscs.init();
 146         return oscs;
 147     }
 148 
 149     public ModelPerformer getPerformer() {
 150         // Create performer for my custom oscillirator
 151         ModelPerformer performer = new ModelPerformer();
 152         performer.getOscillators().add(this);
 153         return performer;
 154 
 155     }
 156 
 157     public ModelInstrument getInstrument() {
 158         // Create Instrument object around my performer
 159         SimpleInstrument ins = new SimpleInstrument();
 160         ins.setName(getName());
 161         ins.add(getPerformer());
 162         ins.setPatch(getPatch());
 163         return ins;
 164 
 165     }
 166 
 167     public Soundbank getSoundBank() {
 168         // Create Soundbank object around the instrument
 169         SimpleSoundbank sbk = new SimpleSoundbank();
 170         sbk.addInstrument(getInstrument());
 171         return sbk;
 172     }
 173 
 174     @Override
 175     public String getDescription() {
 176         return getName();
 177     }
 178 
 179     @Override
 180     public Instrument getInstrument(Patch patch) {
 181         Instrument ins = getInstrument();
 182         Patch p = ins.getPatch();
 183         if (p.getBank() != patch.getBank())
 184             return null;
 185         if (p.getProgram() != patch.getProgram())
 186             return null;
 187         if (p instanceof ModelPatch && patch instanceof ModelPatch) {
 188             if (((ModelPatch)p).isPercussion()
 189                     != ((ModelPatch)patch).isPercussion()) {
 190                 return null;
 191             }
 192         }
 193         return ins;
 194     }
 195 
 196     @Override
 197     public Instrument[] getInstruments() {
 198         return new Instrument[]{getInstrument()};
 199     }
 200 
 201     @Override
 202     public SoundbankResource[] getResources() {
 203         return new SoundbankResource[0];
 204     }
 205 
 206     @Override
 207     public String getVendor() {
 208         return null;
 209     }
 210 
 211     @Override
 212     public String getVersion() {
 213         return null;
 214     }
 215 }