To stream MP3 files via D++ you need to link an additional dependency to your bot, namely libmpg123. It is relatively simple when linking this library to your bot to then decode audio to PCM and send it to the dpp::discord_voice_client::send_audio_raw function as shown below:
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <dpp/fmt/format.h>
#include <iomanip>
#include <sstream>
#include <vector>
#include <fstream>
#include <iostream>
#include <mpg123.h>
#include <out123.h>
#define MUSIC_FILE "/media/music/Rick Astley/Whenever You Need Somebody/Never Gonna Give You Up.mp3"
int main(int argc, char const *argv[])
{
std::vector<uint8_t> pcmdata;
mpg123_init();
int err = 0;
unsigned char* buffer;
size_t buffer_size, done;
int channels, encoding;
long rate;
mpg123_handle *mh = mpg123_new(NULL, &err);
mpg123_param(mh, MPG123_FORCE_RATE, 48000, 48000.0);
buffer_size = mpg123_outblock(mh);
buffer = new unsigned char[buffer_size];
mpg123_open(mh, MUSIC_FILE);
mpg123_getformat(mh, &rate, &channels, &encoding);
unsigned int counter = 0;
for (int totalBtyes = 0; mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK; ) {
for (auto i = 0; i < buffer_size; i++) {
pcmdata.push_back(buffer[i]);
}
counter += buffer_size;
totalBtyes += done;
}
delete buffer;
mpg123_close(mh);
mpg123_delete(mh);
std::string command;
ss >> command;
if (command == ".join") {
}
}
if (command == ".mp3") {
}
}
});
std::cout << event.message << "\n";
}
});
bot.start(false);
mpg123_exit();
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:373
discord_voice_client & send_audio_raw(uint16_t *audio_data, const size_t length)
Send raw audio 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:357
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: discord.h:48
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
DPP_EXPORT guild * find_guild(snowflake id)
@ ll_trace
Trace.
Definition: discord.h:81
Log messages.
Definition: dispatcher.h:92
loglevel severity
Definition: dispatcher.h:100
Create message.
Definition: dispatcher.h:1122
message msg
message that was created (sent).
Definition: dispatcher.h:1131
Represents messages sent and received on Discord.
Definition: message.h:954
user author
Definition: message.h:960
snowflake guild_id
Definition: message.h:958
std::string content
Definition: message.h:964
snowflake channel_id
Definition: message.h:956