Skip to content
Permalink
Browse files

use `CallbackWorker` for all worker processes

  • Loading branch information...
mmckegg committed Dec 30, 2017
1 parent 8dc1599 commit 3a6834a7a916d73002c6ab0b7412fabb312eea69
Showing with 72 additions and 132 deletions.
  1. +3 −21 lib/detect-peaks.js
  2. +9 −12 lib/{worker/peaks.js → get-peaks.js}
  3. +4 −21 lib/grid-slice-peaks.js
  4. +54 −21 lib/params/wave-hook.js
  5. +2 −2 lib/wave-hook.js
  6. +0 −55 lib/worker/wave-path.js
@@ -1,16 +1,6 @@
var worker = new Worker('file://' + __dirname + '/worker/peaks.js')
var callbacks = {}
var nextId = 0

worker.onmessage = function (e) {
if (callbacks[e.data.id]) {
callbacks[e.data.id](e.data.result)
delete callbacks[e.data.id]
}
}
var getPeaks = require('lib/get-peaks')

module.exports = function (data, count, offset, cb) {
var id = nextId++
offset = offset || [0, 1]

var range = (offset[1] - offset[0]) * data.length
@@ -19,7 +9,8 @@ module.exports = function (data, count, offset, cb) {
var start = Math.floor(offset[0] * data.length)
var end = Math.floor(offset[1] * data.length)

callbacks[id] = function (peaks) {
getPeaks({data, windowSize, step, start, end}, (err, peaks) => {
if (err) return cb(err)
peaks = [[start, 1]].concat(peaks)
cb(peaks.filter(function (peak, i) {
var prev = peaks[i - 1]
@@ -31,14 +22,5 @@ module.exports = function (data, count, offset, cb) {
}).map(function (item) {
return Math.min(offset[1], item[0] / data.length)
}))
}

worker.postMessage({
id: id,
data: data,
windowSize: windowSize,
step: step,
start: start,
end: end
})
}
@@ -1,14 +1,14 @@
self.onmessage = function (e) {
var CallbackWorker = require('lib/callback-worker')
module.exports = CallbackWorker((opts, cb) => {
var result = []
var highestValue = 0
var highestValuePos = 0
var lastCross = 0
var id = e.data.id
var data = e.data.data
var windowSize = e.data.windowSize
var start = Math.max(0, e.data.start)
var step = e.data.step || e.data.windowSize
var end = Math.min(e.data.end, data.length)
var data = opts.data
var windowSize = opts.windowSize
var start = Math.max(0, opts.start)
var step = opts.step || opts.windowSize
var end = Math.min(opts.end, data.length)

for (var i = start; i < end; i += step) {
for (var pos = i; pos < windowSize + i; pos++) {
@@ -27,8 +27,5 @@ self.onmessage = function (e) {
highestValue = 0
}

self.postMessage({
id: id,
result: result
})
}
cb(null, result)
})
@@ -1,16 +1,6 @@
var worker = new Worker('file://' + __dirname + '/worker/peaks.js')
var callbacks = {}
var nextId = 0

worker.onmessage = function (e) {
if (callbacks[e.data.id]) {
callbacks[e.data.id](e.data.result)
delete callbacks[e.data.id]
}
}
var getPeaks = require('lib/get-peaks')

module.exports = function (data, count, offset, cb) {
var id = nextId++
offset = offset || [0, 1]

var range = (offset[1] - offset[0]) * data.length
@@ -19,17 +9,10 @@ module.exports = function (data, count, offset, cb) {
var end = Math.floor(offset[1] * data.length)
var searchStart = Math.floor(start + (windowSize / 2))

callbacks[id] = function (peaks) {
cb([[start, 1]].concat(peaks).map(function (item) {
getPeaks({data, windowSize, start: searchStart, end}, (err, peaks) => {
if (err) return cb(err)
cb(null, [[start, 1]].concat(peaks).map(function (item) {
return item[0] / data.length
}))
}

worker.postMessage({
id: id,
data: data,
windowSize: windowSize,
start: searchStart,
end: end
})
}
@@ -1,19 +1,9 @@
var watchAll = require('mutant/watch-all')
var getValue = require('lib/get-value')
var CallbackWorker = require('lib/callback-worker')

module.exports = WaveHook

// WORKER
var worker = new global.Worker('file://' + __dirname + '/../worker/wave-path.js')
var callbacks = {}
var caches = {}
var nextId = 0
worker.onmessage = function (e) {
if (callbacks[e.data.id]) {
callbacks[e.data.id](e.data.result)
delete callbacks[e.data.id]
}
}

function WaveHook (node, gain, width, height) {
return function (element) {
@@ -31,7 +21,8 @@ function WaveHook (node, gain, width, height) {
var path = cache.get(buffer)

if (!path) {
getPathForData(data, width, height, function (path) {
getPathForData({data, width, height}, function (err, path) {
if (err) throw err
cache.set(buffer, path)
element.setAttribute('d', path)
})
@@ -51,13 +42,55 @@ function getCache (width, height) {
return caches[key]
}

function getPathForData (data, width, height, cb) {
var id = nextId++
callbacks[id] = cb
worker.postMessage({
data: data,
id: id,
width: width,
height: height
var getPathForData = CallbackWorker(function (opts, cb) {
var data = opts.data
var width = opts.width
var height = opts.height

var step = Math.ceil(data.length / width)
var amp = (height / 2)

var maxValues = []
var minValues = []

for (var i = 0; i < width; i++) {
var min = 1.0
var max = -1.0
var defined = false
for (var j = 0; j < step; j++) {
var datum = data[(i * step) + j]
if (datum < min) {
min = datum
defined = true
}
if (datum > max) {
max = datum
defined = true
}
}

if (defined) {
maxValues[i] = max
minValues[i] = min
} else {
maxValues[i] = 0
minValues[i] = 0
}
}

// top
var result = 'M0,' + (height / 2)
maxValues.forEach(function (val, i) {
result += ' L' + i + ',' + Math.round(amp + (val * amp))
})
}

// end point
result += ' L' + width + ',' + (height / 2)

// bottom
minValues.reverse().forEach(function (val, i) {
result += ' L' + (width - i - 1) + ',' + Math.round(amp + (val * amp))
})

cb(null, result + ' Z')
})
@@ -32,11 +32,11 @@ function WaveHook (context, src) {

function waitForLoad (svg, cb) {
if (svg()) {
setImmediate(cb)
process.nextTick(cb)
} else {
var release = svg(function (val) {
if (val) {
setImmediate(release)
process.nextTick(release)
cb()
}
})

This file was deleted.

0 comments on commit 3a6834a

Please sign in to comment.
You can’t perform that action at this time.