import java.awt.*;
import java.util.Arrays;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.util.EventObject;
import java.util.Vector;
import javax.sound.sampled.*;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.SourceDataLine;
import javax.swing.*;
import javax.sound.sampled.AudioFormat;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Enumeration;
import java.io.*;
public class Wav extends Thread
 {
  int autoscale=1;
  int autowig=1;
  int tracecount=0;
  int tc=20;
  boolean graphrms=false; 
  boolean graphpeaks=false; 
  boolean graphdiff=false;
  static String type="Wav";
  int tflx=0;
  double tfsc=100000;
  int tfw=128;
  double d1=26000,d2=22000,d3=18000,d4=14000,d5=2000;
  double e1=1.1,e2=1.2,e3=1.3,e4=1.4,e5=1.6;
  int opoplx=0;
  int poppkint=0;
  boolean busy=false;
  boolean deb=true;
  boolean popoff=true;
  long popss=5000;
  double popmx=0;
  double popd=0;
  int popmxi=0;
  int popki=0;
  int popadv=10;
  int poppost=40;
  double popmxth=45;
  double popdy=0;
  double popddy=0;
  int popdyi=0;
  int popddyi=0;
  double popdyth=20;
  double popddyth=20;
  int popri=0;
  int popti=0;
  long tymo=800000,tyma=0,tymb=0;
  int ti=1;
  int disres=0;
  int dispa=1;
  int dispb=1;
  double offay=100;
  double offby=-100;
  double offy=0;
  int msab=0,msbb=0,msae=0,msbe=0,msa=0,msb=0;
  double mutefactor=.01;
  int beth=1000;
  int bedx=1000;
  int beax=0,bebx=0;
  int becut=0;
  int locmax=0;
  int crms=0;
  int trackguess=0,orms=0,czrms=0,tcrms=5800;
  int flower=8;
  boolean fc=true;
  int gb=1;
  Thread thr;
  long maxlength=365000000;
  boolean saveable;
  boolean stopable;
  int expo=2;
  int stpth=0;
  int lvlth=0;
  int conoff=0;
  int kth=0;
  int coth=0;
  int fsib;
  int blif;
  int blib;
  int nbr=0;
  boolean flashon=false;
  boolean flashoff=false;
  int rnglx=0;
  int rngux=0;
  int rmsss=8;
  short apk=0;
  short bpk=0;
  byte[] buff1;
  byte[][] budd;
  int buffi=0;
  int mode=0;
  boolean psdone=false;
  boolean boo;
  Rec rbrec=new Rec(0,0,1,1);
  double dx,glx,gly,gwx,gwy;
  double grscy=.01;
  double xa;
  int pkint=0;
  Te te;
  LC lc; 
  dP lcsc=new dP(1,1);
  Tu tu;
  static final double PI=Math.PI;
  static final double DR=PI/180;
  static final double RD=180/PI;
  int deg=2;
  int tpts;
  int mi=-1;
  int bi=0;
  int ei=0;
  double x=0,y=0;
  double orgpw=3;
  Point p;
  // Point2D p2d;
  dP dp;
  dP[] dpa=new dP[0];
  dP[] dpb=new dP[0];
  Rec[] reca=new Rec[0];
  Rec[] recb=new Rec[0];
  int msdpa=-1;
  int msdpb=-1;
  int pmode=1;
  dP odpa,odpb;
  Graphics2D g;
  boolean handles=true;
  boolean cs=true;
  double pw=3;
  boolean bige=false;
  boolean mkl=false;
 
 
  int bs;
  int ibs=(int)(2*2*10000000);
  double rf=440;
  double lf=880;
  int rc=1000;
  byte sd[];
  int lx=0;
  int olx=0;
  int ow=0;
  int w=0;
  int wig=7;
  int owig=0;
  boolean fixlxw=true;
  ByteArrayOutputStream baos;
  TargetDataLine tdline;
  SourceDataLine sdline;
  AudioFormat format;
  Info sdlineInfo;
  int capbufsize=65536;
  int playbufsize=65536;
  //int playbufsize=1024;
  boolean announce=true;
  int chs=1;
  
  int bcs;
  static M parent;
  short[] ca;
  short[] cb;
  int cai=0;
  int chbi=0;
  short[] cap;
  short[] cbp;
  short[] capk2,capk3,capk4,capk5,capk6,capk7,capk8,capk9,capk10,capk11,capk12,capk13,capk14,capk15,capk16,capk17,capk18,capk19,capk20,capk21,capk22,capk23,capk24;
  short[] cbpk2,cbpk3,cbpk4,cbpk5,cbpk6,cbpk7,cbpk8,cbpk9,cbpk10,cbpk11,cbpk12,cbpk13,cbpk14,cbpk15,cbpk16,cbpk17,cbpk18,cbpk19,cbpk20,cbpk21,cbpk22,cbpk23,cbpk24;
  short[] bca;
  short[] bcb;
  int[] pkaboo=new int[30];
  int peakint=0; 
  String name="wav";
  String fn="notset";
  AudioFormat af;
  byte[] a;
  float samplerate;
  float blockrate; // framerate
  double blockbytes; //framesize
  int samplebits; // sampleSizeInBits
  double databytes;
  long chshorts;
  double secs;
  float stdbr=44100;
  // 8,000 Hz	Adequate for human speech but without sibilance. Used in telephone/walkie-talkie.
  // 11,025 Hz	Used for lower-quality PCM, MPEG audio and for audio analysis of subwoofer bandpasses.
  // 16,000 Hz	Used in most VoIP and VVoIP, extension of telephone narrowband.
  // 22,050 Hz	Used for lower-quality PCM and MPEG audio and for audio analysis of low frequency energy.
  // 44,100 Hz	Audio CD, most commonly used rate with MPEG-1 audio (VCD, SVCD, MP3). Covers the 20 kHz bandwidth.
  //float sampleRate, int sampleSizeInBits, int channels, int frameSize, float frameRate,
  float scbr=stdbr;
  float scbp=(float)1.0/stdbr;
  public Wav(M p)
   {
    tta("Wav constructor no args");
    thr=new Thread(this,"main");
    parent=p;
    name="jj";
    tu=parent.tu;
    lc=parent.lc;
    lc.hh.put(name,type);
   }
  public Wav(M p,String n) // argWav(i)  argWav()
   {
    thr=new Thread(this,"main");
    parent=p;
    name=n;
    tu=parent.tu;
    lc=parent.lc;
    lc.hh.put(name,type);
    precommb();
   }
  public Wav(M p,String n,String f) // most common aWav p("jojo")
   {
    thr=new Thread(this,"main");
    parent=p;
    name=n;
    tu=parent.tu;
    lc=parent.lc;
    lc.hh.put(name,type); 
    fn=f;
    a=parent.fio.readbytefile(fn,maxlength);// has all header info.
    chs=mkai(22);
    samplerate=mkaf(24);
    blockrate=mkaf(28);
    blockbytes=mkai(32);
    samplebits=mkai(34); 
    databytes=mkaf(40); 
    chshorts=(int)databytes/(chs*(samplebits/8));  
    ba2cab();
    lx=0;
    w=(int)chshorts;
    setWig();
    parent.te.teS("lx.ul "+ca.length+" = lx "+lx+" w.ul "+ca.length+" = w "+w+" = wig"+wig);
    parent.te.teS("ux.ul "+ca.length+" center.ul "+ca.length);
    parent.te.teS("autowig.i "+autowig+" autoscale.i "+autoscale);
    tta("lx.ul "+chshorts+" = lx "+lx+" w.ul "+ca.length+" = w "+w+" = wig"+wig);
    tta("Wav constructor file read chshorts:"+chshorts+" ca.length:"+ca.length);
   }
  public Wav(M p,String n,String s,long m) //argWav(i)
   {
    thr=new Thread(this,"main");
    parent=p;
    name=n; 
    tu=parent.tu;
    lc=parent.lc;
    lc.hh.put(name,type);
    maxlength=m;
    combo(s);
   }
  public Wav(M p,String n,byte[] ba) //argWav(i)
   {
    thr=new Thread(this,"main");
    parent=p;
    name=n; 
    databytes=ba.length;
    a=ba;
    tu=parent.tu;
    lc=parent.lc;
    lc.hh.put(name,type);
    conn();
    ba2cab();
   }
  public Wav(M p,String n,int sb,float sr,int ch,double seconds) 
   {
	  
	    samplebits=sb; 
	    samplerate=sr; 
	    chs=ch;
	    secs=seconds; 
    thr=new Thread(this,"main"); // for schleep
    parent=p;
    name=n;  
    fn="/webroot/ajlogo/html/ajlogo/version3beta/audio/"+n+".wav";
    tu=parent.tu;
    lc=parent.lc; 
    lc.hh.put(name,type);
    mode=2; 
    if (sr != 192000 && sr != 176400 && sr != 96000 && sr != 88200 && sr != 64000 && sr != 48000 && sr != 44100 && sr != 32000 && sr != 22050 && sr != 16000 && sr != 11025 && sr != 8000 && sr != 6000 && sr != 3000 && sr != 1500 && sr != 750 && sr != 325) 
     {
      tta("bad samplerate:"+sr);
      return;
     }
     calcparams();
   
    w=(int)chshorts;
    lx=0;
    parent.te.teS("lx.ul "+ca.length+" = lx "+lx);
    parent.te.teS("w.ul "+ca.length+" = w "+w);
    //wig=7;
    parent.te.teS("= wig "+wig);
    psdone=true;
    
    conn();
    tta("Wav.constructor bottom");
   }
  public void calcparams()
  {
  blockbytes=chs*(samplebits/8);
  blockrate=(float)(samplerate*blockbytes);  
  chshorts=Math.round(secs*samplerate);
  //databytes=Math.round(chs*secs*samplerate*samplebits/8);  
  databytes=Math.round(chshorts*chs*samplebits/8);
  a=new byte[44+(int)databytes];  
  ca=new short[(int)chshorts];
  cb=new short[(int)chshorts]; 
  mkhead(); 
 
 }
  public void mkhead()  
   { 
    a[0]='R';
    a[1]='I';
    a[2]='F';
    a[3]='F';
    a[8]='W';
    a[9]='A';
    a[10]='V';
    a[11]='E';
    a[12]='f';
    a[13]='m';
    a[14]='t';
    a[15]=' ' ;
    a[16]=0x10;
    a[17]=0x0;
    a[18]=0x0;
    a[19]=0x0;
    a[20]=0x1;
    a[21]=0x0;
    a[36]='d';
    a[37]='a';
    a[38]='t';
    a[39]='a';
    setbade(a,36+databytes,4);   // 4-7 
    setbaie(a,chs,22); // 22-23
    setbade(a,samplerate,24); // 24-27
    setbade(a,blockrate,28);  // 28-31
    setbade(a,blockbytes,32); // 32-33
    setbaie(a,samplebits,34); // 34-35
    setbade(a,databytes,40);  // 40-43
   }
  public Wav(M p,String n,double[] da)
   {
    thr=new Thread(this,"main");
    parent=p;
    name=n;  
    tu=parent.tu;
    lc=parent.lc;
    tta("Fix Wav(da) lc.hh.put");
    lc.hh.put(name,type);
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    int chansamples=da.length; // number of (16 bit) channelsamples
    secs=chansamples/samplerate; 
    databytes=(int)(4*samplerate*secs);
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    for(int i=0;i<chansamples;i++)
     { 
      ca[i]=(short)(int) da[i];
      cb[i]=(short)(int) da[i];
      cabi2ba(i);
     }
    tta("Wav.da2Wav done   chshorts:"+chshorts);  
    // ba2cab();
    // commb();
    w=(int)chshorts;
    lx=0;
    parent.te.teS("lx.ul "+ca.length+" = lx "+lx);
    parent.te.teS("w.ul "+ca.length+" = w "+w);
    //wig=7;
    parent.te.teS("= wig "+wig);
    psdone=true;
   }
  void setBitrate(float sr)
   {
    if (sr != 192000 && sr != 176400 && sr != 96000 && sr != 88200 && sr != 64000 && sr != 48000 && sr != 44100 && sr != 32000 && sr != 22050 && sr != 16000 && sr != 11025 && sr != 8000 && sr != 6000 && sr != 3000 && sr != 1500 && sr != 750 && sr != 325) 
     {
      tta("bad samplerate:"+sr);
      return;
     }
    samplerate=sr; 
    //  samplebits=16;
    //  chs=2;
    //  blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    //  int chansamples=da.length; // number of (16 bit) channelsamples
    //databytes=(int)(4*chansamples);
    //databytes=(int)a.length-44;
    //  samplerate=mkaf(24);
    //  blockrate=mkaf(28);
    setbade(a,samplerate,24);
    setbade(a,blockrate,28);
    format=new AudioFormat((float)samplerate,samplebits,chs,true,bige);
    tta("Wav.setBitrate complete mkl() ");
   }
  void precommb()
   {
    // tu=parent.tu;
    //  lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=0;
    a=new byte[44];
    // set te.
    // lc.hh.put(name,type); 
    commb();
   }
  void combo(String f)
   {
    fn=f;
    a=parent.fio.readbytefile(fn,maxlength);
    tta("Wav.combo post using old readbyte method");
    //a=parent.fio.readByteArrayFile(fn,maxlength);
    //tta("post using new readbyte method");
    chs=mkai(22);
    samplerate=mkaf(24);
    blockrate=mkaf(28);
    blockbytes=mkai(32);
    samplebits=mkai(34);
    af=new AudioFormat((float)samplerate,samplebits,chs,true,false);
    tta("combo format:"+af.toString());
    databytes=mkaf(40);
    int l=a.length-44;
    if(databytes != l)
     {
      tta("header reports:"+databytes);
      tta("file says     :"+l);
      databytes=l;
      chshorts=(int)databytes/(chs*(samplebits/8));
      tta("Saving repaired file");
      commb(); 
      //save("C:/webroot/ajlogo/html/ajlogo/version3beta/audio/fixed.wav");
      //tta("C:/webroot/ajlogo/html/ajlogo/version3beta/audio/fixed.wav saved");
      save("/webroot/ajlogo/html/ajlogo/version3beta/audio/fixed.wav");
      tta("combo Saved: /webroot/ajlogo/html/ajlogo/version3beta/audio/fixed.wav");
     }
    else
     {
      chshorts=(int)databytes/(chs*(samplebits/8));
      // commb maybe
      tta("Wav.combo file read chshorts:"+chshorts);
     }
    psdone=false;
   }
  byte slea(short s)
   {
    return (byte)s;
   }
  byte sleb(short s)
   {
    return (byte)(s>>>8);
   }
  short leabs(byte h,byte l)
   {
    return (short)(h<<8 | l & 0xff);
   }
  void conn()
   {
    chs=mkai(22);
    samplerate=mkaf(24);
    blockrate=mkaf(28);
    blockbytes=mkai(32);
    samplebits=mkai(34);
    databytes=mkaf(40);
    chshorts=(int)databytes/(chs*(samplebits/8));
    int l=ca.length;
    if(l == chshorts) tta("Wav.conn GO chshorts equal to ca.length:"+chshorts);
    else tta("Wav.conn ERROR chshorts:"+chshorts+" ca.length:"+l);
    tta("secs:"+secs+" chs:"+chs+"  samplerate:"+samplerate+" blockrate:"+blockrate);
    tta("samplebits:"+samplebits+"  blockbytes:"+blockbytes);
    tta("databytes:"+databytes); 
    tta("Wav.conn chshorts:"+chshorts+" ca.length:"+l); 
   }
  short cainterp(double ind)
   {
    if(ind+1<ca.length)
     {
      int indi=(int)ind;
      double moe=ind-indi;
      return (short)((ca[indi+1]-ca[indi])*moe+ca[indi]);  
     }
    else return 0;
   }
  short chbinterp(double ind)
   {
    if(ind+1<cb.length)
     {
      int indi=(int)ind;
      double moe=ind-indi;
      return (short)((cb[indi+1]-cb[indi])*moe+cb[indi]);
     }
    else return 0;
   }
  int mkai(int i)
   {
    int aa=a[i++];
    int bb=a[i];
    if(aa <0)aa+=256;
    if(bb <0)bb+=256;
    return aa+(bb<<8); 
   }
  float mkaf(int ind)
  {
	  int i=ind;
   int aa=0;
   float rez=0;
   for(int t=0;t<25;t+=8) // 0 8 16 24
    {
     aa=a[i++];
     if(aa <0) aa+=256;
     if(t==0) rez=aa;
     else rez+=(aa<<t);
    }
   return rez;
  }
  double mkad(int i)
   {
	  tta("Wav.mkad use mkaf");
    int aa=0;
    double rez=0;
    for(int t=0;t<25;t+=8)
     {
      aa=a[i++];
      if(aa <0) aa+=256;
      if(t==0) rez=aa;
      else rez+=(aa<<t);
     }
    return rez;
   }
  int getbaie(byte[] z,int i)
   {
    int aa=z[i++];
    int bb=z[i];
    if(aa <0)aa+=256;
    if(bb <0)bb+=256;
    return aa+(bb<<8); 
   }
  double getbade(byte[] z,int i)
   {
    int aa=0;
    double rez=0;
    for(int t=0;t<25;t+=8)
     {
      aa=z[i++];
      if(aa <0) aa+=256;
      if(t==0) rez=aa;
      else rez+=(aa<<t);
     }
    return rez;
   }
  void comma()
   {
   }
  //static byte[] stdcommb(double databytes)
  byte[] stdcommb(double databytes)
   {
    return  commb(new byte[44+(int)databytes],databytes,2,(double)stdbr,(double)4*stdbr,4,16);
   }
  void commb()
   {
    if(a==null)
     {
      tta("a is null");
      return;
     }
    this.a=commb(a);
   }
  byte[] commb(byte[] a)
   {
    return commb(a,databytes,chs,samplerate,blockrate,blockbytes,samplebits);
   }
  byte[] commb(byte[] a,double databytes,int chs,double samplerate,double blockrate,double blockbytes,int samplebits)
   {
    a[0]='R';
    a[1]='I';
    a[2]='F';
    a[3]='F';
    setbade(a,36+databytes,4);
    Double dd=36+databytes;
    int ii=dd.intValue();
    String hs=Integer.toHexString(ii);
    //tta("36+databytes hexstring:"+hs+" dd:"+dd+" ii:"+ii);
    //tta("a[4]:"+a[4]+" a[5]:"+a[5]+" a[6]:"+a[6]+" a[7]:"+a[7]);
    String hs4=Integer.toHexString(a[4]);
    String hs5=Integer.toHexString(a[5]);
    String hs6=Integer.toHexString(a[6]);
    String hs7=Integer.toHexString(a[7]);
    //tta("a[4]:"+hs4+" a[5]:"+hs5+" a[6]:"+hs6+" a[7]:"+hs7);
    a[8]='W';
    a[9]='A';
    a[10]='V';
    a[11]='E';
    a[12]='f';
    a[13]='m';
    a[14]='t';
    a[15]=' ' ;
    a[16]=0x10;
    a[17]=0x0;
    a[18]=0x0;
    a[19]=0x0;
    a[20]=0x1;
    a[21]=0x0;
    setbaie(a,chs,22);
    setbade(a,samplerate,24);
    setbade(a,blockrate,28);
    setbade(a,blockbytes,32);
    setbaie(a,samplebits,34);
    a[36]='d';
    a[37]='a';
    a[38]='t';
    a[39]='a';
    setbade(a,databytes,40);
    return a;
   }
  void setbade(byte[] z,double vv,int ind) // sets element ind in the byte array z with the double value vv
   {
    long v=(long)vv;
    byte b0=(byte)(v&0xff);
    byte b1=(byte)((v&0xff00)>>8);
    byte b2=(byte)((v&0xff0000)>>16);
    byte b3=(byte)((v&0xff000000)>>24);
    //tta("Wav.setbaie long v:"+v+" ind:"+ind+" b0:"+b0+" b1:"+b1+" b2:"+b2+" b3:"+b3);
    z[ind++]=(byte)(v&0xff);
    z[ind++]=(byte)((v&0xff00)>>8);
    z[ind++]=(byte)((v&0xff0000)>>16);
    z[ind]=(byte)((v&0xff000000)>>24); 
   } 
  void setbaie(byte[] z,int v,int ind) // sets element ind in the byte array z with the int value v
   {
    byte b0=(byte)(v&0xff);
    byte b1=(byte)(v&0xff00>>8);
    // tta("Wav.setbaie int v:"+v+" ind:"+ind+" b0:"+b0+" b1:"+b1);
    z[ind++]=(byte)(v&0xff);
    z[ind]=(byte)((v&0xff00)>>8);
   }
  void mka(double vv,int ind)
   {
    long v=(long)vv;
    a[ind++]=(byte)(v&0xff);
    a[ind++]=(byte)((v&0xff00)>>8);
    a[ind++]=(byte)((v&0xff0000)>>16);
    a[ind]=(byte)((v&0xff000000)>>24);
   }
  void mka(int v,int ind)
   {
    a[ind++]=(byte)(v&0xff);
    a[ind]=(byte)((v&0xff00)>>8);
   }
  void ed()
   {
    tta1(name+".fn "+fn);
    tta1(name+".samplerate "+Te.Svo(samplerate));
    tta1(name+".blockrate "+Te.Svo(blockrate));
    tta1(name+".blockbytes "+Te.Svo(blockbytes));
    tta1(name+".databytes "+Te.Svo(databytes));
    tta1(name+".announce "+Te.Svo(announce));
    tta1(name+".chs "+Te.Svo(chs));
    tta1(name+".samplebits "+Te.Svo(samplebits));
    tta1(name+".chshorts "+Te.Svo(chshorts));
    tta1(name+".name "+name);
    tta1(name+".disres "+disres);
    tta1(name+".rf "+rf);
    tta1(name+".rc "+rc);
    tta1(name+".lf "+lf);
    tta1(name+".lc "+lc.name);
    tta1(name+".tu "+tu.name);
    tta1(name+".bs "+bs);
    tta1(name+".w "+w);
    tta1(name+".lx "+lx);
    tta1(name+".tpts "+Te.Svo(tpts));
    tta1(name+".pmode "+Te.Svo(pmode));
    tta1(name+".pw "+Te.Svo(pw));
    tta1(name+".rbrec "+Te.Svo(rbrec));
    tta1(name+".mode "+Te.Svo(mode));
    tta1(name+".wig "+Te.Svo(wig));
    tta1(name+".rmsss "+Te.Svo(rmsss));
    tta1(name+".crms "+Te.Svo(crms));
    tta1(name+".tcrms "+Te.Svo(tcrms));
    tta1(name+".tymo "+Te.Svo(tymo));
    tta1(name+".playbufsize "+Te.Svo(playbufsize));
    tta1(name+".capbufsize "+Te.Svo(capbufsize));
    tta1(name+".beth "+Te.Svo(beth));
    tta1(name+".bedx "+Te.Svo(bedx));
    tta1(name+".locmax "+Te.Svo(locmax));
    tta1(name+".mutefactor "+Te.Svo(mutefactor));
    tta1(name+".offay "+Te.Svo(offay));
    tta1(name+".offby "+Te.Svo(offby));
    tta1(name+".dispa "+Te.Svo(dispa));
    tta1(name+".dispb "+Te.Svo(dispb));
    tta1(name+".popmxth "+Te.Svo(popmxth));
    tta1(name+".gb "+Te.Svo(gb));
   }
  public void finalize()
   {
    sdline.stop();
    sdline.flush();
    sdline.close();
   }
  void mkl()
   {
    //if(mkl) return;
    tta("Wav.mkl force new AudioFormat SourceDataLine");
    sdline = null;
    // format=new AudioFormat(scbr,16,chs,true,bige);
    tta("Wav.mkl  format:"+format.toString());
    sdlineInfo=new Info(SourceDataLine.class, format);
    try
     {
      sdline=(SourceDataLine)AudioSystem.getLine(sdlineInfo);
      //sdline.open(format,ibs);
      sdline.open(format);
     }
    catch(LineUnavailableException e) { tta("There was no available sdline."); }
    tta("Wav.mkl()  sdline.start()");
    sdline.start();
    mkl=true;
   }
  void src()
   {
   }
  void slc()
   {
   }
  void sm(double a,int b)
   {
    rf=a;
    rc=b;
    sm();
   }
  void sm()
   {
    chs=1;
    double rpd=1.0/rf;
    double rscs=Math.round(scbr*rpd);
    //rc=500;
    bs=(int)(2*chs*rc*rscs);
    ibs=bs;
    mkl();
    sd=new byte[bs];
    int i=0;
    short t=0;
    for(int j=0;j<bs-1;j+=2)
     {
      double yValue = 32767D * Math.sin((j%rscs)/rscs*6.283185307179586D);
      if(bige)
       {
        t=(short)(int)Math.round(yValue);
        sd[j+1] = (byte)t;
        t >>= 8;
        sd[j] = (byte)t;
       }
      else
       {
        t=(short)(int)Math.round(yValue);
        sd[j] = (byte)t;
        t >>= 8;
        sd[j+1] = (byte)t;
       }
     }
   }
  void repplayWindow()  // Not playWindow
   {
    mkl=false;
    buff1=new byte[playbufsize];
    mkl();
    int j=0;
    int i=cai2bai(lx);
    int m=cai2bai(lx+w);
    stopable=false;
    mode=1;
    while(mode==1 )
     {
      while(mode==1 && i<m-playbufsize)
       { 
        buff1=Arrays.copyOfRange(a,i,i+playbufsize);  
        //for(j=0;j<playbufsize;j++) buff1[j]=a[i++];
        sdline.write(buff1,0,playbufsize);
        if(gb==1)
         {
          mkbcs(buff1);
          graphbuff();
         }
       }
      i=cai2bai(lx);
     }
    stopable=true;
    parent.te.teS("Wav.playWindow.i 0"); // depends on Bu named playWindow
   }
  void playWindow()
   {
    mkl=true;
    int j=0;
    // int i=44;    if starting from beginning
    int ux=lx+w;
    int i=cai2bai(lx);
    int m=cai2bai(ux);
    tta("Wav.playWindow lx:"+lx+" w:"+w+" ux:"+ux+" i:"+i+" m:"+m+" playbufsize:"+playbufsize);
    if( i>=m-playbufsize) 
     {
      playbufsize=m-i;
      tta("new playbufsize:"+playbufsize);
     } 
    stopable=false;
    mode=1;
    buff1=new byte[playbufsize]; 
    format=new AudioFormat((float)samplerate,samplebits,chs,true,false);
    // format=new AudioFormat(scbr,16,chs,true,bige);
    if(format==null)
     {
      tta("Wav.playWindow format is null");
      return;
     }
    tta("Wav.playWindow  format:"+format.toString());
    sdlineInfo=new Info(SourceDataLine.class, format);
    try
     {
      sdline=(SourceDataLine)AudioSystem.getLine(sdlineInfo);
      sdline.open(format);
     }
    catch(LineUnavailableException e) { tta("There was no available sdline."); }
    sdline.start(); 
    // while(mode==1 && i<a.length-playbufsize)
    // for(i=0;i<40;i++)	  {tta("Wav.playWindow inside i:"+i+" a[i]:"+a[i]);	  }
    while(mode==1 && i<m-playbufsize)
     {
      buff1=Arrays.copyOfRange(a,i,i+playbufsize);
      //for(j=0;j<playbufsize;j++) buff1[j]=a[i++];  
      //tta("Wav.playWindow inside i:"+i+" m:"+m); 
      //for(i=0;i<40;i++)    	  {      tta("Wav.playWindow inside i:"+i+" a[i]:"+a[i]);    	  }
      sdline.write(buff1,0,playbufsize); 
      if(gb==1)
       {
        mkbcs(buff1);
        graphbuff();
       }
      i+=playbufsize;
     }
    stopable=true;
    sdline.drain();
    sdline.close();
    tta("Wav.playWindow done");
    parent.te.teS("Wav.playWindow.i 0");  // depends on a Bu playWindow
   }
  void oldplayWindow()
   { 
    mkl=false;
    buff1=new byte[playbufsize];
    mkl();
    int j=0;
    int i=cai2bai(lx);
    int m=cai2bai(lx+w);
    stopable=false;
    mode=1;
    while(mode==1 && i<m-playbufsize)
     { 
      for(j=0;j<playbufsize;j++) buff1[j]=a[i++];
      tta("Wav.playWindow pbs:"+playbufsize);
      sdline.write(buff1,0,playbufsize);
      if(gb==1)
       {
        mkbcs(buff1);
        graphbuff();
       }
     }
    stopable=true; 
    parent.te.teS("Wav.playWindow.i 0"); // depends on Bu named playWindow
   }
  synchronized void a(byte[] a)
   {
    buff1=(byte[])a.clone();
   }
  synchronized byte[] a()
   {
    return buff1;
   }
  void rppw()
   {
    tta("rppw");
    rppwprep();
    rppwdu();
   }
  void rppwprep()
   {
    tta("rppwprep");
    int j=0;
    int i=cai2bai(lx);
    int m=cai2bai(lx+w);
    mkl();
    int pbs=cw2baw(w);
    buff1=new byte[100*pbs];
    int lbi=0;
    int bl=buff1.length;
    tta("lx:"+lx+"  w:"+w+"  pbs:"+pbs+"  bl:"+bl);
    for(int oj=0;oj<100;oj++)
     {
      i=cai2bai(lx);
      for(j=0;j<pbs;j++) buff1[lbi++]=a[i++];
     }
    a(buff1);// makes clone redundant??
    tta("post loops lbi:"+lbi);
   }
  void rppwdu()
   {
    stopable=false;
    mode=1;
    byte[] mba=null;
    int j=0;
    while(mode==1)
     {
      tta("rppwdu top while");
      //if(j%100 ==0){ tta("repeatwindow j:"+j);  }
      mba=a(); 
      sdline.write(mba,0,mba.length);
      //j++;
     }
    stopable=true;
    parent.te.teS("Wav.playWindow.i 0");  // depends on a Bu playWindow
   }
  void play()
  {  
   mkl=true;  
   gb=0;
   //  sb,sr,ch,secs
  /* blockbytes=chs*(samplebits/8);
   blockrate=samplerate*blockbytes;  
   chshorts=Math.round(secs*samplerate);
   //databytes=Math.round(chs*secs*samplerate*samplebits/8);  
   databytes=Math.round(chshorts*chs*samplebits/8);
   a=new byte[44+(int)databytes];  
   ca=new short[(int)chshorts];
   cb=new short[(int)chshorts]; */
   int j=0;
   int i=44;
   stopable=false;
   mode=1;
   buff1=new byte[playbufsize]; // 64k
   format=new AudioFormat(samplerate,samplebits,chs,true,bige); 
   //AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) 
   tta("Wav.play  format:"+format.toString());
   sdlineInfo=new Info(SourceDataLine.class, format); 
   try
    {
     sdline=(SourceDataLine)AudioSystem.getLine(sdlineInfo); 
     sdline.open(format);
     tta("Wav.play sdline open");
    }
   // catch(LineUnavailableException e) { tta("There was no available sdline."); }
   catch(Exception e)
    {
     tta("Wav.play error"); 
     return;
    }
   sdline.start();
   int chunk=0;
   int bs=playbufsize;
   int offset= chunk*bs;
   int alm=a.length-44;
 
   while(mode==1 && offset< alm)
    {
	   if(offset+bs > alm ) bs=alm-offset;
     tta("Wav.play while offset:"+offset+" bs:"+bs+" alm:"+alm);
     buff1=Arrays.copyOfRange(a,offset,offset+bs);         // buff1=Arrays.copyOfRange(a,i,i+playbufsize);   
     sdline.write(buff1,0,bs);
     if(gb==1)
      {
       mkbcs(buff1);
       graphbuff();
      } 
     offset+=bs;
    }
   stopable=true;
   sdline.drain();
   sdline.close(); 
   parent.te.teS("play.i 0");  // depends on a Bu play
  }
  void goodplay()
   {  
    mkl=true;  
    gb=0;
    //  sb,sr,ch,secs
   /* blockbytes=chs*(samplebits/8);
    blockrate=samplerate*blockbytes;  
    chshorts=Math.round(secs*samplerate);
    //databytes=Math.round(chs*secs*samplerate*samplebits/8);  
    databytes=Math.round(chshorts*chs*samplebits/8);
    a=new byte[44+(int)databytes];  
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts]; */
    int j=0;
    int i=44;
    stopable=false;
    mode=1;
    buff1=new byte[playbufsize]; // 64k
    format=new AudioFormat(samplerate,samplebits,chs,true,bige); 
    //AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) 
    tta("Wav.play  format:"+format.toString());
    sdlineInfo=new Info(SourceDataLine.class, format); 
    try
     {
      sdline=(SourceDataLine)AudioSystem.getLine(sdlineInfo); 
      sdline.open(format);
      tta("Wav.play sdline open");
     }
    // catch(LineUnavailableException e) { tta("There was no available sdline."); }
    catch(Exception e)
     {
      tta("Wav.play error"); 
      return;
     }
    sdline.start();
    int chunk=0;
    int bs=playbufsize;
    int offset= chunk*bs;
    int alm=a.length-44;
  
    while(mode==1 && offset+bs < alm)
     {
      tta("Wav.play while offset:"+offset+" alm:"+alm);
      buff1=Arrays.copyOfRange(a,offset,offset+bs);         // buff1=Arrays.copyOfRange(a,i,i+playbufsize);   
      sdline.write(buff1,0,bs);
      if(gb==1)
       {
        mkbcs(buff1);
        graphbuff();
       } 
      offset+=bs;
     }
     if(offset<alm)
     {
    	 bs=alm-offset;
    	  tta("Wav.play post while new bs:"+bs+" offset:"+offset+" alm:"+alm);
    	 sdline.write(buff1,0,bs);
         if(gb==1)
          {
           mkbcs(buff1);
           graphbuff();
          } 
     }
    stopable=true;
    sdline.drain();
    sdline.close(); 
    parent.te.teS("play.i 0");  // depends on a Bu play
   }
  void oldplay()
  {  
   mkl=true;  
   int j=0;
   int i=44;
   stopable=false;
   mode=1;
   buff1=new byte[playbufsize]; 
   format=new AudioFormat(scbr,16,chs,true,bige); 
   tta("Wav.play  format:"+format.toString());
   sdlineInfo=new Info(SourceDataLine.class, format); 
   try
    {
     sdline=(SourceDataLine)AudioSystem.getLine(sdlineInfo); 
     sdline.open(format);
    }
   // catch(LineUnavailableException e) { tta("There was no available sdline."); }
   catch(Exception e)
    {
     tta("Wav.play error"); 
     return;
    }
   sdline.start();
   int chunk=0;
   while(mode==1 && i<a.length-playbufsize)
    {
     buff1=Arrays.copyOfRange(a,i,i+playbufsize);   
     //for(j=0;j<playbufsize;j++) buff1[j]=a[i++];
     //tta("chunk:"+chunk);
     chunk++; 
     sdline.write(buff1,0,playbufsize);
     if(gb==1)
      {
       mkbcs(buff1);
       graphbuff();
      } 
    }
   stopable=true;
   sdline.drain();
   sdline.close(); 
   parent.te.teS("play.i 0");  // depends on a Bu play
  }
  void renderrecs() { renderrecs(0,tpts); } //tpts=w-1; 
  void renderrecs(int l,int u)
   {
    if (chs==1)    dispb=0;
    lc.spw(1);
    for(int i=l;i<u;i++) 
     {
      //tta("renderrecs i:"+i+" reca.length:"+reca.length);
      if(dispa==1)
       {
        lc.offsgsetColor(reca[i].fk.c);
        lc.offsgfill(reca[i].rect);
       }
      if(dispb==1)
       {
        lc.offsgsetColor(recb[i].fk.c);
        lc.offsgfill(recb[i].rect);
       }
     }
    lc.repaint();
    lc.spw(orgpw);
   }
  boolean mmdp(dP dp)  // mouseMoved
   {
    //tta("Wav.mmdp");
    if(! lc.rband && ! lc.wavrband) 
     {
      for(int i=0;i<tpts;i++)
       {
        if(reca[i].contains(dp))
         {
          mi=i; 
          lc.onsgsetColor(Color.green);
          lc.onsgfill(reca[i].rect);
          return true;
         }
        if(mi==i)
         {
          mi=-1;
          lc.onsgsetColor(Color.black);
          lc.onsgfill(reca[i].rect);
         }
       }
      return false;
     }
    return false;
   }
  boolean mpdp(dP dp) // mousePressed
   {
    //tta("Wav.mpdp mouse Pressed  dp:"+te.Svo(dp));
    if(lc.rband && lc.wavrband )
     {
      return true;
     }
    else
     {
      //tta("Wav.mpdp testing reca and recb for single");
      msdpa=-1;
      msdpb=-1;
      dp.de(lcsc); 
      for(int i=0;i<tpts;i++) 
       {
        //tta("Wav.mpdp testing:"+i+" element of reca");
        if(reca[i].contains(dp))
         {
          msdpa=i;
          odpa=new dP(reca[msdpa].x,reca[msdpa].y);
          lc.onsgsetColor(Color.yellow);
          lc.onsgfill(reca[i].rect);
          //tta("Wav.mpdp Found Reca; msdpa:"+msdpa+"  original dP:"+te.Svo(odpa));
          return true;
         }
        else if(recb[i].contains(dp))
         {
          msdpb=i;
          odpb=new dP(recb[msdpb].x,recb[msdpb].y);
          lc.onsgsetColor(Color.yellow);
          lc.onsgfill(recb[i].rect);
          //tta("Wav.mpdp Found Recb; msdpb:"+msdpb+"  original dP:"+te.Svo(odpa));
          return true;
         }
       }
      return false;
     }
   }
  void mddp(dP dp) //mouseDragged
   {
    //tta("Wav.mddp msdpa:"+msdpa+"  msdpb:"+msdpb+"  dp:"+te.Svo(dp));
    if(lc.rband)
     {
      //  tta("Wav.mddp lc.rband true");
     }
    else if(lc.wavrband)
     {
      //tta("Wav.mddp lc.wavrband true");
     }
    else
     {
      //tta("Wav.mddp");
      if(msdpa != -1)
       {
        reca[msdpa]=new Rec(reca[msdpa].x,dp.y-4,8,8);
        reca[msdpa].setfk(new K(60,1,1,255)); 
        // int aa=ca[lx+msdpa];
        ca[lx+msdpa]=(short)((dp.y-offay)/grscy);   
        int caa=ca[lx+msdpa]; 
        tta("Wav.mddp caa:"+caa);
        cabi2ba(lx+msdpa);
        //a[44+2*i]=(byte)ca[i];
        //a[45+2*i]=(byte)(ca[i] >>>8);
       }
      else tta("Wav.mddp msdpa is -1");
      if(msdpb != -1)
       {
        recb[msdpb]=new Rec(recb[msdpb].x,dp.y-4,8,8);
        recb[msdpb].setfk(new K(60,1,1,255));
        cb[lx+msdpb]=(short)((dp.y-offby)/grscy);    
        int cbb=cb[lx+msdpb]; 
        tta("Wav.mddp cbb:"+cbb);
       }
      lc.clearscreen();
      tu.dgrid();
      graph(lx,w);
      renderrecs();
     }
   }
  void mrdp(dP dp) //mouseReleased
   {
    //tta("Wav.mrdp mouse released");
    dp.scale(lcsc);
    if(lc.rbanded>0)
     {
      lc.rband=false;
      //tta("Wav.mrdp lc.rbanded:"+lc.rbanded);
     }
    else if(dispa != -1 || dispb != -1)
     {
      graph(lx,w);
      renderrecs();
      msdpa=-1;
      msdpb=-1;
     }
   }
  void mkreca()
   {
    //tta("Wav.mkreca tpts:"+tpts);
    reca=new Rec[tpts];
    double xl=glx;
    int k=lx;
    for(int j=0;j<tpts;j++) 
     {
      if(reca.length> j && ca.length> k)
       {
        reca[j]=new Rec(xl-4,offay-4+grscy*ca[k],8,8);
       }
      else
       {
        //tta("yepp reca.length:"+reca.length+" j:"+j+" ca.length:"+ca.length+" k:"+k);
       }
      k+=wig;
      xl+=dx;
     }
   }
  void mkrecb()
   {
    if(chs==1) return;
    recb=new Rec[tpts];
    double xl=glx;
    int k=lx;
    for(int j=0;j<tpts;j++)
     {
      if(recb.length> j && cb.length> k)
       {
        recb[j]=new Rec(xl-4,offby-4+grscy*cb[k],8,8);
       }
      else
       {
        //tta("yepp reba.length:"+recb.length+" j:"+j+" cb.length:"+cb.length+" k:"+k);
       }
      k+=wig;
      xl+=dx;
     }
   }
  boolean mcdp(dP dp)
   {
    //tta("Wav.mcdp:"+te.Svo(dp));
    dp.de(lcsc);
    for(int i=0;i<tpts;i++)
     {
      if(reca[i].contains(dp))
       {
        //tta("mcdp found reca:"+i);
        return true;
       }
     }
    return false;
   }
  boolean secs()
   {
    if(rbrec == null) return false;
    for(int i=0;i<tpts;i++)
     {
      if(rbrec.contains(reca[i]))
       {
        //tta("secs found rec and setting to yellow:"+i);
        reca[i].setfk(new K(60,1,1,255));
       }
     }
    renderrecs();
    return true;
   }
  void popkill() { popkill(popki,popadv,poppost); }
  void popkill(int li,int adv,int post)
   {
    int aa=li-adv;
    int bb=li+post;
    short ado=ca[aa];
    short bdo=cb[aa];
    double ady=(ca[bb]-ado)/(bb-aa);
    double bdy=(cb[bb]-bdo)/(bb-aa);
    for(int t=1;t<bb-aa;t++)
     {
      ca[aa+t]=(short)(ado+ady*t);
      cb[aa+t]=(short)(bdo+bdy*t);
      cabi2ba(aa+t);
     }
    if(popss > 100) graph(lx,200);
    int pkl=aa/poppkint;
    int pku=bb/poppkint;
    short j=0;
    for(int ci=pkl;ci<(1+pku);ci++)
     {
      capk8[ci]=0;
      cbpk8[ci]=0;
     }
    for(int ci=pkl*poppkint;ci<(1+pku)*poppkint;ci+=2)
     {
      if(ci<chshorts)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk8[(int)ci/64]<j)  capk8[(int)ci/64]=j;;
        j=(short)Math.abs(cb[ci]);
        if(cbpk8[(int)ci/64]<j)  cbpk8[(int)ci/64]=j;;
       }
     }
    for(ti=1;ti<25;ti++) pkaboo[ti]=0;
    pkaboo[8]=1;
   }
  void rec()
   {
    saveable=false;
    mode=2;
    recpre();
    recmaine();
    recpost();
    saveable=true;
   }
  void recexp()
   {
    saveable=false;
    mode=2;
    recpre();
    recmaine();
    recpost();
    saveable=true;
   }
  void recpre() 
   { 
    psdone=false; 
    apk=0;
    bpk=0;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4;
    blockrate=(float)(samplerate*blockbytes);
    af=new AudioFormat((float)samplerate,samplebits,chs,true,false);
    //format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100,16,2,4,44100,true);
    tta("Quiet on the set");
    sdlineInfo = new Info(TargetDataLine.class,af);
    if (!AudioSystem.isLineSupported(sdlineInfo)) 
     {
      tta("Line matching " + sdlineInfo + " not supported.");
      return;
     }
    try 
     {
      tdline = (TargetDataLine) AudioSystem.getLine(sdlineInfo); 
      tdline.open(af,capbufsize); 
      tta("capbufsize:"+capbufsize+"  actual:"+tdline.getBufferSize());
     }
    catch(IllegalArgumentException iae)
     {
      tta("IllegalArgumentException");
     }
    catch (LineUnavailableException ex) 
     {
      tta("Unable to open the tdline");
      return;
     }
    catch (SecurityException ex) 
     {
      tta(ex.toString());
      return;
     }
    catch (Exception ex) 
     {
      tta("Wav.recpre  "+ ex.toString());
      return;
     }
    fsib=af.getFrameSize();
    blif=tdline.getBufferSize()/8;
    blib=blif*fsib;
    buff1=new byte[blib];
    budd=new byte[flower][blib];
    ckbuffprep(buff1);
    baos=new ByteArrayOutputStream();
    a=new byte[44];
    commb();
    for(int jj=0;jj<44;jj++) baos.write(a[jj]); // array copy
    tdline.start();
    databytes=0;
   }
  void recmaine()
   {
    int rms=0;
    locmax=0;
    tyma=System.currentTimeMillis();
    while(mode==2)
     {
      if(tdline ==null)
       {
        tta("Wav.recmaine tdline is null");
        return;
       }
      nbr=tdline.read(buff1,0,blib);
      if(nbr== -1) tta("Wav.recmaine bail out");
      baos.write(buff1,0,nbr);
      databytes+=nbr;
      if(databytes> maxlength) 
       {
        mode=0;
        tta("Wav.recmaine Maxlength reached");
       }
      rms=0;
      for(int i=1;i<blib;i+=2)
       {
        if(Math.abs(buff1[i])>locmax)
         {
          locmax=Math.abs(buff1[i]);
          //tta("locmax:"+locmax);
         }
        rms+=Math.pow(buff1[i],2);
       }
      rms/=buff1.length;
      rms= (int)Math.pow(rms,.5);
      if(rms<=2) rms=0;
      if(crms >0)
       {
        tymb=System.currentTimeMillis()-tyma;
        //tta("rms:"+rms+"  crms:"+crms+"  tymb:"+tymb);
       }
      if(locmax > 20)
       {
        if(rms==0) 
         {
          czrms++;
          crms+=czrms;
         }
        if(orms * rms >7)
         {
          czrms=0;
         }
        if(orms >2 && rms > 2)
         {
          crms=0;
          trackguess++;
         }
        if(crms > tcrms) 
         {
          if(tymb < tymo) 
           {
            parent.ta0.clear();
            //tta("Resetting crms");
            crms=0;
           }
          else mode=21;
         }
       }
      if(gb==1)
       {
        tracecount++;
        if(tracecount==tc) 
         {
          lc.clearscreen();
          tu.dgrid();  
          tracecount=0;
         }
        mkbcs(buff1);
        graphbuff();
       }
      if(Math.abs(orms)> Math.abs(rms)+3 || Math.abs(rms)> Math.abs(orms)+3 )
       {
        //tta("rms:"+rms);
       }
      orms=rms;
     }
    tdline.stop();
    tdline.close();
    tdline=null;
   }
  void mon()
   {
    if(mode==2)
     {
      tta("Monitoring and Recording");
      return;
     }
    else tta("Monitoring");
    mode=3;
    recpre();
    monmaine();
   }
  void monmaine()
   {
    locmax=0;
    while(mode==3)
     {
      if(tdline !=null)
       {
        nbr=tdline.read(buff1,0,blib);
        if(nbr== -1) tta("bail out");
        for(int i=1;i<blib;i+=2)
         {
          if(Math.abs(buff1[i])>locmax)
           {
            locmax=Math.abs(buff1[i]);
            tta("locmax:"+locmax);
           }
         }
        mkbcs(buff1);
        graphbuff();
        databytes+=nbr;
       }
     }
    tdline.stop();
    tdline.close();
    tdline=null;
   }
  void recpost()
   {
    tta("Wav.recpost");
    a=baos.toByteArray();
    if(databytes==a.length-44) tta("Good match:"+databytes);
    else
     {
      double db=a.length-44;
      tta("Mismatch a.length-44:"+db+"  databytes:"+databytes);
      databytes=db;
     }
    commb();
    tta("Wav.recpost  Recording complete");
    psdone=false;
    if(mode==21)
     {
      mode=0;
      parent.te.teS("durp2");
     }
   }
  void ckbuffprep(byte[] buff)
   {
    chshorts=buff.length/(chs*(samplebits/8));
    if(chs==2) cb=new short[(int)chshorts];
    ca=new short[(int)chshorts];
   }
  void mkbcs(byte[] buff)
   {
    //tta("mkbcs wig:"+wig);
    int lwig=wig;
    //tta("buff.length:"+buff.length+" lwig:"+lwig+" chs:"+chs+" samplebits:"+samplebits);
    bcs=buff.length/(lwig*chs*(samplebits/8)); // hmmmm
    //tta("Wav.mkbcs bcs:"+bcs);
    if(chs==2) bcb=new short[bcs];
    bca=new short[bcs];
    //tta("Wav.mkbcs bcs:"+bcs+" bca.length:"+bca.length);
    int ci=0;
    int ai=0;
    double t=0;
    while(ci< bcs)
     {
      bca[ci]=(short)(buff[ai++] & 0xff | buff[ai++] << 8);
      if(chs==2)
       {
        bcb[ci++]=(short)(buff[ai++] & 0xff | buff[ai++] << 8);
       }
      if(chs==2)
       {
        ai+=(wig-1)*4; // zippy should be *2 for ch==1 ??
       }
      else
       {
        ai+=(wig-1)*4;
       } 
     }
   }
  void save(String s) 
   {
    save(s,a); // std byte array
   }
  void save(String s,byte[] z) 
   {
    String fn=s;
    int jo=z.length;
    if(fn.indexOf("/")==-1) fn="/webroot/ajlogo/html/ajlogo/version3beta/audio/"+fn;
    tta("Wav.save  fn:"+fn+" file size:"+jo);
    try
     {
      parent.fio.wfba(fn,z); 
     }
    catch(Exception e)
     {
      tta("Wav.save  error fn:"+fn);
      return;
     }
    tta("save complete fn:"+fn);
   }
  void resave()
   {
    tta("file name:"+fn+":");
    if(! fn.equals("")) save(fn);
    else tta("no file name established");
   }
  void schtop()
   {
    if(mode==1) playstop();
    else if(mode==2) recstop();
    else if(mode==3) monstop();
   }
  void playstop()
   {
    mode=0;
    int count=0;
    while(! stopable && count < 10)
     {
      tta("sleeping:"+ stopable);
      schleep(1000);
      count++;
     }
    if(sdline !=null)
     {
      sdline.drain();
      sdline.close(); 
     }
    tta("stopped");
   }
  void monstop()
   {
    mode=0;
   }
  void recstop() 
   {
    tta("Wav.recstop");
    mode=0; 
    while(! saveable)
     {
      tta("rec stop sleeping not saveable");
      schleep(1000);
     }
    tta("Wav.recstop   saveable");
   }
  void psdo()
   {
    if(databytes==a.length-44)
     {
      //tta("psdo match:"+databytes);
     }
    else
     {
      double db=a.length-44;
      tta("psdo mismatch a.length-44:"+db+"  databytes:"+databytes);
      databytes=db;
     }
    chshorts=(int)databytes/(chs*(samplebits/8));
    ba2cab();
    commb();
    w=(int)chshorts;
    lx=0;
    ssl();
    psdone=true;
   }
  short cai2Short(int i)
   {
    return ca[i];
   }
  short bai2Short(int i)  // byte array index to short unknown channel 
   {
    int bai=i;
    return (short)(a[bai++] & 0xff | a[bai] << 8); 
   }
  void bai2cab(int i) // ba indexes ca and cb shorts
   {	  
    int ai=i;
    if(samplebits!=16)
     {
      tta("Wav.ba2cab only 16 samplebits for now");
      return;
     }
    int ci=ai-44; //
    if(chs==1)
     { 
      ca[ci]=(short)(a[ai++] & 0xff | a[ai] << 8); 
     }
    else if(chs==2)
     {  
      ca[ci]=(short)(a[ai++] & 0xff | a[ai++] << 8); 
      cb[ci++]=(short)(a[ai++] & 0xff | a[ai] << 8);  
     }
   }
  void ba2cab()  // full bytearray a to channel shorts ca  cb
   {
    int l=a.length-44;
    tta("Wav.ba2cab a.length-44:"+l+" databytes:"+databytes);
    if(chs==2) cb=new short[(int)chshorts];
    ca=new short[(int)chshorts];
    int ai=44; // a[] index
    int ci=0; // ca[] index
    if(samplebits!=16)
     {
      tta("Wav.ba2cab only 16 samplebits for now");
      return;
     }
    if(chs==1)
     { 
      while(ai<44+databytes)  ca[ci++]=(short)(a[ai++] & 0xff | a[ai++] << 8); 
     }
    else if(chs==2)
     { 
      while(ai<44+databytes)
       {
        ca[ci]=(short)(a[ai++] & 0xff | a[ai++] << 8); 
        cb[ci++]=(short)(a[ai++] & 0xff | a[ai++] << 8); 
       }
     }
   }
  int cai2bai(int i) // convert  'a' channel array index to databyte array index
   { 
    int chai=i;
    if(samplebits==16) 
     {
      if(chs==1)  return (int)(44+2*chai);
      else if(chs==2)  return (int)(44+4*chai);
     }
    else if(samplebits==8)
     {
      if(chs==1)   return  (int)(44+chai);
      else if(chs==2) return (int)(44+2*chai);
     }
    return -1;
   }
  int cbi2bai(int i)// convert  'b' channel array index to databyte array index
   {
    int cbi=i;
    if(samplebits==16)
     {
      return (int)(46+4*chbi);
     }
    else if(samplebits==8)
     {
      return (int)(46+2*chbi);
     }
    return -1;
   }
  short cai2gbav(int i)
   {
    int cai=i;
    int bai=cai2bai(i);
    if(samplebits!=16)
     {
      tta("Wav.cai2gba only 16 samplebits for now");
      return -1;
     }
    return (short)(a[bai++] & 0xff | a[bai] << 8); 
   }
  void list(int xx,int ww)
   {
    int u=xx+ww;
    if (u>(int)chshorts) u=(int)chshorts;
    double gwy=lc.height;
    double grscy=gwy/150000;
    double offay=gwy/4; 
    int gs=(int) grscy;
    tta("gwy:"+gwy+" grscy:"+gs+" offay:"+offay);
    int bai=0;
    short bav=0;
    for(int i=xx;i<u;i++)
     {
      bav=cai2gbav(i);
      tta("i:"+i+" ca[i]:"+ca[i]+" bav:"+bav); 
     }
   }
  void list(int xx)
   {
    list(xx,20);
   } 
  void list()
   {
    list(lx,20);
   }
  void graphbuff()
   {
    long tymc=System.currentTimeMillis();
    K mk=new K(0,0,0,255);
    lc.spk(mk);
    double bw=bcs;
    double lgwx=lc.width;
    double lgwy=lc.height;
    if(chs==1)
     {
      dispb=0;
     }
    if(dispa==1 && dispb==1)
     {
      grscy=lgwy/130000;
      offay=lgwy/4;
      offby=-lgwy/4;
     }
    else
     {
      grscy=lgwy/65000;
      offay=0;
      offby=0;
     }
    dx=lgwx/(bw-1);
    double lxa=-lgwx/2;
    glx=lxa;
    double ya=bca[0]*grscy;
    double xb=lxa;
    double yb=0;
    //int h=1;
    long deltyme=System.currentTimeMillis()-tymc;
    //tta("tymc:"+deltyme);
    for(int i=1;i<bw;i++)
     {
      xb+=dx;
      yb=bca[i]*grscy;
      lc.drawLine(lxa,ya,xb,yb);
      lxa=xb;
      ya=yb;
     }
   }
  void graphcapk()
   {
    if (chs==1)
     {
      dispb=0;
     }
    if(dispa==1 && dispb==1)
     {
      grscy=gwy/130000;
      offy=gwy/4;
      //offay=gwy/4;
      //offby=-gwy/4;
     }
    else
     {
      grscy=gwy/65000;
      offy=0;
      //offay=0;
      //offby=0;
     }
    if(popss>5000) tta("graphcapk ti:"+ti);
    if(ti==24)graphpk(capk24,16777216);
    else if(ti==23)graphpk(capk23,8388608);
    else if(ti==22)graphpk(capk22,4194304);
    else if(ti==21)graphpk(capk21,2097152);
    else if(ti==20)graphpk(capk20,1048576);
    else if(ti==19)graphpk(capk19,524288);
    else if(ti==18)graphpk(capk18,262144);
    else if(ti==17)graphpk(capk17,131072);
    else if(ti==16)graphpk(capk16,65536);
    else if(ti==15)graphpk(capk15,32768);
    else if(ti==14)graphpk(capk14,16384);
    else if(ti==13)graphpk(capk13,8192);
    else if(ti==12)graphpk(capk12,4096);
    else if(ti==11)graphpk(capk11,2048);
    else if(ti==10)graphpk(capk10,1024);
    else if(ti==9)graphpk(capk9,512);
    else if(ti==8)graphpk(capk8,256);
    else if(ti==7)graphpk(capk7,128);
    else if(ti==6)graphpk(capk6,64);
    else if(ti==5)graphpk(capk5,32);
    else if(ti==4)graphpk(capk4,16);
    else if(ti==3)graphpk(capk3,8);
    else if(ti==2)graphpk(capk2,4);
   }
  void graphcbpk()
   {
    if (chs==1)
     {
      dispb=0;
     }
    if(dispa==1 && dispb==1)
     {
      grscy=gwy/130000;
      //offay=gwy/4;
      //offby=-gwy/4;
      offy=-gwy/4;
     }
    else
     {
      grscy=gwy/65000;
      //offay=0;
      offy=0;
      //offby=0;
     }
    if(ti==24)graphpk(cbpk24,16777216);
    else if(ti==23)graphpk(cbpk23,8388608);
    else if(ti==22)graphpk(cbpk22,4194304);
    else if(ti==21)graphpk(cbpk21,2097152);
    else if(ti==20)graphpk(cbpk20,1048576);
    else if(ti==19)graphpk(cbpk19,524288);
    else if(ti==18)graphpk(cbpk18,262144);
    else if(ti==17)graphpk(cbpk17,131072);
    else if(ti==16)graphpk(cbpk16,65536);
    else if(ti==15)graphpk(cbpk15,32768);
    else if(ti==14)graphpk(cbpk14,16384);
    else if(ti==13)graphpk(cbpk13,8192);
    else if(ti==12)graphpk(cbpk12,4096);
    else if(ti==11)graphpk(cbpk11,2048);
    else if(ti==10)graphpk(cbpk10,1024);
    else if(ti==9)graphpk(cbpk9,512);
    else if(ti==8)graphpk(cbpk8,256);
    else if(ti==7)graphpk(cbpk7,128);
    else if(ti==6)graphpk(cbpk6,64);
    else if(ti==5)graphpk(cbpk5,32);
    else if(ti==4)graphpk(cbpk4,16);
    else if(ti==3)graphpk(cbpk3,8);
    else if(ti==2)graphpk(cbpk2,4);
   }
  void calccapk()
   {
    calccapk(0,(int)chshorts);
   }
  void calccbpk()
   {
    calccbpk(0,(int)chshorts);
    pkaboo[ti]=1;
   }
  void calccapk(int l,int u)
   {
    short j;
    if(u>ca.length)
     {
      tta("Wav.calccapk u:"+u+" is > ca.l  set to ca.length:"+ca.length);
      u=ca.length;
     }
    else if(u==ca.length)
     {
      int z=ca.length-1;
      tta("Wav.calccapk u:"+u+" is > ca.l  set to ca.length-1 :"+z);
      u=z;
     }
    if(pkaboo[ti]==1) return;
    if(ti==24)
     {
      if(capk24==null) capk24=new short[(int)(1+(int)chshorts/16777216)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk24[(int)ci/16777216]<j)  capk24[(int)ci/16777216]=j;
       }
     }
    else if(ti==23)
     {
      if(capk23==null) capk23=new short[(int)(1+(int)chshorts/8388608)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk23[(int)ci/8388608]<j)  capk23[(int)ci/8388608]=j;
       }
     }
    else if(ti==22)
     {
      if(capk22==null) capk22=new short[(int)(1+(int)chshorts/4194304)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk22[(int)ci/4194304]<j)  capk22[(int)ci/4194304]=j;
       }
     }
    else if(ti==21)
     {
      if(capk21==null) capk21=new short[(int)(1+(int)chshorts/2097152)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk21[(int)ci/2097152]<j)  capk21[(int)ci/2097152]=j;
       }
     }
    else if(ti==20)
     {
      if(capk20==null) capk20=new short[(int)(1+(int)chshorts/1048576)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk20[(int)ci/1048576]<j)  capk20[(int)ci/1048576]=j;
       }
     }
    else if(ti==19)
     {
      if(capk19==null) capk19=new short[(int)(1+(int)chshorts/524288)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk19[(int)ci/524288]<j)  capk19[(int)ci/524288]=j;
       }
     }
    else if(ti==18)
     {
      if(capk18==null) capk18=new short[(int)(1+(int)chshorts/262144)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk18[(int)ci/262144]<j)  capk18[(int)ci/262144]=j;
       }
     }
    else if(ti==17)
     {
      if(capk17==null) capk17=new short[(int)(1+(int)chshorts/131072)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk17[(int)ci/131072]<j)  capk17[(int)ci/131072]=j;
       }
     }
    else if(ti==16)
     {
      if(capk16==null) capk16=new short[(int)(1+(int)chshorts/65536)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk16[(int)ci/65536]<j)  capk16[(int)ci/65536]=j;
       }
     }
    else if(ti==15)
     {
      if(capk15==null) capk15=new short[(int)(1+(int)chshorts/32768)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk15[(int)ci/32768]<j)  capk15[(int)ci/32768]=j;
       }
     }
    else if(ti==14)
     {
      if(capk14==null) capk14=new short[(int)(1+(int)chshorts/16384)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk14[(int)ci/16384]<j)  capk14[(int)ci/16384]=j;
       }
     }
    else if(ti==13)
     {
      if(capk13==null) capk13=new short[(int)(1+(int)chshorts/8192)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk13[(int)ci/8192]<j)  capk13[(int)ci/8192]=j;
       }
     }
    else if(ti==12)
     {
      if(capk12==null) capk12=new short[(int)(1+(int)chshorts/4096)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk12[(int)ci/4096]<j)  capk12[(int)ci/4096]=j;
       }
     }
    else if(ti==11)
     {
      if(capk11==null) capk11=new short[(int)(1+(int)chshorts/2048)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk11[(int)ci/2048]<j)  capk11[(int)ci/2048]=j;
       }
     }
    else if(ti==10)
     {
      if(capk10==null) capk10=new short[(int)(1+(int)chshorts/1024)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk10[(int)ci/1024]<j)  capk10[(int)ci/1024]=j;
       }
     }
    else if(ti==9)
     {
      if(capk9==null) capk9=new short[(int)(1+(int)chshorts/512)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk9[(int)ci/512]<j)  capk9[(int)ci/512]=j;
       }
     }
    else if(ti==8)
     {
      if(capk8==null) capk8=new short[(int)(1+(int)chshorts/256)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk8[(int)ci/256]<j)  capk8[(int)ci/256]=j;
       }
     }
    else if(ti==7)
     {
      if(capk7==null) capk7=new short[(int)(1+(int)chshorts/128)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk7[(int)ci/128]<j)  capk7[(int)ci/128]=j;
       }
     }
    else if(ti==6)
     {
      if(capk6==null) capk6=new short[(int)(1+(int)chshorts/64)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk6[(int)ci/64]<j)  capk6[(int)ci/64]=j;
       }
     }
    else if(ti==5)
     {
      if(capk5==null) capk5=new short[(int)(1+(int)chshorts/32)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk5[(int)ci/32]<j)  capk5[(int)ci/32]=j;
       }
     }
    else if(ti==4)
     {
      if(capk4==null) capk4=new short[(int)(1+(int)chshorts/16)];
      for(int ci=l;ci<u;ci+=1)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk4[(int)ci/16]<j)  capk4[(int)ci/16]=j;
       }
     }
    else if(ti==3)
     {
      if(capk3==null) capk3=new short[(int)(1+(int)chshorts/8)];
      for(int ci=l;ci<u;ci+=1)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk3[(int)ci/8]<j)  capk3[(int)ci/8]=j;
       }
     }
    else if(ti==2)
     {
      if(capk2==null) capk2=new short[(int)(1+(int)chshorts/4)];
      for(int ci=l;ci<u;ci+=1)
       {
        j=(short)Math.abs(ca[ci]);
        if(capk2[(int)ci/4]<j)  capk2[(int)ci/4]=j;
       }
     }
   }
  void calccbpk(int l,int u)
   {
    if(u>cb.length)
     {
      tta("Wav.calccbpk u:"+u+" is > cb.l  set to cb.length:"+cb.length);
      u=cb.length;
     }
    else if(u==cb.length)
     {
      int z=cb.length-1;
      tta("Wav.calccbpk u:"+u+" is > cb.l  set to cb.length-1 :"+z);
      u=z;
     }
    short j;
    if(pkaboo[ti]==1) return;
    if(ti==24)
     {
      if(cbpk24==null) cbpk24=new short[(int)(1+(int)chshorts/16777216)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk24[(int)ci/16777216]<j)  cbpk24[(int)ci/16777216]=j;
       }
     }
    else if(ti==23)
     {
      if(cbpk23==null) cbpk23=new short[(int)(1+(int)chshorts/8388608)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk23[(int)ci/8388608]<j)  cbpk23[(int)ci/8388608]=j;
       }
     }
    else if(ti==22)
     {
      if(cbpk22==null) cbpk22=new short[(int)(1+(int)chshorts/4194304)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk22[(int)ci/4194304]<j)  cbpk22[(int)ci/4194304]=j;
       }
     }
    else if(ti==21)
     {
      if(cbpk21==null) cbpk21=new short[(int)(1+(int)chshorts/2097152)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk21[(int)ci/2097152]<j)  cbpk21[(int)ci/2097152]=j;
       }
     }
    else if(ti==20)
     {
      if(cbpk20==null) cbpk20=new short[(int)(1+(int)chshorts/1048576)];
      for(int ci=l;ci<u;ci+=5)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk20[(int)ci/1048576]<j)  cbpk20[(int)ci/1048576]=j;
       }
     }
    else if(ti==19)
     {
      if(cbpk19==null) cbpk19=new short[(int)(1+(int)chshorts/524288)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk19[(int)ci/524288]<j)  cbpk19[(int)ci/524288]=j;
       }
     }
    else if(ti==18)
     {
      if(cbpk18==null) cbpk18=new short[(int)(1+(int)chshorts/262144)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk18[(int)ci/262144]<j)  cbpk18[(int)ci/262144]=j;
       }
     }
    else if(ti==17)
     {
      if(cbpk17==null) cbpk17=new short[(int)(1+(int)chshorts/131072)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk17[(int)ci/131072]<j)  cbpk17[(int)ci/131072]=j;
       }
     }
    else if(ti==16)
     {
      if(cbpk16==null) cbpk16=new short[(int)(1+(int)chshorts/65536)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk16[(int)ci/65536]<j)  cbpk16[(int)ci/65536]=j;
       }
     }
    else if(ti==15)
     {
      if(cbpk15==null) cbpk15=new short[(int)(1+(int)chshorts/32768)];
      for(int ci=l;ci<u;ci+=4)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk15[(int)ci/32768]<j)  cbpk15[(int)ci/32768]=j;
       }
     }
    else if(ti==14)
     {
      if(cbpk14==null) cbpk14=new short[(int)(1+(int)chshorts/16384)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk14[(int)ci/16384]<j)  cbpk14[(int)ci/16384]=j;
       }
     }
    else if(ti==13)
     {
      if(cbpk13==null) cbpk13=new short[(int)(1+(int)chshorts/8192)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk13[(int)ci/8192]<j)  cbpk13[(int)ci/8192]=j;
       }
     }
    else if(ti==12)
     {
      if(cbpk12==null) cbpk12=new short[(int)(1+(int)chshorts/4096)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk12[(int)ci/4096]<j)  cbpk12[(int)ci/4096]=j;
       }
     }
    else if(ti==11)
     {
      if(cbpk11==null) cbpk11=new short[(int)(1+(int)chshorts/2048)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk11[(int)ci/2048]<j)  cbpk11[(int)ci/2048]=j;
       }
     }
    else if(ti==10)
     {
      if(cbpk10==null) cbpk10=new short[(int)(1+(int)chshorts/1024)];
      for(int ci=l;ci<u;ci+=3)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk10[(int)ci/1024]<j)  cbpk10[(int)ci/1024]=j;
       }
     }
    else if(ti==9)
     {
      if(cbpk9==null) cbpk9=new short[(int)(1+(int)chshorts/512)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk9[(int)ci/512]<j)  cbpk9[(int)ci/512]=j;
       }
     }
    else if(ti==8)
     {
      if(cbpk8==null) cbpk8=new short[(int)(1+(int)chshorts/256)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk8[(int)ci/256]<j)  cbpk8[(int)ci/256]=j;
       }
     }
    else if(ti==7)
     {
      if(cbpk7==null) cbpk7=new short[(int)(1+(int)chshorts/128)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk7[(int)ci/128]<j)  cbpk7[(int)ci/128]=j;
       }
     }
    else if(ti==6)
     {
      if(cbpk6==null) cbpk6=new short[(int)(1+(int)chshorts/64)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk6[(int)ci/64]<j)  cbpk6[(int)ci/64]=j;
       }
     }
    else if(ti==5)
     {
      if(cbpk5==null) cbpk5=new short[(int)(1+(int)chshorts/32)];
      for(int ci=l;ci<u;ci+=2)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk5[(int)ci/32]<j)  cbpk5[(int)ci/32]=j;
       }
     }
    else if(ti==4)
     {
      if(cbpk4==null) cbpk4=new short[(int)(1+(int)chshorts/16)];
      for(int ci=l;ci<u;ci+=1)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk4[(int)ci/16]<j)  cbpk4[(int)ci/16]=j;
       }
     }
    else if(ti==3)
     {
      if(cbpk3==null) cbpk3=new short[(int)(1+(int)chshorts/8)];
      for(int ci=l;ci<u;ci+=1)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk3[(int)ci/8]<j)  cbpk3[(int)ci/8]=j;
       }
     }
    else if(ti==2)
     {
      if(cbpk2==null) cbpk2=new short[(int)(1+(int)chshorts/4)];
      for(int ci=l;ci<u;ci+=1)
       {
        j=(short)Math.abs(cb[ci]);
        if(cbpk2[(int)ci/4]<j)  cbpk2[(int)ci/4]=j;
       }
     }
   }
  void ave()
   {
    ave(0,(int)chshorts,4);
   }
  void ave(int x,long ww,int sam)
   {
	  int www=(int)ww;
    short cc[]= new short[ca.length];
    for (int i=x;i< x+ww;i++)
     {
      int sum=0;
      int s=0;
      for(int j=i-sam;j<i+2*sam;j++)
       {
        if(j>=x && j<= x+ww)
         {
          s++;
          sum+=ca[j];				  
         }
       }
      cc[i]=(short)(sum/s);
     } 
    System.arraycopy(ca, 0, cc, 0, cc.length);
    graph(x,www);
   }
  synchronized  void graph(int ll,int ww)  // NEW
   {
    if(! testlxw(ll,ww)) return; 
    graph();
   }
  synchronized  void graph()  // pre tested vaild lx,w
   {	  
    long tyme=System.currentTimeMillis();
    dispa=1;
    if(chs==2) dispb=1; 
    lc.clearscreen();
    tu.dgrid();  
    gwx=lc.width;
    gwy=lc.height;
    if(dispa==1 && dispb==1) 
     { 
      grscy=gwy/150000;
      offay=gwy/4;
      offby=-gwy/4;
     }
    else
     { 
      grscy=gwy/75000;
      offay=0;
      offby=0;
     }  
    double ya=0,yb=0,oya=0,oyb=0;  
    double gdx=(double)wig*gwx/(w-1);
    dx=gdx;
    glx=-gwx/2; 
    double gx,gy,gox,goy; 
    lc.spk(new K(270,1,1,255));
    gox=glx;  
    long tymea=System.currentTimeMillis();
    long tdelta=tymea-tyme; 
    if(dispa==1)
     {  
      gx=glx;
      gy=0;
      gox=gx; 
      gx+=gdx;
      goy=offay+ca[lx]*grscy;
      int i=lx+wig;
      int j=lx+w;
      if(j>ca.length) j=ca.length;
      while(i<j) 
       {  
        gy=offay+ca[i]*grscy;   
        lc.drawLine(gox,goy,gx,gy);
        gox=gx; 
        goy=gy;
        gx+=gdx;
        i+=wig;
       }
     }
    long tymeb=System.currentTimeMillis();
    tdelta=tymeb-tymea; 
    // tta("cha:"+tdelta);
    if(dispb==1)
     {  
      lc.spk(new K(180,1,1,255));
      gx=glx;
      gy=0;
      gox=gx; 
      gx+=gdx;
      goy=offby+cb[lx]*grscy;
      int i=lx+wig;
      //while(i<cb.length) 
      int j=lx+w;
      if(j>cb.length) j=cb.length;
      while(i<j) 
       {  
        gy=offby+cb[i]*grscy;   
        lc.drawLine(gox,goy,gx,gy);
        gox=gx; 
        goy=gy;
        gx+=gdx;
        i+=wig;
       }
     }
    if(w<300)
     {
      tpts=w-1; 
      //   tta("w:"+w+" wig:"+wig+" tpts:"+tpts); 
      //  tta("mkrec renderrec");
      if(dispa==1)  mkreca(); 
      if(dispb==1)  mkrecb();
      renderrecs();
     }
    long tymec=System.currentTimeMillis();
    tdelta=tymec-tymeb;
    //tta("chb:"+tdelta);
   }
  boolean testrbrec(Rec rbrec)
   {
    this.rbrec=rbrec;
    int rbmlx=(int)(lx+(w*(rbrec.x-glx)/gwx));
    int rbmw=(int)(w*rbrec.w/gwx);
    return testlxw(rbmlx,rbmw); 
   }
  boolean testlxw(int nlx,int nw)  // test only schmuggly
   {
    if(databytes!=a.length-44)
     {
      tta("Wav.graph a.length-44 not equal to databytes:"+databytes);
      return false;
     } 
    if(nlx==lx && nw==w )   return true;
    //if(nw > ca.length || nw <=0 || nlx > ca.length || nlx <0 )
    if(nw > ca.length || nw <0 || nlx > ca.length || nlx <0 )
     {
      tta("Wav.testlxw Bad request ");
      return false;
     }
    if(nlx+nw>(int)chshorts)
     {
      if(autoscale==1)
       {
        if(olx != nlx) nw=(int)chshorts-nlx; 
        else if(ow != nw)  nlx=(int)chshorts-nw; 
       }
      else
       {
        tta("Wav.testlxw Bad request");
        return false;
       }
     }
    //if(nlx>(int)chshorts ||  nw>(int)chshorts || nw==0 || nlx+nw>(int)chshorts  )
    if(nlx>(int)chshorts ||  nw>(int)chshorts || nlx+nw>(int)chshorts  )
     {
      // tta("Wav.test;xw early return  if(lx>(int)chshorts ||  w>(int)chshorts || w==0 || lx+w>(int)chshorts ) ");
      tta("Wav.test;xw early return  if(lx>(int)chshorts ||  w>(int)chshorts || lx+w>(int)chshorts ) ");
      return false;
     }
    if(autowig==1) setWig();  
    if(nlx != lx)
     {
      lx=nlx;
      parent.te.teS("= lx "+lx);
     } 
    if(nw != w)
     {
      w=nw;
      parent.te.teS("= w "+w);
     }
    olx=lx;
    ow=w;
    tta("Wav.testlxw true lx:"+lx+" w:"+w);
    return true;
   }
  // graph(xx,ww); is zoom(xx,ww);
  void zoomFull()
   {
    graph (0,ca.length);  // graph has test first thing
   }
  void zoomIn()
   {      
    graph((int)(lx+ w/4),w/2);
   }
  void zoomOut()
   { 
    graph((int)(lx - w/2),w*2); 
   }
  void setLx() // changed by lx slider update others
   {
   }
  void setLx(int ll) // changed by lx slider update others
   {
    lx=ll;
    if(lx+w > ca.length)
     {
      w=ca.length;
     }
    olx=lx;
   }
  void setW(int ww)
   {
    w=ww;
   }
  void setWig()
   {
    setWig(-1);
   }
  void setWig(int ww)
   {
    if (ww==-1) // override autowig
     {
      wig=(w/100000)-4; // w below 60000 wig =1
     }
    else wig=ww;
    if(wig <=0) wig=1; 
    if(wig != owig) 
     {
      M.te0.teS("= wig "+wig);
      tta("setWig wig:"+wig);
     }
    owig=wig;
   }
  void graphpk(short[] pk,int pki)
   {
    pkint=pki;
    if(pk==null) 
     {
      calccapk();
      calccbpk();
      tta("calced pk's and returning no graph");
      return;
     }
    int pkl=pk.length;
    double rlx=(lx/pkint);
    if(lx+w>(int)chshorts || lx>(int)chshorts ||  w>(int)chshorts)
     {
      tta("graphpk early out");
      return;
     }
    gwx=lc.width;
    gwy=lc.height;
    // dx=(double)wig*gwx/(w-1);
    // dx=lgwx/(bw-1);
    //dx=(double)gwx/(w/pkint);
    dx=(double)gwx/((w-1)/pkint);
    double lxa=-gwx/2;
    glx=lxa;
    double ya=offy+pk[(int)rlx]*grscy;
    double xb=lxa;
    double yb=0,yc=0,r=0,kr=0,ro=offy;
    lc.spk(new K(0,0,0,255));
    for(int i=lx+1;i<lx+w+pkint;i+=pkint)
     {
      xb+=dx;
      int ind=(int)(i/pkint);
      if(ind<pkl)
       {
        //double d=pk[ind];
        //double e=pkrms(ind,pk); 
        if(popss>1000)
         {
          kr=pkrms(ind,pk)*grscy;
          r=offy+kr;
          if(kr<2) lc.spk(new K(90,1,1,55));
          else if(kr<5) lc.spk(new K(120,1,1,55));
          else if(kr<10) lc.spk(new K(150,1,1,55));
          else if(kr<15) lc.spk(new K(180,1,1,55));
          else if(kr<65) lc.spk(new K(60,1,1,55));
          else if(kr<85) lc.spk(new K(40,1,1,55));
          else if(kr<125) lc.spk(new K(20,1,1,55));
          else  lc.spk(new K(0,1,1,55));
          lc.drawLine(xb,ro,xb,r);
          lc.spk(new K(0,0,0,255));
         }
        if(popss>100) calcedK(pk[ind],pkrms(ind,pk));
        yb=offy+pk[ind]*grscy;
        lc.drawLine(lxa,ya,xb,yb);
        lxa=xb;
        ya=yb;
       }
     }
    tta("graphpk bottom");
   }
  int calced(double d,double e)
   {
    if(d > d1  && d > e1 * e) return 1;
    else if(d > d2 && d > e2 * e)  return 2;
    else if(d > d3 && d > e3 * e)  return 3;
    else if(d > d4 && d > e4 * e)  return 4;
    else if(d > d5 && d > e5 * e)  return 5;
    else return 6;
   }
  void calcedK(double d,double e)
   {
    int k=calced(d,e);
    if(k==1) lc.spk(new K(60,1,1,255));
    else if(k==2) lc.spk(new K(120,1,1,255));
    else if(k==3) lc.spk(new K(180,1,1,255));
    else if(k==4) lc.spk(new K(240,1,1,255));
    else if(k==5) lc.spk(new K(300,1,1,255));
    else lc.spk(new K(0,0,0,255));
   }
  void altgraphpk(short[] pk,int pki)
   {
    if (chs==1)
     {
      dispb=0;
     }
    pkint=pki;
    int pkl=pk.length;
    double rlx=(lx/pkint);
    if(lx+w>(int)chshorts || lx>(int)chshorts ||  w>(int)chshorts) return;
    gwx=lc.width;
    gwy=lc.height;
    if(dispa==1 && dispb==1)
     {
      grscy=gwy/130000;
      offay=gwy/4;
      offby=-gwy/4;
     }
    else
     {
      grscy=gwy/65000;
      offay=0;
      offby=0;
     }
    //dx=(double)gwx/(w/pkint);
    dx=(double)gwx/((w-1)/pkint);
    double lxa=-gwx/2;
    glx=lxa;
    double ya=offby+pk[(int)rlx]*grscy;
    double xb=lxa;
    double yb=0,yc=0,r=0,kr=0,ro=offby;
    for(int i=lx+1;i<lx+w+pkint;i+=pkint)
     {
      xb+=dx;
      int ind=(int)(i/pkint);
      if(ind<pkl)
       {
        double d=pk[ind];
        double e=pkrms(ind,pk);
        yb=offby+pk[ind]*grscy;
        kr=pkrms(ind,pk)*grscy;
        r=offby+kr;
        lc.spk(new K(kr,1,1,55));
        lc.drawLine(xb,ro,xb,r);
        calcedK(d,e);
        lc.drawLine(lxa,ya,xb,yb);
        lxa=xb;
        ya=yb;
       }
     }
   }
  void rbNormalize()
   {
    int tlx=lx;
    int tw=w;
    rbrec=lc.rbRec(); 
    if (! testrbrec(rbrec)) return;
    normalize(); // tested above
    lx=tlx;
    w=tw;
    graph(); // assume good/no test
   }
  void normalize(int nlx,int nw)
   {
    if (! testlxw(nlx,nw)) return; 
    normalize();
   }
  void normalize()  // valid lx,w
   {   	   
    tta("Wav.normalize attend to camin cbmin");
    short camax=-10000;
    short cbmax=-10000;
    short camin=10000;
    short cbmin=10000;
    double caoff=0;
    double cboff=0;
    double caamp=10;
    double cbamp=10; 
    for(int i=lx+1;i< lx+w;i++) 
     {
      if(ca[i]>camax) camax=ca[i]; 
      else if(ca[i]<camin) camin=ca[i];
     }
    // new camin=0 camax=32767
    caoff=camax-camin;
    caamp=32767D/caoff;
    for(int i=lx+1;i< lx+w;i++) 
     {
      ca[i]-=caoff;
      ca[i]*=caamp;
      cai2ba(i);
     }
    tta("Wav.normalize camax:"+camax+" caamp:"+caamp);
    if(chs==2)
     {
      for(int i=lx+1;i< lx+w;i++) 
       { 
        if(cb[i]>cbmax) cbmax=cb[i];
        else if(cb[i]<cbmin) cbmin=cb[i];
       } 
      cboff=cbmax-cbmin;
      cbamp=32767D/cboff;
      tta("Wav normalize cbmax:"+cbmax+" cbamp:"+cbamp);
      for(int i=lx+1;i< lx+w;i++) 
       {
        cb[i]-=cboff;
        cb[i]*=cbamp;
        cbi2ba(i);
       }
     } 
    repeak(lx,lx+w-1); 
    graph(lx,w-1);
    tta("Wav.normalize done");
   }
  void rbCreate(String n)
   {
    int rbmlx=(int)(lx+(w*(rbrec.x-glx)/gwx));
    int rbmw=(int)(w*rbrec.w/gwx);
    //databytes=4*rbmw;
    byte[] ba=stdcommb(4*rbmw);
    for(int i=0;i<44;i++) tta("ba["+i+"]:"+ba[i]);
    int j=44;
    for(int i=rbmlx;i<rbmw+rbmlx;i++)
     {
      ba[j++]=(byte)ca[i];
      ba[j++]=(byte)(ca[i] >>>8);
      ba[j++]=(byte)cb[i];
      ba[j++]=(byte)(cb[i] >>>8);
     }
    Wav nw=new Wav(parent,n,ba);
    Te.Wavh.put(n,nw);
   }
  void mute()
   {  
    for(int i=lx;i<= lx+w;i++) 
     { 
      ca[i]=(short) (ca[i]/10);  
      cai2ba(i);
      cb[i]=cb[lx+1];
      cbi2ba(i);
     } 
   }
  void mute(int xx,int ww)
   { 
    int tlx=lx;
    int tw=w;
    if(! testlxw(xx,ww)) return;
    mute();
    lx=tlx;
    w=tw;
    graph();
   } 
  void rbMute()
   { 
    int tlx=lx;
    int tw=w;
    rbrec=lc.rbRec();
    if (! testrbrec(rbrec)) return; 
    mute(); // tested above
    lx=tlx;
    w=tw;
    parent.te.teS("= lx "+lx+" = w "+w); 
    graph(); // assume good no test
   }
  void rbZoom()
   {
    rbrec=lc.rbRec();
    if(! testrbrec(rbrec)) return; 
    graph();  // new lx, w valid
   }
  void ssl()
   {
    parent.te.teS("= lx "+lx+" w.ul "+ca.length+" = w "+w+" = wig"+wig);
   }
  void rbCut()
   {
    int tlx=lx;
    int tw=w;
    rbrec=lc.rbRec();
    if (! testrbrec(rbrec)) return; 
    cut();
    lx=tlx;  
    w=tw; 
    parent.te.teS("= lx "+lx+" = w "+w); 
    graph();
   }
  void cut()
   {
    int wl=cai2bai(lx-1);
    int wu=cai2bai(lx+w);
    int aw=cw2baw(w+1);
    tta("Wav.cut wl:"+wl+" wu:"+wu+" aw:"+aw+" lx:"+lx+" w:"+w);
    // wl 40 wu 68 aw 28  lx 0 w 6
    // lx is cut lx
    // w is cut w
    // aw is wu - wl
    if(w<30)
     {
      for(int ind=lx;ind<w;ind++)
       {
        short cashort= bai2Short(ind);
        tta("ind:"+ind+" a[ind]:"+a[ind]+" cashort:"+cashort);
        // tta("ind:"+ind+" a[ind]:"+a[ind]);
       }
     }
    tta("cut complete");
    //graph(lx,w);
   }
  void oldcut()
   {
    int wl=cai2bai(lx-1);
    int wu=cai2bai(lx+w);
    int aw=cw2baw(w+1);
    if(w== (int)chshorts-lx)
     {
      tta("Modified cloning trial");
      databytes-=aw;
      byte[] b=new byte[44+(int)databytes];
      for(int i=0;i<b.length;i++) b[i]=a[i];
      a=(byte[])b.clone();
      if(a.length != databytes) tta("bad clone databytes:"+databytes+" a.length:"+a.length);
      commb();
     }
    else
     {
      tta("Cloning");
      byte[] b=(byte[])a.clone();
      databytes-=aw;
      a=new byte[b.length-aw];
      int j=0;
      int i=0;
      for(i=0;i<wl;i++) a[i]=b[i];
      j=i;
      i+=aw;
      for(i=i;i<b.length;i++) a[j++]=b[i];
     }
    tta("cut complete");
    psdone=false;
    psdo();
    ssl();
    graph(lx,w);
   }
  void modify()
   {
    tta("starting a array modify");
    for(int i=0;i<(int)chshorts;i++) cabi2ba(i);
    tta("done modify");
    psdone=false;
    psdo();
    ssl();
   }
  void rbSave(String fn) 
   {
    int tlx=lx;
    int tw=w;
    rbrec=lc.rbRec();
    if (! testrbrec(rbrec)) return;
    int wl=cai2bai(lx-1);
    int wu=cai2bai(lx+w);
    int aw=cw2baw(w+1); // databytes for new array
    byte[] b=new byte[44+aw]; 
    int l=b.length-44;
    int m=36+l;
    tta("Wav.rbSave lx:"+lx+" w:"+w+" aw:"+aw+" b.length:"+l);
    int j=0; 
    System.arraycopy(a, 0, b, 0, 45); // clone header
    int slx=wl+1;
    int sw=wu-slx; 
    System.arraycopy(a, slx,b,45,sw);
    tta("Wav.rbSave a.length:"+a.length+" databytes:"+databytes+" b.length:"+b.length);
    setbade(b,m,4);
    setbade(b,l,40); 
    double db=getbade(b,40);
    tta("Wav.rbSave new databytes:"+db);
    save(fn,b);
    lx=tlx;
    w=tw; 
   }
  void cai2gba(int i)
   {
    if(samplebits==16)
     {
      if(chs==1)
       {
        a[44+2*i]=(byte)ca[i];
        a[45+2*i]=(byte)(ca[i] >>>8);
       }
      else if(chs==2) 
       {
        a[44+4*i]=(byte)ca[i];   // was ca[i]
        a[45+4*i]=(byte)(ca[i] >>>8); // zilog was ca[i]
       }
     }
   }
  void cabi2ba(int i)   //channel short(s (if two channels)) placed into wav file array (after header)
   {
	  int hi=i;
    cai2ba(hi);
    cbi2ba(hi);
   }
  void cai2ba(int i) // channel a short value at index placed into wavefile bytearray
   {
	 // if(i%100 ==0) tta("Wav.cai2ba  i:"+i+" samplebits:"+samplebits+" chs:"+chs); 
    int ci=i;
    int hi=i;
    if(samplebits==16)
     {
      if(chs==1)
       {
        a[44+2*hi]=(byte)ca[ci];
        a[45+2*hi]=(byte)(ca[ci] >>>8);
       }
      else if(chs==2) 
       {
        a[44+4*hi]=(byte)ca[ci];   
        a[45+4*hi]=(byte)(ca[ci] >>>8);  
       }
     }
    else if(samplebits==8)
     {
      if(chs==1) a[44+hi]=(byte)ca[ci];
      else if(chs==2) a[44+2*hi]=(byte)ca[ci];  
     }
   }
  void cbi2ba(int i)
   {
    int ci=i;
    int hi=i;
    if(samplebits==16)  // assumes b channel means 2 channels 
     {
      a[46+4*hi]=(byte)cb[ci];
      a[47+4*hi]=(byte)(cb[ci] >>>8);
     }
    else if(samplebits==8) a[45+2*hi]=(byte)cb[ci];
   }
  int cw2baw(int i) // convert chann
   {
    int ci=i;
    int hi=i;
    if(samplebits==16)
     {
      if(chs==1)   return (int)(2*ci);
      else if(chs==2) return (int)(4*ci);
     }
    else if(samplebits==8)
     {
      if(chs==1)   return (int)(ci);
     }
    return -1;
   }
  int carms(int i)
   {
	  int hi=i;
    double al=0;
    int h=(int)(rmsss/2);
    if(hi-h<0 || hi+h>(int)chshorts) return -1;
    for (int j=hi-h;j<hi+h;j++) al+=Math.pow(ca[j],2);
    al/=2*h;
    return (int)Math.pow(al,.5);
   }
  int cbrms(int i)
   {
	int hi=i;
    double al=0;
    int h=(int)(rmsss/2);
    if(hi-h<0 || hi+h>(int)chshorts) return -1;
    for (int j=hi-h;j<hi+h;j++) al+=Math.pow(cb[j],2);
    al/=2*h;
    return (int)Math.pow(al,.5);
   }
  int pkrms(int i,short[] pk)
   {
    double pkrms=0;
    int hss=(int)rmsss/2;
    if(i<= hss || i>= pk.length -hss ) return -1;
    int jj=i-hss;
    int k=i+hss;
    for (int j=i-hss;j<=i+hss;j++) pkrms+=Math.pow(pk[j],2);
    pkrms/=rmsss;
    return (int)Math.pow(pkrms,.5);
   }
  void ckwright()
   {
    int si=0;
    int ci=0;
    double arms=0;
    double brms=0;
    while(ci<(int)chshorts-rmsss)
     {
      for(int i=1;i<=rmsss;i++)
       {
        arms+=Math.pow(ca[ci++],2);
        brms+=Math.pow(cb[ci++],2);
       }
      arms/=rmsss;
      brms/=rmsss;
      arms=Math.pow(arms,.5);
      brms=Math.pow(brms,.5);
     }
   }
  void bea() { bea((int)chshorts-1,(int)chshorts); }
  void bea(long ci,long ux)
   {
    int hi=(int)ci;
    while(hi - bedx > 10 && Math.abs(ca[hi]) < beth ) { hi -= 2; }
    hi-= bedx;
    beax=hi;
    becut=(int)(ux-beax);
    tta("hi:"+hi+" ci:"+ci+" beax:"+beax+" ux:"+ux+" becut:"+becut);
    graph(beax,becut);
   }
  void beb() { beb((int)chshorts-1,(int)chshorts); }
  void beb(long ci,long ux)
   {
    while(ci - bedx > 10 && Math.abs(ca[(int)ci]) < beth ) { ci -= 2; }
    ci-= bedx;
    bebx=(int)ci;
    graph(bebx,(int)(ux-bebx));
   }
  void repeak() { repeak(0,(int)chshorts); }
  void repeak(int lx,int w)
   {
    int ux=lx+w;
    if(ux>=(int)chshorts)
     {
      tta("Wav.repeak fix ux");
      ux=(int)chshorts-1;
     }
    tta("Repeaking lx:"+lx+" ux:"+ux);
    int oti=ti;
    for(ti=0;ti<25;ti++)
     {
      pkaboo[ti]=0;
      calccapk(lx,ux);
      calccbpk(lx,ux);
      pkaboo[ti]=1;
     }
    ti=oti;
   }
  void lcsc(dP a)
   {
    lc.tformsc(a.x/lcsc.x,a.y/lcsc.y);
    lcsc=a;
   }
  void stdCommb(double s)
   {
	  secs=s;
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=(int)(chs*samplerate*secs);
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
   }
  void  Sine(double freq,double s)
   {
    Sine(freq,s,true);
   }
  void  Sine(double freq,double s, Boolean b)
   {
    // mess with secs to end on zero crossing
    secs=s;
    if(b) stdCommb(secs);
    else
     {
      calcparams();
     }
    int l=ca.length;
    int al=a.length;
    if(l != (int)chshorts )
    {
    	tta("Wav.sine chsorts:"+chshorts+" not equal to ca.length:"+l+" a.length:"+al);
    	return;
    }
    tta("Wav.sine Good chsorts:"+chshorts+" ==  ca.length:"+l+" a.length:"+al);
    double rscs=Math.round(samplerate/freq); // samples/period of F
    double rj=6.283185307179586D*freq/samplerate; 
    for(int i=0;i<(int)chshorts; i++)
     {	 
    	// if(i%100 ==0) tta("i:"+i+" a.length:"+al); 
      //tta("sine i:"+i+" (int)chshorts:"+(int)chshorts);
      //   double rscs=Math.round(samplerate/10000);
      //   double yValue = 32767D * Math.sin((i%rscs)/rscs*6.283185307179586D);
      //            double yValue = 32767D * Math.sin(i*rj);
      ca[i]=(short)(int)Math.round(32767D * Math.sin(i*rj));
      cb[i]=ca[i];
      cabi2ba(i);
     // if(i%100 ==0) tta("post i:"+i); 
     }
    tta("synth done");
    conn();
   }
  void  Binaural(double freqa,double freqb,double s)
   {
	  secs=s;
    // mess with secs to end on zero crossing
    stdCommb(secs); // creates (int)chshorts
    //double rscsa=Math.round(samplerate/freqa); // samples/period of Fa
    double rja=6.283185307179586D*freqa/samplerate; 
    double rjb=6.283185307179586D*freqb/samplerate; 
    for(int i=0;i<(int)chshorts; i++)
     {	  
      ca[i]=(short)(int)Math.round(32767D * Math.sin(i*rja)); 
      cb[i]=(short)(int)Math.round(32767D * Math.sin(i*rjb));
      cabi2ba(i);
     }
    tta("Binaural synth done");
   }
  // steps,secs  ll,ul,steps,secs   
  // (pattern)  (1,0,-,-,1,1,1,0,1,0,0,0,-,1,-,1,1,0,1,-,0,0,-,0), secs/step
  // level(x) continuous play (can be shoooort)
  void Stair()
   {
   }
  void  Sweep(double bf,double ef,double s)
   {
	secs=s;
    stdCommb(secs); // creates chshorts
    double df=(ef-bf)/(int)chshorts;
    for(int i=0;i<(int)chshorts; i++)
     {
      double rscs=samplerate/(bf+i*df);
      double yValue = 32767D * Math.sin(i*6.283185307179586D/rscs);
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
     }
    tta("synth done");
   }
  void da2Wav(String daname)
   {
    double[] da=te.dahg(daname);
    da2Wav(da);
   }
  void da2Wav(double[] da)
   {
	  tta("Wav.da2Wav  needs attention");
    int chansamples=da.length; // number of (16 bit) channelsamples
    secs=chansamples/samplerate; 
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=(int)(4*samplerate*secs);
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    for(int i=0;i<chansamples;i++)
     { 
      ca[i]=(short)(int) da[i];
      cb[i]=(short)(int) da[i];
      cabi2ba(i);
     }
    tta("Wav.da2Wav done   chshorts:"+chshorts);
   }
  void Normalize()
   {
   }
  void Rampsamp(int samples)
   {
    Ramp(samples/samplerate);
   }
  void Ramp(double s)
   {
	  secs=s;
    stdCommb(secs); 
    double rampstep=0;
    double yValue=-32767D;
    double ystep=65536D/(int)chshorts;
    tta("Wav.Ramp secs:"+secs+" (int)chshorts:"+(int)chshorts+" ystep:"+ystep);
    for(int i=0;i<(int)chshorts;i++) 
     {
      yValue+=ystep;
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
     }
   }
  void synth()
   {
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=4*samplerate*600; // 60 sec
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    int tr=44100;
    int ttr=4410;
    int j=0;
    int jj=ttr;
    double yValue = 32767D;
    double oyV = 32767D;
    double yV = 32767D;
    double ny = 0D;
    int kt=6;
    for(int i=0;i<(int)chshorts; i++)
     {
      if(j==0)
       {
        j=tr;
        oyV=yV;
        // 65536 ;
        if(yV == 32767D) yV=0D;
        else if(yV == 16384D) yV=0D;
        else if(yV == 0D) yV=-16384D;
        else if(yV == -16384D) yV=-32767D;
        else if(yV == -32767D) yV=32767D;
        jj=ttr;
       }
      if(jj > 0) jj--;
      yValue= yV+((oyV-yV)*jj/ttr);
      if(yValue<0) yValue=65536D+yValue;
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
      j--;
     }
    tta("synth done");
   }
  void pulsesynth()
   {
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=4*samplerate*600; // 60 sec
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    int j=0;
    int tr=100;
    double yValue = 32767D;
    double oyV = 32767D;
    double yV = 32767D;
    int kt=1;
    for(int i=0;i<(int)chshorts; i++)
     {
      if(j==0)
       {
        tr=100;
        oyV=yValue;
        j=(int)(100+kt*1000);
        kt++;
        yV = 9767D*(-1+(int)(3*Math.random()));
       }
      if(tr==0) yValue=yV;
      else 
       {
        yValue= yV+((oyV-yV)*tr/100);
        tr--;
       }
      //double rscs=Math.round(samplerate/440);
      //double cf=sf+((ef-sf)*i/(int)chshorts);
      //double rscs=Math.round(samplerate/cf);
      //double rscs=samplerate/cf;
      //  double yValue = 32767D * Math.sin(i*6.283185307179586D/rscs);
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
      j--;
     }
    tta("synth done");
   }
  void holdsynth()
   {
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=4*samplerate*600; // 60 sec
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    double sf=10;
    double ef=.005;
    for(int i=0;i<(int)chshorts; i++)
     {
      //double rscs=Math.round(samplerate/440);
      double cf=sf+((ef-sf)*i/(int)chshorts);
      //double rscs=Math.round(samplerate/cf);
      double rscs=samplerate/cf;
      double yValue = 32767D * Math.sin(i*6.283185307179586D/rscs);
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
     }
    tta("synth done");
   }
  void singlesynth()
   {
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=4*samplerate*600; // 60 sec
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    double sf=100;
    double ef=.01;
    for(int i=0;i<(int)chshorts; i++)
     {
      //double rscs=Math.round(samplerate/440);
      double cf=sf+((ef-sf)*i/(int)chshorts);
      //double rscs=Math.round(samplerate/cf);
      double rscs=samplerate/cf;
      double yValue = 32767D * Math.sin(i*6.283185307179586D/rscs);
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
     }
    tta("synth done");
   }
  void synthsweep()
   {
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=4*samplerate*600; // 60 sec
    a=commb(new byte[44+(int)databytes],databytes,2,(double)stdbr,(double)4*stdbr,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    double sf=5;
    double ef=.01;
    for(int i=0;i<(int)chshorts; i++)
     {
      //double rscs=Math.round(samplerate/440);
      double cf=sf+((ef-sf)*i/(int)chshorts);
      //double rscs=Math.round(samplerate/cf);
      double rscs=samplerate/cf;
      double yValue = 32767D * Math.sin(i*6.283185307179586D/rscs);
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
     }
    tta("synth done");
   }
  void oldsynth()
   {
    tu=parent.tu;
    lc=parent.lc;
    mode=2;
    samplerate=stdbr;
    samplebits=16;
    chs=2;
    blockbytes=4; // chs*(samplebits/8)
    blockrate=(float)(samplerate*blockbytes);
    databytes=4*samplerate*20;
    a=commb(new byte[44+(int)databytes],databytes,2,samplerate,blockrate,4,16);
    chshorts=(int)databytes/(chs*(samplebits/8));
    ca=new short[(int)chshorts];
    cb=new short[(int)chshorts];
    for(int i=0;i<(int)chshorts; i++)
     {
      //double yValue = 32767D * Math.sin((j%rscs)/rscs*6.283185307179586D);
      //double rpd=1.0/rf;
      double rscs=Math.round(samplerate/10000);
      double yValue = 32767D * Math.sin((i%rscs)/rscs*6.283185307179586D);
      ca[i]=(short)(int)Math.round(yValue);
      cb[i]=ca[i];
      cabi2ba(i);
     }
    tta("synth done");
   }
  void square()
   {
    square(1);
   }
  void compab()
   {
    tta("Start compab");
    for(int i=0;i<(int)chshorts; i++)
     {	     
      cb[i]=(short)-ca[i]; 
      cabi2ba(i);
     } 
   }
  void compba()
   {
    tta("Start compab");
    for(int i=0;i<(int)chshorts; i++)
     {	     
      ca[i]=(short)-cb[i]; 
      cabi2ba(i);
     } 
   }
  void cpab()
   {
    tta("Start cpab");
    for(int i=0;i<(int)chshorts; i++)
     {	     
      cb[i]=ca[i]; 
      cabi2ba(i);
     } 
   }
  void cpba()
   {
    tta("Start cpba");
    for(int i=0;i<(int)chshorts; i++)
     {	     
      ca[i]=cb[i]; 
      cabi2ba(i);
     } 
   }
  void square(int lode)
   {
    tta("Start square lode:"+lode);
    if(lode==1)
     {
      for(int i=0;i<(int)chshorts; i++)
       {
        if(ca[i]< -27000) ca[i]=-30000;
        else if(ca[i]<3000) ca[i]=0;
        else ca[i]=30000;
        // cb[i]=ca[i]; keep cb
        cabi2ba(i);
       } 
     }
    else if(lode==2)
     {
      for(int i=0;i<(int)chshorts; i++)
       { 
        if(ca[i]< -27000) ca[i]=-30000;
        else if(ca[i]<3000) ca[i]=0;
        else ca[i]=30000;
        // cb[i]=ca[i]; keep cb
        int j=i;
        double ave=0;
        while(j>0 &&j > i-31)
         {
          ave+=ca[j];
          j--;
         }
        ave/=30;
        cb[i]=(short)(ave);
        cabi2ba(i);
       } 
     }
    if(lode==3)
     {
      for(int i=0;i<(int)chshorts; i++)
       {
        if(ca[i]< -21000) ca[i]=-30000;
        else if(ca[i]>=-21000 && ca[i]<21000) ca[i]=0;
        else ca[i]=30000;	    
        cb[i]=(short)-ca[i]; 
        cabi2ba(i);
       } 
     }
    tta("End square");
   }
  public void bigg() // https://www.codejava.net/coding/how-to-play-back-audio-in-java-with-examples
   {
    loadbigg(); 
   }
  public void loadbigg()
   {
    String audioFilePath="/webroot/ajlogo/html/ajlogo/version3beta/audio/3sec.wav";
    File audioFile = new File(audioFilePath);
    try
     {
      AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
      AudioFormat format = audioStream.getFormat();
      tta("bigg format:"+format.toString()); 
      sdlineInfo = new Info(SourceDataLine.class, format); 
      sdline = (SourceDataLine) AudioSystem.getLine(sdlineInfo);
      sdline.open(format);
      sdline.start(); // must be started 
      buff1=new byte[playbufsize]; 
      int bytesRead = -1; 
      int chunk=0; 
      while ((bytesRead = audioStream.read(buff1)) != -1)
       {
        for (int rko=0;rko<20;rko++)  tta("rko:"+rko+" buff1[rko]:"+buff1[rko]); 
        tta("chunk:"+chunk);
        chunk++;
        sdline.write(buff1, 0, bytesRead);
       }       
      sdline.drain();
      sdline.close();
      audioStream.close();
     }
    catch (UnsupportedAudioFileException ex)
     {
      System.out.println("The specified audio file is not supported.");
      ex.printStackTrace();
     } 
    catch (LineUnavailableException ex)
     {
      System.out.println("Audio line for playing back is unavailable.");
      ex.printStackTrace();
     } 
    catch (IOException ex)
     {
      System.out.println("Error playing the audio file.");
      ex.printStackTrace();
     }
   }
  public void exp()
   { 
    //import java.util.Arrays;
    int[] original = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};   // original array has 10 elements
    System.out.println("original array: " + Arrays.toString(original));
    int[] copy = Arrays.copyOf(original, 10);    // let's create an exact copy of the array
    System.out.println("exact copy: " + Arrays.toString(copy));
    int[] firstFive = Arrays.copyOf(original, 5); // let's copy only first 5 elements
    System.out.println("exact copy: " + Arrays.toString(firstFive));  
    int[] bigger = Arrays.copyOf(original, 15);// let's create a larger array by copying
    System.out.println("bigger copy: " + Arrays.toString(bigger));  
    int[] range = Arrays.copyOfRange(original, 2, 5);  // Now, let's copy a range of values from one array to another copying subarray from 2nd element to 5th element
    System.out.println("copying range of values 2 to 5: "+ Arrays.toString(range));
    //for (int rko=0;rko<20;rko++)  tta("rko:"+rko+" buff1[rko]:"+buff1[rko]); 
   }
  public String toStr()
   {
    //  public Wav(M p,String n,int sb,double sr,int ch,double seconds)
    secs=(int)(chshorts/samplerate);
    return "aWav "+name+","+samplebits+","+samplerate+","+chs+","+secs;
   }
  public static void tta(String s) { parent.tta(s); }
  public static void tta1(String s) { parent.tta1(s); }
  void schleep(long zzz)
   {
    try { thr.sleep(zzz); }
    catch(InterruptedException e) { }
   }
 }
