- Home /
Convert Audio Beeps And Dashes into Text
Ok I thought this would be a cool sort of project using this morse code script I found in the Forum. Looking at it makes me think it should be possible to do the reverse, that is to say take the audio clips of the "Dash" and "Dot" and then convert them back into text (Alphabet) to make a Morse Code receiver.
Not quite sure where to begin on that but it looks possible so if someone could save be a little time and get me pointed in the right direction as to where to begin that would be great. My initial thought is that maybe the "Dash" and "Dot" clips could be identified by "AudioClip length?"
public void PlayMorseCodeMessage(string message)
{
StartCoroutine("_PlayMorseCodeMessage", message);
}
private IEnumerator _PlayMorseCodeMessage(string message)
{
// Remove all characters that are not supported by Morse code...
Regex regex = new Regex("[^A-z0-9 ]");
message = regex.Replace(message.ToUpper(), "");
// Convert the message into Morse code audio...
foreach(char letter in message)
{
if (letter == ' ')
yield return new WaitForSeconds(spaceDelay);
else
{
int index = letter - 'A';
if (index < 0)
index = letter - '0' + 26;
string letterCode = alphabet[index];
foreach(char bit in letterCode)
{
// Dot or Dash?
AudioClip sound = dotSound;
if (bit == '-') sound = dashSound;
// Play the audio clip and wait for it to end before playing the next one.
GetComponent<AudioSource>().PlayOneShot(sound);
yield return new WaitForSeconds(sound.length + letterDelay);
}
}
}
True enough, but that's kind of rough. Why not use unity's event system? iirc you can signal events from sounds...maybe that's only for animation. $$anonymous$$ight be annoying to set up if there's a bunch of different sounds though.
Thanks @theLittleSettler, I thought of something like that. The current code uses just two short "Beep" sounds one a little longer for "Dash" and a short beep for "Dot"
I was thinking something through code to deter$$anonymous$$e the audio clip lengths and their spacing durations for word separation. I'm not great with code but it looks like a lot of that is already there in how it produces the dots and dashes. So I was wondering if it could be done in reverse somehow to turn the beeps back into a character string and print that out to a UI text. I'm just not sure where to begin with that, given enough time I'll figure it out but hoping someone will save me a lot of headache if they already have a pretty good idea ;)