javax.sound.midi.Instrument Java Examples

The following examples show how to use javax.sound.midi.Instrument. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DLSSoundbank.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch) patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch) patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #2
Source File: DLSSoundbank.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch) patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch) patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #3
Source File: RemapInstrument.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.openStream(null, null);
    Soundbank defsbk = synth.getDefaultSoundbank();
    if(defsbk != null)
    {
        Instrument ins3 = defsbk.getInstrument(new Patch(0,3));
        Instrument ins10 = defsbk.getInstrument(new Patch(0,10));
        assertTrue(synth.remapInstrument(ins3, ins10));
        Instrument[] loaded = synth.getLoadedInstruments();
        for (int i = 0; i < loaded.length; i++) {
            if(loaded[i].getPatch().getBank() == ins3.getPatch().getBank())
            if(loaded[i].getPatch().getProgram() == ins3.getPatch().getProgram())
            {
                assertEquals(loaded[i].getName(), ins10.getName());
                break;
            }
        }

    }
    synth.close();

}
 
Example #4
Source File: SoftSynthesizer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private boolean loadInstruments(List<ModelInstrument> instruments) {
    if (!isOpen())
        return false;
    if (!loadSamples(instruments))
        return false;

    synchronized (control_mutex) {
        if (channels != null)
            for (SoftChannel c : channels)
            {
                c.current_instrument = null;
                c.current_director = null;
            }
        for (Instrument instrument : instruments) {
            String pat = patchToString(instrument.getPatch());
            SoftInstrument softins
                    = new SoftInstrument((ModelInstrument) instrument);
            inslist.put(pat, softins);
            loadedlist.put(pat, (ModelInstrument) instrument);
        }
    }

    return true;
}
 
Example #5
Source File: ExtraCharInSoundbank.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static boolean checkInstrumentNames(Synthesizer theSynthesizer)
{
    boolean containsControlCharacters = false;

    Instrument[] theLoadedInstruments = theSynthesizer.getLoadedInstruments();

    System.out.println("Checking soundbank...");
    for(int theInstrumentIndex = 0; theInstrumentIndex < theLoadedInstruments.length; theInstrumentIndex++) {
        String name = theLoadedInstruments[theInstrumentIndex].getName();
        if (containsControlChar(name)) {
            containsControlCharacters = true;
            System.out.print("Instrument[" + theInstrumentIndex + "] contains unexpected control characters: ");
            printName(name);
        }
    }
    return !containsControlCharacters;
}
 
Example #6
Source File: RemapInstrument.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.openStream(null, null);
    Soundbank defsbk = synth.getDefaultSoundbank();
    if(defsbk != null)
    {
        Instrument ins3 = defsbk.getInstrument(new Patch(0,3));
        Instrument ins10 = defsbk.getInstrument(new Patch(0,10));
        assertTrue(synth.remapInstrument(ins3, ins10));
        Instrument[] loaded = synth.getLoadedInstruments();
        for (int i = 0; i < loaded.length; i++) {
            if(loaded[i].getPatch().getBank() == ins3.getPatch().getBank())
            if(loaded[i].getPatch().getProgram() == ins3.getPatch().getProgram())
            {
                assertEquals(loaded[i].getName(), ins10.getName());
                break;
            }
        }

    }
    synth.close();

}
 
Example #7
Source File: SimpleSoundbank.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch)patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch)patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #8
Source File: DLSSoundbank.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch) patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch) patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #9
Source File: RemapInstrument.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.openStream(null, null);
    Soundbank defsbk = synth.getDefaultSoundbank();
    if(defsbk != null)
    {
        Instrument ins3 = defsbk.getInstrument(new Patch(0,3));
        Instrument ins10 = defsbk.getInstrument(new Patch(0,10));
        assertTrue(synth.remapInstrument(ins3, ins10));
        Instrument[] loaded = synth.getLoadedInstruments();
        for (int i = 0; i < loaded.length; i++) {
            if(loaded[i].getPatch().getBank() == ins3.getPatch().getBank())
            if(loaded[i].getPatch().getProgram() == ins3.getPatch().getProgram())
            {
                assertEquals(loaded[i].getName(), ins10.getName());
                break;
            }
        }

    }
    synth.close();

}
 
Example #10
Source File: SoftSynthesizer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public boolean remapInstrument(Instrument from, Instrument to) {

        if (from == null)
            throw new NullPointerException();
        if (to == null)
            throw new NullPointerException();
        if (!(from instanceof ModelInstrument)) {
            throw new IllegalArgumentException("Unsupported instrument: " +
                    from.toString());
        }
        if (!(to instanceof ModelInstrument)) {
            throw new IllegalArgumentException("Unsupported instrument: " +
                    to.toString());
        }
        if (!isOpen())
            return false;

        synchronized (control_mutex) {
            if (!loadedlist.containsValue(to))
                throw new IllegalArgumentException("Instrument to is not loaded.");
            unloadInstrument(from);
            ModelMappedInstrument mfrom = new ModelMappedInstrument(
                    (ModelInstrument)to, from.getPatch());
            return loadInstrument(mfrom);
        }
    }
 
