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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Morse code step by step in the Gambas 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 Gambas programming language

Source code in the gambas programming language

'Requires component 'gb.sdl2.audio'

Public Sub Main()
Dim sMessage As String = "Hello World"
Dim sFile As String = File.Load("../morse.txt") 'Contains A,.- B,-... etc
Dim sChar As New String[]
Dim sMorse As New String[]
Dim sOutPut As New String[]
Dim sTemp As String
Dim siCount, siLoop As Short
Dim bTrigger As Boolean
Dim fDelay As Float = 0.4

If Not Exist("/tmp/dot.ogg") Then Copy "../dot.ogg" To "/tmp/dot.ogg"     'Sounds downloaded from 
If Not Exist("/tmp/dash.ogg") Then Copy "../dash.ogg" To "/tmp/dash.ogg"  'https://en.wikipedia.org/wiki/Morse_code#Letters.2C_numbers.2C_punctuation.2C_prosigns_for_Morse_code_and_non-English_variants

For Each sTemp In Split(sFile, gb.NewLine)
  sChar.add(Split(Trim(sTemp))[0])
  sMorse.add(Split(Trim(sTemp))[1])
Next

For siCount = 1 To Len(sMessage)
  For siLoop = 0 To sChar.Max
    If sChar[siLoop] = Mid(UCase(sMessage), siCount, 1) Then
      sOutPut.Add(sMorse[siLoop])
      bTrigger = True
      Break
    End If 
  Next
  If bTrigger = False Then sOutPut.Add(" ")
  bTrigger = False
Next

Print sOutPut.Join(" ")

For siCount = 0 To Len(sMessage) - 1
  For siLoop = 0 To Len(sOutPut[siCount]) - 1
    If Mid(sOutPut[siCount], siLoop + 1, 1) = "." Then
      Music.Load("/tmp/dot.ogg")
      Music.Play
      Wait fDelay
    Else If Mid(sOutPut[siCount], siLoop + 1, 1) = "-" Then
      Music.Load("/tmp/dash.ogg")
      Music.Play 
      Wait fDelay
    Else
      Wait fDelay
    Endif
  Next
Next

End

  

You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the bc programming language
You may also check:How to resolve the algorithm CUSIP step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the BQN programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the ATS programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the TXR programming language