How to resolve the algorithm Morse code step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Morse code step by step in the BASIC programming language

Table of Contents

Problem Statement

Morse code is one of the simplest and most versatile methods of telecommunication in existence. It has been in use for more than 175 years — longer than any other electronic encoding system.

Send a string as audible Morse code to an audio device   (e.g., the PC speaker).

As the standard Morse code does not contain all possible characters, you may either ignore unknown characters in the file, or indicate them somehow   (e.g. with a different pitch).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Morse code step by step in the BASIC programming language

Source code in the basic programming language

DECLARE SUB player (what AS STRING)

'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$ = "<<<<c>>>>"
        END SELECT
        PLAY o$
        PLAY "p" + LTRIM$(STR$(noteLen))
    NEXT i%
END SUB


      *TEMPO 8
      DIM morse$(63)
      FOR char% = 0 TO 63 : READ morse$(char%) : NEXT char%
      
      PROCmorse("The five boxing wizards jump quickly.")
      END
      
      DEF PROCmorse(text$)
      LOCAL element%, index%, char&, morse$
      FOR index% = 1 TO LEN(text$)
        char& = ASC(MID$(text$,index%)) AND &7F
        IF char& < 32 char& = 32
        IF char& > 95 char& -= 32
        morse$ = morse$(char&-32)
        FOR element% = 1 TO LEN(morse$)
          SOUND 1, -15, 148, VAL(MID$(morse$,element%,1))
          SOUND 1, -15, 0, 1
        NEXT element%
        SOUND 1, -15, 0, 2
      NEXT index%
      ENDPROC
      
      DATA 00,313133,131131,6,1113113,6,13111,133331,31331,313313,6,13131,331133,311113,131313,31131
      DATA 33333,13333,11333,11133,11113,11111,31111,33111,33311,33331,333111,313131,6,31113,6,113311
      DATA 133131,13,3111,3131,311,1,1131,331,1111,11,1333,313,1311,33,31,333
      DATA 1331,3313,131,111,3,113,1113,133,3113,3133,3311,6,6,6,6,113313


10 rem Morse code
20 dim c$(54)
30 for f = 1 to 54
40  read l$ : read m$
50  d$ = d$+l$ : c$(f) = m$
60 next f
100 line input "Message? ";t$
105 t$ = ucase$(t$)
110 for f = 1 to len(t$)
120  p = instr(d$,mid$(t$,f,1))
130  if p > 0 then print c$(p); else print "?";
140 next f
150 print
160 goto 100
1000 data "A","._","B","_...","C","_._.","D","_..","E",".","F",".._."
1010 data "G","__.","H","....","I","..","J",".___","K","_._","L","._.."
1020 data "M","__","N","_.","O","___","P",".__.","Q","__._","R","._."
1030 data "S","...","T","_","U",".._","V","..._","W",".__","X","_.._"
1040 data "Y","_.__","Z","__..","1",".____","2","..___","3","...__"
1050 data "4","...._","5",".....","6","_....","7","__...","8","___.."
1060 data "9","____.","0","_____",".","._._._",",","__..__","?","..__.."
1070 data "'",".____.","!","_._.__","/","_.._.","(","_.__.",")","_.__._"
1080 data "&","._...",":","___...",";","_._._.","=","_..._","+","._._.","-","_...._"
1090 data "_","..__._","\","._.._.","$","..._.._","@",".__._."


100 PROGRAM "Morse.bas"
110 STRING TONE$(48 TO 90)*5,ST$*254
120 SET CHARACTER 46,0,0,0,0,24,24,0,0,0:SET CHARACTER 47,0,0,0,0,126,126,0,0,0
130 FOR I=48 TO 90
140   READ TONE$(I)
150 NEXT
160 DO
170   PRINT :PRINT "String to convert to Morse code: ":INPUT PROMPT ">":ST$
180   LET ST$=LTRIM$(RTRIM$(UCASE$(ST$)))
190   FOR I=1 TO LEN(ST$)
200     LET C=ORD(ST$(I:I))
210     IF C>47 AND C<91 THEN
220       PRINT TONE$(C);" ";
230       FOR J=1 TO LEN(TONE$(C))
240         SOUND PITCH 48,DURATION(ORD(TONE$(C)(J))-45)^3+4
250         SOUND PITCH 126,DURATION 8
260       NEXT 
270     ELSE
280       PRINT 
290       SOUND PITCH 126,DURATION 16
300     END IF
310     SOUND PITCH 126,DURATION 16
320   NEXT
330   PRINT
340 LOOP UNTIL ST$=""
350 CLEAR FONT
360 DATA .////,..///,...//,..../,.....,/....,//...,///..,////.,/////,"","","","","","",""
370 DATA ./,/...,/./.,/..,.,../.,//.,....,..,.///,/./,./..,//,/.,///,.//.,//./,./.,...,/,../,.../,.//,/../,/.//,//..

100 DI$="IA" : REM DIT = EIGHTH NOTE
110 DA$="Q.A": REM DA = DOTTED QUARTER NOTE
120 IS$="IR" : REM SPACE BETWEEN  SYMS=1xDOT
130 IC$="QR" : REM EXTRA BETWEEN CHARS=2xDOT(TOTAL 3)
140 IW$="HR" : REM SPACE BETWEEN WORDS=4xDOT(TOTAL 7)
150 DIM MC$(127): REM READ CODE TABLE
160 DO
170 : READ C$
180 : IF C$="" THEN EXIT
190 : C=ASC(C$)
200 : READ C$
210 : MC$(C) = C$
220 LOOP
230 PRINT "ENTER MESSAGE:"
240 OPEN1,0:INPUT#1,M$:CLOSE1
250 TEMPO 128
260 P$="T7": REM ENVELOPE = ORGAN
270 FOR I=1 TO LEN(M$)
280 : A=ASC(MID$(M$,I))
290 : IF A=32 THEN BEGIN
300 :   P$=P$+IW$
310 : BEND:ELSE IF MC$(A)<>"" THEN BEGIN
320 :   C$=MC$(A)
330 :   FOR J=1 TO LEN(C$)
340 :     S$=MID$(C$,J,1)
350 :     IF S$="." THEN BEGIN
360 :       P$=P$+DI$
370 :     BEND:ELSE BEGIN
380 :       P$=P$+DA$
390 :     BEND
400 :     P$=P$+IS$
410 :   NEXT J
420 :   P$=P$+IC$
430 : BEND
440 NEXT I
450 PLAY P$
460 END
470 REM MORSE CODE TABLE DATA
500 DATA A,".-",   B,"-...", C,"-.-."
510 DATA D,"-..",  E,".",    F,"..-.",G,"--."
520 DATA H,"....", I,"..",   J,".---"
530 DATA K,"-.-",  L,".-..", M,"--",N,"-."
540 DATA O,"---",  P,".--.", Q,"--.-"
550 DATA R,".-.",  S,"...",  T,"-",U,"..-"
560 DATA V,"...-", W,".--",  X,"-..-"
570 DATA Y,"-.--", Z,"--.."
580 DATA 0,"-----",1,".----",2,"..---"
590 DATA 3,"...--",4,"....-",5,"....."
600 DATA 6,"-....",7,"--...",8,"---.."
610 DATA 9,"----."
620 DATA ".",".-.-.-", ",","--..--"
630 DATA "?","..--.."
640 DATA ""


  

You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the QB64 programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Lua programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Phix programming language
You may also check:How to resolve the algorithm Record sound step by step in the Diego programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the GAP programming language