mirror of
https://gitlab.com/80486DX2-66/gists
synced 2024-11-10 06:42:02 +05:30
bytebeat-render.js: improve the loop algorithm
This commit is contained in:
parent
ac9bdfdd75
commit
2e15e4addc
@ -90,11 +90,17 @@ let filePath = generateRandomFilePath()
|
||||
writeFileSync(filePath, Buffer.alloc(0))
|
||||
|
||||
// the loop of sequential file writing, created to ease load on RAM
|
||||
for (let buffer = 0; t < PRODUCT; buffer++) {
|
||||
let audioData = new Uint8Array(BUFFER_SIZE)
|
||||
// (it doesn't work as intended)
|
||||
const max = (PRODUCT + (BUFFER_SIZE - 1)) / BUFFER_SIZE
|
||||
|
||||
let idx = 0
|
||||
for (; idx < BUFFER_SIZE && t < PRODUCT; idx++) {
|
||||
for (let seq = 0; seq < max; seq++) {
|
||||
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)
|
||||
if (sample.constructor === Array)
|
||||
sample.forEach((sample, index) => {
|
||||
@ -102,13 +108,6 @@ for (let buffer = 0; t < PRODUCT; buffer++) {
|
||||
})
|
||||
else
|
||||
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))
|
||||
|
Loading…
Reference in New Issue
Block a user