Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/DLSInstrument.java
38924 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.media.sound;2526import java.util.ArrayList;27import java.util.Arrays;28import java.util.HashMap;29import java.util.List;30import java.util.Map;3132import javax.sound.midi.Patch;3334/**35* This class is used to store information to describe instrument.36* It contains list of regions and modulators.37* It is stored inside a "ins " List Chunk inside DLS files.38* In the DLS documentation a modulator is called articulator.39*40* @author Karl Helgason41*/42public final class DLSInstrument extends ModelInstrument {4344int preset = 0;45int bank = 0;46boolean druminstrument = false;47byte[] guid = null;48DLSInfo info = new DLSInfo();49List<DLSRegion> regions = new ArrayList<DLSRegion>();50List<DLSModulator> modulators = new ArrayList<DLSModulator>();5152public DLSInstrument() {53super(null, null, null, null);54}5556public DLSInstrument(DLSSoundbank soundbank) {57super(soundbank, null, null, null);58}5960public DLSInfo getInfo() {61return info;62}6364public String getName() {65return info.name;66}6768public void setName(String name) {69info.name = name;70}7172public ModelPatch getPatch() {73return new ModelPatch(bank, preset, druminstrument);74}7576public void setPatch(Patch patch) {77if (patch instanceof ModelPatch && ((ModelPatch)patch).isPercussion()) {78druminstrument = true;79bank = patch.getBank();80preset = patch.getProgram();81} else {82druminstrument = false;83bank = patch.getBank();84preset = patch.getProgram();85}86}8788public Object getData() {89return null;90}9192public List<DLSRegion> getRegions() {93return regions;94}9596public List<DLSModulator> getModulators() {97return modulators;98}99100public String toString() {101if (druminstrument)102return "Drumkit: " + info.name103+ " bank #" + bank + " preset #" + preset;104else105return "Instrument: " + info.name106+ " bank #" + bank + " preset #" + preset;107}108109private ModelIdentifier convertToModelDest(int dest) {110if (dest == DLSModulator.CONN_DST_NONE)111return null;112if (dest == DLSModulator.CONN_DST_GAIN)113return ModelDestination.DESTINATION_GAIN;114if (dest == DLSModulator.CONN_DST_PITCH)115return ModelDestination.DESTINATION_PITCH;116if (dest == DLSModulator.CONN_DST_PAN)117return ModelDestination.DESTINATION_PAN;118119if (dest == DLSModulator.CONN_DST_LFO_FREQUENCY)120return ModelDestination.DESTINATION_LFO1_FREQ;121if (dest == DLSModulator.CONN_DST_LFO_STARTDELAY)122return ModelDestination.DESTINATION_LFO1_DELAY;123124if (dest == DLSModulator.CONN_DST_EG1_ATTACKTIME)125return ModelDestination.DESTINATION_EG1_ATTACK;126if (dest == DLSModulator.CONN_DST_EG1_DECAYTIME)127return ModelDestination.DESTINATION_EG1_DECAY;128if (dest == DLSModulator.CONN_DST_EG1_RELEASETIME)129return ModelDestination.DESTINATION_EG1_RELEASE;130if (dest == DLSModulator.CONN_DST_EG1_SUSTAINLEVEL)131return ModelDestination.DESTINATION_EG1_SUSTAIN;132133if (dest == DLSModulator.CONN_DST_EG2_ATTACKTIME)134return ModelDestination.DESTINATION_EG2_ATTACK;135if (dest == DLSModulator.CONN_DST_EG2_DECAYTIME)136return ModelDestination.DESTINATION_EG2_DECAY;137if (dest == DLSModulator.CONN_DST_EG2_RELEASETIME)138return ModelDestination.DESTINATION_EG2_RELEASE;139if (dest == DLSModulator.CONN_DST_EG2_SUSTAINLEVEL)140return ModelDestination.DESTINATION_EG2_SUSTAIN;141142// DLS2 Destinations143if (dest == DLSModulator.CONN_DST_KEYNUMBER)144return ModelDestination.DESTINATION_KEYNUMBER;145146if (dest == DLSModulator.CONN_DST_CHORUS)147return ModelDestination.DESTINATION_CHORUS;148if (dest == DLSModulator.CONN_DST_REVERB)149return ModelDestination.DESTINATION_REVERB;150151if (dest == DLSModulator.CONN_DST_VIB_FREQUENCY)152return ModelDestination.DESTINATION_LFO2_FREQ;153if (dest == DLSModulator.CONN_DST_VIB_STARTDELAY)154return ModelDestination.DESTINATION_LFO2_DELAY;155156if (dest == DLSModulator.CONN_DST_EG1_DELAYTIME)157return ModelDestination.DESTINATION_EG1_DELAY;158if (dest == DLSModulator.CONN_DST_EG1_HOLDTIME)159return ModelDestination.DESTINATION_EG1_HOLD;160if (dest == DLSModulator.CONN_DST_EG1_SHUTDOWNTIME)161return ModelDestination.DESTINATION_EG1_SHUTDOWN;162163if (dest == DLSModulator.CONN_DST_EG2_DELAYTIME)164return ModelDestination.DESTINATION_EG2_DELAY;165if (dest == DLSModulator.CONN_DST_EG2_HOLDTIME)166return ModelDestination.DESTINATION_EG2_HOLD;167168if (dest == DLSModulator.CONN_DST_FILTER_CUTOFF)169return ModelDestination.DESTINATION_FILTER_FREQ;170if (dest == DLSModulator.CONN_DST_FILTER_Q)171return ModelDestination.DESTINATION_FILTER_Q;172173return null;174}175176private ModelIdentifier convertToModelSrc(int src) {177if (src == DLSModulator.CONN_SRC_NONE)178return null;179180if (src == DLSModulator.CONN_SRC_LFO)181return ModelSource.SOURCE_LFO1;182if (src == DLSModulator.CONN_SRC_KEYONVELOCITY)183return ModelSource.SOURCE_NOTEON_VELOCITY;184if (src == DLSModulator.CONN_SRC_KEYNUMBER)185return ModelSource.SOURCE_NOTEON_KEYNUMBER;186if (src == DLSModulator.CONN_SRC_EG1)187return ModelSource.SOURCE_EG1;188if (src == DLSModulator.CONN_SRC_EG2)189return ModelSource.SOURCE_EG2;190if (src == DLSModulator.CONN_SRC_PITCHWHEEL)191return ModelSource.SOURCE_MIDI_PITCH;192if (src == DLSModulator.CONN_SRC_CC1)193return new ModelIdentifier("midi_cc", "1", 0);194if (src == DLSModulator.CONN_SRC_CC7)195return new ModelIdentifier("midi_cc", "7", 0);196if (src == DLSModulator.CONN_SRC_CC10)197return new ModelIdentifier("midi_cc", "10", 0);198if (src == DLSModulator.CONN_SRC_CC11)199return new ModelIdentifier("midi_cc", "11", 0);200if (src == DLSModulator.CONN_SRC_RPN0)201return new ModelIdentifier("midi_rpn", "0", 0);202if (src == DLSModulator.CONN_SRC_RPN1)203return new ModelIdentifier("midi_rpn", "1", 0);204205if (src == DLSModulator.CONN_SRC_POLYPRESSURE)206return ModelSource.SOURCE_MIDI_POLY_PRESSURE;207if (src == DLSModulator.CONN_SRC_CHANNELPRESSURE)208return ModelSource.SOURCE_MIDI_CHANNEL_PRESSURE;209if (src == DLSModulator.CONN_SRC_VIBRATO)210return ModelSource.SOURCE_LFO2;211if (src == DLSModulator.CONN_SRC_MONOPRESSURE)212return ModelSource.SOURCE_MIDI_CHANNEL_PRESSURE;213214if (src == DLSModulator.CONN_SRC_CC91)215return new ModelIdentifier("midi_cc", "91", 0);216if (src == DLSModulator.CONN_SRC_CC93)217return new ModelIdentifier("midi_cc", "93", 0);218219return null;220}221222private ModelConnectionBlock convertToModel(DLSModulator mod) {223ModelIdentifier source = convertToModelSrc(mod.getSource());224ModelIdentifier control = convertToModelSrc(mod.getControl());225ModelIdentifier destination_id =226convertToModelDest(mod.getDestination());227228int scale = mod.getScale();229double f_scale;230if (scale == Integer.MIN_VALUE)231f_scale = Double.NEGATIVE_INFINITY;232else233f_scale = scale / 65536.0;234235if (destination_id != null) {236ModelSource src = null;237ModelSource ctrl = null;238ModelConnectionBlock block = new ModelConnectionBlock();239if (control != null) {240ModelSource s = new ModelSource();241if (control == ModelSource.SOURCE_MIDI_PITCH) {242((ModelStandardTransform)s.getTransform()).setPolarity(243ModelStandardTransform.POLARITY_BIPOLAR);244} else if (control == ModelSource.SOURCE_LFO1245|| control == ModelSource.SOURCE_LFO2) {246((ModelStandardTransform)s.getTransform()).setPolarity(247ModelStandardTransform.POLARITY_BIPOLAR);248}249s.setIdentifier(control);250block.addSource(s);251ctrl = s;252}253if (source != null) {254ModelSource s = new ModelSource();255if (source == ModelSource.SOURCE_MIDI_PITCH) {256((ModelStandardTransform)s.getTransform()).setPolarity(257ModelStandardTransform.POLARITY_BIPOLAR);258} else if (source == ModelSource.SOURCE_LFO1259|| source == ModelSource.SOURCE_LFO2) {260((ModelStandardTransform)s.getTransform()).setPolarity(261ModelStandardTransform.POLARITY_BIPOLAR);262}263s.setIdentifier(source);264block.addSource(s);265src = s;266}267ModelDestination destination = new ModelDestination();268destination.setIdentifier(destination_id);269block.setDestination(destination);270271if (mod.getVersion() == 1) {272//if (mod.getTransform() == DLSModulator.CONN_TRN_CONCAVE) {273// ((ModelStandardTransform)destination.getTransform())274// .setTransform(275// ModelStandardTransform.TRANSFORM_CONCAVE);276//}277if (mod.getTransform() == DLSModulator.CONN_TRN_CONCAVE) {278if (src != null) {279((ModelStandardTransform)src.getTransform())280.setTransform(281ModelStandardTransform.TRANSFORM_CONCAVE);282((ModelStandardTransform)src.getTransform())283.setDirection(284ModelStandardTransform.DIRECTION_MAX2MIN);285}286if (ctrl != null) {287((ModelStandardTransform)ctrl.getTransform())288.setTransform(289ModelStandardTransform.TRANSFORM_CONCAVE);290((ModelStandardTransform)ctrl.getTransform())291.setDirection(292ModelStandardTransform.DIRECTION_MAX2MIN);293}294}295296} else if (mod.getVersion() == 2) {297int transform = mod.getTransform();298int src_transform_invert = (transform >> 15) & 1;299int src_transform_bipolar = (transform >> 14) & 1;300int src_transform = (transform >> 10) & 8;301int ctr_transform_invert = (transform >> 9) & 1;302int ctr_transform_bipolar = (transform >> 8) & 1;303int ctr_transform = (transform >> 4) & 8;304305306if (src != null) {307int trans = ModelStandardTransform.TRANSFORM_LINEAR;308if (src_transform == DLSModulator.CONN_TRN_SWITCH)309trans = ModelStandardTransform.TRANSFORM_SWITCH;310if (src_transform == DLSModulator.CONN_TRN_CONCAVE)311trans = ModelStandardTransform.TRANSFORM_CONCAVE;312if (src_transform == DLSModulator.CONN_TRN_CONVEX)313trans = ModelStandardTransform.TRANSFORM_CONVEX;314((ModelStandardTransform)src.getTransform())315.setTransform(trans);316((ModelStandardTransform)src.getTransform())317.setPolarity(src_transform_bipolar == 1);318((ModelStandardTransform)src.getTransform())319.setDirection(src_transform_invert == 1);320321}322323if (ctrl != null) {324int trans = ModelStandardTransform.TRANSFORM_LINEAR;325if (ctr_transform == DLSModulator.CONN_TRN_SWITCH)326trans = ModelStandardTransform.TRANSFORM_SWITCH;327if (ctr_transform == DLSModulator.CONN_TRN_CONCAVE)328trans = ModelStandardTransform.TRANSFORM_CONCAVE;329if (ctr_transform == DLSModulator.CONN_TRN_CONVEX)330trans = ModelStandardTransform.TRANSFORM_CONVEX;331((ModelStandardTransform)ctrl.getTransform())332.setTransform(trans);333((ModelStandardTransform)ctrl.getTransform())334.setPolarity(ctr_transform_bipolar == 1);335((ModelStandardTransform)ctrl.getTransform())336.setDirection(ctr_transform_invert == 1);337}338339/* No output transforms are defined the DLS Level 2340int out_transform = transform % 8;341int trans = ModelStandardTransform.TRANSFORM_LINEAR;342if (out_transform == DLSModulator.CONN_TRN_SWITCH)343trans = ModelStandardTransform.TRANSFORM_SWITCH;344if (out_transform == DLSModulator.CONN_TRN_CONCAVE)345trans = ModelStandardTransform.TRANSFORM_CONCAVE;346if (out_transform == DLSModulator.CONN_TRN_CONVEX)347trans = ModelStandardTransform.TRANSFORM_CONVEX;348if (ctrl != null) {349((ModelStandardTransform)destination.getTransform())350.setTransform(trans);351}352*/353354}355356block.setScale(f_scale);357358return block;359}360361return null;362}363364public ModelPerformer[] getPerformers() {365List<ModelPerformer> performers = new ArrayList<ModelPerformer>();366367Map<String, DLSModulator> modmap = new HashMap<String, DLSModulator>();368for (DLSModulator mod: getModulators()) {369modmap.put(mod.getSource() + "x" + mod.getControl() + "=" +370mod.getDestination(), mod);371}372373Map<String, DLSModulator> insmodmap =374new HashMap<String, DLSModulator>();375376for (DLSRegion zone: regions) {377ModelPerformer performer = new ModelPerformer();378performer.setName(zone.getSample().getName());379performer.setSelfNonExclusive((zone.getFusoptions() &380DLSRegion.OPTION_SELFNONEXCLUSIVE) != 0);381performer.setExclusiveClass(zone.getExclusiveClass());382performer.setKeyFrom(zone.getKeyfrom());383performer.setKeyTo(zone.getKeyto());384performer.setVelFrom(zone.getVelfrom());385performer.setVelTo(zone.getVelto());386387insmodmap.clear();388insmodmap.putAll(modmap);389for (DLSModulator mod: zone.getModulators()) {390insmodmap.put(mod.getSource() + "x" + mod.getControl() + "=" +391mod.getDestination(), mod);392}393394List<ModelConnectionBlock> blocks = performer.getConnectionBlocks();395for (DLSModulator mod: insmodmap.values()) {396ModelConnectionBlock p = convertToModel(mod);397if (p != null)398blocks.add(p);399}400401402DLSSample sample = zone.getSample();403DLSSampleOptions sampleopt = zone.getSampleoptions();404if (sampleopt == null)405sampleopt = sample.getSampleoptions();406407ModelByteBuffer buff = sample.getDataBuffer();408409float pitchcorrection = (-sampleopt.unitynote * 100) +410sampleopt.finetune;411412ModelByteBufferWavetable osc = new ModelByteBufferWavetable(buff,413sample.getFormat(), pitchcorrection);414osc.setAttenuation(osc.getAttenuation() / 65536f);415if (sampleopt.getLoops().size() != 0) {416DLSSampleLoop loop = sampleopt.getLoops().get(0);417osc.setLoopStart((int)loop.getStart());418osc.setLoopLength((int)loop.getLength());419if (loop.getType() == DLSSampleLoop.LOOP_TYPE_FORWARD)420osc.setLoopType(ModelWavetable.LOOP_TYPE_FORWARD);421if (loop.getType() == DLSSampleLoop.LOOP_TYPE_RELEASE)422osc.setLoopType(ModelWavetable.LOOP_TYPE_RELEASE);423else424osc.setLoopType(ModelWavetable.LOOP_TYPE_FORWARD);425}426427performer.getConnectionBlocks().add(428new ModelConnectionBlock(SoftFilter.FILTERTYPE_LP12,429new ModelDestination(430new ModelIdentifier("filter", "type", 1))));431432performer.getOscillators().add(osc);433434performers.add(performer);435436}437438return performers.toArray(new ModelPerformer[performers.size()]);439}440441public byte[] getGuid() {442return guid == null ? null : Arrays.copyOf(guid, guid.length);443}444445public void setGuid(byte[] guid) {446this.guid = guid == null ? null : Arrays.copyOf(guid, guid.length);447}448}449450451