| 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"cxx . . $ `<file>/AudioEncode/adecode.h`/ |
| 20 | #include "libacm.h"cxx . . $ `<file>/third_party/libacm/libacm.h`/ |
| 21 | |
| 22 | using namespace AudioDecoder; |
| 23 | |
| 24 | namespacecxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/ { |
| 25 | |
| 26 | class InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder# : public IAudioDecodercxx . . $ AudioDecoder/IAudioDecoder# { |
| 27 | public: |
| 28 | InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#InternalAudioDecoder(a712b2d794fcc98d).(ReadDataFunctioncxx . . $ AudioDecoder/ReadDataFunction# readerFunction, void *pReaderData); |
| 29 | ~cxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#`~InternalAudioDecoder`(ced63f7c635d850d).InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#(); |
| 30 | |
| 31 | // Initialize the decoder |
| 32 | bool Initializecxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#Initialize(c9857a73d68e59ba).(); |
| 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_tcxx . . $ uint32_t# Readcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#Read(35feec062aeef876).(void *pBuffer, uint32_tcxx . . $ uint32_t# amount); |
| 39 | |
| 40 | ACMStream *m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_acm. = nullptr; |
| 41 | ReadDataFunctioncxx . . $ AudioDecoder/ReadDataFunction# m_readerFunctioncxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_readerFunction.; |
| 42 | void *m_pReaderDatacxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_pReaderData.; |
| 43 | }; |
| 44 | |
| 45 | /**************************************************************/ |
| 46 | /* Construction */ |
| 47 | /**************************************************************/ |
| 48 | |
| 49 | int AcmReadFunccxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/AcmReadFunc(ad9295b7f5fafbd2).(void *ptr, int size, int n, void *datasrc) { |
| 50 | InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder# *iad = reinterpret_cast<InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder# *>(datasrc); |
| 51 | int ret = iad->m_readerFunctioncxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_readerFunction.(iad->m_pReaderDatacxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_pReaderData., ptr, (uint32_tcxx . . $ 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 | InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#::InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#InternalAudioDecoder(a712b2d794fcc98d).(ReadDataFunctioncxx . . $ AudioDecoder/ReadDataFunction# readerFunction, void *pReaderData) |
| 63 | : m_readerFunctioncxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_readerFunction.(readerFunction), m_pReaderDatacxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_pReaderData.(pReaderData) {} |
| 64 | |
| 65 | // Initialize the decoder |
| 66 | bool InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#::Initializecxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#Initialize(c9857a73d68e59ba).() { |
| 67 | |
| 68 | // set the read function, the others are optional |
| 69 | acm_io_callbacks io = {AcmReadFunccxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/AcmReadFunc(ad9295b7f5fafbd2).}; |
| 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_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_acm., this, io, force_channels); |
| 74 | return ret == ACM_OKcxx . . $ `third_party/libacm/libacm.h:31:9`!; |
| 75 | } |
| 76 | |
| 77 | /**************************************************************/ |
| 78 | /* Destruction */ |
| 79 | /**************************************************************/ |
| 80 | |
| 81 | InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#::~cxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#`~InternalAudioDecoder`(ced63f7c635d850d).InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#() { |
| 82 | if (m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_acm. != nullptr) |
| 83 | acm_close(m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#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_tcxx . . $ uint32_t# InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#::Readcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#Read(35feec062aeef876).(void *pBuffer, uint32_tcxx . . $ 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_tcxx . . $ uint32_t# totalBytesRead = 0; |
| 99 | uint8_tcxx . . $ uint8_t# *pBuf = reinterpret_cast<uint8_tcxx . . $ uint8_t# *>(pBuffer); |
| 100 | |
| 101 | while (totalBytesRead < amount) { |
| 102 | int numRead = acm_read(m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#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 | IAudioDecodercxx . . $ AudioDecoder/IAudioDecoder# *AudioDecodercxx . . $ AudioDecoder/::CreateDecodercxx . . $ AudioDecoder/CreateDecoder(8b054913a2e04880).(ReadDataFunctioncxx . . $ AudioDecoder/ReadDataFunction# readerFunction, void *pReaderData, uint32_tcxx . . $ uint32_t# &numChannels, |
| 128 | uint32_tcxx . . $ uint32_t# &sampleRate, uint32_tcxx . . $ uint32_t# &sampleCount) { |
| 129 | // allocate our decoder |
| 130 | InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder# *pDecoder = new InternalAudioDecodercxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#(readerFunction, pReaderData); |
| 131 | if (pDecoder == nullptr) |
| 132 | return nullptr; |
| 133 | |
| 134 | // initialize |
| 135 | if (!pDecoder->Initializecxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#Initialize(c9857a73d68e59ba).()) { |
| 136 | // Failed |
| 137 | delete pDecoder; |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | // extract the header information for the caller |
| 142 | numChannels = pDecoder->m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_acm.->info.channels; |
| 143 | sampleRate = pDecoder->m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_acm.->info.rate; |
| 144 | sampleCount = pDecoder->m_acmcxx . . $ `$anonymous_namespace_AudioEncode/adecode.cpp`/InternalAudioDecoder#m_acm.->total_values; |
| 145 | |
| 146 | // return the decoder back to the user |
| 147 | return pDecoder; |
| 148 | } |