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