/* GSM Clear */

crlf=d2c(13)d2c(10)

parse arg infile

if infile='' then
do
say crlf "GSM Clear (c) By Alek Winner."
say crlf "Prepare GSM file fo use w/ pcm2wav by Dr.POLLAK"
say "Usage: gsmclear infile" crlf
exit
end

if stream(infile,'c','query exist')='' then
 do
 say "File not found :" infile
 exit
 END

call time(R)

say "Clearing..."

abs_poz=0

do forever

frame=charin(infile,,2048)
if frame='' then
  do
  say "Clearing Done..."
  say "time elapsed: " time(E)
  exit
  end

rc=c2x(right(frame,3))
rc2=right(rc,4)

if rc = 'A5A5FE' | rc = 'A5A5B6' | rc2 = 'FEFE' | rc2 = 'B6B6' then
  do
  frm=charin(infile,,2)
  frame=frame||frm
  end


subs='FEFE'
call patcher


subs='B6B6'
call patcher


subs='10'
call patcher



call charout infile,frame, abs_poz+1

abs_poz=abs_poz+length(frame)


end


patcher:

poz=1

do forever
poz = Pos( X2C(subs), frame, poz)

If poz>0 Then
  Do
  if subs='10' then frame=left(frame,poz-1)||x2c(11)||substr(frame,poz+1)
  else
    do
    poz=poz+2
    rc=right(c2x(substr(frame,poz,1)),1)
    frame=left(frame,poz-1)||x2c("D"||rc)||substr(frame,poz+1)
    rc=c2x(substr(frame,poz,1))
    end
  end
else leave

end

return
