simplified constant definitions, started examples
[ambros.git] / src / morse / sniptime
blobf20bf9d24dcb339b4de2cc7f72636ef93b54bc32
1 #!/bin/sh
2 # sniptime:
3 # cut input text to given duration,
4 # or calculate total duration (if duration is missing or 0)
6 # args: speed/WpM, [duration/sec]
7 # stdin: standardized text
8 # stdout: duration/sec | cut text
10 # Morse character table, generated by chargen.sh
11 chartab=`dirname $0`/chartab.txt
12 # speedbase: VVVVV=64, CODEX=60, PARIS=50
13 speedbase=${SPEEDBASE:-50}
15 # calculate duration/ditunits of characters
16 # (one character per argument)
17 wordduration() {
18 local wd cd
19 wd=0
20 # loop through characters
21 while test ! -z $1
23 # search character in chartab, remove first and third column
24 cd=`grep -F "_$1" $chartab`
25 cd=${cd#* }
26 cd=${cd% *}
27 # echo : $1 : cd=$cd >&2
28 # empty if not found, then set duration 0
29 cd=${cd:-0}
30 # and add to word total
31 wd=`expr $wd + $cd`
32 shift
33 done
34 #echo : $1 : wd=$wd >&2
35 echo $wd
38 if test -z $1
39 then echo "$0 : missing speed/WpM" >&2
40 exit 2
43 # speed in dit units per minute
44 ditspeed=`expr $speedbase '*' $1`
45 #echo : ditspeed $ditspeed
46 # set duration to 0 if empty, i.e calculate duration
47 duration=${2:-0}
48 #echo : $duration sec
49 # convert from seconds into ditunits
50 duration=`expr $duration '*' $ditspeed / 60`
51 #echo : $duration ditunits
53 # separate words into lines, then characters by space, and remove empty lines
54 sed -e 's/[ _ ][ _ ]*/\
55 /g;s/\(.\)/ \1/g;/^[ _ ]*$/d' | {
56 total=0
57 while read word
58 # calculate duration of word including inter-word pause (_)
59 do total="$total + `wordduration $word _`"
60 # echo :: total = $total >&2
61 total=`expr $total`
62 # duration>0, then echo input text until duration reached
63 if test $duration -gt 0
64 then if test $duration -lt $total
65 then break
66 # again remove separating spaces from word
67 else echo "$word" | sed -e 's/ //g'
70 # echo :$word: >&2
71 done
72 # duration<=0, then report duration
73 if test $duration -le 0
74 # convert from ditunits back into seconds
75 then expr $total '*' 60 / $ditspeed