Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ Install the package via `npm` or `yarn`:
$ npm install cw
```

You can then use:
#### ES Modules

```node
```javascript
import cw from "cw";
console.log(cw.codes["C"]); // "-.-."
cw.play("hello world");
```

#### CommonJS

```javascript
const cw = require("cw");
console.log(cw.codes["C"]); // "-.-."
```
Expand Down Expand Up @@ -74,8 +82,10 @@ cw.play("abcd", {
In cases of multiple plays, it is recommended to create a global `audioContext` object:

```js
// Initialize audio context once
let actx = cw.initAudioContext({ tone: 600 }); // tone is passed here

// Reuse the audio context for multiple plays
cw.play("abcd", { actx, wpm: 10 });
cw.play("efgh", { actx, wpm: 20 });
cw.play("ijkl", { actx, wpm: 30 });
Expand Down
6 changes: 3 additions & 3 deletions examples/jsdelivr-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ <h2>Example 3: Play Morse Code</h2>

<h2>How to Include cw.js in Your Project</h2>
<h3>Via jsDelivr CDN:</h3>
<pre>&lt;script src="https://cdn.jsdelivr.net/npm/cw@0.3.0/dist/cw.min.js"&gt;&lt;/script&gt;</pre>
<pre>&lt;script src="https://cdn.jsdelivr.net/npm/cw@0.4.0/dist/cw.min.js"&gt;&lt;/script&gt;</pre>

<h3>Via npm:</h3>
<pre>npm install cw</pre>

<h3>ES Module Import:</h3>
<pre>import { tdit, tfdit, codes, initAudioContext, play } from 'cw';</pre>
<pre>import cw from 'cw';</pre>

<!-- Include the library from jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/cw@0.3.0/dist/cw.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cw@0.4.0/dist/cw.min.js"></script>

<script>
// Initialize audio context
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cw",
"version": "0.3.0",
"version": "0.4.0",
"description": "A comprehensive Morse Code (CW) library",
"main": "dist/cw.cjs.js",
"module": "dist/cw.esm.js",
Expand Down
5 changes: 3 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ export default [
file: pkg.browser,
format: "umd",
sourcemap: true,
exports: "default",
},
plugins: [resolve(), commonjs(), typescript({ tsconfig: "./tsconfig.json" }), terser()],
},
{
input: "src/index.ts",
external: ["cw"],
output: [
{ file: pkg.main, format: "cjs", sourcemap: true },
{ file: pkg.module, format: "es", sourcemap: true },
{ file: pkg.main, format: "cjs", sourcemap: true, exports: "default" },
{ file: pkg.module, format: "es", sourcemap: true, exports: "default" },
],
plugins: [typescript({ tsconfig: "./tsconfig.json" })],
},
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ import { tdit, tfdit } from "./timings.js";
import { codes } from "./codes.js";
import { initAudioContext, play } from "./audio.js";

export { tdit, tfdit, codes, initAudioContext, play };
const cw = {
tdit,
tfdit,
codes,
initAudioContext,
play
};

export default cw;
21 changes: 21 additions & 0 deletions src/timings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/**
* Morse Code Timing Utilities
*
* This module provides functions for calculating Morse code timing parameters.
*
* Farnsworth Timing:
* Farnsworth timing is a method of sending Morse code where the individual characters
* are sent at a higher speed (wpm), but the spacing between characters and words is
* increased to achieve a lower effective speed (fwpm). This technique was developed by
* Russ Farnsworth (W6TTB) to help students learn Morse code more effectively.
*
* The idea behind Farnsworth timing is that learners can become accustomed to hearing
* and recognizing characters at higher speeds, while the extended spacing gives them
* more time to process each character before the next one arrives. As proficiency
* increases, the spacing can be gradually reduced until standard timing is achieved.
*
* In standard Morse timing, characters and spaces are proportional to the dit length.
* In Farnsworth timing, the characters maintain the same internal timing as they would
* at the higher speed, but the spaces between characters and words are extended.
*/

/**
* Calculate the dit length in seconds based on words per minute
* @param wpm Words per minute
Expand Down
24 changes: 12 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { expect } from 'chai';
import { tdit, tfdit, codes } from '../dist/cw.esm.js';
import cw from '../dist/cw.esm.js';

describe("cw", function () {
describe("#tdit()", function () {
it("should calculate the correct dit length in ms", function () {
expect(tdit(20)).to.equal(0.06);
expect(tdit(50)).to.equal(0.024);
expect(cw.tdit(20)).to.equal(0.06);
expect(cw.tdit(50)).to.equal(0.024);
});
});

describe("#tfdit()", function () {
it("should calculate the correct farnsworth dit length in ms", function () {
expect(tfdit(20, 10)).to.equal(0.21789473684210525);
expect(tfdit(20, 20)).to.equal(0.06);
expect(tfdit(20)).to.equal(0.06);
expect(cw.tfdit(20, 10)).to.equal(0.21789473684210525);
expect(cw.tfdit(20, 20)).to.equal(0.06);
expect(cw.tfdit(20)).to.equal(0.06);
});
});

describe("#codes", function () {
it("should provide an object mapping characters to morse code", function () {
expect(codes["C"]).to.equal("-.-.");
expect(codes["8"]).to.equal("---..");
expect(codes["."]).to.equal(".-.-.-");
expect(codes["w"]).to.be.undefined;
expect(cw.codes["C"]).to.equal("-.-.");
expect(cw.codes["8"]).to.equal("---..");
expect(cw.codes["."]).to.equal(".-.-.-");
expect(cw.codes["w"]).to.be.undefined;
});
});
});