tonegenerator
Advanced tools
Comparing version 0.1.0 to 0.2.0
/* | ||
* ToneGenerator for node.js | ||
* generates raw PCM data for a tone, | ||
* specify length and frequency | ||
* specify frequency, length, volume and sampling rate | ||
*/ | ||
@@ -16,9 +16,10 @@ function generateCycle(cycle, volume) { | ||
module.exports = function(freq, lengthInSecs, volume) { | ||
module.exports = function(freq, lengthInSecs, volume, rate) { | ||
freq = freq || 440; | ||
rate = rate || 44100 | ||
lengthInSecs = lengthInSecs || 2.0; | ||
volume = volume || 30 | ||
var cycle = Math.floor(44100/freq); | ||
var samplesLeft = lengthInSecs * 44100; | ||
var cycle = Math.floor(rate/freq); | ||
var samplesLeft = lengthInSecs * rate; | ||
var cycles = samplesLeft/cycle; | ||
@@ -25,0 +26,0 @@ var ret = []; |
{ | ||
"name": "tonegenerator", | ||
"description": "Generates a tone as raw PCM WAV data, so you can do operations on it", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "test": "node test.js", |
@@ -8,8 +8,13 @@ ToneGenerator for node.js | ||
```javascript | ||
tone(frequency, lengthInSeconds, [volumen]) | ||
tone(frequency, lengthInSeconds, volume = 30, sampleRate, = 44100) | ||
``` | ||
**volume** and **sampleRate** are optional, the default is shown above. | ||
**If you want to specify sampleRate, you have to specify volume!** | ||
```javascript | ||
var tone = require("tonegenerator"); | ||
var A440 = tone(440, 20, 30); // get PCM data for a 440hz A, 20 seconds, volume 30 | ||
var A440_low_sample = tone(440, 20, 30, 22050); // this array has lower sample rate and will only be half as long | ||
``` | ||
@@ -25,3 +30,5 @@ | ||
var tone = require("tonegenerator"); | ||
var header = require("waveheader"); // https://www.npmjs.org/package/waveheader | ||
// Use this package to write a header for the wave file | ||
// https://www.npmjs.org/package/waveheader | ||
var header = require("waveheader"); | ||
var fs = require("fs"); | ||
@@ -28,0 +35,0 @@ |
@@ -9,2 +9,3 @@ var assert = require('assert') | ||
var tone2 = tonegenerator(440, 2, 30) | ||
var tonefrequency = tonegenerator(440, 2, 10, 22050) | ||
@@ -15,5 +16,6 @@ assert(Array.isArray(tone1), 'Data is an array') | ||
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') | ||
assert.equal(tone1.length/2, tonefrequency.length, 'when halving audio sampling rate, the array length should be half of default') | ||
console.log('...done') |
4246
40
59