2

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

我的问题是每个样本应该分别有哪些部分:层?还是地区?两个都?不幸的是,格维尔术语似乎与我发现的另一个术语略有不同,所以我有点困惑。

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

/*
 * 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 键。我在哪里可以这样做?

4

1 回答 1

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);
于 2013-02-18T09:38:39.233 回答