cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

yOYwRJ

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">
  <div>
    <h3>Message</h3>
    <input id="input" type="text" placeholder="Input Message"/>
    <button id="send">Convert to Morse Code</button>
  </div>
  <hr />
  <div>
    <h3>Key</h3>
    <br />
    <h4><em>only alphanumeric values and the space character are valid inputs</em></h4>
    <br />
    <h4><em>dot is 1 time unit</em></h4>
    <h4><em>dash is 3 time units</em></h4>
    <h4><em>word space is 7 time units</em></h4>
    <br />
    <h4><em>same letter space ('' empty string) is 1 time unit</em></h4>
    <h4><em>same word space (+ plus sign) is 3 time units</em></h4>
  </div>
  <hr />
  <div>
    <h3>Morse Code</h3>
    <p id="output"><em>None</em></p>
  </div>
</div>
            
          
!
            
              body {
  background-color: #ccc;
}

.container {
  margin: 15px;
  padding: 15px;
}

h3, h4 {
  margin: 0;
  text-align: center;
  font-size: 18pt;
}

h4 {
  font-size: 11pt;
}

#output {
  margin-top: 15px;
  font-size: 14pt;
}

input, button {
  text-align: center;
  padding: 10px;
  margin: 20px 0 0;
  box-sizing: border-box;
  position: relative;
  width: 100%;
}

button {
  cursor: pointer;
  margin-top: 7px;
}

hr {
  margin: 30px;
}
            
          
!
            
              'use strict';

//cache
var $input = document.getElementById('input'),
    $output = document.getElementById('output'),
    $btnSend = document.getElementById('send'),
    speedMultiplier = 100,
    dot = '.', dash = '-', spaceLetter = '+', spaceWord = ' ', invalid = '?',
    soundDot = new Howl({urls: ['https://a.clyp.it/kysuohe2.mp3']}), //ideally short
    soundDash = new Howl({urls: ['https://a.clyp.it/kysuohe2.mp3']}), //ideally extended short sample vs the same sample
    
    //via https://en.wikipedia.org/wiki/Morse_code#/media/File:International_Morse_Code.svg
    charCodesAndTimeUnits = {
      '.': 1, //dot
      '-': 3, //dash
      ' ': 7, //word new
      '+': 3, //letter new
      '': 1, //letter same
    },
    morseCode = [
      { "character": " ", "morsecode": " "},
      { "character": "A", "morsecode": ".-"},
      { "character": "B", "morsecode": "-..."},
      { "character": "C", "morsecode": "-.-."},
      { "character": "D", "morsecode": "-.."},
      { "character": "E", "morsecode": "."},
      { "character": "F", "morsecode": "..-."},
      { "character": "G", "morsecode": "--."},
      { "character": "H", "morsecode": "...."},
      { "character": "I", "morsecode": ".."},
      { "character": "J", "morsecode": ".---"},
      { "character": "K", "morsecode": "-.-"},
      { "character": "L", "morsecode": ".-.."},
      { "character": "M", "morsecode": "--"},
      { "character": "N", "morsecode": "-."},
      { "character": "O", "morsecode": "---"},
      { "character": "P", "morsecode": ".--."},
      { "character": "Q", "morsecode": "--.-"},
      { "character": "R", "morsecode": ".-."},
      { "character": "S", "morsecode": "..."},
      { "character": "T", "morsecode": "-"},
      { "character": "U", "morsecode": "..-"},
      { "character": "V", "morsecode": "...-"},
      { "character": "W", "morsecode": ".--"},
      { "character": "X", "morsecode": "-..-"},
      { "character": "Y", "morsecode": "-.--"},
      { "character": "Z", "morsecode": "--.."},
      
      { "character": "1", "morsecode": ".----"},
      { "character": "2", "morsecode": "..---"},
      { "character": "3", "morsecode": "...--"},
      { "character": "4", "morsecode": "....-"},
      { "character": "5", "morsecode": "....."},
      { "character": "6", "morsecode": "-...."},
      { "character": "7", "morsecode": "--..."},
      { "character": "8", "morsecode": "---.."},
      { "character": "9", "morsecode": "----."},
      { "character": "0", "morsecode": "-----"},
  ];

//init
$btnSend.addEventListener('click', onSend);

//methods
function parseMessage() {
  
  //cache
  var inputArr = $input.value.toUpperCase().split(''),
      appender,
      result = "";

  //convert
  for(var character of inputArr) {
    appender = character === spaceWord ? '' : spaceLetter;
    result += parseCharacter(character) + appender;
  }
  
  //clean append  
  if(result.endsWith(spaceLetter)) {
    result = result.slice(0, result.length - 1);
  }
  
  //result
  return result;
}

function parseCharacter(character) {
  var result;
  
  //match
  for(var i = 0, len = morseCode.length; i < len; i++) {
    if(morseCode[i].character === character) {
      result = morseCode[i].morsecode;
      break;
    }
  }
  
  //invalid
  if(!result) {
    result = invalid;
  }
  
  return result;
}

function playMessage(message) {
  
  //pattern display
  $output.innerHTML = message + '<br/>';
 
  //playback
  for(let i = 0, delay = 500, key, len = message.length; i < len; i++) {

    //play tone
    key = message.charAt(i);
    setTimeout(function() { playTone(key); }, delay);
    
    //delay update
    delay += calcDelay(key);
  }
}

function playTone(key)  {
  $output.innerHTML += key;
  if(key === dot) {
    soundDot.play();
  }  else if (key === dash) {
    soundDash.play();
  }
}

function calcDelay(key) {
    return speedMultiplier * charCodesAndTimeUnits[key];
}

//handlers
function onSend() {
  playMessage(parseMessage());
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console