DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
AudioDecoder.hpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
5
6 Copyright 2022, 2023 Chris M. (RealTimeChris)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26/// AudioDecoder.hpp - Header for the audio decoder class.
27/// Jul 29, 2021
28/// https://discordcoreapi.com
29/// \file AudioDecoder.hpp
30#pragma once
31
33#include <opus/opus.h>
34
35namespace discord_core_api {
36
37 namespace discord_core_internal {
38
39 /**
40 * \addtogroup discord_core_internal
41 * @{
42 */
43
44
45 /// @brief A wrapper class for the opus audio decoder.
47 /// @brief Deleter foropus_decoder instances.
49 /// @brief Operator to destroy an OpusDecoder instance.
50 /// @param other the OpusDecoder pointer to be deleted.
51 DCA_INLINE void operator()(OpusDecoder* other) {
52 if (other) {
53 opus_decoder_destroy(other);
54 other = nullptr;
55 }
56 }
57 };
58
59 /// @brief Constructor for opus_decoder_wrapper. initializes and configures the opus decoder.
60 DCA_INLINE opus_decoder_wrapper() {
61 int32_t error{};
62
63 // ensure data vector has minimum size
64 if (data.size() < 23040) {
65 data.resize(23040);
66 }
67
68 // create OpusDecoder instance
69 ptr.reset(opus_decoder_create(48000, 2, &error));
70
71 // check for initialization errors
72 if (error != OPUS_OK) {
73 throw dca_exception{ "Failed to create the opus decoder, reason: " + jsonifier::string{ opus_strerror(error) } };
74 }
75 }
76
77 /// @brief Decode opus audio data.
78 /// @param dataToDecode the opus-encoded audio data to decode.
79 /// @return a basic_string_view containing the decoded audio samples.
80 /// @throws dca_exception if decoding fails.
81 DCA_INLINE jsonifier::string_view_base<opus_int16> decodeData(jsonifier::string_view_base<uint8_t> dataToDecode) {
82 const int32_t sampleCount = opus_decode(ptr.get(), dataToDecode.data(), static_cast<opus_int32>(dataToDecode.size() & 0x7FFFFFFF), data.data(), 5760, 0);
83
84 // check for successful decoding
85 if (sampleCount > 0) {
86 return jsonifier::string_view_base<opus_int16>{ data.data(), static_cast<uint64_t>(sampleCount) * 2ULL };
87 } else {
88 throw dca_exception{ "Failed to decode a user's voice payload, reason: " + jsonifier::string{ opus_strerror(sampleCount) } };
89 }
90 }
91
92 protected:
93 unique_ptr<OpusDecoder, opus_decoder_deleter> ptr{};///< Unique pointer to OpusDecoder instance.
94 jsonifier::vector<opus_int16> data{};///< Buffer for decoded audio samples.
95 };
96
97 /**@}*/
98
99 }
100
101}
The main namespace for the forward-facing interfaces.
DCA_INLINE void operator()(OpusDecoder *other)
Operator to destroy an OpusDecoder instance.
A wrapper class for the opus audio decoder.
jsonifier::vector< opus_int16 > data
Buffer for decoded audio samples.
DCA_INLINE jsonifier::string_view_base< opus_int16 > decodeData(jsonifier::string_view_base< uint8_t > dataToDecode)
Decode opus audio data.
unique_ptr< OpusDecoder, opus_decoder_deleter > ptr
Unique pointer to OpusDecoder instance.
DCA_INLINE opus_decoder_wrapper()
Constructor for opus_decoder_wrapper. initializes and configures the opus decoder.