!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! This macro uses an ICE card to tune 1 or more channels from an
! input file on disk to seperate out files
!
! The list file is a 3000 file with the subrecords 
!	BW = desired bandwidth in Hz
!	FREQ = tuned center freq in Hz
!	NAME = output file name
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
startmacro/quiet u:device a:ifile u:aname u:abw u:afreq u:again u:aform a:olist

local idur odur ntun nchan ctype cpp cids iform idelta nlist 
local ogain osamp ilen itc oform prate list fname drate
local ii iim nn irate ichan orate odec oname obw ofreq 

calc ogain 0 	! default gain
calc prate 10e6	! playback rate
calc idur 1	! default buffer length
calc odur 5	! default buffer length

if /gen gt 0 then
  callp getList 
  xdata/mod olist
  return
endif

status ifile f=iform xd=idelta sz=ilen tc=itc
res irate 1/idelta

calc idur idelta*ilen/2 idur min
calc odur idelta*ilen odur min

if /pbrate gt 0 res prate /pbrate

if olist neqs " "
  status olist sz=nlist
else
  res nlist 1
endif

callp getCardConfig

pic create pic_ifile iform idur*prate irate 

! loop through list of signals 
res ii 0
res iim 0
loop nlist nn

  ! initiate pipe mode and setup data source
  if ii eq 0 then
    picd reset ^device t1
    pipe/nw=256 on
    sinkpic/throttle=2/srate=prate/pause=0.1/port=module2/flags=intclk/tl=64k &
	ifile pic_ifile ^device
  endif
  res ii ii+1

  ! setup a tuner channel
  if aname eqss "~" 
    res oname olist(nn;^aname)
  elseif nlist gt 1
    res oname ^{aname}^nn
  else
    res oname ^aname
  endif
  if abw eqss "~" 
    res obw olist(nn;^abw)
  else
    res obw ^abw
  endif
  if afreq eqss "~" 
    res ofreq olist(nn;^afreq)
  else
    res ofreq ^afreq
  endif
  if again eqss "~" 
    res ogain olist(nn;^again)
  else
    res ogain ^again
  endif
  if aform eqss "~" 
    res oform olist(nn;^aform)
  else
    res oform ^aform
  endif
  res ichan cids(^ii)	! next available tuner
  picd/nopipe TDEC ^device ,, odec 1 irate/2/obw
  calc orate irate/odec
  if oform eqs "CI" calc orate orate/2
  pic/nopipe create pic_ofile_^ii oform odur*orate orate 
  calc osamp ilen irate / orate * fix 
  if /echo gt 0 
    say "Tuner ^nn BW=^obw FREQ=^ofreq GAIN=^ogain FORM=^oform FILE=^oname"
  endif
  sourcepic/port=tuner^ichan/flags=iom=test/tl=16k&
		/maxout=osamp/tc=ztc/tcoff=itc &
	pic_ofile_^ii ^oname ^device odec ofreq ogain

  ! kick off this pass
  if ii eq cpp or nn eq nlist
    if /echo gt 0 say "Starting pass with ^ii tuner channels"
    timer set
    pipe off
    timer elapse
    calc iim iim ii max
    res ii 0
  endif

endloop

erase pic_ifile
loop iim ii
  erase pic_ofile_^ii
endloop

picd reset ^device

endmacro


procedure getCardConfig

picd get ^device "CHNS" ntun
picd get ^device "CTYPE" ctype

if ctype eq 8 or ctype eq 9
  if ntun eq 6 or abw eqss "~"
    res cpp 5
    res cids (1,9,17,2,10)
  else if ntun eq 12
    res cpp 10
    res cids (1,3,9,11,17,18,2,4,10,12)
  else if ntun eq 24
    res cpp 20
    res cids (1,3,5,7,9,11,13,15,17,19,21,23,2,4,6,8,10,12,14,16)
  endif
elseif ctype eq 4
  if ntun eq 2 or abw eqss "~"
    res cpp 2
    res cids (1,2)
  else if ntun eq 4
    res cpp 4
    res cids (1,3,2,4)
  else if ntun eq 8
    res cpp 8
    res cids (1,3,5,7,2,4,6,8)
  endif
else
  res cpp 2
  res cids (1,2)
endif
return


procedure getList 
if olist eqs " " res olist "PICTUNER_LIST"
fcreate olist
header olist t=3000 f=nh nsr=5  rl=64 &
	sr(1)=(FREQ,SD,0) sr(2)=(BW,SF,8) sr(3)=(GAIN,SF,12) sr(4)=(FORM,1A,16) sr(5)=(NAME,5A,24)
res olist(1;~FREQ) 1e6
res olist(1;~BW)   1e3
res olist(1;~GAIN) 0
res olist(1;~FORM) "CI"
res olist(1;~NAME) "TUNE_SIGNAL_X
return

