Skip to content

Commit

Permalink
Initial commit, v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Jun 13, 2017
0 parents commit 2065bad
Show file tree
Hide file tree
Showing 10 changed files with 552 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
extends: 'standard',

env: {
mocha: true
},

// Almost Standard
rules: {
// Disable: Closing curly brace does not appear on the same line as the subsequent block
'brace-style': [2, 'stroustrup'],

// Disable: More than 1 blank line not allowed
'no-multiple-empty-lines': 'off',

// Disable: Object properties must go on a new line if they aren't all on the same line
'object-property-newline': 'off',

// Disable: Block must not be padded by blank lines
'padded-blocks': 'off'
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage/
node_modules/
npm-debug.log
package-lock.json
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage/
npm-debug.log
package-lock.json
.idea
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

language: node_js

node_js:
- node
- 6
- 4

after_script:
- npm run test-cov
- cat ./coverage/lcov.info | coveralls

sudo: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Burak Özdemir <mail@burakozdemir.co.uk>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# morsify

[![npm-version]][npm] [![travis-ci]][travis] [![coveralls-status]][coveralls]

Morse code encoder and decoder with no dependencies supports Latin, Cyrilic, Greek, Hebrew,
Arabic, Persian, Japanese, and Korean characters with audio generation functionality.

```js
var morsify = require('morsify');

morsify.encode('SOS'); // .../---/...
morsify.decode('.../---/...'); // S O S
morsify.audio('SOS') // will return base64 encoded audio/wav data
```

You can customize the dash, dot or space characters and specify the alphabet with the priority option for
an accurate encoding and decoding.

What priority option does is, gives direction to the plugin to start to searching for the given character set first.

Set the priority option according to the list below.

- 1 => ASCII (Default)
- 2 => Numbers
- 3 => Punctuation
- 4 => Latin Extended (Turkish, Polski etc.)
- 5 => Cyrilic
- 6 => Greek
- 7 => Hebrew
- 8 => Arabic
- 9 => Persian
- 10 => Japanese
- 11 => Korean

```js
morsify.encode('Ленинград', { priority: 5 }) // .-.././-./../-./--./.-./.-/-..
morsify.decode('.../.-/--./.-/.--./.--', { priority: 6 }) // Σ Α Γ Α Π Ω
morsify.decode('––– –... ––– –. ––. .. .–.. –––', { dash: '', dot: '.', space: ' ', priority: 7 }) // ה ב ה נ ג י ל ה
morsify.audio('البُراق‎‎', { // generates the morse .-/.-../-.../.-./.-/--.- then generates the audio from it
channels: 1,
sampleRate: 1012,
bitDepth: 16,
unit: 0.1,
frequency: 440.0,
volume: 32767,
priority: 8
})
```

[npm-version]: https://img.shields.io/npm/v/morsify.svg?style=flat-square (NPM Package Version)
[travis-ci]: https://img.shields.io/travis/simov/morsify/master.svg?style=flat-square (Build Status - Travis CI)
[coveralls-status]: https://img.shields.io/coveralls/simov/morsify.svg?style=flat-square (Test Coverage - Coveralls)

[npm]: https://www.npmjs.com/package/morsify
[travis]: https://travis-ci.org/simov/morsify
[coveralls]: https://coveralls.io/r/simov/morsify?branch=master
218 changes: 218 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
;(function (name, root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root[name] = factory();
}
} ('morsify', this, function () {

var characters = {
'1': { // Latin => https://en.wikipedia.org/wiki/Morse_code
'A': '01', 'B': '1000', 'C': '1010', 'D': '100', 'E': '0', 'F': '0010',
'G': '110', 'H': '0000', 'I': '00', 'J': '0111', 'K': '101', 'L': '0100',
'M': '11', 'N': '10', 'O': '111', 'P': '0110', 'Q': '1101', 'R': '010',
'S': '000', 'T': '1', 'U': '001', 'V': '0001', 'W': '011', 'X': '1001',
'Y': '1011', 'Z': '1100'
},
'2': { // Numbers
'0': '11111', '1': '01111', '2': '00111', '3': '00011', '4': '00001',
'5': '00000', '6': '10000', '7': '11000', '8': '11100', '9': '11110'
},
'3': { // Punctuation
'.': '010101', ',': '110011', '?': '001100', '\'': '011110', '!': '101011', '/': '10010',
'(': '10110', ')': '101101', '&': '01000', ':': '111000', ';': '101010', '=': '10001',
'+': '01010', '-': '100001', '_': '001101', '"': '010010', '$': '0001001', '@': '011010',
'¿': '00101', '¡': '110001'
},
'4': { // Latin Extended => https://ham.stackexchange.com/questions/1379/international-characters-in-morse-code
'Ã': '01101', 'Á': '01101', 'Å': '01101', 'À': '01101', 'Â': '01101', 'Ä': '0101',
'Ą': '0101', 'Æ': '0101', 'Ç': '10100', 'Ć': '10100', 'Ĉ': '10100', 'Č': '110',
'Đ': '00100', 'Ð': '00110', 'È': '01001', 'Ë': '00100', 'Ę': '00100', 'É': '00100',
'Ê': '10010', 'Ğ': '11010', 'Ĝ': '11010', 'Ĥ': '1111', 'İ': '01001', 'Ï': '10011',
'Ì': '01110', 'Ĵ': '01110', 'Ł': '01001', 'Ń': '11011', 'Ñ': '11011', 'Ó': '1110',
'Ò': '1110', 'Ö': '1110', 'Ô': '1110', 'Ø': '1110', 'Ś': '0001000', 'Ş': '01100',
'Ș': '1111', 'Š': '1111', 'Ŝ': '00010', 'ß': '000000', 'Þ': '01100', 'Ü': '0011',
'Ù': '0011', 'Ŭ': '0011', 'Ž': '11001', 'Ź': '110010', 'Ż': '11001'
},
'5': { // Cyrilic Alphabet => https://en.wikipedia.org/wiki/Russian_Morse_code
'А': '01', 'Б': '1000', 'В': '011', 'Г': '110', 'Д': '100', 'Е': '0',
'Ж': '0001', 'З': '1100', 'И': '00', 'Й': '0111', 'К': '101','Л': '0100',
'М': '11', 'Н': '10', 'О': '111', 'П': '0110', 'Р': '010', 'С': '000',
'Т': '1', 'У': '001', 'Ф': '0010', 'Х': '0000', 'Ц': '1010', 'Ч': '1110',
'Ш': '1111', 'Щ': '1101', 'Ъ': '11011', 'Ы': '1011', 'Ь': '1001', 'Э': '00100',
'Ю': '0011', 'Я': '0101'
},
'6': { // Greek Alphabet => https://en.wikipedia.org/wiki/Morse_code_for_non-Latin_alphabets
'Α': '01', 'Β':'1000', 'Γ':'110', 'Δ':'100', 'Ε':'0', 'Ζ':'1100',
'Η':'0000', 'Θ':'1010', 'Ι': '00', 'Κ': '101', 'Λ': '0100', 'Μ': '11',
'Ν': '10', 'Ξ': '1001', 'Ο': '111', 'Π': '0110', 'Ρ': '010', 'Σ':'000',
'Τ':'1', 'Υ': '1011', 'Φ':'0010', 'Χ': '1111', 'Ψ': '1101', 'Ω':'011'
},
'7': { // Hebrew Alphabet => https://en.wikipedia.org/wiki/Morse_code_for_non-Latin_alphabets
'א': '01', 'ב': '1000', 'ג': '110', 'ד': '100', 'ה': '111', 'ו': '0',
'ז': '1100', 'ח': '0000', 'ט': '001', 'י': '00', 'כ': '101', 'ל': '0100',
'מ': '11', 'נ': '10', 'ס': '1010', 'ע': '0111', 'פ': '0110', 'צ': '011',
'ק': '1101', 'ר': '010', 'ש': '000', 'ת': '1'
},
'8': { // Arabic Alphabet => https://en.wikipedia.org/wiki/Morse_code_for_non-Latin_alphabets
'ا': '01', 'ب': '1000', 'ت': '1', 'ث': '1010', 'ج': '0111', 'ح': '0000',
'خ': '111', 'د': '100', 'ذ': '1100', 'ر': '010', 'ز': '1110', 'س': '000',
'ش': '1111', 'ص': '1001', 'ض': '0001', 'ط': '001', 'ظ': '1011', 'ع': '0101',
'غ': '110', 'ف': '0010', 'ق': '1101', 'ك': '101', 'ل': '0100', 'م': '11',
'ن': '10', 'ه': '00100', 'و': '011', 'ي': '00', 'ﺀ': '0'
},
'9': { // Persian Alphabet => https://en.wikipedia.org/wiki/Morse_code_for_non-Latin_alphabets
'ا': '01', 'ب': '1000', 'پ': '0110', 'ت': '1', 'ث': '1010', 'ج': '0111',
'چ': '1110', 'ح': '0000', 'خ' : '1001', 'د': '100', 'ذ': '0001', 'ر': '010',
'ز': '1100', 'ژ': '110', 'س': '000', 'ش': '1111', 'ص': '0101', 'ض': '00100',
'ط': '001', 'ظ': '1011', 'ع': '111', 'غ': '0011', 'ف': '0010', 'ق': '111000',
'ک': '101', 'گ': '1101', 'ل': '0100', 'م': '11', 'ن': '10', 'و': '011',
'ه': '0', 'ی': '00'
},
'10': { // Japanese Alphabet => https://en.wikipedia.org/wiki/Wabun_code
'ア': '11011', 'カ': '0100', 'サ': '10101', 'タ': '10', 'ナ': '010', 'ハ': '1000',
'マ': '1001', 'ヤ': '011', 'ラ': '000', 'ワ': '101', 'イ': '01', 'キ': '10100',
'シ': '11010', 'チ': '0010', 'ニ': '1010', 'ヒ': '11001', 'ミ': '00101', 'リ': '110',
'ヰ': '01001', 'ウ': '001', 'ク': '0001', 'ス': '11101', 'ツ': '0110', 'ヌ': '0000',
'フ': '1100', 'ム': '1', 'ユ': '10011', 'ル': '10110', 'ン': '01010', 'エ': '01000',
'ケ': '1011', 'セ': '01110', 'テ': '01011', 'ネ': '1101', 'ヘ': '0', 'メ': '10001',
'レ': '111', 'ヱ': '01100', '、': '010101', 'オ': '10111', 'コ': '1111', 'ソ':'1110',
'ト': '00100', 'ノ': '0011', 'ホ': '100', 'モ': '10010', 'ヨ': '11', 'ロ': '0101',
'ヲ': '0111', '。': '010100'
},
'11': { // Korean Alphabet => https://en.wikipedia.org/wiki/Wabun_code
'ㄱ': '0100', 'ㄴ': '0010', 'ㄷ': '1000', 'ㄹ': '0001', 'ㅁ': '11', 'ㅂ': '011',
'ㅅ': '110', 'ㅇ': '101', 'ㅈ': '0110', 'ㅊ': '1010', 'ㅋ': '1001', 'ㅌ': '1100',
'ㅍ': '111', 'ㅎ': '0111', 'ㅏ': '0', 'ㅑ': '00', 'ㅓ': '1', 'ㅕ': '000',
'ㅗ': '01', 'ㅛ': '10', 'ㅜ': '0000', 'ㅠ': '010', 'ㅡ': '100', 'ㅣ': '001'
}
};

var swapCharacters = function (options) {
var swapped = {};
for (var set in characters) {
for (var key in characters[set]) {
var mapped = characters[set][key].replace(/0/g, options.dot).replace(/1/g, options.dash);
if (typeof swapped[mapped] === 'undefined') {
swapped[mapped] = key;
}
}
}
return swapped;
};

var getOptions = function (options) {
options = options || {};
options = {
dash: options.dash || '-',
dot: options.dot || '.',
space: options.space || '/',
invalid: options.invalid || '#',
priority: options.priority || 1,
channels: options.channels || 1,
sampleRate: options.sampleRate || 1012,
bitDepth: options.bitDepth || 16,
unit: options.unit || 0.1,
frequency: options.frequency || 440.0,
volume: options.volume || 32767
};
characters[0] = characters[options.priority];
return options;
};

var encode = function (text, opts) {
var options = getOptions(opts);
return text.replace(/\s+/g, '').toLocaleUpperCase().split('').map(function(character) {
for (var set in characters) {
if (typeof characters[set][character] !== 'undefined') {
return characters[set][character];
}
}
return options.invalid;
}).join(options.space).replace(/0/g, options.dot).replace(/1/g, options.dash);
};

var decode = function (morse, opts) {
var options = getOptions(opts), swapped = swapCharacters(options);
return morse.split(options.space).map(function(characters) {
return swapped[characters] || options.invalid;
}).join(' ').replace(/\s+/g, ' ');
};

// Source: https://github.com/mattt/Morse.js
var audio = function (text, opts) {
var options = getOptions(opts), morse = encode(text, opts), data = [], samples = 0,
pack = function (e) {
for (var b = '', c = 1, d = 0; d < e.length; d++) {
var f = e.charAt(d), a = arguments[c++];
b += f === 'v' ? String.fromCharCode(a & 255, a >> 8 & 255) : String.fromCharCode(a & 255, a >> 8 & 255, a >> 16 & 255, a >> 24 & 255);
}
return b;
}, tone = function (length) {
for (var i = 0; i < options.sampleRate * options.unit * length; i++) {
for (var c = 0; c < options.channels; c++) {
var v = options.volume * Math.sin((2 * Math.PI) * (i / options.sampleRate) * options.frequency);
data.push(pack('v', v)); samples++;
}
}
}, silence = function (length) {
for (var i = 0; i < options.sampleRate * options.unit * length; i++) {
for (var c = 0; c < options.channels; c++) {
data.push(pack('v', 0)); samples++;
}
}
};

for (var i = 0; i <= morse.length; i++) {
if (morse[i] === options.space) {
silence(7);
} else if (morse[i] === options.dot) {
tone(1);
silence(1);
} else if (morse[i] === options.dash) {
tone(3);
silence(1);
} else {
silence(3);
}
}

var chunk1 = [
'fmt ',
pack('V', 16),
pack('v', 1),
pack('v', options.channels),
pack('V', options.sampleRate),
pack('V', options.sampleRate * options.channels * options.bitDepth / 8),
pack('v', options.channels * options.bitDepth / 8),
pack('v', options.bitDepth)
].join(''),
chunk2 = [
'data',
pack('V', samples * options.channels * options.bitDepth / 8),
data.join('')
].join(''),
header = [
'RIFF',
pack('V', 4 + (8 + chunk1.length) + (8 + chunk2.length)),
'WAVE'
].join('');

if (typeof btoa === 'undefined') {
global.btoa = function (str) {
return new Buffer(str).toString('base64');
};
}

return 'data:audio/wav;base64,' + encodeURI(btoa([header, chunk1, chunk2].join('')));
};

return {
decode: decode,
encode: encode,
audio: audio
};

}));
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "morsify",
"version": "0.1.0",
"description": "Encodes and decodes morse code, creates morse code audio from text.",
"keywords": [
"morse",
"morse code",
"morse alphabet",
"morse audio",
"morsify"
],
"license": "MIT",
"homepage": "https://github.com/ozdemirburak/morsify",
"author": "Burak Özdemir <mail@burakozdemir.co.uk> (https://burakozdemir.co.uk)",
"repository": {
"type": "git",
"url": "https://github.com/ozdemirburak/morsify.git"
},
"devDependencies": {
"coveralls": "^2.13.0",
"jshint": "^2.9.0",
"istanbul": "^0.4.5",
"mocha": "^3.4.0"
},
"main": "./index.js",
"files": [
"LICENSE",
"README.md",
"index.js"
],
"scripts": {
"test": "npm run lint && mocha",
"test-cov": "istanbul cover _mocha",
"lint": "jshint index.js"
},
"engines": {
"node": ">=4.0.0"
}
}
Loading

0 comments on commit 2065bad

Please sign in to comment.