1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-05-31 08:31:41 +05:30

bytebeat-render.js: fix visibility scope

This commit is contained in:
2024-07-13 17:16:18 +03:00
parent 0df2f8f4a0
commit 83a50dc22a

View File

@@ -18,31 +18,7 @@
* License: Creative Commons Zero 1.0 Universal
*/
const { appendFileSync, unlinkSync, writeFileSync } = require("fs")
const { tmpdir } = require("os")
const { execSync } = require("child_process")
const { basename } = require("path")
let BUFFER_SIZE = 65536 // feel free to change this
const LIGHTNING_MODE = false // disables sequential file write optimization,
// feel free to enable this
const SAMPLE_RATE = 8000 // feel free to change this
const SECONDS = 30 // feel free to change this
const CHANNELS = 1 // feel free to change this
const FINAL_SAMPLE_RATE = 44100 // feel free to change this
const FINAL_SAMPLE_RATE_CONVERSION = SAMPLE_RATE / FINAL_SAMPLE_RATE / CHANNELS
const SAMPLES = SECONDS * FINAL_SAMPLE_RATE // feel free to change this
const PRODUCT = SAMPLES * CHANNELS
BUFFER_SIZE = LIGHTNING_MODE ? PRODUCT : BUFFER_SIZE
const TYPE_BYTEBEAT = 0
const TYPE_SIGNED_BYTEBEAT = 1
const TYPE_FLOATBEAT = 2
const SELECTED_TYPE = TYPE_BYTEBEAT // feel free to change this
const generateAudio = t => {
// for bytebeat
var int = x => Math.floor(x)
var abs = Math.abs
@@ -70,23 +46,49 @@ var sqrt = Math.sqrt
var tan = Math.tan
var tanh = Math.tanh
const generateAudio = t => {
return t&t>>8
}
function main() {
const { appendFileSync, unlinkSync, writeFileSync } = require("fs")
const { tmpdir } = require("os")
const { execSync } = require("child_process")
const { basename } = require("path")
let BUFFER_SIZE = 65536 // feel free to change this
const LIGHTNING_MODE = false // disables sequential file write optimization,
// feel free to enable this
const SAMPLE_RATE = 8000 // feel free to change this
const SECONDS = 30 // feel free to change this
const CHANNELS = 1 // feel free to change this
const FINAL_SAMPLE_RATE = 44100 // feel free to change this
const FINAL_SAMPLE_RATE_CONVERSION = SAMPLE_RATE / FINAL_SAMPLE_RATE /
CHANNELS
const SAMPLES = SECONDS * FINAL_SAMPLE_RATE // feel free to change this
const PRODUCT = SAMPLES * CHANNELS
BUFFER_SIZE = LIGHTNING_MODE ? PRODUCT : BUFFER_SIZE
const TYPE_BYTEBEAT = 0
const TYPE_SIGNED_BYTEBEAT = 1
const TYPE_FLOATBEAT = 2
const SELECTED_TYPE = TYPE_BYTEBEAT // feel free to change this
const clamp = (a, b, c) => max(min(a, c), b)
let lastCorrectSample
switch (SELECTED_TYPE) {
case TYPE_BYTEBEAT:
lastCorrectSample = 127
break;
break
case TYPE_SIGNED_BYTEBEAT:
lastCorrectSample = 0
break;
break
case TYPE_FLOATBEAT:
lastCorrectSample = 0.0
break;
break
}
const constrainValue = sample => {
@@ -101,13 +103,14 @@ const constrainValue = sample => {
case TYPE_SIGNED_BYTEBEAT:
return ((sample + 127) & 255) - 128
case TYPE_FLOATBEAT:
// NOTE: temporary fix copied from a code by lehandsomeguy, see https://
// www.reddit.com/r/bytebeat/comments/48r00a/floatbeat_test/
// NOTE: temporary fix copied from a code by lehandsomeguy, see
// https://www.reddit.com/r/bytebeat/comments/48r00a/floatbeat_test/
return floor((clamp(sample, -0.9999, 0.9999) * 128) + 128)
}
}
const random_choice = choices => choices[Math.floor(Math.random() * choices.length)]
const random_choice = choices => choices[Math.floor(Math.random() *
choices.length)]
const randomFileNameAlphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
@@ -119,8 +122,6 @@ const generateRandomFilePath = () => {
return res
}
let t = 0
let filePath = generateRandomFilePath()
writeFileSync(filePath, Buffer.alloc(0))
@@ -130,6 +131,8 @@ const buffer_max = Math.floor((PRODUCT + (BUFFER_SIZE - 1)) / BUFFER_SIZE),
let audioData = new Uint8Array(needSingleBuffer ? PRODUCT : BUFFER_SIZE)
let t = 0
for (let seq = 0; seq < buffer_max; seq++) {
if (needTwoBuffers && (t + BUFFER_SIZE) >= PRODUCT) {
let calculatedSize = PRODUCT - t
@@ -153,3 +156,7 @@ execSync(
`ffmpeg -f u8 -ar ${FINAL_SAMPLE_RATE} -ac ${CHANNELS} ` +
`-i ${filePath} output_${+new Date()}.wav`)
unlinkSync(filePath)
}
if (require.main === module)
main()