L.MorseCodeTranslator
1.1.1
See the version list below for details.
dotnet add package L.MorseCodeTranslator --version 1.1.1
NuGet\Install-Package L.MorseCodeTranslator -Version 1.1.1
<PackageReference Include="L.MorseCodeTranslator" Version="1.1.1" />
paket add L.MorseCodeTranslator --version 1.1.1
#r "nuget: L.MorseCodeTranslator, 1.1.1"
// Install L.MorseCodeTranslator as a Cake Addin #addin nuget:?package=L.MorseCodeTranslator&version=1.1.1 // Install L.MorseCodeTranslator as a Cake Tool #tool nuget:?package=L.MorseCodeTranslator&version=1.1.1
MorseCode-Translator
A small morsecode translator written in C# for general morsecode translation for a console app.
Description
This is one of the very first versions of my MorseCode-Translator containing simple convertions. It makes use of a flexible MorseCharCollection in which you can chose from plenty of characters to display. You can either start with the whole alphabet, only a few of them or completely new ones that are not defined yet (e.g dollar sign $).
The library offers:
- Basic alphabet convertions from text-to-morse/morse-to-text
- Basic alphabet soundfile creation aswell as output to console
- Custom morsecharacter convertions from text-to-morse/morse-to-text
- Custom morsecharacter soundfile creation aswell as output to console
- Morse-soundfile-to-text-conversion as in Soundfile → MorseChar (Only works with the morse soundfiles created within this project or those similiar)
Installation
To install the library, you can use the NuGet-package market through visual studio or per NuGet package installer:
dotnet add package L.MorseCodeTranslator --version xxx
Or the build in version in Visual Studio:
NuGet\Install-Package L.MorseCodeTranslator -Version xxx
Newest version is listed here: https://www.nuget.org/packages/L.MorseCodeTranslator/
Usage
Depending on what you want to use you can go different routes.
Getting started
To make complete use of the MorseCode-Translator start by creating an instance of a MorseCodeTranslator:
MorseCodeTranslator translator = new MorseCodeTranslator();
The constructor with no parameters initializes all MorseChars from the alphabet inside of a MorseCharCollection. Alternatively you can create only a handful of MorseChars with custom MorseRepresentations:
List<MorseChar> morseList = new List<MorseChar>
{
new MorseChar('n', "..."),
new MorseChar('a',".--"),
new MorseChar('h',".-.-")
};
MorseCharCollection morseCharCollection = new MorseCharCollection(morseList);
MorseCodeTranslator translator = new MorseCodeTranslator(morseCharCollection);
//use translator and or the collection from here on...
Once you've created a collection or a translator, you can now start using their methods for MorseConvertions or SoundFile convertions. Keep in mind that if you chose to use only a handful of MorseChars, the MorseCodeTranslator wont have access to the specific soundfiles of the alphabet. In any case, the programm will create the soundfiles for each char specified.
However, if there are only e.g. 3 chars inside the MorseCodeTranslator and you use any kind of convertion method, the MorseCodeTranslator refers back to the original MorseRepresentations from the alphabet (See examples).
MorseConvertions
First of all, you can convert the whole MorseCharCollection back into its MorseRepresentations and use it for example like so:
string[] morseRepresentations = morseCharCollection.ConvertToMorseCharRepresentation();
for (int i = 0; i < morseRepresentations.Length; i++)
{
Console.WriteLine(morseRepresentations[i]);
}
//Output with example MorseCharCollection from before
//...
//.--
//.-.-
Moreover, you can use the translator to for example read a string input and convert it to the MorseCharRepresentations:
MorseCodeTranslator translator = new MorseCodeTranslator();
while (true)
{
Console.WriteLine("Type in some text: ");
string input = Console.ReadLine();
try
{
string[] inputToMorse = translator.ConvertStringToMorse(input);
for (int i = 0; i < inputToMorse.Length; i++)
{
Console.WriteLine(inputToMorse[i]);
}
}
catch (MorseCharNotFoundException ex)
{
Console.WriteLine("Wrong input: " + ex.Message);
}
}
//input: hello world
/*output: ....
.
.-..
.-..
---
.--
---
.-.
.-..
-.. */
If we were to type in hello world with any upper case letters we'd get an expected exception. This is because the methods are all case sensitive, meaning you can have a different morse representation for 'a' and 'A' respectively.
//input Hello World
//Wrong input: Error: cannot convert text into morse-representation because there are characters that were not defined yet
There are many more Morse/text-based convertions including MorseToChar, CharToMorse, StringToMorse, MorseToString etc..
SoundConversions
There are many incorporated methods that help you realise the sound aspect of morse code within this project. First of all, you can play the morse sounds to the console (or most other application) by using the PlayMorse methods.
MorseCodeTranslator translator = new MorseCodeTranslator();
while (true)
{
Console.WriteLine("Type in some text: ");
string input = Console.ReadLine();
try
{
string[] inputToMorse = translator.ConvertStringToMorse(input);
for (int i = 0; i < inputToMorse.Length; i++)
{
Console.WriteLine(inputToMorse[i]);
}
translator.PlayMorseFromString(input);
}
catch (MorseCharNotFoundException ex)
{
Console.WriteLine("Wrong input: " + ex.Message);
}
}
For the PlayMorseFromString version, you can chose to implement a delay between each char.
translator.PlayMorseFromString(input,TimeSpan.FromSeconds(1));
Moreover, you can use the MorseCodeTranslator's (in some cases static) methods to encode and decode soundfiles. Encode and then decode like so:
MorseCodeTranslator translator = new MorseCodeTranslator();
var morseFromString = translator.ConvertStringToMorse("csharp is the best language ever");
MorseCodeTranslator.EncodeMorseToSoundFile(morseFromString, "CSHARP");
var morseFromSoundFile = MorseCodeTranslator.DecodeSoundFileToMorse(@"MorseSoundFiles\CSHARP.wav");
var textFromSoundFile = translator.DecodeSoundFileToText(@"MorseSoundFiles\CSHARP.wav");
foreach (var morse in morseFromSoundFile)
{
Console.WriteLine("MorseFromSoundFile: " + morse);
}
Console.WriteLine("TextFromConversion: " + translator.ConvertMorseToString(morseFromSoundFile));
Console.WriteLine("TextFromSoundFile: "+ textFromSoundFile);
//Output:
/*
MorseFromSoundFile: -.-.
MorseFromSoundFile: ...
MorseFromSoundFile: ....
MorseFromSoundFile: .-
MorseFromSoundFile: .-.
MorseFromSoundFile: .--.
MorseFromSoundFile:
MorseFromSoundFile: ..
MorseFromSoundFile: ...
MorseFromSoundFile:
MorseFromSoundFile: -
MorseFromSoundFile: ....
MorseFromSoundFile: .
MorseFromSoundFile:
MorseFromSoundFile: -...
MorseFromSoundFile: .
MorseFromSoundFile: ...
MorseFromSoundFile: -
MorseFromSoundFile:
MorseFromSoundFile: .-..
MorseFromSoundFile: .-
MorseFromSoundFile: -.
MorseFromSoundFile: --.
MorseFromSoundFile: ..-
MorseFromSoundFile: .-
MorseFromSoundFile: --.
MorseFromSoundFile: .
MorseFromSoundFile:
MorseFromSoundFile: .
MorseFromSoundFile: ...-
MorseFromSoundFile: .
MorseFromSoundFile: .-.
TextFromConversion: csharp is the best language ever
TextFromSoundFile: csharp is the best language ever
*/
The reading/decoding algorithm is based upon the assumption that the moments of silence are 0 when normalized as a float sample and that the waveformat is the same as this: 32 bit IEEFloat: 8000Hz 1 channel. Other waveformats won't work. If your custom wavfiles match this pattern, but your beeps/silences are longer/shorter than my prefabs (see MorseCodeAudio dir) then try changing the static value sample_difference_threashold_factor around.
Known issues
- Some documentations are not correct/missing.
- Inconsistent use of alphabet lookup/not clear what methods only use the internal MorseCharCollection and what methods also use the alphabet.
- Unit tests done for:
- MorseCodeTranslator
- MorseChar
- MorseCharCollection
- MorseAudioReader
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- NAudio (>= 2.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fixed some bugs regarding the audio decoding, now correctly decoding