将多个样品添加到Gervill SF2Soundbank中的一台仪器上

时间:2013-02-15 16:19:51

标签: java midi openjdk sample gervill

我正在使用Gervill用乐器创建一个音库。我为每个音调记录了一个样本,现在我想将这些样本放入一个乐器中。 我到目前为止使用的文档是来自openjdk6源代码的测试。除此之外,我找到了Karl Helgason的一个例子,这有很大帮助。该示例将音频文件加载到音库中,但每个乐器仅使用一个样本。我修改了他的示例文件,当我使用声音库进行重放时,似乎只使用了一个样本并根据请求的音调进行调整。相反,我希望每音调使用一个特定的样本。 我怀疑我的for循环是围绕方法的错误部分构建的,另外一个样本会覆盖以前保存的那个。

我的问题是每个样本应该分开哪些部分:图层?还是这个地区?都?不幸的是Gervill术语似乎与我发现的稍微不同from another one,所以我有点困惑。

我使用了以下源代码(我在修改后的源代码中留下了版权说明,我不是律师,所以我不确定这是否正确。):

/*
 * 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;

        }
    }
编辑:我似乎立即听取了所有样本。它们是同时播放的,所以我只是意识到以下内容:我只是设置了样本的原始音高,但我没有设置范围,即我没有将样本分配给某些midi键。我在哪里可以做到?

1 个答案:

答案 0 :(得分:1)

我在阅读了一些Gervill源代码后自己找到了解决方案。使用GENERATOR_KEYRANGE可以定义特定样本的音调范围:

        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);