S=load('gong');
Fs=11e3;
t0=(7+10+6+5+12)*60;
st=clock;
Nt=fix(t0*Fs*1.2);
Ng=length(S.y);
t=0:2/Fs:Ng/S.Fs;
gong=interp1(S.y/2,t*S.Fs);
Ng=length(gong);
y=zeros(1,Nt);
nt=1;
while(nt<fix(Nt/1.02))
  N=fix(stdchoose(20,40));
  dn=abs(randn)/2+.2;
  dx=2*abs(randn)/2+1;
  g1=2^(.25+3.75*rand);
  dir1=double(rand>.5);
  r1=1+rand;
  
  fn=440+abs(randn)*100;
  fx=440-abs(randn)*100;
  g2=2^(.25+3.75*rand);
  dir2=double(rand>.5);
  r2=1+rand;
  
  n=0:N;
  s1=(dir1+(-1)^(dir1)*n/N);
  d=dn+(dx-dn)*sin(pi/r1*s1.^g1);
  off=d/2;
  s2=(dir2+(-1)^(dir2)*n/N);
  f=fn+(fx-fn)*sin(pi/r2*s2.^g2);
  plot(n/N,[d;f/440],'o-');
  drawnow;
  
  et=nt/Fs;
  if(rand<.3 +.7*(tanh(3*(et/t0-1))+1)/2)
    d=.5
    t=0:1/Fs:d;
    if(rand<.01+.8*(tanh(3*(et/t0-1))+1)/2)
      y(nt+(1:length(t)))=A*sin(2*pi*300*((t/d)).^2);
      nt=nt+length(t);
    else
      y(nt+(1:length(t)))=A*sin(2*pi*300*(1-(t/d)).^2);
      nt=nt+length(t);
    end
    d=stdchoose(15,10);
    f=440;
    A=1;
    t=0:1/Fs:d;
    y(nt+(1:length(t)))=A*(.3+.7*t./d).*exp(-(sin(2*pi*t.*(1+4*t/d)/2)/.2).^2).*sin(2*pi*f*t)/2;
    nt=nt+length(t);
    y(nt+(1:Ng))=gong;
    nt=nt+Ng;
  else
    for n=0:N
      s1=(dir1+(-1)^(dir1)*n/N);
      d=dn+(dx-dn)*sin(pi/r1*s1.^g1);
      off=d*(.5+randn/10);
      s2=(dir2+(-1)^(dir2)*n/N);
      f=fn+(fx-fn)*sin(pi/r2*s2.^g2);
      A=1;
      t=0:1/Fs:d;
      y(nt+(1:length(t)))=A*exp(-((t-off)/.04).^2).*sin(2*pi*f*t)/2;
      nt=nt+length(t);
    end
    d=.5;
    t=0:1/Fs:d;
    y(nt+(1:length(t)))=A*sin(2*pi*300*(1-(t/d)).^2);
    nt=nt+length(t);
  end
  %   pause(stdchoose(15,20));
  t=0:1/Fs:stdchoose(1,20);
  y(nt+(1:length(t)))=0;
  nt=nt+length(t);
end
wavwrite(y,Fs,'randsnd');
