Skip to content

Commit

Permalink
fixed AudioContext error on Chrome, removed unicode support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Mar 5, 2020
1 parent 0d25bab commit 18d4265
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 63 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![npm-version]][npm] [![npm-downloads]][npm] [![travis-ci]][travis]

Morse code encoder and decoder with no dependencies supports Latin, Cyrillic, Greek, Hebrew,
Arabic, Persian, Japanese, Korean, Thai, and Unicode (Chinese and the others) characters with audio generation
Arabic, Persian, Japanese, Korean and Thai characters with audio generation
functionality using the [Web Audio API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API).

See Morse Translator in action: [https://morsify.net](https://morsify.net)
Expand Down Expand Up @@ -72,14 +72,12 @@ Set the priority option according to the list below.
- 10 => Japanese
- 11 => Korean
- 12 => Thai
- 13 => Unicode (Chinese and the others)

```js
const cyrillic = morsify.encode('Ленинград', { priority: 5 }); // .-.. . -. .. -. --. .-. .- -..
const greek = morsify.decode('... .- --. .- .--. .--', { priority: 6 }); // ΣΑΓΑΠΩ
const hebrew = morsify.decode('.. ––– . –––', { dash: '', dot: '.', priority: 7 }); // יהוה
const japanese = morsify.encode('NEWS', { priority: 10, dash: '', dot: '', separator: ' ' }); // -・ ・ ・-- ・・・
const chinese = morsify.encode('你好', { priority: 13 }); // -..----.--..... -.--..-.-----.-
const characters = morsify.characters({ dash: '', dot: '' }); // {'1': {'A': '•–', ...}, ..., '11': {'ㄱ': '•–••', ...}}
const 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
Expand Down
35 changes: 9 additions & 26 deletions dist/morsify.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,27 +470,6 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
return swapped;
};

var unicodeToMorse = function unicodeToMorse(character) {
var ch = [];

for (var i = 0; i < character.length; i++) {
ch[i] = ('00' + character.charCodeAt(i).toString(16)).slice(-4);
}

return parseInt(ch.join(''), 16).toString(2);
};

var unicodeToHex = function unicodeToHex(morse, options) {
morse = morse.replace(new RegExp('\\' + options.dot, 'g'), '0').replace(new RegExp('\\' + options.dash, 'g'), '1');
morse = parseInt(morse, 2);

if (isNaN(morse)) {
return options.invalid;
}

return decodeURIComponent(JSON.parse('"' + "\\u" + morse.toString(16) + '"'));
};

var getOptions = function getOptions(options) {
options = options || {};
options.oscillator = options.oscillator || {};
Expand Down Expand Up @@ -526,7 +505,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
}
}

return parseInt(options.priority) === 13 ? unicodeToMorse(character) : options.invalid;
return options.invalid;
}).join(options.separator).replace(/0/g, options.dot).replace(/1/g, options.dash);
};

Expand All @@ -538,15 +517,19 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
return swapped[characters];
}

return parseInt(options.priority) === 13 ? unicodeToHex(characters, options) : options.invalid;
return options.invalid;
}).join('');
};

var isBrowser = typeof window !== 'undefined';
var AudioContext = isBrowser ? window.AudioContext || window.webkitAudioContext : null;
var context = isBrowser ? new AudioContext() : null;
var AudioContext = null;
var context = null;

var audio = function audio(text, opts) {
if (AudioContext === null && typeof window !== 'undefined') {
AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
}

var options = getOptions(opts);
var morse = encode(text, opts);
var oscillator = context.createOscillator();
Expand Down
2 changes: 1 addition & 1 deletion dist/morsify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "morsify",
"version": "2.2.1",
"version": "2.3.0",
"description": "Morse code translator, Morse encoder and decoder which can also generate audio.",
"keywords": [
"morsify",
Expand Down
32 changes: 10 additions & 22 deletions src/morsify.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,6 @@
return swapped;
};

const unicodeToMorse = (character) => {
const ch = [];
for (let i = 0; i < character.length; i++) {
ch[i] = ('00' + character.charCodeAt(i).toString(16)).slice(-4);
}
return parseInt(ch.join(''), 16).toString(2);
};

const unicodeToHex = (morse, options) => {
morse = morse.replace(new RegExp('\\' + options.dot, 'g'), '0').replace(new RegExp('\\' + options.dash, 'g'), '1');
morse = parseInt(morse, 2);
if (isNaN(morse)) {
return options.invalid;
}
return decodeURIComponent(JSON.parse('"'+ '\\u' + morse.toString(16) +'"'));
};

const getOptions = (options) => {
options = options || {};
options.oscillator = options.oscillator || {};
Expand Down Expand Up @@ -180,7 +163,7 @@
return characters[set][character];
}
}
return parseInt(options.priority) === 13 ? unicodeToMorse(character) : options.invalid;
return options.invalid;
}).join(options.separator).replace(/0/g, options.dot).replace(/1/g, options.dash);
};

Expand All @@ -190,15 +173,20 @@
if (typeof swapped[characters] !== 'undefined') {
return swapped[characters];
}
return parseInt(options.priority) === 13 ? unicodeToHex(characters, options) : options.invalid;
return options.invalid;
}).join('');
};

const isBrowser = typeof window !== 'undefined';
const AudioContext = isBrowser ? window.AudioContext || window.webkitAudioContext : null;
const context = isBrowser ? new AudioContext() : null;
let AudioContext = null;
let context = null;

const audio = (text, opts) => {

if (AudioContext === null && typeof window !== 'undefined') {
AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
}

const options = getOptions(opts);
const morse = encode(text, opts);
const oscillator = context.createOscillator();
Expand Down
Loading

0 comments on commit 18d4265

Please sign in to comment.