stream.js
JavaScript source for Endless music with MusicGen, 58 lines. Raw file
const Ai = require("Ai");
const Security = require("Security");
const fs = require("fs");
const settings = {
_is: "Ai.Audio.Continuous",
// Tempo in beats per minute
bpm: 85.0,
beats_per_chunk: 16,
// Reduces sampling to the k most likely tokens
top_k: 250,
// Reduces sampling to tokens with cumulative probability of p. When set to `0` (default), top_k sampling is used.
top_p: 1.0,
// Controls the 'conservativeness' of the sampling process. Higher temperature means more diversity.
temperature: 1.0,
// Increases the influence of inputs on the output. Higher values produce lower-varience outputs that adhere more closely to inputs.
cfg_coef: 3.0,
buffer: 4,
device: 'cpu',
accuracy: 'large',
}
function randomPop() {
let randomIndex = Math.floor(Math.random() * hiphop_prompts.length);
return hiphop_prompts.splice(randomIndex, 1)[0];
}
const hiphop_prompts = [
"A smooth, mellow beat with deep bass and a touch of jazz influence",
"Hip hop instrumental with high-energy trap influences and tight snares",
"Chilled lo-fi hip hop beat, featuring soft piano and vinyl scratches",
"Old school hip hop beat with heavy 808s and soulful samples",
"A hip hop track influenced by East Coast style, featuring boom-bap drums",
"Melodic hip hop beat featuring strings and crisp percussion elements",
"Heavy southern hip hop beat with vibrant brass and pounding 808s",
"Dark, atmospheric hip hop instrumental with eerie synths and tight drums",
"Upbeat hip hop track with a funk influence and punchy rhythm",
"Gritty, underground hip hop beat with a raw and edgy sound"
]
const seed_prompt = randomPop();
let chunkNumber = 1;
Ai.Audio.Continuous(
seed_prompt,
settings,
(newChunkEncoded) => {
console.log("chunk received")
Security.String(newChunkEncoded)
// Save new chunk
const audioBuffer = Buffer.from(newChunkEncoded.split(',')[1], 'base64');
fs.writeFileSync(`/root/stream/${chunkNumber++}.wav`, audioBuffer);
},
() => {
console.log("prompt requested")
// ...
// Perform some logic to get next prompt, this can be a call to AI
const new_prompt = randomPop()
return new_prompt ?? ""
}
);