Skip to content

Commit

Permalink
feat: add a volume parameter, setup semantic release
Browse files Browse the repository at this point in the history
  • Loading branch information
karlwestin committed Aug 4, 2016
1 parent 6778b3d commit 3802068
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
"node"
cache:
directories:
- node_modules
after_success:
- npm run semantic-release
branches:
except:
# ignore git tags created by semantic-release, like "v1.2.3"
- /^v\d+\.\d+\.\d+$/
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
ToneGenerator for node.js
====

This thing generates raw PCM data, specified by
This thing generates raw PCM data, specified by
a frequency and length in seconds.

```javascript
tone(frequency, lengthInSeconds, [volumen])
```

```javascript
var tone = require("tonegenerator");
var A440 = tone(440, 20); // get PCM data for a 440hz A, 20 seconds.
var A440 = tone(440, 20, 30); // get PCM data for a 440hz A, 20 seconds, volume 30
```

The data is returned as a normal array, so you can do operations on it.
I'm really unsure what the 'volume' value means, but you can use it to create
different tones with different volumes, let me know how it works for you!

The data is returned as a normal array, so you can do operations on it.
Before writing to a file, you need to convert it to a buffer:

```javascript
Expand All @@ -18,12 +25,12 @@ var header = require("waveheader"); // https://www.npmjs.org/package/waveheader
var fs = require("fs");

// An A-major chord
var tone1 = tone(440, 2);
var tone2 = tone(554.37, 2);
var tone3 = tone(659.26, 2);
var tone1 = tone(440, 2, 60);
var tone2 = tone(554.37, 2, 30);
var tone3 = tone(659.26, 2, 30);

// "playing" one tone at the time
// note that at this time, our sound is just an array
// note that at this time, our sound is just an array
// of gain values. By appending the raw PCM data for one after another,
// we can play them in a sequence
var res = [].concat(tone1);
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ToneGenerator for node.js
* generates raw PCM data for a tone,
* generates raw PCM data for a tone,
* specify length and frequency
*/
function generateCycle(cycle, volume) {
Expand All @@ -13,12 +13,12 @@ function generateCycle(cycle, volume) {
return data;
}

module.exports = function(freq, lengthInSecs) {
module.exports = function(freq, lengthInSecs, volume) {
freq = freq || 440;
lengthInSecs = lengthInSecs || 2.0;
volume = volume || 30

var cycle = Math.floor(44100/freq);
var volume = 30;
var samplesLeft = lengthInSecs * 44100;
var cycles = samplesLeft/cycle;
var ret = [];
Expand Down
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
{
"name": "tonegenerator",
"description": "Generates a tone as raw PCM WAV data, so you can do operations on it",
"version": "0.0.2",
"version": "0.0.0-semantically-released",
"scripts": {
"test": "node test.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"author": {
"name": "Karl Westin",
"email": "[email protected]"
},
"repository" : {
"type" : "git",
"url" : "https://github.com/karlwestin/node-tonegenerator.git"
"repository": {
"type": "git",
"url": "https://github.com/karlwestin/node-tonegenerator.git"
},
"keywords": [
"audio",
"tone",
"frequency",
"wav",
"pcm"
],
"devDependencies": {
"semantic-release": "^4.3.5"
},
"keywords": ["audio", "tone", "frequency", "wav", "pcm"],
"dependencies": {},
"main": "./index.js",
"readmeFilename": "README.md"
}
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var assert = require('assert')
var tonegenerator = require('./')

console.log('Testing Tonegenerator...')


var tone1 = tonegenerator(440, 2, 10)
var tone2 = tonegenerator(440, 2, 30)

assert(Array.isArray(tone1), 'Data is an array')

assert.strictEqual(Math.max.apply(Math, tone1), 10, 'takes the volume argument as max')
assert.strictEqual(Math.min.apply(Math, tone1), -10, 'takes the volume argument as max')

assert.strictEqual(Math.max.apply(Math, tone2), 30, 'takes the volume argument as max')

console.log('...done')

0 comments on commit 3802068

Please sign in to comment.