// ----
// ---- file   : CustomSynthInstrument.tks
// ---- author : Bastian Spiegel <bs@tkscript.de>
// ---- legal  : (c) 2011 by Bastian Spiegel. 
// ----          Distributed under terms of the GNU LESSER GENERAL PUBLIC LICENSE (LGPL). See 
// ----          http://www.gnu.org/licenses/licenses.html#LGPL or COPYING for further information.
// ----
// ---- info   : This is part of the "syntracker" midi sequencer.
// ----
// ---- created: 06Apr2011
// ----          
// ---- changed: 
// ----          
// ----
// ----

module MCustomSynthInstrument;

use tkcustomsynth;


class CustomSynthInstrument : ST_Instrument {

   CustomSynth *synth;


   public virtual init() {
      ST_Instrument::init();
   }

   public virtual copyFrom(CustomSynthInstrument _ins, int _appendixNr) {
      if(_ins instanceof CustomSynthInstrument)
      {
         ST_Instrument::copyFrom(_ins, _appendixNr);
      }
   }
   
   public virtual noteOn(float _note, _vel, int _noteOffTicks, boolean _bResetInstrument, int _trackNr) : ST_Voice {
      explain "Allocate/start voice. Returns voice object. Do not start voice immediately.";
      
      trace "xxx CustomSynthInstrument::noteOn: note="+_note+" vel="+_vel+" ticks="+_noteOffTicks+" bReset="+_bResetInstrument;

      if(null != generator)
      {
         CustomSynthVoice voice <= generator.allocVoice(this, _note, _vel, _noteOffTicks, _trackNr);
         if(null != voice)
         {
            trace "xxx CustomSynthInstrument::noteOn: voice.key="+voice.voice_nr+" voice="+#(voice);
            voice.setInstrument(this);
            voice.queueNoteOn(_note, _vel, _noteOffTicks);
            return voice;
         }
      }
      return null;
   }

   public virtual startVoice(STX_AudioVoice _voice, float _note, _vel, int _noteOffTicks, boolean _bResetInstrument) {
      // never called for audio voices. the actual noteOn happens in CustomSynthVoice.endFrame() (after ctls have been set)
   }

   public virtual getPolyphony() : int {
      return 1; // monophonic
   }

}
