discordjs / voice

Implementation of the Discord Voice API for discord.js and other JS/TS libraries
Apache License 2.0
327 stars 110 forks source link

Unable to create oggStream #210

Open Yvtq8K3n opened 3 years ago

Yvtq8K3n commented 3 years ago
member.guild.channels.cache.get(voiceChannel.id).members.forEach((tempMember) => {
                const filename = './recordings/${Date.now()}-${tempMember.id}.ogg';

                const opusStream = receiver.subscribe(tempMember.id, {
                    end: {
                      behavior: EndBehaviorType.AfterSilence,
                      duration: 100,
                    },
                  });

                //const rawAudio = opusStream.pipe(new opus.Decoder({ frameSize: 960, channels: 2, rate: 48000 }));

                const oggStream = new opus.OggLogicalBitstream({
                    opusHead: new opus.OpusHead({
                        channelCount: 2,
                        sampleRate: 48000,
                    }),
                    opusTags: new opus.OpusTags({
                        maxPackets: 10,
                    })
                });

                const out = fs.createWriteStream(filename);

                pipeline(opusStream, oggStream, out,  (err) => {
                    console.log(err);
                    if (err) {
                        console.warn('❌ Error recording file ${filename} - ${err.message}');
                    } else {
                        console.log('✅ Recorded ${filename}');
                    }
                });
            });
        }

TypeError: failed to downcast any to number at crc (C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\node_modules\node-crc\lib\lib.js:11:17) at OggLogicalBitstream.calculateCRC (C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\node_modules\prism-media\dist\ogg\OggLogicalBitstream.js:92:23) at OggLogicalBitstream.writePage (C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\node_modules\prism-media\dist\ogg\OggLogicalBitstream.js:150:33) at OggLogicalBitstream.writeHeaderPages (C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\node_modules\prism-media\dist\ogg\OggLogicalBitstream.js:74:18) at new OggLogicalBitstream (C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\node_modules\prism-media\dist\opus\OggLogicalBitstream.js:16:14) at C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\index.js:51:35 at Map.forEach () at start (C:\Users\Yvtq8\Desktop\SideProjects\BobbyMcLovin\index.js:37:70)

odysseaspapadimas commented 3 years ago

Any updates on this? Or some alternative so I can record in a voice channel and output it?

Yvtq8K3n commented 3 years ago

Any updates on this? Or some alternative so I can record in a voice channel and output it?

I did manage to find a way to successfully record and store voice recordings. However, I have no clue how it works. First, I made a class called Decoder, as follows: ` class OpusDecodingStream extends Transform { encoder

constructor(options, encoder) {
    super(options)
    this.encoder = encoder
}

_transform(data, encoding, callback) {
    this.push(this.encoder.decode(data))
    callback()
}

} ` Then with this decoder, which I still have no clue how it works, I parsed the audio and was able to store it.

await entersState(connection, VoiceConnectionStatus.Ready, 20e3);
        if (connection) {       
            const receiver = connection.receiver;
            member.guild.channels.cache.get(voiceChannel.id).members.forEach((tempMember) => {
                const username = findUsername(tempMember.id)
                const filename = "./recordings/test_${Date.now()}_${username}.wav";

                const encoder = new OpusEncoder(16000, 1)
                const commandAudioStream = receiver.subscribe(tempMember.id, {
                    end: {
                      behavior: EndBehaviorType.AfterSilence,
                      duration: 100,
                    },
                })
                .pipe(new OpusDecodingStream({}, encoder))
                .pipe(new FileWriter(filename, {
                    channels: 1,
                    sampleRate: 16000
                }))
            });

Hopes this helps, but I really would like to see a cleaner guide. Not everyone is an audio engineer;( It would be nice to know how to store in other audio formats. You can see the full code in here: https://github.com/Yvtq8K3n/BobbyMcLovin/blob/742d041f5d3bd621628681c9ded0d7acde096c24/index.js#L72-L84