summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_jtty_gap_merge.f90
blob: ff13e28e36d21c7c96cbd493cae4baf381195501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
program test_jtty_gap_merge

  ! When a frame in the middle of a multi-frame message fails to decode
  ! (common at marginal SNR), the message must still remain one message with
  ! a gap marker between the surviving fragments, not split into two
  ! messages (the bug Joe reported on 000000_000011.wav: "THE QUICK BROWN FOX
  ! JUMPED OVER THE LAZY DOG." decoded as two separate lines, "THE Q" and
  ! "BROWN FOX JUMPED OVER THE LAZY DOG.", losing "UICK " with no trace).

  use iso_fortran_env, only: int16
  use jtty_fec, only: is13, TOTAL_K
  use jtty_mdec, only: npending,pending_updates,discard_pending_updates
  use jtty_mod, only: MAX_FRAMES
  implicit none

  character(len=*), parameter :: msg='THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.'
  integer, parameter :: nsps=384
  integer, parameter :: frame_symbols=size(is13)+TOTAL_K
  integer :: failures,total_samples,nframes
  integer(int16), allocatable :: reference_pcm(:)

  failures=0

  call build_message_pcm(msg,reference_pcm,total_samples,nframes)
  call run_dropped_frame_case(msg,reference_pcm,total_samples,nframes,1,failures)

  if(failures.ne.0) then
     write(*,'(a,i0)') 'test_jtty_gap_merge: failures=',failures
     stop 1
  endif
  write(*,'(a)') 'test_jtty_gap_merge: all checks passed'

contains

  subroutine build_message_pcm(message,pcm,total_samples,nframes)
    character(len=*), intent(in) :: message
    integer(int16), allocatable, intent(out) :: pcm(:)
    integer, intent(out) :: total_samples,nframes
    integer :: tones(MAX_FRAMES*frame_symbols)
    integer :: nsymbols
    real, allocatable :: wave(:)
    complex, allocatable :: complex_wave(:)
    character(len=80) :: input

    input=message
    call genjtty(input,tones,nsymbols)
    nframes=nsymbols/frame_symbols
    total_samples=(nsymbols+frame_symbols)*nsps
    allocate(wave(nsymbols*nsps),complex_wave(nsymbols*nsps),pcm(total_samples))
    call gen_jttywave(tones,nsymbols,nsps,2.0,12000.0,1500.0, &
         complex_wave,wave,0,nsymbols*nsps)
    pcm=0_int16
    pcm(1:nsymbols*nsps)=int(nint(30000.0*wave),int16)
  end subroutine build_message_pcm

  ! Drops consecutive middle frames and confirms the message still merges
  ! into one slot with a fixed-width gap marker.
  subroutine run_dropped_frame_case(expected_text,reference_pcm, &
       total_samples,nframes,n_dropped,count)
    character(len=*), intent(in) :: expected_text
    integer(int16), intent(in) :: reference_pcm(:)
    integer, intent(in) :: total_samples,nframes,n_dropped
    integer, intent(inout) :: count
    integer :: idrop
    integer :: drop_start,drop_end
    integer(int16), allocatable :: pcm(:)
    character(len=80) :: before,after
    character(len=200) :: description
    integer :: gap_pos
    logical :: surviving_text_matches

    pcm=reference_pcm
    idrop=nframes/2

    ! Zero out idrop..idrop+n_dropped-1's audio so those frames can't sync.
    drop_start=(idrop-1)*frame_symbols*nsps+1
    drop_end=(idrop+n_dropped-1)*frame_symbols*nsps
    pcm(drop_start:drop_end)=0_int16

    call discard_pending_updates()
    call rjtty_sub(pcm,1,nsps,200,2800,1500.0,50.0)
    call rjtty_sub(pcm,total_samples,nsps,200,2800,1500.0,50.0)

    write(description,'(a,i0,a)') 'dropping ',n_dropped, &
         ' consecutive frame(s) still merges into one message'
    call expect(npending.eq.1,trim(description),count)
    if(npending.ne.1) return

    ! The gap sentinel is a fixed-width 5-tilde run regardless of how many
    ! frames were actually dropped -- " ... " is a generic "something's
    ! missing" marker, not a precise character count (matches Joe's own
    ! suggested display).
    gap_pos=index(pending_updates(1)%decoded,'~~~~~')
    write(description,'(a,i0,a)') 'dropping ',n_dropped, &
         ' consecutive frame(s) leaves a gap sentinel'
    call expect(gap_pos.gt.0,trim(description),count)
    if(gap_pos.le.0) return

    before=trim(normalized(adjustl(pending_updates(1)%decoded(1:gap_pos-1))))
    after=trim(normalized(adjustl(pending_updates(1)%decoded(gap_pos+5:))))
    write(description,'(a,i0,a)') 'dropping ',n_dropped, &
         ' consecutive frame(s): surviving text matches the original message'
    surviving_text_matches=.false.
    if(len_trim(before).gt.0 .and. len_trim(after).gt.0 .and. &
         len_trim(after).le.len_trim(expected_text)) then
       surviving_text_matches=index(expected_text,trim(before)).eq.1 .and. &
            expected_text(len_trim(expected_text)-len_trim(after)+1: &
            len_trim(expected_text)).eq.trim(after)
    endif
    call expect(surviving_text_matches,trim(description),count)
  end subroutine run_dropped_frame_case

  function normalized(value) result(result_value)
    character(len=*), intent(in) :: value
    character(len=80) :: result_value
    integer :: i

    result_value=adjustl(value)
    do i=1,len_trim(result_value)
       if(result_value(i:i).eq.'~') result_value(i:i)=' '
    enddo
  end function normalized

  subroutine expect(condition,description,count)
    logical, intent(in) :: condition
    character(len=*), intent(in) :: description
    integer, intent(inout) :: count

    if(.not.condition) then
       count=count+1
       write(*,'(a)') 'FAIL: '//description
    endif
  end subroutine expect

end program test_jtty_gap_merge