//    Sfront, a SAOL to C translator
//    Copyright (C) 2006 John Lazzaro
//    This file: Multi-sampled piano written in SAOL.
//    Maintainers's address: lazzaro@eecs.berkeley.edu;
// 
// This license covers also covers SASL file in this directory.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation (Version 1, 1989).
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


global { 
  srate 44100; 
  krate 1050; 
  table right_060_mf(sample, -1, 
  "/Users/lazzaro/work/class/mu209/lec/feb23/saol/samples/right/060_C3KM56_M.wav");
} 
 

instr piano_proto (pitch, vel) preset 0 { 

  imports exports table right_060_mf;           /* 44100 Hz samplerate */
  ivar rel_time, full_scale, volume;
  ksig k, rel;
  asig i;

  // **********************
  // computed during i-pass
  // **********************

  rel_time = 0.250;
  full_scale = 0.25;  
  volume = full_scale*(vel/127);  // NoteOn velocity scales volume

  // **********************
  // computed during k-pass
  // **********************

  if (released && !rel)          // Add release time when NoteOff occurs
    {
      rel = 1;
      extend(rel_time); 
    }
  
  if (!rel && (k > ftlen(right_060_mf) - rel_time*s_rate - 2*(s_rate/k_rate)))
  {
      turnoff;   // Force NoteOff before we run out of sample to play
    }

  k = k + (s_rate/k_rate);

  // **********************
  // computed during a-pass
  // **********************
      
  if (!rel)    // Attack and sustain portion of note
    {
      output(volume*tableread(right_060_mf, i));
    }
  else        // Release envelope after NoteOff
    {
      output(aline(volume, rel_time, 0)*tableread(right_060_mf, i));
    }
  i = i + 1;
}