! Test case description/purpose: We are testing the MULAWDECODE. The mulaw encode is tested
! by the MULAWDECODE.
!
! NOTE: The mulaw encode and decode should generate very similar results within
!       very small margin of error. When encoding and decoding is used some distortion
!       may occur. to avoid distortion, user should scale input data such 
!       that no sample exceeds +/-32767.
!
!       We are creating a 4k and 16k sine wave file, combining them, and then taking the FFT.
!       We then take the combined file mulaw encode and
!       decode it, then we take the FFT of the decoded file and compare
!       with the original combined fft file. Both fft files should show
!       spikes around the 4k and 16k.
!
! @bug NXM-2959 Add ULAW encode and decode library/primitives to AUDIO option tree
! @author Juan J Velasquez
! @since 3.7.1

startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  res prefix                    "test_mulawdecode_"
  res sin4kwave                 "^{prefix}sin4kwave.tmp"
  res sin16kwave                "^{prefix}sin16kwave.tmp"
  res sound4_16_combined        "^{prefix}sound4_16_combined.tmp"
  res sound4_16_fft_file        "^{prefix}sound4_16_fft_file.tmp"
  res sound4_16_mu_enc_file     "^{prefix}sound4_16_mu_enc_file.tmp"
  res sound4_16_mu_dec_file     "^{prefix}sound4_16_mu_dec_file.tmp"
  res sound4_16_mu_dec_fft_file "^{prefix}sound4_16_mu_dec_fft_file.tmp"

  ! result labels to be used by timer
  set len 0
  set len1 0

  set instructions "This test is for the ulaw encode and decode audio primitives\n &
                    we have created a 4K and 16K sine wave files, combined them and taking the FFT.\n&
                    From the combined file we have taken the ulaw encode and decode and then taken the FFT.\n &
                    Click on button PASS if both files show spikes at 4k and 16k\n"

  ! when running on Windows system it will take longer to run this test case
  if (interAct and (ENV.OSTYPE EQS "DOS")) then
    say "This test case is a long running test case and It will take around 18 sec to complete"
  endif

  waveform sin4kwave FORM=SF ELEM=50m SHAPE=SINE AMP=5 FREQ=4k  DELTA=1/(16k*5)
  waveform sin16kwave FORM=SF ELEM=50m SHAPE=SINE AMP=5 FREQ=16k DELTA=1/(16k*5)

  ! we need to concatenate both 4k and 16k file into a single before fft
  fcalc sound4_16_combined sin4kwave sin16kwave +
  fft sound4_16_combined sound4_16_fft_file nfft=16k

  timer on
  timer set
  ! encode the combined waveforms
  mulawencode sound4_16_combined sound4_16_mu_enc_file
  timer elapse len
  ! decode the encoded file
  mulawdecode sound4_16_mu_enc_file sound4_16_mu_dec_file
  timer elapse len1
  timer off

  say "mulawencode took ^len seconds"
  say "mulawdecode took ^len1 seconds"
  ! generate fft file of the decoded file
  fft sound4_16_mu_dec_file, sound4_16_mu_dec_fft_file nfft=16k

  assert/text="^reg.main.name: Verify that combined/fft file is equal to a generated by XMidas binary " sound4_16_fft_file FEQ/B nxm.audio.test.sound4_16fft
  assert/text="^reg.main.name: Verify that mulaw decoded/fft file is equal to a generated by XMidas binary " sound4_16_mu_dec_fft_file FEQ/B nxm.audio.test.sound4_16_mu_dec_fft

  if interAct then
    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft
      gcontrol text  idtext "Instructions" instructions 24 14 /title

      ! plot both decoded fft and combined fft
      plot/ID=deccombfft sound4_16_mu_dec_fft_file|sound4_16_fft_file TYPE=line
      ! plot the decoded fft file
      plot/ID=mudecodefft sound4_16_mu_dec_fft_file TYPE=line
    pipe off
  endif

  ! Cleanup test files
  files/e ^{prefix}*
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return
