summaryrefslogtreecommitdiff
path: root/lib/jtty/genjtty.f90
blob: 65294a150f2274aa3548fcfafbe96eba0faa2c44 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
subroutine genjtty(umsg,itone,nsym)

! Input:  character*80 umsg               !User message
! Output: integer*4 itone(1:nsym)         !Tones for channel symbols
!         integer*4 nsym                  !Number of channel symbols

  use jtty_mod, only: JTTY_EXCHANGE_UNKNOWN
  parameter (MAX_TONES=59*16)       !Max number of channel symbols
  character*80 umsg                 !User-formatted message
  integer itone(MAX_TONES)          !Array of tone frequencies for this message

  interface
     subroutine genjtty_profile(umsg,exchange_profile,itone,nsym,frame_starts,is_final)
       use jtty_mod, only: MAX_FRAMES
       character(len=80), intent(inout) :: umsg
       integer, intent(in) :: exchange_profile
       integer, intent(out) :: itone(59*MAX_FRAMES),nsym
       integer, intent(out), optional :: frame_starts(MAX_FRAMES)
       integer, intent(in), optional :: is_final
     end subroutine genjtty_profile
  end interface

  call genjtty_profile(umsg,JTTY_EXCHANGE_UNKNOWN,itone,nsym)

 return
end subroutine genjtty

subroutine genjtty_profile(umsg,exchange_profile,itone,nsym,frame_starts,is_final)

  use jtty_mod, only: pack_jtty,MAX_FRAMES
  implicit none
  character(len=80), intent(inout) :: umsg
  integer, intent(in) :: exchange_profile
  integer, intent(out) :: itone(59*MAX_FRAMES),nsym
  integer, intent(out), optional :: frame_starts(MAX_FRAMES)
  integer, intent(in), optional :: is_final
  character(len=34) :: frames(MAX_FRAMES)
  integer :: nframes

  nsym=0
  call pack_jtty(umsg,frames,nframes,exchange_profile,frame_starts,is_final)
  if(nframes.le.0) return
  call genjtty_frames(frames,nframes,itone,nsym)
end subroutine genjtty_profile

subroutine genjtty_atoms(atoms,natoms,itone,nsym)

  use jtty_mod, only: jtty_source_atom,pack_jtty_atoms,MAX_FRAMES
  type(jtty_source_atom), intent(in) :: atoms(:)
  integer, intent(in) :: natoms
  integer, intent(out) :: itone(*)
  integer, intent(out) :: nsym
  character(len=34) :: frames(MAX_FRAMES)
  integer :: nframes
  logical :: valid

  call pack_jtty_atoms(atoms,natoms,frames,nframes,valid)
  nsym=0
  if(.not.valid) return
  call genjtty_frames(frames,nframes,itone,nsym)
end subroutine genjtty_atoms

subroutine genjtty_frames(frames,nframes,itone,nsym)

  use jtty_fec, only: PAYLOAD_BITS,tbcc_encode,is13
  use jtty_tbcc_code_profiles, only: JTTY_TBCC_PROFILE_1167_1545_80F
  implicit none
  character(len=34), intent(in) :: frames(*)
  integer, intent(in) :: nframes
  integer, intent(out) :: itone(*),nsym
  integer :: payload(PAYLOAD_BITS),tone_symbols(46),i,ib

  nsym=0
  do i=1,nframes
     read(frames(i),'(34i1)') payload
     call tbcc_encode(payload,tone_symbols,JTTY_TBCC_PROFILE_1167_1545_80F)
     ib=(i-1)*59+1
     itone(ib:ib+12)=is13
     itone(ib+13:ib+58)=tone_symbols
     nsym=nsym+59
  enddo
end subroutine genjtty_frames

