[英]Add multiple samples to one instrument in Gervill SF2Soundbank
我正在使用Gervill來創建帶有樂器的音庫。 我已經為每個音高記錄了一個樣本,現在我想將這些樣本放入一個樂器中。 到目前為止,我使用的文檔是來自openjdk6源代碼的測試。 除此之外,我找到了卡爾·赫爾加森(Karl Helgason)的例子,這很有幫助。 該示例將音頻文件加載到音庫中,但是每個樂器僅使用一個樣本。 我已經修改了他的示例文件,當我使用聲音庫進行重放時,似乎僅使用了一個樣本並根據請求的音高進行了音高調整。 相反,我想針對每個音高使用特定的樣本。 我懷疑我的for循環是圍繞該方法的錯誤部分構建的,並且另外一個示例正在覆蓋以前保存的示例。
我的問題是每個樣本應分別具有哪些部分:層? 還是該地區? 都? 不幸的是,Gervill術語似乎與我發現的另一術語略有不同,因此我有些困惑。
我使用了以下源代碼(我在更改后的源代碼中保留了版權說明,我不是律師,因此不確定這樣做是否正確。):
/*
* 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.