This example shows how to stream an Ogg Opus file to a voice channel. This example requires some additional dependencies, namely libogg
and opusfile
.
#include <dpp/dpp.h>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ogg/ogg.h>
#include <opus/opusfile.h>
int main(int argc, char const *argv[])
{
std::string command;
ss >> command;
if (command == ".join") {
}
}
if (command == ".play") {
ogg_sync_state oy;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
OpusHead header;
char *buffer;
FILE *fd;
fd = fopen("/path/to/opus.ogg", "rb");
fseek(fd, 0L, SEEK_END);
size_t sz = ftell(fd);
rewind(fd);
ogg_sync_init(&oy);
int eos = 0;
int i;
buffer = ogg_sync_buffer(&oy, sz);
fread(buffer, 1, sz, fd);
ogg_sync_wrote(&oy, sz);
if (ogg_sync_pageout(&oy, &og) != 1)
{
fprintf(stderr,"Does not appear to be ogg stream.\n");
exit(1);
}
ogg_stream_init(&os, ogg_page_serialno(&og));
if (ogg_stream_pagein(&os,&og) < 0) {
fprintf(stderr,"Error reading initial page of ogg stream.\n");
exit(1);
}
if (ogg_stream_packetout(&os,&op) != 1)
{
fprintf(stderr,"Error reading header packet of ogg stream.\n");
exit(1);
}
if (!(op.bytes > 8 && !memcmp("OpusHead", op.packet, 8)))
{
fprintf(stderr,"Not an ogg opus stream.\n");
exit(1);
}
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err)
{
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
if (header.channel_count != 2 && header.input_sample_rate != 48000)
{
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
while (ogg_sync_pageout(&oy, &og) == 1){
ogg_stream_init(&os, ogg_page_serialno(&og));
if(ogg_stream_pagein(&os,&og)<0){
fprintf(stderr,"Error reading page of Ogg bitstream data.\n");
exit(1);
}
while (ogg_stream_packetout(&os,&op) != 0)
{
if (op.bytes > 8 && !memcmp("OpusHead", op.packet, 8))
{
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err)
{
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
if (header.channel_count != 2 && header.input_sample_rate != 48000)
{
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
continue;
}
if (op.bytes > 8 && !memcmp("OpusTags", op.packet, 8))
continue;
int samples = opus_packet_get_samples_per_frame(op.packet, 48000);
}
}
ogg_stream_clear(&os);
ogg_sync_clear(&oy);
}
}
});
bot.start(false);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:435
discord_voice_client & send_audio_opus(uint8_t *opus_packet, const size_t length, uint64_t duration)
Send opus packets to the voice channel.
bool is_ready()
voice client is ready to stream audio. The voice client is considered ready if it has a secret key.
Represents a guild on Discord (AKA a server)
Definition: guild.h:368
bool connect_member_voice(snowflake user_id, bool self_mute=false, bool self_deaf=false)
Connect to a voice channel another guild member is in.
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition: managed.h:38
Represents a connection to a voice channel. A client can only connect to one voice channel per guild ...
Definition: discordclient.h:60
class discord_voice_client * voiceclient
voice websocket client
Definition: discordclient.h:89
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:64
@ i_default_intents
Default D++ intents (all non-privileged intents)
Definition: intents.h:68
DPP_EXPORT class guild * find_guild(snowflake id)
Create message.
Definition: dispatcher.h:1172
message msg
message that was created (sent).
Definition: dispatcher.h:1181
Represents messages sent and received on Discord.
Definition: message.h:1036
user author
Definition: message.h:1042
snowflake guild_id
Definition: message.h:1040
std::string content
Definition: message.h:1046
snowflake channel_id
Definition: message.h:1038