/**
   * Creates a new oscillator from a preset in a SoundFont file.
   *
   * @param frequency - The frequency determines what key was pressed.
   * @parma preset - The SoundFont preset to get the sample data from.
   * @param sampleRateInHz - The sample rate of the output synthesizer.
   */
  public SoundFontOscillator(FrequencyProvider frequency, Preset preset, double sampleRateInHz) {
    super(frequency);
    logger_ = Logger.getLogger(getClass().getName());
    preset_ = preset;
    sampleRateInHz_ = sampleRateInHz;

    // Find the max sample length.
    maxSampleLength_ = 1;
    for (Zone pzone : preset_.getZoneList()) {
      Instrument instrument = pzone.getInstrument();
      if (instrument != null) {
        for (Zone izone : instrument.getZoneList()) {
          Sample sample = izone.getSample();
          int length = (int) Math.ceil((sampleRateInHz_ * izone.getCount()) / sample.getRate());
          if (length > maxSampleLength_) {
            maxSampleLength_ = length;
          }
        }
      }
    }

    currentSample_ = null;
    currentSampleIndex_ = 0;

    buffer_ = new double[maxSampleLength_];
    Arrays.fill(buffer_, 0.0);
    bufferIndex_ = 0;
  }
 /** Initializes the "current sample" based on the given key and velocity. */
 private synchronized void initSample(int key, int velocity) {
   // Find any sample in this preset that can handle the given key and velocity.
   currentSample_ = null;
   for (Zone pzone : preset_.getZoneList()) {
     Instrument instrument = pzone.getInstrument();
     if (instrument != null) {
       for (Zone izone : instrument.getZoneList()) {
         if (izone.inKeyRange(key)
             && izone.inVelocityRange(velocity)
             && izone.getSample() != null) {
           currentSample_ = izone;
           currentSampleIndex_ = currentSample_.getStart();
           return;
         }
       }
     }
   }
 }