DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
AudioEncoder.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/// AudioEncoder.hpp - Header for the audio encoder class.
27/// Aug 22, 2021
28/// https://discordcoreapi.com
29/// \file AudioEncoder.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 /// @brief Structure to hold the encoded data and sample count returned by the encoder.
46 jsonifier::string_view_base<uint8_t> data{};///< Encoded data.
47 uint64_t sampleCount{};///< Number of audio samples in the input frame.
48 };
49
50 /// @brief Wrapper class for the opus audio encoder.
52 /// @brief Deleter for OpusEncoder instances.
54 /// @brief Operator to destroy an OpusEncoder instance.
55 /// @param other the OpusEncoder pointer to be deleted.
56 DCA_INLINE void operator()(OpusEncoder* other) {
57 if (other) {
58 opus_encoder_destroy(other);
59 other = nullptr;
60 }
61 }
62 };
63
64 /// @brief Constructor for opus_encoder_wrapper. initializes and configures the opus encoder.
65 DCA_INLINE opus_encoder_wrapper() {
66 int32_t error{};
67 ptr.reset(opus_encoder_create(sampleRate, nChannels, OPUS_APPLICATION_AUDIO, &error));
68
69 // set opus signal type to music
70 auto result = opus_encoder_ctl(ptr.get(), OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC));
71 if (result != OPUS_OK) {
72 throw dca_exception{ "Failed to set the opus signal type, reason: " + jsonifier::string{ opus_strerror(result) } };
73 }
74
75 // set opus bitrate to maximum
76 result = opus_encoder_ctl(ptr.get(), OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
77 if (result != OPUS_OK) {
78 throw dca_exception{ "Failed to set the opus bitrate, reason: " + jsonifier::string{ opus_strerror(result) } };
79 }
80 }
81
82 /// @brief Encode opus audio data.
83 /// @param inputFrame the audio data to encode.
84 /// @return encoded data and sample count.
85 /// @throws dca_exception if encoding fails.
86 DCA_INLINE encoder_return_data encodeData(jsonifier::string_view_base<uint8_t> inputFrame) {
87 if (inputFrame.size() == 0) {
88 return {};
89 }
90
91 if (encodedData.size() == 0) {
93 }
94 if (resampleVector.size() < inputFrame.size() / 2) {
95 resampleVector.resize(inputFrame.size() / 2);
96 }
97 std::memcpy(resampleVector.data(), inputFrame.data(), inputFrame.size());
98 uint64_t sampleCount = inputFrame.size() / 2 / 2;
99 int32_t count = opus_encode(ptr.get(), resampleVector.data(), static_cast<int32_t>(inputFrame.size() / 2 / 2), encodedData.data(), maxBufferSize);
100 if (count <= 0) {
101 throw dca_exception{ "Failed to encode the bitstream, reason: " + jsonifier::string{ opus_strerror(count) } };
102 }
103
104 encoder_return_data returnData{};
105 returnData.sampleCount = sampleCount;
106 returnData.data = jsonifier::string_view_base<uint8_t>{ encodedData.data(), encodedData.size() };
107 return returnData;
108 }
109
110 protected:
111 unique_ptr<OpusEncoder, opus_encoder_deleter> ptr{};///< Unique pointer to OpusEncoder instance.
112 jsonifier::vector<opus_int16> resampleVector{};///< For properly copying the values without type-punning.
113 jsonifier::string_base<uint8_t> encodedData{};///< Buffer for encoded audio data.
114 static constexpr uint64_t maxBufferSize{ 1276 };///< Maximum size of the encoded data buffer.
115 static constexpr int64_t sampleRate{ 48000 };///< Sample rate of the audio data.
116 static constexpr int64_t nChannels{ 2 };///< Number of audio channels.
117 };
118
119 /**@}*/
120
121 }
122
123}
The main namespace for the forward-facing interfaces.
Structure to hold the encoded data and sample count returned by the encoder.
jsonifier::string_view_base< uint8_t > data
Encoded data.
uint64_t sampleCount
Number of audio samples in the input frame.
DCA_INLINE void operator()(OpusEncoder *other)
Operator to destroy an OpusEncoder instance.
unique_ptr< OpusEncoder, opus_encoder_deleter > ptr
Unique pointer to OpusEncoder instance.
jsonifier::vector< opus_int16 > resampleVector
For properly copying the values without type-punning.
static constexpr uint64_t maxBufferSize
Maximum size of the encoded data buffer.
static constexpr int64_t sampleRate
Sample rate of the audio data.
DCA_INLINE opus_encoder_wrapper()
Constructor for opus_encoder_wrapper. initializes and configures the opus encoder.
static constexpr int64_t nChannels
Number of audio channels.
jsonifier::string_base< uint8_t > encodedData
Buffer for encoded audio data.
DCA_INLINE encoder_return_data encodeData(jsonifier::string_view_base< uint8_t > inputFrame)
Encode opus audio data.