Debian Codegraph @

AudioEncode/adecode.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "adecode.h"
20 #include "libacm.h"
21
22 using namespace AudioDecoder;
23
24 namespace {
25
26 class InternalAudioDecoder : public IAudioDecoder {
27 public:
28 InternalAudioDecoder(ReadDataFunction readerFunction, void *pReaderData);
29 ~InternalAudioDecoder();
30
31 // Initialize the decoder
32 bool Initialize();
33
34 // Read data from the audio decoder.
35 // pBuffer: The buffer to receive the data into
36 // amount: How much data to read
37 // Returns the number of bytes read - zero when we're at the end of the file
38 uint32_t Read(void *pBuffer, uint32_t amount);
39
40 ACMStream *m_acm = nullptr;
41 ReadDataFunction m_readerFunction;
42 void *m_pReaderData;
43 };
44
45 /**************************************************************/
46 /* Construction */
47 /**************************************************************/
48
49 int AcmReadFunc(void *ptr, int size, int n, void *datasrc) {
50 InternalAudioDecoder *iad = reinterpret_cast<InternalAudioDecoder *>(datasrc);
51 int ret = iad->m_readerFunction(iad->m_pReaderData, ptr, (uint32_t)size * n);
52 // ret < 0: error, ret == 0: EOF, ret > 0: read ret bytes of data
53 // apparently acm_io_callbacks::read() expects pretty much the same behavior,
54 // except that for > 0 it's not number of bytes but number of items (like in
55 // fread())
56 if (ret > 0) {
57 ret /= size;
58 }
59 return ret;
60 }
61
62 InternalAudioDecoder::InternalAudioDecoder(ReadDataFunction readerFunction, void *pReaderData)
63 : m_readerFunction(readerFunction), m_pReaderData(pReaderData) {}
64
65 // Initialize the decoder
66 bool InternalAudioDecoder::Initialize() {
67
68 // set the read function, the others are optional
69 acm_io_callbacks io = {AcmReadFunc};
70
71 // force_channels 0 means libacm will use number of chans from ACM file header
72 const int force_channels = 0;
73 int ret = acm_open_decoder(&m_acm, this, io, force_channels);
74 return ret == ACM_OK;
75 }
76
77 /**************************************************************/
78 /* Destruction */
79 /**************************************************************/
80
81 InternalAudioDecoder::~InternalAudioDecoder() {
82 if (m_acm != nullptr)
83 acm_close(m_acm);
84 }
85
86 /**************************************************************/
87 /* Reading */
88 /**************************************************************/
89
90 // Read data from the audio decoder.
91 // pBuffer: The buffer to receive the data into
92 // amount: How much data to read
93 // Returns the number of bytes read - zero when we're at the end of the file
94 uint32_t InternalAudioDecoder::Read(void *pBuffer, uint32_t amount) {
95 const int bigendianp = 0; // we want little endian samples - TODO: or only on little endian platforms?
96 const int wordlen = 2; // the only supported value
97 const int sgned = 1; // we want signed samples
98 uint32_t totalBytesRead = 0;
99 uint8_t *pBuf = reinterpret_cast<uint8_t *>(pBuffer);
100
101 while (totalBytesRead < amount) {
102 int numRead = acm_read(m_acm, pBuf, amount - totalBytesRead, bigendianp, wordlen, sgned);
103 // numRead < 0: error, numRead == 0: EOF, numRead > 0: amount of bytes read
104 if (numRead <= 0)
105 break;
106 totalBytesRead += numRead;
107 pBuf += numRead;
108 }
109
110 return totalBytesRead;
111 }
112
113 } // namespace
114
115 /**************************************************************/
116 /* Interface Functions */
117 /**************************************************************/
118
119 // Create an audio decoder
120 // You supply a function for reading bytes from the compressed data via a
121 // void* pData handle, and the handle itself (typically a FILE *).
122 // Create_AudioDecoder returns a new AudioDecoder which can be used to
123 // read uncompressed decoded data from the compressed stream,
124 // and also returns the number of channels (1 or 2), the sample rate
125 // (e.g. 22050), and the number of samples contained in the compressed file
126 // (in case you want to pre-allocate a buffer to load them all into memory).
127 IAudioDecoder *AudioDecoder::CreateDecoder(ReadDataFunction readerFunction, void *pReaderData, uint32_t &numChannels,
128 uint32_t &sampleRate, uint32_t &sampleCount) {
129 // allocate our decoder
130 InternalAudioDecoder *pDecoder = new InternalAudioDecoder(readerFunction, pReaderData);
131 if (pDecoder == nullptr)
132 return nullptr;
133
134 // initialize
135 if (!pDecoder->Initialize()) {
136 // Failed
137 delete pDecoder;
138 return nullptr;
139 }
140
141 // extract the header information for the caller
142 numChannels = pDecoder->m_acm->info.channels;
143 sampleRate = pDecoder->m_acm->info.rate;
144 sampleCount = pDecoder->m_acm->total_values;
145
146 // return the decoder back to the user
147 return pDecoder;
148 }