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.Arrays; 29 import java.util.List; 30 31 import javax.sound.midi.Instrument; 32 import javax.sound.midi.Patch; 33 import javax.sound.midi.Soundbank; 34 import javax.sound.midi.SoundbankResource; 35 36 /** 37 * A simple soundbank that contains instruments and soundbankresources. 38 * 39 * @author Karl Helgason 40 */ 41 public class SimpleSoundbank implements Soundbank { 42 43 String name = ""; 44 String version = ""; 45 String vendor = ""; 46 String description = ""; 47 List<SoundbankResource> resources = new ArrayList<SoundbankResource>(); 48 List<Instrument> instruments = new ArrayList<Instrument>(); 49 50 public String getName() { 51 return name; 52 } 53 54 public String getVersion() { 55 return version; 56 } 57 58 public String getVendor() { 59 return vendor; 60 } 61 62 public String getDescription() { 63 return description; 64 } 65 66 public void setDescription(String description) { 67 this.description = description; 68 } 69 70 public void setName(String name) { 71 this.name = name; 72 } 73 74 public void setVendor(String vendor) { 75 this.vendor = vendor; 76 } 77 78 public void setVersion(String version) { 79 this.version = version; 80 } 81 82 public SoundbankResource[] getResources() { 83 return resources.toArray(new SoundbankResource[resources.size()]); 84 } 85 86 public Instrument[] getInstruments() { 87 Instrument[] inslist_array 88 = instruments.toArray(new Instrument[resources.size()]); 89 Arrays.sort(inslist_array, new ModelInstrumentComparator()); 90 return inslist_array; 91 } 92 93 public Instrument getInstrument(Patch patch) { 94 int program = patch.getProgram(); 95 int bank = patch.getBank(); 96 boolean percussion = false; 97 if (patch instanceof ModelPatch) 98 percussion = ((ModelPatch)patch).isPercussion(); 99 for (Instrument instrument : instruments) { 100 Patch patch2 = instrument.getPatch(); 101 int program2 = patch2.getProgram(); 102 int bank2 = patch2.getBank(); 103 if (program == program2 && bank == bank2) { 104 boolean percussion2 = false; 105 if (patch2 instanceof ModelPatch) 106 percussion2 = ((ModelPatch)patch2).isPercussion(); 107 if (percussion == percussion2) 108 return instrument; 109 } 110 } 111 return null; 112 } 113 114 public void addResource(SoundbankResource resource) { 115 if (resource instanceof Instrument) 116 instruments.add((Instrument) resource); 117 else 118 resources.add(resource); 119 } 120 121 public void removeResource(SoundbankResource resource) { 122 if (resource instanceof Instrument) 123 instruments.remove((Instrument) resource); 124 else 125 resources.remove(resource); 126 } 127 128 public void addInstrument(Instrument resource) { 129 instruments.add(resource); 130 } 131 132 public void removeInstrument(Instrument resource) { 133 instruments.remove(resource); 134 } 135 136 public void addAllInstruments(Soundbank soundbank) { 137 for (Instrument ins : soundbank.getInstruments()) 138 addInstrument(ins); 139 } 140 141 public void removeAllInstruments(Soundbank soundbank) { 142 for (Instrument ins : soundbank.getInstruments()) 143 removeInstrument(ins); | 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.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.List; 31 32 import javax.sound.midi.Instrument; 33 import javax.sound.midi.Patch; 34 import javax.sound.midi.Soundbank; 35 import javax.sound.midi.SoundbankResource; 36 37 /** 38 * A simple soundbank that contains instruments and soundbankresources. 39 * 40 * @author Karl Helgason 41 */ 42 public class SimpleSoundbank implements Soundbank { 43 44 String name = ""; 45 String version = ""; 46 String vendor = ""; 47 String description = ""; 48 List<SoundbankResource> resources = new ArrayList<>(); 49 List<Instrument> instruments = new ArrayList<>(); 50 51 @Override 52 public String getName() { 53 return name; 54 } 55 56 @Override 57 public String getVersion() { 58 return version; 59 } 60 61 @Override 62 public String getVendor() { 63 return vendor; 64 } 65 66 @Override 67 public String getDescription() { 68 return description; 69 } 70 71 public void setDescription(String description) { 72 this.description = description; 73 } 74 75 public void setName(String name) { 76 this.name = name; 77 } 78 79 public void setVendor(String vendor) { 80 this.vendor = vendor; 81 } 82 83 public void setVersion(String version) { 84 this.version = version; 85 } 86 87 @Override 88 public SoundbankResource[] getResources() { 89 return resources.toArray(new SoundbankResource[resources.size()]); 90 } 91 92 @Override 93 public Instrument[] getInstruments() { 94 Instrument[] inslist_array 95 = instruments.toArray(new Instrument[resources.size()]); 96 Arrays.sort(inslist_array, new ModelInstrumentComparator()); 97 return inslist_array; 98 } 99 100 @Override 101 public Instrument getInstrument(Patch patch) { 102 int program = patch.getProgram(); 103 int bank = patch.getBank(); 104 boolean percussion = false; 105 if (patch instanceof ModelPatch) 106 percussion = ((ModelPatch)patch).isPercussion(); 107 for (Instrument instrument : instruments) { 108 Patch patch2 = instrument.getPatch(); 109 int program2 = patch2.getProgram(); 110 int bank2 = patch2.getBank(); 111 if (program == program2 && bank == bank2) { 112 boolean percussion2 = false; 113 if (patch2 instanceof ModelPatch) 114 percussion2 = ((ModelPatch)patch2).isPercussion(); 115 if (percussion == percussion2) 116 return instrument; 117 } 118 } 119 return null; 120 } 121 122 public void addResource(SoundbankResource resource) { 123 if (resource instanceof Instrument) 124 instruments.add((Instrument) resource); 125 else 126 resources.add(resource); 127 } 128 129 public void removeResource(SoundbankResource resource) { 130 if (resource instanceof Instrument) 131 instruments.remove(resource); 132 else 133 resources.remove(resource); 134 } 135 136 public void addInstrument(Instrument resource) { 137 instruments.add(resource); 138 } 139 140 public void removeInstrument(Instrument resource) { 141 instruments.remove(resource); 142 } 143 144 public void addAllInstruments(Soundbank soundbank) { 145 for (Instrument ins : soundbank.getInstruments()) 146 addInstrument(ins); 147 } 148 149 public void removeAllInstruments(Soundbank soundbank) { 150 for (Instrument ins : soundbank.getInstruments()) 151 removeInstrument(ins); |