Question

I am using Gervill to create a soundbank with instruments. I've recorded a sample for each tone pitch and now I'd like to put these samples into one instrument. The documentation that I used so far are tests from the openjdk6 source code. Apart from that I found an example by Karl Helgason, that helped greatly. The example loads an audio file into a soundbank however it only uses one sample per instrument. I've modified his example file and when I use the sound bank for replay, it seems that only one sample is used and pitched according to the requested tone pitch. In contrast I want to use a specific sample per tone pitch. I suspect that my for-loop is constructed around the wrong parts of the method and that an additional sample is overwriting the formerly saved one.

My question is which parts should every sample have separately: The layer? Or the region? Both? Unfortunately Gervill terminology seems to be slightly different from another one that I found, so I am a bit confused.

I've used the following source code (I left the copyright note in the altered source code, I am not a lawyer, so I am not sure if this is the right thing to do.):

/*
 * Copyright (c) 2007 by Karl Helgason
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package midiplay;

    import com.sun.media.sound.*;
    import java.io.File;
    import java.io.IOException;
    import javax.sound.midi.Patch;
    import javax.sound.sampled.*;

    public class MakeSoundfont {

        public SF2Soundbank CreateSoundbank()
                throws UnsupportedAudioFileException, IOException {

            SF2Soundbank sf2 = new SF2Soundbank();

            String[] fnames = new String[]{"C.wav", "Cs.wav", "D.wav", "Ds.wav"};

            SF2Layer layer = new SF2Layer(sf2);
            layer.setName("fname Layer");
            sf2.addResource(layer);


            int i = 0;
            for (String fname : fnames) {

                File audiofile = new File("./" + fname);
                AudioInputStream audiostream = AudioSystem
                        .getAudioInputStream(audiofile);

                AudioFormat format = new AudioFormat(audiostream.getFormat()
                        .getSampleRate(), 16, 2, true, false);
                AudioInputStream convaudiostream = AudioSystem.getAudioInputStream(
                        format, audiostream);

                /*
                 * Read the content of the file into a byte array.
                 */

                int datalength = (int) convaudiostream.getFrameLength()
                        * format.getFrameSize();
                byte[] data = new byte[datalength];
                convaudiostream.read(data, 0, data.length);
                audiostream.close();

                /*
                 * Create SoundFont2 sample.
                 */

                SF2Sample sample = new SF2Sample(sf2);
                sample.setName(fname);
                sample.setData(data);
                sample.setSampleRate((long) format.getSampleRate());
                sample.setOriginalPitch(60 + i);
                sf2.addResource(sample);
                i++;

                /*
                 * Create region for layer.
                 */
                SF2LayerRegion region = new SF2LayerRegion();
                region.putInteger(SF2Region.GENERATOR_RELEASEVOLENV, 12000);
                region.setSample(sample);

                layer.getRegions().add(region);
            }

            /*
             * Create SoundFont2 instrument.
             */
            SF2Instrument ins = new SF2Instrument(sf2);
            ins.setName("Back Instrument");
            ins.setPatch(new Patch(0, 0));
            sf2.addInstrument(ins);

            /*
             * Create region for instrument.
             */
            SF2InstrumentRegion insregion = new SF2InstrumentRegion();
            insregion.setLayer(layer);
            ins.getRegions().add(insregion);

            return sf2;

        }
    }

EDIT: I seem to listen to all the samples at once. They are played at the same time, so I just realized the following: I am only setting the original pitch of the sample, but nowhere do I set the scope, i.e. I do not assign the samples to certain midi keys. Where can I do that?

Was it helpful?

Solution

I found the solution myself after reading some Gervill source code. Using GENERATOR_KEYRANGE one can define the range of tone pitches for a specific sample:

        SF2LayerRegion region = new SF2LayerRegion();
        region.putInteger(SF2Region.GENERATOR_RELEASEVOLENV, 12000); 
        region.putBytes(SF2Region.GENERATOR_KEYRANGE, new byte[]{(byte)(60+i),(byte)(60+i)});
        i++;
        region.setSample(sample);
        layer.getRegions().add(region);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top