我正在使用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键。我在哪里可以做?

最佳答案

阅读一些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);

08-24 21:45