Skip to content

Commit

Permalink
Merge pull request #1 from noblesamurai/init
Browse files Browse the repository at this point in the history
Initial implementation.
  • Loading branch information
Tim Allen authored Jul 28, 2019
2 parents 91c75b2 + 50674bc commit a21557f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
37 changes: 35 additions & 2 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rms-amplitude",
"name": "@noblesam/rms-amplitude",
"description": "Get RMS amplitude of an audio file.",
"version": "0.0.1",
"version": "1.0.0-init.3",
"author": "Tim Allen <tim@noblesamurai.com>",
"license": "BSD",
"main": "src/index.js",
Expand All @@ -27,7 +27,9 @@
"node": "10.x",
"npm": "6.x"
},
"dependencies": {},
"dependencies": {
"tempy": "^0.3.0"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
Expand Down
19 changes: 19 additions & 0 deletions src/index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const tempy = require('tempy');

/**
* @param {string} filename
* @param {object} opts
* @param {string} opts.ffmpeg ffmpeg full path
* @param {string} opts.sox sox full path
*/
module.exports = async function sox (filename, opts = {}) {
const { ffmpeg = 'ffmpeg', sox = 'sox' } = opts;
const tempfile = tempy.file({ extension: 'wav' });
await exec(`${ffmpeg} -i ${filename} ${tempfile}`);
const { stderr } = await exec(`${sox} ${tempfile} -n stat`);
const rms = stderr.match(/RMS[ ]+amplitude:[ ]+([\d.]+)/);
const max = stderr.match(/Maximum[ ]+amplitude:[ ]+([\d.]+)/);
return rms && max && { rms: parseFloat(rms[1]), max: parseFloat(max[1]) };
};
Binary file added test/fixtures/Allure10.mp3
Binary file not shown.
Binary file added test/fixtures/user_video-30.mp4
Binary file not shown.
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const expect = require('chai').expect;
const path = require('path');
const rmsAmplitude = require('..');

describe('rms-amplitude', function () {
it('gives the RMS amplitude of an audio file', async function () {
const result = await rmsAmplitude(path.join(__dirname, 'fixtures/Allure10.mp3'));
expect(result.max).to.be.a('number');
expect(result.rms).to.be.a('number');
expect(result.rms).to.be.lessThan(result.max);
});
it('handles a .mp4 file', async function () {
const result = await rmsAmplitude(path.join(__dirname, 'fixtures/user_video-30.mp4'));
expect(result.max).to.be.a('number');
expect(result.rms).to.be.a('number');
});
});

0 comments on commit a21557f

Please sign in to comment.