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