function streamer(action)
%STREAMER A demonstration of two-tone streaming
%
% Martin Cooke, April 1998
%    Revision May 98, to upgrade for V5.2


if nargin < 1
  action='init'; 
end

switch action

case 'init'
  % does main figure window exist? If so, bring it to front
  f=findobj('Tag','streamer_fig');
  if ~isempty(f)
    figure(f);
  else
    streamer_gui;		         % otherwise, create one
    ud=get(gcf,'UserData');
    ud.cycles=1;
    ud.fs=8192;
    ud.lowfreq=1000;
    ud.freqsep=0;
    ud.duration=50;
    ud.trt=100;
    ud.lft=tone(ud.lowfreq,65,40);
    % van Noorden's data
    ud.fission=line([60 150],...
    [2.5 4.0],'Visible','off','LineWidth',3,'LineStyle','--');
    ud.tcb=line([60 70 80 90 100 110 120 130 140 150],...
    [4 4 4.5 5 6 7 8 9.5 11 12.5],'Visible','off','LineWidth',3,'LineStyle','--');
    ud.t1=text(70,2,'fission boundary','Visible','off');
    ud.t2=text(30,12,'temporal coherence boundary','Visible','off');
    ud.t3=text(130,6,'ambiguous region ','Visible','off');
    set(gcf,'KeyPressFcn','streamer keypressed','UserData',ud); 
  end
    
  case 'showexpt'
    ud=get(gcf,'UserData');
    if get(gco,'Value')
      set([ud.tcb ud.fission ud.t1 ud.t2 ud.t3],'Visible','on');
    else  
      set([ud.tcb ud.fission ud.t1 ud.t2 ud.t3],'Visible','off');
    end
  
  case 'numCyclesChanged'
    ud=get(gcf,'UserData');
    s=get(gcbo,'Label');
    ud.cycles=str2num(get(gcbo,'Label'));
    set(findobj('Tag','cyclesmenu'),'Checked','off');
    set(gcbo,'Checked','on');  
    set(gcf,'UserData',ud);
    
  case 'generate'
    title('hit f if you hear 1 streams, s if you hear two; click again to repeat');
    cp=get(gca,'CurrentPoint');
    ud=get(gcf,'UserData');
    ud.trt=cp(1);
    ud.freqsep=cp(3);
    hf=fromSemitone(ud.lowfreq,ud.freqsep);
    ud.expecting=1;
    %y=synthInterleave([ud.lowfreq],[ud.lowfreq+ud.freqsep],ud.trt);
    
    hft=tone(hf,65,40);
    lft=ud.lft;
    gap=zeros(1,round(((ud.trt-40)*8012)/1000));
    trtgap=zeros(1,round((ud.trt*8012)/1000));
    onecycle=[hft gap lft gap hft gap trtgap]';
    % allcycles=zeros(ud.cycles,length(onecycle));
    o=ones(1,ud.cycles);
    allsig=onecycle(:,o);
    allsig=allsig(:);
    allsig=0.1*(allsig/max(allsig));
    soundsc(allsig(:),ud.fs);	  
    set(gcf,'UserData',ud);
    
  case 'keypressed'
    ud=get(gcf,'UserData');
    if (ud.expecting==1)
      % expecting a press
      if (get(gcf,'CurrentCharacter')=='f')
        % fusion
        hold on;
        plot(ud.trt,ud.freqsep,'g+','EraseMode','xor');
      elseif (get(gcf,'CurrentCharacter')=='s')
        hold on;
        plot(ud.trt,ud.freqsep,'ro','EraseMode','xor');
      end
      ud.expecting=0;
      set(gcf,'UserData',ud);
      title('click in the grid to hear a tone at specified frequency/level');
    end
   
otherwise
  maduievent('streamer',action);
     
  end
  
