Comparing version 0.1.0 to 0.2.0
93
index.js
@@ -39,3 +39,3 @@ ;(function (name, root, factory) { | ||
}, | ||
'5': { // Cyrilic Alphabet => https://en.wikipedia.org/wiki/Russian_Morse_code | ||
'5': { // Cyrillic Alphabet => https://en.wikipedia.org/wiki/Russian_Morse_code | ||
'А': '01', 'Б': '1000', 'В': '011', 'Г': '110', 'Д': '100', 'Е': '0', | ||
@@ -109,2 +109,3 @@ 'Ж': '0001', 'З': '1100', 'И': '00', 'Й': '0111', 'К': '101','Л': '0100', | ||
options = options || {}; | ||
options.oscillator = options.oscillator || {}; | ||
options = { | ||
@@ -116,8 +117,8 @@ dash: options.dash || '-', | ||
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 | ||
unit: options.unit || 0.08, // period of one unit, in seconds, 1.2 / c where c is speed of transmission, in words per minute | ||
oscillator: { | ||
type: options.oscillator.type || 'sine', // sine, square, sawtooth, triangle | ||
frequency: options.oscillator.frequency || 500, // value in hertz | ||
onended: options.oscillator.onended || null, // event that fires when the tone has stopped playing | ||
} | ||
}; | ||
@@ -147,26 +148,21 @@ characters[0] = characters[options.priority]; | ||
// 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++; | ||
} | ||
} | ||
}; | ||
var options = getOptions(opts), morse = encode(text, opts), | ||
AudioContext = window.AudioContext || window.webkitAudioContext, ctx = new AudioContext(), | ||
t = ctx.currentTime, oscillator = ctx.createOscillator(), gainNode = ctx.createGain(); | ||
oscillator.type = options.oscillator.type; | ||
oscillator.frequency.value = options.oscillator.frequency; | ||
oscillator.onended = options.oscillator.onended; | ||
gainNode.gain.setValueAtTime(0, t); | ||
var tone = function (i) { | ||
gainNode.gain.setValueAtTime(1, t); | ||
t += i * options.unit; | ||
}, silence = function (i) { | ||
gainNode.gain.setValueAtTime(0, t); | ||
t += i * options.unit; | ||
}; | ||
for (var i = 0; i <= morse.length; i++) { | ||
@@ -186,30 +182,15 @@ if (morse[i] === options.space) { | ||
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(''); | ||
oscillator.connect(gainNode); | ||
gainNode.connect(ctx.destination); | ||
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 { | ||
play: function () { | ||
oscillator.start(); | ||
oscillator.stop(t); | ||
}, | ||
stop: function () { | ||
oscillator.stop(); | ||
}, | ||
oscillator: oscillator | ||
}; | ||
}; | ||
@@ -216,0 +197,0 @@ |
{ | ||
"name": "morsify", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Encodes and decodes morse code, creates morse code audio from text.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# morsify | ||
[![npm-version]][npm] [![travis-ci]][travis] [![coveralls-status]][coveralls] | ||
[![npm-version]][npm] [![npm-downloads]][npm] [![travis-ci]][travis] | ||
Morse code encoder and decoder with no dependencies supports Latin, Cyrilic, Greek, Hebrew, | ||
Arabic, Persian, Japanese, and Korean characters with audio generation functionality. | ||
Morse code encoder and decoder with no dependencies supports Latin, Cyrillic, Greek, Hebrew, | ||
Arabic, Persian, Japanese, and Korean characters with audio generation functionality using the [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API). | ||
## Install | ||
## Installation | ||
### npm | ||
```bash | ||
@@ -13,2 +16,8 @@ $ npm install morsify --save | ||
### yarn | ||
```bash | ||
$ yarn add morsify | ||
``` | ||
## Usage | ||
@@ -19,7 +28,24 @@ | ||
morsify.encode('SOS'); // .../---/... | ||
morsify.decode('.../---/...'); // S O S | ||
morsify.audio('SOS') // will return base64 encoded audio/wav data | ||
var encoded = morsify.encode('SOS'); // .../---/... | ||
var decoded = morsify.decode('.../---/...'); // S O S | ||
var audio = morsify.audio('SOS'), oscillator = audio.oscillator; // OscillatorNode | ||
audio.play(); // play audio | ||
audio.stop(); // stop audio | ||
``` | ||
Or alternatively, you can also use the library directly with including the source file. | ||
```html | ||
<script src="https://rawgit.com/ozdemirburak/morsify/master/index.js"></script> | ||
<script> | ||
var encoded = morsify.encode('SOS'); // .../---/... | ||
var decoded = morsify.decode('.../---/...'); // S O S | ||
var audio = morsify.audio('SOS'), oscillator = audio.oscillator; // OscillatorNode | ||
audio.play(); // play audio | ||
audio.stop(); // stop audio | ||
</script> | ||
``` | ||
## Options and Localization | ||
You can customize the dash, dot or space characters and specify the alphabet with the priority option for | ||
@@ -35,4 +61,4 @@ an accurate encoding and decoding. | ||
- 3 => Punctuation | ||
- 4 => Latin Extended (Turkish, Polski etc.) | ||
- 5 => Cyrilic | ||
- 4 => Latin Extended (Turkish, Polish etc.) | ||
- 5 => Cyrillic | ||
- 6 => Greek | ||
@@ -46,22 +72,35 @@ - 7 => Hebrew | ||
```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 | ||
}) | ||
var cyrillic = morsify.encode('Ленинград', { priority: 5 }) // .-.././-./../-./--./.-./.-/-.. | ||
var greek = morsify.decode('.../.-/--./.-/.--./.--', { priority: 6 }) // Σ Α Γ Α Π Ω | ||
var hebrew = morsify.decode('––– –... ––– –. ––. .. .–.. –––', { dash: '–', dot: '.', space: ' ', priority: 7 }) // ה ב ה נ ג י ל ה | ||
var arabicAudio = morsify.audio('البُراق', { // generates the morse .-/.-../-.../.-./.-/--.- then generates the audio from it | ||
unit: 0.1, // period of one unit, in seconds, 1.2 / c where c is speed of transmission, in words per minute | ||
oscillator: { | ||
type: 'sine', // sine, square, sawtooth, triangle | ||
frequency: 500, // value in hertz | ||
onended: function () { // event that fires when the tone has stopped playing | ||
console.log('ended'); | ||
}, | ||
} | ||
}), oscillator = arabicAudio.oscillator; // OscillatorNode | ||
arabicAudio.play(); // will start playing morse audio | ||
arabicAudio.stop(); // will stop playing morse audio | ||
``` | ||
[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) | ||
## Contributing and Known Issues | ||
Contributions are welcome. | ||
Currently, as a major drawback, Chinese characters are missing. Someone with the knowledge of | ||
[Chinese telegraph code](https://en.wikipedia.org/wiki/Chinese_telegraph_code) can help to implement it. Also someone who is proficient in Thai can help | ||
to include [Thai alphabet](https://th.wikipedia.org/wiki/รหัสมอร์ส). | ||
## License | ||
The MIT License (MIT). Please see [License File](LICENSE) for more information. | ||
[npm-version]: https://img.shields.io/npm/v/morsify.svg?style=flat-square | ||
[npm-downloads]: https://img.shields.io/npm/dm/morsify.svg?style=flat-square | ||
[travis-ci]: https://img.shields.io/travis/ozdemirburak/morsify/master.svg?style=flat-square | ||
[npm]: https://www.npmjs.com/package/morsify | ||
[travis]: https://travis-ci.org/simov/morsify | ||
[coveralls]: https://coveralls.io/r/simov/morsify?branch=master | ||
[travis]: https://travis-ci.org/ozdemirburak/morsify |
14874
103
185