i have an arraybuffer how can i trasform it to use in createAudioResource()?

help
34 Replies
d.js toolkit
d.js toolkit2mo ago
- What are your intents? GuildVoiceStates is required to receive voice data! - Show what dependencies you are using -- generateDependencyReport() is exported from @discordjs/voice. - Try looking at common examples: https://github.com/discordjs/voice-examples. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
pat
pat2mo ago
where are you getting an arraybuffer from anyways turn it into a Readable
luomo
luomoOP2mo ago
and after that i can push it in the method? do u have the example code ? i never worked on audio so im very dumb at that
pat
pat2mo ago
yes no
luomo
luomoOP2mo ago
ty i'll try now ❤️ is it possible to play the audio instantaneously, like a normal user, I enclose the code from which I have to take the buffers
callCharacter.output.on("frameReceived", ev => {

if(isValidBuffer(ev.frame.data.buffer)){
logger('Adding audio', 'info');
const resource = createAudioResource(bufferToReadable(ev.frame.data.buffer), { inlineVolume: true });
resource.volume.setVolume(1.0);
player.play(resource);
}
});
callCharacter.output.on("frameReceived", ev => {

if(isValidBuffer(ev.frame.data.buffer)){
logger('Adding audio', 'info');
const resource = createAudioResource(bufferToReadable(ev.frame.data.buffer), { inlineVolume: true });
resource.volume.setVolume(1.0);
player.play(resource);
}
});
pat
pat2mo ago
what are you working with? what is frame im thinking its better to just create a Readable and push your buffer into it and play the readable outside of this loop
luomo
luomoOP2mo ago
im trying to do something like voice call with an ia
pat
pat2mo ago
but what is ev.frame @luomo what package/website/whatever are you getting the data from
luomo
luomoOP2mo ago
from another api
pat
pat2mo ago
which is?
luomo
luomoOP2mo ago
🫠
pat
pat2mo ago
okay nevermind, just do this
luomo
luomoOP2mo ago
i thought to do an intervall
pat
pat2mo ago
no what i said is the best option because the way you have it now (and with interval) you are re-creating a different stream every time you receive an audio buffer it is bad
luomo
luomoOP2mo ago
i want make an intervall out of the event , because i have to create the readable again if i want do it automatically
pat
pat2mo ago
but u only need to do make the readable once if you keep pushing the buffer data to it
luomo
luomoOP2mo ago
or are you telling me that by writing it out, the readable is automatically updated once created and also plays the new buffers?
pat
pat2mo ago
if you push yes thats how streams work
luomo
luomoOP2mo ago
luomo
luomoOP2mo ago
tysm i'll test it
callCharacter.output.on("frameReceived", ev => {

if(isValidBuffer(ev.frame.data.buffer)){
logger('Adding audio', 'info');
readable.push(ev.frame.data.buffer);

}
});

player.play(createAudioResource(readable))
callCharacter.output.on("frameReceived", ev => {

if(isValidBuffer(ev.frame.data.buffer)){
logger('Adding audio', 'info');
readable.push(ev.frame.data.buffer);

}
});

player.play(createAudioResource(readable))
is this what you mean? @pat
pat
pat2mo ago
try
luomo
luomoOP2mo ago
it saves the data but if i play i can't play it if its null since it runs it once and the readable is empty at that time it will not play the audio
pat
pat2mo ago
put infinity as option for silence frames
luomo
luomoOP2mo ago
what do u mean?
pat
pat2mo ago
when the stream fully consumes the buffer you gave it, the stream will be empty (and end), not playing audio is correct so you need to put silence frame inbetween in createAudioResource theres an option
luomo
luomoOP2mo ago
name?
pat
pat2mo ago
discord.js
discord.js
discord.js is a powerful Node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
luomo
luomoOP2mo ago
five is ok? or i need to increment it?
d.js docs
d.js docs2mo ago
:interface: CreateAudioResourceOptions @0.17.0 Options that are set when creating a new audio resource.
luomo
luomoOP2mo ago
const readable = new Readable({
read(){}
});
const player = createAudioPlayer();
connection.subscribe(player);

callCharacter.output.on("frameReceived", ev => {
readable.push(Buffer.from(ev.frame.data.buffer));
readable.push(null);
});

const resource = createAudioResource(readable, {silencePaddingFrames: 5});
player.play(resource);
const readable = new Readable({
read(){}
});
const player = createAudioPlayer();
connection.subscribe(player);

callCharacter.output.on("frameReceived", ev => {
readable.push(Buffer.from(ev.frame.data.buffer));
readable.push(null);
});

const resource = createAudioResource(readable, {silencePaddingFrames: 5});
player.play(resource);
@pat i tried but not works how i create it help pls 😭
d.js docs
d.js docs2mo ago
:node: fs.createReadStream(path[, options]) Unlike the 16 KiB default highWaterMark for a <stream.Readable>, the stream returned by this method has a default highWaterMark of 64 KiB.options can include start and end values to read a range of bytes from the file instead of the entire file.
luomo
luomoOP2mo ago
i have to write the push in it? 🫠 ok, and for the method read()?
luomo
luomoOP2mo ago
luomo
luomoOP2mo ago
const readable = fs.createReadStream(filepath);
callCharacter.output.on("frameReceived", ev => {
readable.push(Buffer.from(ev.frame.data.buffer));
readable.push(null)
});
player.play(createAudioResource(readable, {silencePaddingFrames: 5}));
const readable = fs.createReadStream(filepath);
callCharacter.output.on("frameReceived", ev => {
readable.push(Buffer.from(ev.frame.data.buffer));
readable.push(null)
});
player.play(createAudioResource(readable, {silencePaddingFrames: 5}));
right now? @Qjuh like that "readable = createReadStream(Buffer.from(ev.frame.data.buffer));" ? TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string, Uint8Array, or URL without null bytes. @Qjuh i think i can't use it in the way u told me

Did you find this page helpful?