1 /* 2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import java.io.IOException; 27 28 import javax.sound.midi.*; 29 import javax.sound.sampled.*; 30 31 import com.sun.media.sound.*; 32 33 public class SoftTestUtils { 34 35 public AudioSynthesizer synth = new SoftSynthesizer(); 36 public AudioInputStream stream; 37 public byte[] tmpbuffer = new byte[1024]; 38 39 public static SF2Soundbank createTestSoundBank() 40 { 41 SF2Soundbank sf2 = new SF2Soundbank(); 42 AudioFormat format = new AudioFormat(44100, 16, 1, true, false); 43 float[] data = new float[44100+1000]; 44 float fr = 440/format.getSampleRate(); 45 for (int i = 0; i < data.length; i++) 46 data[i] = (float)Math.sin(i*fr*2*Math.PI); 47 byte[] bdata = new byte[data.length*format.getFrameSize()]; 48 AudioFloatConverter.getConverter(format).toByteArray(data, bdata); 49 SF2Sample sample = new SF2Sample(sf2); 50 sample.setName("Test Sample"); 51 sample.setData(bdata); 52 sample.setStartLoop(500); 53 sample.setEndLoop(data.length - 500); 54 sample.setSampleRate((long) format.getSampleRate()); 55 sample.setOriginalPitch(69); 56 sf2.addResource(sample); 57 SF2Layer layer = new SF2Layer(sf2); 58 layer.setName("Test Layer"); 59 sf2.addResource(layer); 60 SF2LayerRegion region = new SF2LayerRegion(); 61 region.putInteger(SF2Region.GENERATOR_SAMPLEMODES, 1); 62 region.setSample(sample); 63 layer.getRegions().add(region); 64 SF2Instrument ins = new SF2Instrument(sf2); 65 ins.setName("Test Instrument"); 66 sf2.addInstrument(ins); 67 SF2InstrumentRegion insregion = new SF2InstrumentRegion(); 68 insregion.setLayer(layer); 69 ins.getRegions().add(insregion); 70 71 return sf2; 72 } 73 74 public SoftTestUtils() throws Exception { 75 stream = synth.openStream(null, null); 76 synth.unloadAllInstruments(synth.getDefaultSoundbank()); 77 synth.loadAllInstruments(createTestSoundBank()); 78 } 79 80 public void close() throws Exception { 81 stream.close(); 82 stream = null; 83 synth.close(); 84 synth = null; 85 } 86 87 public void read(double seconds) throws IOException 88 { 89 int bufflen = 90 stream.getFormat().getFrameSize() * 91 (int)(stream.getFormat().getFrameRate() * seconds); 92 while(bufflen != 0) 93 { 94 if(bufflen > 1024) 95 bufflen -= stream.read(tmpbuffer,0,1024); 96 else 97 bufflen -= stream.read(tmpbuffer,0, bufflen); 98 } 99 } 100 101 public VoiceStatus findVoice(int channel, int note) { 102 VoiceStatus[] v = synth.getVoiceStatus(); 103 for (int k = 0; k < v.length; k++) 104 if(v[k].active) 105 if(v[k].channel == channel) 106 if(v[k].note == note) 107 return v[k]; 108 return null; 109 } 110 111 }