Morse Code with Associative Array

Barry, i couldnt resist and made some changes :slight_smile:
Also, after some tries, figured, i cannot differ signs (.-) from chars (a-z) or words, without the use of some delaying sleep calls...

time morse -l "Play it again barry." | while read line; do morse-snd "$line";done
You now hear: .--.;.-..;.-;-.--;SP
You now hear: ..;-;SP
You now hear: .-;--.;.-;..;-.;SP
You now hear: -...;.-;.-.;.-.;-.--;STOP
You now hear: SP
You now hear: EOT
            
real	0m33.488s
user	0m0.220s
sys	0m0.315s
+ ~ $ morse  "Play it again barry."
.--.;.-..;.-;-.--;SP..;-;SP.-;--.;.-;..;-.;SP-...;.-;.-.;.-.;-.--;STOPSPEOT

+ ~ $ morse-snd ".--.;.-..;.-;-.--;SP..;-;SP.-;--.;.-;..;-.;SP-...;.-;.-.;.-.;-.--;STOPSPEOT"
You now hear: .--.;.-..;.-;-.--;SP..;-;SP.-;--.;.-;..;-.;SP-...;.-;.-.;.-.;-.--;STOPSPEOT
+ ~ $       

[/CODE]

#!/bin/bash
# Working idea, Barry walker, G0LCU...
# Modified by Simon Arjuna Erat, sea
# Morse.sh --> morse-sox aka morse-snd
# Sounder for Morse at about 8 WPM...
# Output can be used to modulate an SSB HF transceiver...
# OSX 10.7.5, default bash terminal AND SOX.
# Just as easily do cat /tmp/sample.raw > /dev/dsp for machines with /dev/dsp.

WAIT_SIGN=0.05
WAIT_CHAR=0.5
WAIT_WORD=1.5
WAIT_SPACE=2

SOX=$(which sox||locate sox)
if [[ ! -f /tmp/dit.raw ]]
then	data="\\x80\\x26\\x00\\x26\\x7F\\xD9\\xFF\\xD9"
	sample=0
	# Create files & permission
	for F in dit dah;do
		> /tmp/$F.raw
		chmod 644 /tmp/$F.raw
	done
	# Increase data string
	for sample in {0..6} ; do data=$data$data ;done
	# Do not touch Barrys creations
	printf "$data" >> /tmp/dit.raw
	printf "$data" >> /tmp/dah.raw
	printf "$data" >> /tmp/dah.raw
	printf "$data" >> /tmp/dah.raw
fi

play_dot(){ $SOX -r 8000 -b 8 -c 1 -e unsigned-integer /tmp/dit.raw -d > /dev/null 2>&1 ; }
play_dash(){ $SOX -r 8000 -b 8 -c 1 -e unsigned-integer /tmp/dah.raw -d > /dev/null 2>&1 ; }

for input in $@;do
	printf "You now hear: " #$input"
	
	case "${input:2}" in
	"SP")	printf "SP"
		sleep $WAIT_SPACE 	;;
	"ST")	printf "STOP"
		sleep $WAIT_WORD 	;;
	*)	for (( i = 0; $i < "${#input}"; i = $i +1 ))
		do	printf "${input:$i:1}"
			case "${input:$i:1}" in
			"-")	play_dash	;;
			".")	play_dot	;;
			";")	sleep $WAIT_CHAR ;;
			esac
			sleep $WAIT_SIGN
		done
		;;
	esac
	printf "\nNew word..."
	sleep $WAIT_WORD
	printf "\r            \r"
done

Hope you like it :slight_smile:

Hi sea...

Ha ha, nice one matey.

I am trying my hardest to think of a way to decode Morse off air using the shell but this is NOT at all easy _just_ through the mic input of this MBP...

It would be difficult to accurately detect time slice _ratios of 1 for a dit and 1 for a gap, 3 for a dah and 1 for a gap and/or 3 for character space and/or 7 for a word space, etc, as this has to be done in real time. Remember; manually sending Morse is not a constant speed and human errors have to be corrected. Phew the more I think of it the more difficult it gets.

It is very easy to send is SSB mode using the tone generation but if physical keying is required to use CW mode on an HF transceiver a simple home brew circuit would be required.

I am looking into this in pure shell scripting but I don't hold any hopes. I am not going to go down the route of a compiled C(++) program to do it.

Wow! Sound and everything. :slight_smile:
But what would a radio operator be without the phonetic alphabet. I wonder if the words could be piped through espeak? hmmmm...

Anyway here it is and some extras...

phon=(
 [A]='Alpha'     ='Bravo'     [C]='Charlie'     [D]='Delta'     [E]='Echo'
 [F]='Foxtrot'     [G]='Golf'     [H]='Hotel'     ='India'     [J]='Juliet'
 [K]='Kilo'     [L]='Lima'     [M]='Mike'     [N]='November'     [O]='Oscar'
 [P]='Papa'     [Q]='Quebec'     [R]='Romeo'     ='Sierra'     [T]='Tango'
 ='Uniform'     [V]='Victor'     [W]='Whiskey'     [X]='X-ray'     [Y]='Yankee'
 [Z]='Zulu'    
)

Extras

 xtra=(
 [.-.-.]='AR'     [...-.-]='SK'     [--..--]=','     [.-.-.-]='.'     [-..-.]='/'
 [.]='SP'     [-...-]='--'     [..--..]='?'    
)
 xtra=(
 [AR]='.-.-.'     [SK]='...-.-'     [,]='--..--'     [.]='.-.-.-'     [/]='-..-.'
 [SP]='.'     [--]='-...-'     [?]='..--..'    
)