utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
mp3_decoder.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22#define DR_MP3_IMPLEMENTATION
23#include <dr_mp3.h>
24#include "utility/sound/decoder/mp3_decoder.hpp"
25
27
28namespace utility::sound
29{
30 static utility::logging::DefaultLogger mp3DecoderLogger("MP3Decoder");
31}
32
34{
35 mp3DecoderLogger.debug() << "MP3 decoder initialized";
36}
37
39{
40 mp3DecoderLogger.debug() << "MP3 decoder destroyed";
41}
42
43bool utility::sound::MP3Decoder::canDecode(const std::string &filePath) const
44{
45 // Check if the file extension is .mp3
46 auto extension = filePath.substr(filePath.find_last_of(".") + 1);
47 return extension == "mp3";
48}
49
50bool utility::sound::MP3Decoder::canDecode(AudioDecoderType type) const
51{
52 return type == AudioDecoderType::MP3;
53}
54
55std::shared_ptr<utility::sound::AudioBuffer>
56 utility::sound::MP3Decoder::decode(std::shared_ptr<utility::File> file)
57{
58 auto audioBuffer = std::make_shared<AudioBuffer>();
59 DecodedAudio decodedAudio;
60 drmp3 mp3 = {};
61
62 if (drmp3_init_memory(&mp3, file->data().data(), file->size(), nullptr)
63 == DRMP3_FALSE) {
64 // Log error but don't throw - return nullptr
65 return nullptr;
66 }
67
68 const drmp3_uint64 frameCount = drmp3_get_pcm_frame_count(&mp3);
69 if (frameCount == 0) {
70 drmp3_uninit(&mp3);
71 return nullptr;
72 }
73 decodedAudio.sampleRate = mp3.sampleRate;
74 decodedAudio.channels = mp3.channels;
75 decodedAudio.bitsPerSample = 16;
76
77 decodedAudio.format =
78 getALFormat(decodedAudio.channels, decodedAudio.bitsPerSample);
79
80 const size_t totalSampleCount = frameCount * mp3.channels;
81
82 std::vector<drmp3_int16> pcm(totalSampleCount);
83
84 const drmp3_uint64 samplesDecoded =
85 drmp3_read_pcm_frames_s16(&mp3, frameCount, pcm.data());
86
87 drmp3_uninit(&mp3);
88
89 if (samplesDecoded == 0) {
90 return nullptr;
91 }
92 decodedAudio.samples.resize(samplesDecoded * mp3.channels
93 * sizeof(drmp3_int16));
94 std::memcpy(decodedAudio.samples.data(), pcm.data(),
95 decodedAudio.samples.size());
96
97 audioBuffer->upload(decodedAudio);
98 return audioBuffer;
99}
LogMessage debug(std::source_location loc=std::source_location::current())
Begin a debug-level log message.
Definition logger.hpp:336
Standard output/error logger implementation.
bool canDecode(const std::string &filePath) const override
Determines whether the specified file can be decoded.
std::shared_ptr< AudioBuffer > decode(std::shared_ptr< utility::File > file) override
Decodes an MP3 file into an AudioBuffer.
~MP3Decoder() override
Destroys the MP3 decoder instance.
MP3Decoder()
Creates an MP3 decoder instance.
Platform-specific default logger alias.
Contains decoded Pulse-Code Modulation (PCM) audio data.
uint16_t bitsPerSample
Number of bits used to represent each sample.
uint16_t channels
Number of audio channels.
std::vector< std::byte > samples
Raw PCM sample data.
ALenum format
OpenAL format corresponding to the decoded audio data.
uint32_t sampleRate
Audio sample rate in hertz.