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
26package com.sun.media.sound;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
31
32import javax.sound.midi.Instrument;
33import javax.sound.midi.Patch;
34import javax.sound.midi.Soundbank;
35import javax.sound.midi.SoundbankResource;
36
37/**
38 * A simple soundbank that contains instruments and soundbankresources.
39 *
40 * @author Karl Helgason
41 */
42public 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);
152    }
153}
154