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