Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bahamas10 committed Aug 19, 2012
0 parents commit 32ae554
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm-debug.log
node_modules
tests/config.json
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
BPM
===

Calculate BPM by tapping

Install
------

Install the command line tool `bpm`

npm install -g bpm

Install locally to use as a module

npm install bpm

Usage
-----

Command line

bpm

As a module

``` js
var BPM = require('bpm');
var b = new BPM();
```

Example
-------

### Command line tool

Run `bpm` on the command line to tap out the beats, and let it calculate the
average BPM

$ bpm
Tap when ready... ctrl-c to break
{ count: 1 }
{ avg: 69.52491309385863, ms: 863, count: 2 }
{ avg: 67.6056338028169, ms: 912, count: 3 }
{ avg: 66.98920729438035, ms: 912, count: 4 }
{ avg: 66.6851903306474, ms: 912, count: 5 }
{ avg: 66.386368665634, ms: 920, count: 6 }
{ avg: 66.4819944598338, ms: 896, count: 7 }

In the output above you can see that it sits there waiting for you to tap. When you
start tapping, it starts calculating the average BPM, and every tap it prints 3 fields.

* `avg` - The average BPM
* `ms` - The time in milliseconds from the previous to the current tap
* `count` - How many times you have tapped

### Node Module

``` js
var BPM = require('bpm'),
b = new BPM();

b.tap();
setTimeout(function() {
console.log(b.tap());
}, 1000);
```
yields
``` json
{
"avg": 59.46481665014866,
"ms": 1009,
"count": 2
}
```

Functions
---------

### var b = new BPM()

Create a new BPM object

### b.tap()

Trigger the tap event, it returns an object showing the current BPM statistics

### b.reset()

Reset the obj

Test
----

npm test

License
-------

MIT Licensed
57 changes: 57 additions & 0 deletions bin/bpm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env node
/**
* Calculate BPM on the command line
*
* Author: Dave Eddy <dave@daveeddy.com>
* License: MIT
*/

// Requires and such
var BPM = require('../'),
b = new BPM(),
path = require('path'),
util = require('util'),
keypress = require('keypress'),
version = require('../package.json').version,
args = process.argv.slice(2);

/**
* Usage
*
* return the usage message
*/
function usage() {
return util.format([
'Usage: %s',
'',
'Tap on the command line to calculate BPM',
'',
'Options',
' --help | -h: Print this help message and exit',
' --version | -v: Print the version number and exit',
''
].join('\n'), path.basename(process.argv[1]));
}

// Command line arguments
switch (args[0]) {
case '-h': case '--help':
console.log(usage());
process.exit(0);
break;
case '-v': case '--version':
console.log(version);
process.exit(0);
break;
}

keypress(process.stdin);

console.log('Tap when ready... ctrl-c to break');
process.stdin.on('keypress', function(c, key) {
if (key && key.ctrl && key.name == 'c') process.exit(0);
console.log(b.tap());
});

process.stdin.setRawMode(true);
process.stdin.resume();
42 changes: 42 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Calculate BPM
*/

module.exports = BPM;
module.exports.BPM = BPM;

function BPM() {
this.count = 0;
this.ts = 0;
this.old_ts = 0;
}

BPM.prototype.tap = function() {
this.ts = new Date().getTime();
if (!this.first_ts) this.first_ts = this.ts;

var ret = {};

// ignore the first tap
if (this.old_ts) {
var ms = this.ts - this.old_ts;

var avg = 60000 * this.count / (this.ts - this.first_ts);

ret.avg = avg;
ret.ms = ms;
}

ret.count = ++this.count;

// Store the old timestamp
this.old_ts = this.ts;
return ret;
};

BPM.prototype.reset = function() {
this.count = 0;
this.ts = 0;
this.old_ts = 0;
this.first_ts = 0;
};
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "bpm",
"description": "Calculate BPM by tapping",
"version": "0.0.0",
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)",
"contributors": [],
"repository": {
"type": "git",
"url": "git://github.com/bahamas10/node-bpm.git"
},
"scripts": {
"test": "for f in tests/*.js; do echo \"$f\"; node \"$f\" || exit 1; done; echo 'Passed!'; exit 0"
},
"dependencies": {
"keypress": "~0.1.0"
},
"bin": {
"bpm": "./bin/bpm.js"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
},
"keywords": [
"bpm",
"beat",
"tap"
]
}
8 changes: 8 additions & 0 deletions tests/120.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var BPM = require('../'),
b = new BPM();

setInterval(function() {
var s = b.tap();
console.log(s);
if (s.count > 10) process.exit(0);
}, 500);
8 changes: 8 additions & 0 deletions tests/240.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var BPM = require('../'),
b = new BPM();

setInterval(function() {
var s = b.tap();
console.log(s);
if (s.count > 20) process.exit(0);
}, 250);
8 changes: 8 additions & 0 deletions tests/60.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var BPM = require('../'),
b = new BPM();

setInterval(function() {
var s = b.tap();
console.log(s);
if (s.count > 10) process.exit(0);
}, 1000);

0 comments on commit 32ae554

Please sign in to comment.