Debian Codegraph @

docpages/example_code/mp3.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2 #include <iomanip>
3 #include <sstream>
4
5 #include <vector>
6 #include <fstream>
7 #include <iostream>
8 #include <mpg123.h>
9 #include <out123.h>
10
11 /* For an example we will hardcode a path to some awesome music here */
12 #define MUSIC_FILE "/media/music/Rick Astley/Whenever You Need Somebody/Never Gonna Give You Up.mp3"
13
14 int main() {
15 /* This will hold the decoded MP3.
16 * The D++ library expects PCM format, which are raw sound
17 * data, 2 channel stereo, 16 bit signed 48000Hz.
18 */
19 std::vector<uint8_t> pcmdata;
20
21 mpg123_init();
22
23 int err = 0;
24 unsigned char* buffer;
25 size_t buffer_size, done;
26 int channels, encoding;
27 long rate;
28
29 /* Note it is important to force the frequency to 48000 for Discord compatibility */
30 mpg123_handle *mh = mpg123_new(NULL, &err);
31 mpg123_param(mh, MPG123_FORCE_RATE, 48000, 48000.0);
32
33 /* Decode entire file into a vector. You could do this on the fly, but if you do that
34 * you may get timing issues if your CPU is busy at the time and you are streaming to
35 * a lot of channels/guilds.
36 */
37 buffer_size = mpg123_outblock(mh);
38 buffer = new unsigned char[buffer_size];
39
40 /* Note: In a real world bot, this should have some error logging */
41 mpg123_open(mh, MUSIC_FILE);
42 mpg123_getformat(mh, &rate, &channels, &encoding);
43
44 while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK) {
45 for (size_t i = 0; i < buffer_size; i++) {
46 pcmdata.push_back(buffer[i]);
47 }
48 }
49 delete[] buffer;
50 mpg123_close(mh);
51 mpg123_delete(mh);
52
53 /* Setup the bot */
54 dpp::cluster bot("token");
55
56 bot.on_log(dpp::utility::cout_logger());
57
58 /* The event is fired when someone issues your commands */
59 bot.on_slashcommand([&pcmdata](const dpp::slashcommand_t& event) {
60 /* Check which command they ran */
61 if (event.command.get_command_name() == "join") {
62 /* Get the guild */
63 dpp::guild* g = dpp::find_guild(event.command.guild_id);
64
65 /* Attempt to connect to a voice channel, returns false if we fail to connect. */
66 if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) {
67 event.reply("You don't seem to be in a voice channel!");
68 return;
69 }
70
71 /* Tell the user we joined their channel. */
72 event.reply("Joined your channel!");
73 } else if (event.command.get_command_name() == "mp3") {
74 /* Get the voice channel the bot is in, in this current guild. */
75 dpp::voiceconn* v = event.from()->get_voice(event.command.guild_id);
76
77 /* If the voice channel was invalid, or there is an issue with it, then tell the user. */
78 if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
79 event.reply("There was an issue with getting the voice channel. Make sure I'm in a voice channel!");
80 return;
81 }
82
83 /* Stream the already decoded MP3 file. This passes the PCM data to the library to be encoded to OPUS */
84 v->voiceclient->send_audio_raw((uint16_t*)pcmdata.data(), pcmdata.size());
85
86 event.reply("Played the mp3 file.");
87 }
88 });
89
90 bot.on_ready([&bot](const dpp::ready_t& event) {
91 if (dpp::run_once<struct register_bot_commands>()) {
92 /* Create a new command. */
93 dpp::slashcommand joincommand("join", "Joins your voice channel.", bot.me.id);
94 dpp::slashcommand mp3command("mp3", "Plays an mp3 file.", bot.me.id);
95
96 bot.global_bulk_command_create({ joincommand, mp3command });
97 }
98 });
99
100 /* Start bot */
101 bot.start(dpp::st_wait);
102
103 /* Clean up */
104 mpg123_exit();
105
106 return 0;
107 }