Path: blob/master/test/jdk/javax/sound/midi/Gervill/SoftReceiver/SoftTestUtils.java
41159 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;2425import javax.sound.midi.*;26import javax.sound.sampled.*;2728import com.sun.media.sound.*;2930public class SoftTestUtils {3132public AudioSynthesizer synth = new SoftSynthesizer();33public AudioInputStream stream;34public byte[] tmpbuffer = new byte[1024];3536public static SF2Soundbank createTestSoundBank()37{38SF2Soundbank sf2 = new SF2Soundbank();39AudioFormat format = new AudioFormat(44100, 16, 1, true, false);40float[] data = new float[44100+1000];41float fr = 440/format.getSampleRate();42for (int i = 0; i < data.length; i++)43data[i] = (float)Math.sin(i*fr*2*Math.PI);44byte[] bdata = new byte[data.length*format.getFrameSize()];45AudioFloatConverter.getConverter(format).toByteArray(data, bdata);46SF2Sample sample = new SF2Sample(sf2);47sample.setName("Test Sample");48sample.setData(bdata);49sample.setStartLoop(500);50sample.setEndLoop(data.length - 500);51sample.setSampleRate((long) format.getSampleRate());52sample.setOriginalPitch(69);53sf2.addResource(sample);54SF2Layer layer = new SF2Layer(sf2);55layer.setName("Test Layer");56sf2.addResource(layer);57SF2LayerRegion region = new SF2LayerRegion();58region.putInteger(SF2Region.GENERATOR_SAMPLEMODES, 1);59region.setSample(sample);60layer.getRegions().add(region);61SF2Instrument ins = new SF2Instrument(sf2);62ins.setName("Test Instrument");63sf2.addInstrument(ins);64SF2InstrumentRegion insregion = new SF2InstrumentRegion();65insregion.setLayer(layer);66ins.getRegions().add(insregion);6768return sf2;69}7071public SoftTestUtils() throws Exception {72stream = synth.openStream(null, null);73synth.unloadAllInstruments(synth.getDefaultSoundbank());74synth.loadAllInstruments(createTestSoundBank());75}7677public void close() throws Exception {78stream.close();79stream = null;80synth.close();81synth = null;82}8384public void read(double seconds) throws IOException85{86int bufflen =87stream.getFormat().getFrameSize() *88(int)(stream.getFormat().getFrameRate() * seconds);89while(bufflen != 0)90{91if(bufflen > 1024)92bufflen -= stream.read(tmpbuffer,0,1024);93else94bufflen -= stream.read(tmpbuffer,0, bufflen);95}96}9798public VoiceStatus findVoice(int channel, int note) {99VoiceStatus[] v = synth.getVoiceStatus();100for (int k = 0; k < v.length; k++)101if(v[k].active)102if(v[k].channel == channel)103if(v[k].note == note)104return v[k];105return null;106}107108}109110111