! Tests the PIPECUTS primitive.
!
!
! @author  J. Czechowski IV
! @version $Id: test_pipecuts.mm,v 1.7 2005/01/04 17:06:11 jxc Exp $
!
startmacro

  switch "FS" L:frameSize GET DEF=8000
  switch "FORMAT" S:format GET DEF="SB"
  switch "INTERACTIVE" interAct GET DEF=TRUE

  res inst "Should see same shape for input and output plots"

  set sr 8e4
  if format EQS "SB" then
    set amp 100
  else
    set amp 1e4
  endif

  info "^reg.main.name: Running with SR = ^sr, format = ^format, amp = ^amp, frame size = ^frameSize"
  if interAct isTRUE then
    pipe init
      panel/msgid=MAIN/id=panel/setup=1/controls=gc

      gcontrol label  macro  "Test Macro"
      gcontrol text   idtext "Instructions" ^inst 24 5 /title
      gcontrol button ctl    "" "Play,Stop,Pause,Resume,RealTime" /tleft /NC=3
      gcontrol menu   MAIN   "Exit with Status: " "PASS,FAIL" /tleft

      waveform/rt=full OUT=_wave FORM=^format ELEM=10e6 SHAPE=square FREQ=sr/10 AMP=amp DELTA=1/sr
      noop/rt _wave{FS=^frameSize} _in
      pipecuts/ID=PCUTS/INT=10 IN=_in OUT=_out
 
      plot/id=PLOT1 _in  type=line y1=-amp y2=amp
      plot/id=PLOT2 _out type=line y1=-amp y2=amp
    pipe off
  else
    set inputFile "apenny"
    if inputFile NFEXISTS then
      error "Could not find ^inputFile"
    endif
    set outFileList "pipecut_0_60,pipecut_100_60"
    set outputFile "temp_pipecuts_out"
    info "Cutting ^inputFile with PIPECUTS..."
    pipecuts ^inputFile 
    info "Joining ^outFileList with PIPECUTS..."
    pipecuts/join="pipecut_0_60,pipecut_100_60",,outputFile
    assert/text="^reg.main.name: pipecuts in = pipecuts out" inputFile FEQ/D outputfile
    erase outputfile
  endif

endmacro

procedure processMessage m:msg
  if msg.name EQSS "MAIN" then
    assert/text="Test" "PASS" SUBS msg.data
    pipe stop

  elseif msg.name EQSS "TVAL"  then
    res time msg.data
    set gc.idtext.value "^msg.data: playback from disk. File at time ^time"
    invoke temp PCUTS.openCutFile(time)

  elseif msg.name EQSS "CTL" then
    if msg.data EQSS "PLAY" then
      gcontrol/temp menu PlayMenu  "" "PlaySingleFile,PlayFromFile,PlayFromIndex,PlayFromTime"

    elseif msg.data EQSS "PAUSE" then
      invoke temp PCUTS.pause()

    elseif msg.data EQSS "RESUME" then
      invoke temp PCUTS.resume()

    elseif msg.data EQSS "STOP" then
      invoke temp PCUTS.stop()

    elseif msg.data EQSS "REALTIME" then
      invoke temp PCUTS.playFromFile(-1)

    else
      say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
    endif

  elseif msg.name EQSS "PLAYMENU" then
    if msg.data EQSS "PLAYSINGLEFILE" then
      gcontrol/temp file playFile "Play File:"

    elseif msg.data EQSS "PLAYFROMFILE" then
      gcontrol/temp file startFile "Start File:"

    elseif msg.data EQSS "PLAYFROMINDEX" then
      gcontrol/temp prompt startIndex "Start Index:" 1

    elseif msg.data EQSS "PLAYFROMTIME" then
      gcontrol/temp prompt startTime  "Start Time (sec):" 0

    else
      say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
    endif

  elseif msg.name EQSS "PLAYFILE" then
    res s:fname msg.data
    invoke temp PCUTS.playSingleFile(fname)

  elseif msg.name EQSS "STARTFILE" then
    res s:fname msg.data
    invoke temp PCUTS.playFromFile(fname)

  elseif msg.name EQSS "STARTINDEX" then
    res l:index msg.data
    invoke temp PCUTS.playFromIndex(index)

  elseif msg.name EQSS "STARTTIME" then
    res d:time msg.data
    invoke temp PCUTS.playFromTime(time)

  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return