Example #11
Source File: MidiPortSynthesizer.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void unloadSoundbank(){
	try {
		this.soundbankLoaded = false;
		
		//unload all available instruments
		Instrument[] available = this.synth.getAvailableInstruments();
		if(available != null){
			for(int i = 0; i < available.length; i++){
				getSynth().unloadInstrument(available[i]);
			}
		}
		
		//unload all loaded instruments
		Instrument[] loaded = this.synth.getLoadedInstruments();
		if(loaded != null){
			for(int i = 0; i < loaded.length; i++){
				getSynth().unloadInstrument(loaded[i]);
			}
		}
	}catch (Throwable throwable) {
		throwable.printStackTrace();
	}
}
 
Example #12
Source File: SoftSynthesizer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void unloadInstrument(Instrument instrument) {
    if (instrument == null || (!(instrument instanceof ModelInstrument))) {
        throw new IllegalArgumentException("Unsupported instrument: " +
                instrument);
    }
    if (!isOpen())
        return;

    String pat = patchToString(instrument.getPatch());
    synchronized (control_mutex) {
        for (SoftChannel c: channels)
            c.current_instrument = null;
        inslist.remove(pat);
        loadedlist.remove(pat);
        for (int i = 0; i < channels.length; i++) {
            channels[i].allSoundOff();
        }
    }
}
 
Example #13
Source File: MidiPortSynthesizer.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void unloadSoundbank(){
	try {
		this.soundbankLoaded = false;
		
		//unload all available instruments
		Instrument[] available = this.synth.getAvailableInstruments();
		if(available != null){
			for(int i = 0; i < available.length; i++){
				getSynth().unloadInstrument(available[i]);
			}
		}
		
		//unload all loaded instruments
		Instrument[] loaded = this.synth.getLoadedInstruments();
		if(loaded != null){
			for(int i = 0; i < loaded.length; i++){
				getSynth().unloadInstrument(loaded[i]);
			}
		}
	}catch (Throwable throwable) {
		throwable.printStackTrace();
	}
}
 
Example #14
Source File: SoftSynthesizer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void unloadInstrument(Instrument instrument) {
    if (instrument == null || (!(instrument instanceof ModelInstrument))) {
        throw new IllegalArgumentException("Unsupported instrument: " +
                instrument);
    }
    if (!isOpen())
        return;

    String pat = patchToString(instrument.getPatch());
    synchronized (control_mutex) {
        for (SoftChannel c: channels)
            c.current_instrument = null;
        inslist.remove(pat);
        loadedlist.remove(pat);
        for (int i = 0; i < channels.length; i++) {
            channels[i].allSoundOff();
        }
    }
}
 
Example #15
Source File: RemapInstrument.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.openStream(null, null);
    Soundbank defsbk = synth.getDefaultSoundbank();
    if(defsbk != null)
    {
        Instrument ins3 = defsbk.getInstrument(new Patch(0,3));
        Instrument ins10 = defsbk.getInstrument(new Patch(0,10));
        assertTrue(synth.remapInstrument(ins3, ins10));
        Instrument[] loaded = synth.getLoadedInstruments();
        for (int i = 0; i < loaded.length; i++) {
            if(loaded[i].getPatch().getBank() == ins3.getPatch().getBank())
            if(loaded[i].getPatch().getProgram() == ins3.getPatch().getProgram())
            {
                assertEquals(loaded[i].getName(), ins10.getName());
                break;
            }
        }

    }
    synth.close();

}
 
Example #16
Source File: SimpleSoundbank.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch)patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch)patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #17
Source File: SimpleSoundbank.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch)patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch)patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #18
Source File: GervillSoundbankFactory.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Instrument getDefaultInstrument(TGContext context, GervillProgram program) throws Exception {
	synchronized (GervillProcessorFactory.class) {
		if( defaultInstruments == null ) {
			defaultInstruments = new Instrument[129][128];
			
			Soundbank soundbank = null;
			String soundbankPath = new GervillSettings(context).getSoundbankPath();
			if( soundbankPath != null && soundbankPath.length() > 0 ) {
				soundbank = this.createSoundbank(context, soundbankPath);
			}
			if( soundbank == null ) {
				soundbank = EmergencySoundbank.createSoundbank();
			}
			if( soundbank != null ) {
				Instrument[] instruments = soundbank.getInstruments();
				for(Instrument instrument : instruments) {
					ModelPatch patch = (ModelPatch) instrument.getPatch();
					if( patch.getProgram() < 128 && patch.getBank() < 128 ) {
						defaultInstruments[patch.isPercussion() ? 128 : patch.getBank()][patch.getProgram()] = instrument;
					}
				}
			}
		}
	}
	return defaultInstruments[program.getBank()][program.getProgram()];
}
 
