From 33c341f7e8ad6ec3e7160e49d1cd392cc91cb47a Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Wed, 31 Jan 2024 23:41:49 +0300 Subject: [PATCH] bytebeat-render.js: fix memory usage, fixup for 2e15e4addc --- js-programming/bytebeat-render.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/js-programming/bytebeat-render.js b/js-programming/bytebeat-render.js index d8ab2fd..19d1cd7 100644 --- a/js-programming/bytebeat-render.js +++ b/js-programming/bytebeat-render.js @@ -90,15 +90,16 @@ let filePath = generateRandomFilePath() writeFileSync(filePath, Buffer.alloc(0)) // the loop of sequential file writing, created to ease load on RAM -// (it doesn't work as intended) -const max = (PRODUCT + (BUFFER_SIZE - 1)) / BUFFER_SIZE +const max = Math.floor((PRODUCT + (BUFFER_SIZE - 1)) / BUFFER_SIZE), + needTwoBuffers = max > 1, needSingleBuffer = !needTwoBuffers + +let audioData = new Uint8Array(needSingleBuffer ? PRODUCT : BUFFER_SIZE) for (let seq = 0; seq < max; seq++) { - let calculatedSize = BUFFER_SIZE - if ((t + BUFFER_SIZE) >= PRODUCT) - calculatedSize = PRODUCT - t - - let audioData = new Uint8Array(calculatedSize) + if (needTwoBuffers && (t + BUFFER_SIZE) >= PRODUCT) { + let calculatedSize = PRODUCT - t + audioData = new Uint8Array(calculatedSize) + } for (let idx = 0; t < PRODUCT && idx < BUFFER_SIZE; idx++, t++) { let sample = generateAudio(t * FINAL_SAMPLE_RATE_CONVERSION) @@ -111,8 +112,6 @@ for (let seq = 0; seq < max; seq++) { } appendFileSync(filePath, Buffer.from(audioData.buffer)) - - audioData = null } execSync(