1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-09-20 09:15:41 +05:30

bytebeat-render.js: improve the loop algorithm

This commit is contained in:
Intel A80486DX2-66 2024-01-31 22:31:55 +03:00
parent ac9bdfdd75
commit 2e15e4addc
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -90,11 +90,17 @@ let filePath = generateRandomFilePath()
writeFileSync(filePath, Buffer.alloc(0)) writeFileSync(filePath, Buffer.alloc(0))
// the loop of sequential file writing, created to ease load on RAM // the loop of sequential file writing, created to ease load on RAM
for (let buffer = 0; t < PRODUCT; buffer++) { // (it doesn't work as intended)
let audioData = new Uint8Array(BUFFER_SIZE) const max = (PRODUCT + (BUFFER_SIZE - 1)) / BUFFER_SIZE
let idx = 0 for (let seq = 0; seq < max; seq++) {
for (; idx < BUFFER_SIZE && t < PRODUCT; idx++) { let calculatedSize = BUFFER_SIZE
if ((t + BUFFER_SIZE) >= PRODUCT)
calculatedSize = PRODUCT - t
let audioData = new Uint8Array(calculatedSize)
for (let idx = 0; t < PRODUCT && idx < BUFFER_SIZE; idx++, t++) {
let sample = generateAudio(t * FINAL_SAMPLE_RATE_CONVERSION) let sample = generateAudio(t * FINAL_SAMPLE_RATE_CONVERSION)
if (sample.constructor === Array) if (sample.constructor === Array)
sample.forEach((sample, index) => { sample.forEach((sample, index) => {
@ -102,13 +108,6 @@ for (let buffer = 0; t < PRODUCT; buffer++) {
}) })
else else
audioData[idx] = constrainValue(sample) audioData[idx] = constrainValue(sample)
t++
}
if (t >= PRODUCT) {
let truncatedArray = new Uint8Array(idx)
truncatedArray.set(audioData.subarray(0, idx))
audioData = truncatedArray
} }
appendFileSync(filePath, Buffer.from(audioData.buffer)) appendFileSync(filePath, Buffer.from(audioData.buffer))