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.HashMap;
30import java.util.List;
31import java.util.Map;
32
33/**
34 * Soundfont general region.
35 *
36 * @author Karl Helgason
37 */
38public class SF2Region {
39
40    public static final int GENERATOR_STARTADDRSOFFSET = 0;
41    public static final int GENERATOR_ENDADDRSOFFSET = 1;
42    public static final int GENERATOR_STARTLOOPADDRSOFFSET = 2;
43    public static final int GENERATOR_ENDLOOPADDRSOFFSET = 3;
44    public static final int GENERATOR_STARTADDRSCOARSEOFFSET = 4;
45    public static final int GENERATOR_MODLFOTOPITCH = 5;
46    public static final int GENERATOR_VIBLFOTOPITCH = 6;
47    public static final int GENERATOR_MODENVTOPITCH = 7;
48    public static final int GENERATOR_INITIALFILTERFC = 8;
49    public static final int GENERATOR_INITIALFILTERQ = 9;
50    public static final int GENERATOR_MODLFOTOFILTERFC = 10;
51    public static final int GENERATOR_MODENVTOFILTERFC = 11;
52    public static final int GENERATOR_ENDADDRSCOARSEOFFSET = 12;
53    public static final int GENERATOR_MODLFOTOVOLUME = 13;
54    public static final int GENERATOR_UNUSED1 = 14;
55    public static final int GENERATOR_CHORUSEFFECTSSEND = 15;
56    public static final int GENERATOR_REVERBEFFECTSSEND = 16;
57    public static final int GENERATOR_PAN = 17;
58    public static final int GENERATOR_UNUSED2 = 18;
59    public static final int GENERATOR_UNUSED3 = 19;
60    public static final int GENERATOR_UNUSED4 = 20;
61    public static final int GENERATOR_DELAYMODLFO = 21;
62    public static final int GENERATOR_FREQMODLFO = 22;
63    public static final int GENERATOR_DELAYVIBLFO = 23;
64    public static final int GENERATOR_FREQVIBLFO = 24;
65    public static final int GENERATOR_DELAYMODENV = 25;
66    public static final int GENERATOR_ATTACKMODENV = 26;
67    public static final int GENERATOR_HOLDMODENV = 27;
68    public static final int GENERATOR_DECAYMODENV = 28;
69    public static final int GENERATOR_SUSTAINMODENV = 29;
70    public static final int GENERATOR_RELEASEMODENV = 30;
71    public static final int GENERATOR_KEYNUMTOMODENVHOLD = 31;
72    public static final int GENERATOR_KEYNUMTOMODENVDECAY = 32;
73    public static final int GENERATOR_DELAYVOLENV = 33;
74    public static final int GENERATOR_ATTACKVOLENV = 34;
75    public static final int GENERATOR_HOLDVOLENV = 35;
76    public static final int GENERATOR_DECAYVOLENV = 36;
77    public static final int GENERATOR_SUSTAINVOLENV = 37;
78    public static final int GENERATOR_RELEASEVOLENV = 38;
79    public static final int GENERATOR_KEYNUMTOVOLENVHOLD = 39;
80    public static final int GENERATOR_KEYNUMTOVOLENVDECAY = 40;
81    public static final int GENERATOR_INSTRUMENT = 41;
82    public static final int GENERATOR_RESERVED1 = 42;
83    public static final int GENERATOR_KEYRANGE = 43;
84    public static final int GENERATOR_VELRANGE = 44;
85    public static final int GENERATOR_STARTLOOPADDRSCOARSEOFFSET = 45;
86    public static final int GENERATOR_KEYNUM = 46;
87    public static final int GENERATOR_VELOCITY = 47;
88    public static final int GENERATOR_INITIALATTENUATION = 48;
89    public static final int GENERATOR_RESERVED2 = 49;
90    public static final int GENERATOR_ENDLOOPADDRSCOARSEOFFSET = 50;
91    public static final int GENERATOR_COARSETUNE = 51;
92    public static final int GENERATOR_FINETUNE = 52;
93    public static final int GENERATOR_SAMPLEID = 53;
94    public static final int GENERATOR_SAMPLEMODES = 54;
95    public static final int GENERATOR_RESERVED3 = 55;
96    public static final int GENERATOR_SCALETUNING = 56;
97    public static final int GENERATOR_EXCLUSIVECLASS = 57;
98    public static final int GENERATOR_OVERRIDINGROOTKEY = 58;
99    public static final int GENERATOR_UNUSED5 = 59;
100    public static final int GENERATOR_ENDOPR = 60;
101    protected Map<Integer, Short> generators = new HashMap<>();
102    protected List<SF2Modulator> modulators = new ArrayList<>();
103
104    public Map<Integer, Short> getGenerators() {
105        return generators;
106    }
107
108    public boolean contains(int generator) {
109        return generators.containsKey(generator);
110    }
111
112    public static short getDefaultValue(int generator) {
113        if (generator == 8) return (short)13500;
114        if (generator == 21) return (short)-12000;
115        if (generator == 23) return (short)-12000;
116        if (generator == 25) return (short)-12000;
117        if (generator == 26) return (short)-12000;
118        if (generator == 27) return (short)-12000;
119        if (generator == 28) return (short)-12000;
120        if (generator == 30) return (short)-12000;
121        if (generator == 33) return (short)-12000;
122        if (generator == 34) return (short)-12000;
123        if (generator == 35) return (short)-12000;
124        if (generator == 36) return (short)-12000;
125        if (generator == 38) return (short)-12000;
126        if (generator == 43) return (short)0x7F00;
127        if (generator == 44) return (short)0x7F00;
128        if (generator == 46) return (short)-1;
129        if (generator == 47) return (short)-1;
130        if (generator == 56) return (short)100;
131        if (generator == 58) return (short)-1;
132        return 0;
133    }
134
135    public short getShort(int generator) {
136        if (!contains(generator))
137            return getDefaultValue(generator);
138        return generators.get(generator);
139    }
140
141    public void putShort(int generator, short value) {
142        generators.put(generator, value);
143    }
144
145    public byte[] getBytes(int generator) {
146        int val = getInteger(generator);
147        byte[] bytes = new byte[2];
148        bytes[0] = (byte) (0xFF & val);
149        bytes[1] = (byte) ((0xFF00 & val) >> 8);
150        return bytes;
151    }
152
153    public void putBytes(int generator, byte[] bytes) {
154        generators.put(generator, (short) (bytes[0] + (bytes[1] << 8)));
155    }
156
157    public int getInteger(int generator) {
158        return 0xFFFF & getShort(generator);
159    }
160
161    public void putInteger(int generator, int value) {
162        generators.put(generator, (short) value);
163    }
164
165    public List<SF2Modulator> getModulators() {
166        return modulators;
167    }
168}
169