Example #19
Source File: DLSSoundbank.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch) patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch) patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #20
Source File: SimpleSoundbank.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch)patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch)patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #21
Source File: SimpleSoundbank.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public Instrument getInstrument(Patch patch) {
    int program = patch.getProgram();
    int bank = patch.getBank();
    boolean percussion = false;
    if (patch instanceof ModelPatch)
        percussion = ((ModelPatch)patch).isPercussion();
    for (Instrument instrument : instruments) {
        Patch patch2 = instrument.getPatch();
        int program2 = patch2.getProgram();
        int bank2 = patch2.getBank();
        if (program == program2 && bank == bank2) {
            boolean percussion2 = false;
            if (patch2 instanceof ModelPatch)
                percussion2 = ((ModelPatch)patch2).isPercussion();
            if (percussion == percussion2)
                return instrument;
        }
    }
    return null;
}
 
Example #22
Source File: SoftSynthesizer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Instrument[] getAvailableInstruments() {
    Soundbank defsbk = getDefaultSoundbank();
    if (defsbk == null)
        return new Instrument[0];
    Instrument[] inslist_array = defsbk.getInstruments();
    Arrays.sort(inslist_array, new ModelInstrumentComparator());
    return inslist_array;
}
 
Example #23
Source File: SoftSynthesizer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean loadInstrument(Instrument instrument) {
    if (instrument == null || (!(instrument instanceof ModelInstrument))) {
        throw new IllegalArgumentException("Unsupported instrument: " +
                instrument);
    }
    List<ModelInstrument> instruments = new ArrayList<ModelInstrument>();
    instruments.add((ModelInstrument)instrument);
    return loadInstruments(instruments);
}
 
Example #24
Source File: ModelInstrumentComparator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public int compare(Instrument arg0, Instrument arg1) {
    Patch p0 = arg0.getPatch();
    Patch p1 = arg1.getPatch();
    int a = p0.getBank() * 128 + p0.getProgram();
    int b = p1.getBank() * 128 + p1.getProgram();
    if (p0 instanceof ModelPatch) {
        a += ((ModelPatch)p0).isPercussion() ? 2097152 : 0;
    }
    if (p1 instanceof ModelPatch) {
        b += ((ModelPatch)p1).isPercussion() ? 2097152 : 0;
    }
    return a - b;
}
 
Example #25
Source File: SoftSynthesizer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Instrument[] getLoadedInstruments() {
    if (!isOpen())
        return new Instrument[0];

    synchronized (control_mutex) {
        ModelInstrument[] inslist_array =
                new ModelInstrument[loadedlist.values().size()];
        loadedlist.values().toArray(inslist_array);
        Arrays.sort(inslist_array, new ModelInstrumentComparator());
        return inslist_array;
    }
}
 
Example #26
Source File: SoftSynthesizer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean loadInstruments(Soundbank soundbank, Patch[] patchList) {
    List<ModelInstrument> instruments = new ArrayList<ModelInstrument>();
    for (Patch patch: patchList) {
        Instrument ins = soundbank.getInstrument(patch);
        if (ins == null || !(ins instanceof ModelInstrument)) {
            throw new IllegalArgumentException(
                    "Unsupported instrument: " + ins);
        }
        instruments.add((ModelInstrument)ins);
    }
    return loadInstruments(instruments);
}
 
Example #27
Source File: SoftSynthesizer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean loadInstrument(Instrument instrument) {
    if (instrument == null || (!(instrument instanceof ModelInstrument))) {
        throw new IllegalArgumentException("Unsupported instrument: " +
                instrument);
    }
    List<ModelInstrument> instruments = new ArrayList<ModelInstrument>();
    instruments.add((ModelInstrument)instrument);
    return loadInstruments(instruments);
}
 
Example #28
Source File: SoftSynthesizer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public Instrument[] getLoadedInstruments() {
    if (!isOpen())
        return new Instrument[0];

    synchronized (control_mutex) {
        ModelInstrument[] inslist_array =
                new ModelInstrument[loadedlist.values().size()];
        loadedlist.values().toArray(inslist_array);
        Arrays.sort(inslist_array, new ModelInstrumentComparator());
        return inslist_array;
    }
}
 
Example #29
Source File: SoftSynthesizer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean loadInstruments(Soundbank soundbank, Patch[] patchList) {
    List<ModelInstrument> instruments = new ArrayList<ModelInstrument>();
    for (Patch patch: patchList) {
        Instrument ins = soundbank.getInstrument(patch);
        if (ins == null || !(ins instanceof ModelInstrument)) {
            throw new IllegalArgumentException(
                    "Unsupported instrument: " + ins);
        }
        instruments.add((ModelInstrument)ins);
    }
    return loadInstruments(instruments);
}
 
Example #30
Source File: SoftSynthesizer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void unloadInstruments(Soundbank soundbank, Patch[] patchList) {
    if (soundbank == null || !isSoundbankSupported(soundbank))
        throw new IllegalArgumentException("Unsupported soundbank: " + soundbank);

    if (!isOpen())
        return;

    for (Patch pat: patchList) {
        Instrument ins = soundbank.getInstrument(pat);
        if (ins instanceof ModelInstrument) {
            unloadInstrument(ins);
        }
    }
}