subroutine genjtty_atoms_c(c_atoms,natoms,itone,nsym,status) bind(C,name='genjtty_atoms_c')

  use iso_c_binding, only: c_char,c_int,c_null_char
  use packjt77_grammar, only: pack77_arrl_section_index,pack77_arrl_section_name
  use jtty_mod, only: jtty_source_atom,jtty_source_atom_c,JTTY_ATOM_CALL, &
       JTTY_ATOM_EXCH_NUM,JTTY_ATOM_EXCH_LOC,JTTY_ATOM_EXCH_PAIR, &
       JTTY_ATOM_CONTROL,JTTY_ATOM_GRID4,jtty_call_atom,jtty_exch_num_atom, &
       jtty_exch_loc_atom,jtty_class_section_atom,jtty_control_atom, &
       jtty_grid4_atom,MAX_FRAMES,JTTY_ENCODE_OK,JTTY_ENCODE_INVALID_DESCRIPTOR, &
       JTTY_ENCODE_UNKNOWN_SECTION
  type(jtty_source_atom_c), intent(in) :: c_atoms(*)
  integer(c_int), value, intent(in) :: natoms
  integer(c_int), intent(out) :: itone(*)
  integer(c_int), intent(out) :: nsym
  integer(c_int), intent(out) :: status
  type(jtty_source_atom) :: atoms(MAX_FRAMES)
  character(len=13) :: descriptor_text
  integer :: i,section_index
  logical :: text_valid

  interface
     subroutine genjtty_atoms(atoms,natoms,itone,nsym)
       use jtty_mod, only: jtty_source_atom
       type(jtty_source_atom), intent(in) :: atoms(:)
       integer, intent(in) :: natoms
       integer, intent(out) :: itone(*)
       integer, intent(out) :: nsym
     end subroutine genjtty_atoms
  end interface

  nsym=0; status=JTTY_ENCODE_INVALID_DESCRIPTOR
  if(natoms.lt.1 .or. natoms.gt.MAX_FRAMES) return
  do i=1,natoms
     if(c_atoms(i)%reserved.ne.0) return
     call unpack_descriptor_text(c_atoms(i)%text,descriptor_text,text_valid)
     if(.not.text_valid) return
     select case(int(c_atoms(i)%kind))
     case(JTTY_ATOM_CALL)
        if(c_atoms(i)%role.ne.0 .or. c_atoms(i)%value.ne.0) return
        atoms(i)=jtty_call_atom(int(c_atoms(i)%subtype),descriptor_text)
     case(JTTY_ATOM_EXCH_NUM)
        if(len_trim(descriptor_text).ne.0) return
        atoms(i)=jtty_exch_num_atom(int(c_atoms(i)%role), &
             int(c_atoms(i)%subtype),int(c_atoms(i)%value))
     case(JTTY_ATOM_EXCH_LOC)
        if(c_atoms(i)%value.ne.0) return
        atoms(i)=jtty_exch_loc_atom(int(c_atoms(i)%role), &
             int(c_atoms(i)%subtype),descriptor_text)
     case(JTTY_ATOM_EXCH_PAIR)
        if(c_atoms(i)%subtype.ne.1) return
        if(c_atoms(i)%role.lt.0 .or. c_atoms(i)%role.gt.5) return
        section_index=pack77_arrl_section_index(descriptor_text)
        if(section_index.lt.1 .or. descriptor_text.ne.pack77_arrl_section_name(section_index)) then
           status=JTTY_ENCODE_UNKNOWN_SECTION
           return
        endif
        atoms(i)=jtty_class_section_atom(int(c_atoms(i)%value), &
             char(ichar('A')+int(c_atoms(i)%role)),section_index)
     case(JTTY_ATOM_CONTROL)
        if(c_atoms(i)%role.ne.0 .or. c_atoms(i)%value.ne.0 .or. &
             len_trim(descriptor_text).ne.0) return
        atoms(i)=jtty_control_atom(int(c_atoms(i)%subtype))
     case(JTTY_ATOM_GRID4)
        if(c_atoms(i)%subtype.ne.0 .or. c_atoms(i)%value.ne.0) return
        atoms(i)=jtty_grid4_atom(int(c_atoms(i)%role),descriptor_text)
     case default
        return
     end select
  enddo
  call genjtty_atoms(atoms,int(natoms),itone,nsym)
  if(nsym.gt.0) status=JTTY_ENCODE_OK

contains

  subroutine unpack_descriptor_text(c_text,text,text_valid)
    character(kind=c_char), intent(in) :: c_text(9)
    character(len=13), intent(out) :: text
    logical, intent(out) :: text_valid
    integer :: j

    text=''
    text_valid=.false.
    do j=1,9
       if(c_text(j).eq.c_null_char) then
          text_valid=.true.
          return
       endif
       if(j.gt.8) return
       text(j:j)=c_text(j)
    enddo
  end subroutine unpack_descriptor_text
end subroutine genjtty_atoms_c