forked from trquoccuong/node-wave
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 21dbddf
Showing
8 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### Example user template template | ||
### Example user template | ||
|
||
# IntelliJ project files | ||
.idea | ||
*.iml | ||
out | ||
gen | ||
node_modules/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/render') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
var AudioContext = require('web-audio-api').AudioContext; | ||
var fs = require('fs'); | ||
var Canvas = require('canvas') | ||
|
||
Array.prototype.max = function () { | ||
return Math.max.apply(null, this); | ||
}; | ||
|
||
var defaultSetting = { | ||
width: 600, | ||
height: 80, | ||
barWidth: 3, | ||
barGap: 0.2, | ||
waveColor: "blue", | ||
waveAlpha: 1, | ||
backgroundColor: '#fff', | ||
baseline: 60, | ||
} | ||
|
||
function bufferMeasure(position, length, data) { | ||
var sum = 0.0 | ||
for (var i = position; i <= (position + length) - 1; i++) { | ||
sum += Math.pow(data[i], 2) | ||
} | ||
return Math.sqrt(sum / data.length) | ||
} | ||
|
||
module.exports = function render(url, options, cb) { | ||
var audioContext = new AudioContext | ||
if (typeof options === "function") { | ||
cb = options | ||
options = {} | ||
} | ||
|
||
options = Object.assign({}, defaultSetting, options) | ||
if (options.baseline < 0 && options.height < 0 && options.width < 0) { | ||
return cb( new Error('Size must be greater than 0')) | ||
} | ||
|
||
if (options.baseline > options.height) { | ||
return cb( new Error('Baseline must be smaller than waveform height')) | ||
} | ||
|
||
var canvas = new Canvas(options.width, options.height) | ||
var canvasContext = canvas.getContext('2d') | ||
|
||
fs.readFile("Canon.mp3", function (err, buffer) { | ||
|
||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
audioContext.decodeAudioData(buffer, function (result) { | ||
var data = result.getChannelData(0) | ||
var step = Math.floor(result.length / options.width) | ||
var ratio = options.baseline / options.height | ||
var vals = [] | ||
|
||
canvasContext.fillStyle = options.backgroundColor | ||
canvasContext.fillRect(0, 0, options.width, options.height) | ||
|
||
canvasContext.fillStyle = options.waveColor | ||
|
||
for (var i = 0; i < options.width; i += options.barWidth) { | ||
vals.push(bufferMeasure(i * step, step, data) * 10000); | ||
} | ||
|
||
for (var j = 0; j < options.width; j += options.barWidth) { | ||
var scale = options.height / vals.max(); | ||
var val = bufferMeasure(j * step, step, data) * 10000; | ||
val *= scale; | ||
val += 1; | ||
var w = options.barWidth; | ||
if (options.barGap !== 0) { | ||
w *= Math.abs(1 - options.barGap); | ||
} | ||
var x = j + (w / 2); | ||
|
||
var lowerHeight = val * ratio; | ||
var upperHeight = val * (1 - ratio); | ||
|
||
if(options.waveAlpha < 1) { | ||
canvasContext.clearRect(x, options.baseline, w, upperHeight); | ||
canvasContext.clearRect(x, options.baseline, w, -lowerHeight); | ||
} | ||
canvasContext.fillRect(x, options.baseline, w, upperHeight); | ||
canvasContext.fillRect(x, options.baseline, w, -lowerHeight); | ||
} | ||
cb(null, canvas.toBuffer()) | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "node-wave", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && node index.js" | ||
}, | ||
"author": "Tran Quoc Cuong <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"canvas": "^1.6.2", | ||
"web-audio-api": "^0.2.2" | ||
}, | ||
"devDependencies": { | ||
"standard": "^8.6.0" | ||
} | ||
} |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var render = require('../lib/render') | ||
var fs = require('fs') | ||
|
||
render('Canon.mp3', function (err, buffer) { | ||
fs.writeFileSync('out.png', buffer); | ||
}) |