databender 2.0.0-alpha.1 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,9 @@ var Databender = (function () {
46
46
  this.effectsChain = effectsChain ? asArray(effectsChain) : null;
47
47
  this.sourceParams = sourceParams ? asArray(sourceParams) : null;
48
48
  this.chainMode = chainMode === 'parallel' ? 'parallel' : 'series';
49
+ this.maxConcurrentRenders = 2;
50
+ this.activeRenderCount = 0;
51
+ this.renderQueue = [];
49
52
 
50
53
  this.convert = function(image) {
51
54
  if (image instanceof Image || image instanceof HTMLVideoElement) {
@@ -92,6 +95,27 @@ var Databender = (function () {
92
95
  };
93
96
 
94
97
  this.render = async function(buffer, bypass = false) {
98
+ const acquireRenderSlot = async () => {
99
+ if (this.activeRenderCount < this.maxConcurrentRenders) {
100
+ this.activeRenderCount += 1;
101
+ return;
102
+ }
103
+ await new Promise((resolve) => {
104
+ this.renderQueue.push({ resolve });
105
+ });
106
+ this.activeRenderCount += 1;
107
+ };
108
+
109
+ const releaseRenderSlot = () => {
110
+ this.activeRenderCount = Math.max(0, this.activeRenderCount - 1);
111
+ const next = this.renderQueue.shift();
112
+ if (next && isFunction(next.resolve)) {
113
+ next.resolve();
114
+ }
115
+ };
116
+
117
+ await acquireRenderSlot();
118
+ try {
95
119
 
96
120
  // Create offlineAudioCtx that will house our rendered buffer
97
121
  var offlineAudioCtx = new OfflineAudioContext(this.channels, buffer.length * this.channels, this.audioCtx.sampleRate);
@@ -189,6 +213,9 @@ var Databender = (function () {
189
213
  this.previousConfig = this.config;
190
214
  // Kick off the render, callback will contain rendered buffer in event
191
215
  return offlineAudioCtx.startRendering();
216
+ } finally {
217
+ releaseRenderSlot();
218
+ }
192
219
  };
193
220
 
194
221
  this.draw = function(buffer, context, sourceX = 0, sourceY = 0, x = 0, y = 0, sourceWidth = this.imageData.width, sourceHeight = this.imageData.height, targetWidth = window.innerWidth, targetHeight = window.innerHeight) {
package/index.js CHANGED
@@ -44,6 +44,9 @@ export default class Databender {
44
44
  this.effectsChain = effectsChain ? asArray(effectsChain) : null;
45
45
  this.sourceParams = sourceParams ? asArray(sourceParams) : null;
46
46
  this.chainMode = chainMode === 'parallel' ? 'parallel' : 'series';
47
+ this.maxConcurrentRenders = 2;
48
+ this.activeRenderCount = 0;
49
+ this.renderQueue = [];
47
50
 
48
51
  this.convert = function(image) {
49
52
  if (image instanceof Image || image instanceof HTMLVideoElement) {
@@ -90,6 +93,27 @@ export default class Databender {
90
93
  };
91
94
 
92
95
  this.render = async function(buffer, bypass = false) {
96
+ const acquireRenderSlot = async () => {
97
+ if (this.activeRenderCount < this.maxConcurrentRenders) {
98
+ this.activeRenderCount += 1;
99
+ return;
100
+ }
101
+ await new Promise((resolve) => {
102
+ this.renderQueue.push({ resolve });
103
+ });
104
+ this.activeRenderCount += 1;
105
+ };
106
+
107
+ const releaseRenderSlot = () => {
108
+ this.activeRenderCount = Math.max(0, this.activeRenderCount - 1);
109
+ const next = this.renderQueue.shift();
110
+ if (next && isFunction(next.resolve)) {
111
+ next.resolve();
112
+ }
113
+ };
114
+
115
+ await acquireRenderSlot();
116
+ try {
93
117
 
94
118
  // Create offlineAudioCtx that will house our rendered buffer
95
119
  var offlineAudioCtx = new OfflineAudioContext(this.channels, buffer.length * this.channels, this.audioCtx.sampleRate);
@@ -187,6 +211,9 @@ export default class Databender {
187
211
  this.previousConfig = this.config;
188
212
  // Kick off the render, callback will contain rendered buffer in event
189
213
  return offlineAudioCtx.startRendering();
214
+ } finally {
215
+ releaseRenderSlot();
216
+ }
190
217
  };
191
218
 
192
219
  this.draw = function(buffer, context, sourceX = 0, sourceY = 0, x = 0, y = 0, sourceWidth = this.imageData.width, sourceHeight = this.imageData.height, targetWidth = window.innerWidth, targetHeight = window.innerHeight) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "databender",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "Create interesting visuals by misusing the Web Audio API",
5
5
  "main": "index.js",
6
6
  "scripts": {