| 1 | /* |
| 2 | * Copyright (C) 2002-2024 D2X Project |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include "byteswap.h" |
| 22 | |
| 23 | static int16_t audio_exp_table[256] = { |
| 24 | 0, 1, 2, 3, 4, 5, 6, 7, // 8 |
| 25 | 8, 9, 10, 11, 12, 13, 14, 15, // |
| 26 | 16, 17, 18, 19, 20, 21, 22, 23, // |
| 27 | 24, 25, 26, 27, 28, 29, 30, 31, // |
| 28 | 32, 33, 34, 35, 36, 37, 38, 39, // |
| 29 | 40, 41, 42, 43, 47, 51, 56, 61, // |
| 30 | 66, 72, 79, 86, 94, 102, 112, 122, // |
| 31 | 133, 145, 158, 173, 189, 206, 225, 245, // 64 |
| 32 | 267, 292, 318, 348, 379, 414, 452, 493, // |
| 33 | 538, 587, 640, 699, 763, 832, 908, 991, // |
| 34 | 1081, 1180, 1288, 1405, 1534, 1673, 1826, 1993, // |
| 35 | 2175, 2373, 2590, 2826, 3084, 3365, 3672, 4008, // |
| 36 | 4373, 4772, 5208, 5683, 6202, 6767, 7385, 8059, // |
| 37 | 8794, 9597, 10472, 11428, 12471, 13609, 14851, 16206, // |
| 38 | 17685, 19298, 21060, 22981, 25078, 27367, 29864, 32589, // |
| 39 | -29973, -26728, -23186, -19322, -15105, -10503, -5481, -1, // 128 |
| 40 | 1, 1, 5481, 10503, 15105, 19322, 23186, 26728, // |
| 41 | 29973, -32589, -29864, -27367, -25078, -22981, -21060, -19298, // |
| 42 | -17685, -16206, -14851, -13609, -12471, -11428, -10472, -9597, // |
| 43 | -8794, -8059, -7385, -6767, -6202, -5683, -5208, -4772, // |
| 44 | -4373, -4008, -3672, -3365, -3084, -2826, -2590, -2373, // |
| 45 | -2175, -1993, -1826, -1673, -1534, -1405, -1288, -1180, // |
| 46 | -1081, -991, -908, -832, -763, -699, -640, -587, // |
| 47 | -538, -493, -452, -414, -379, -348, -318, -292, // 196 |
| 48 | -267, -245, -225, -206, -189, -173, -158, -145, // |
| 49 | -133, -122, -112, -102, -94, -86, -79, -72, // |
| 50 | -66, -61, -56, -51, -47, -43, -42, -41, // |
| 51 | -40, -39, -38, -37, -36, -35, -34, -33, // |
| 52 | -32, -31, -30, -29, -28, -27, -26, -25, // |
| 53 | -24, -23, -22, -21, -20, -19, -18, -17, // |
| 54 | -16, -15, -14, -13, -12, -11, -10, -9, // |
| 55 | -8, -7, -6, -5, -4, -3, -2, -1, // 256 |
| 56 | }; |
| 57 | |
| 58 | static int16_t getWord(unsigned char **fin) { |
| 59 | int16_t value = D3::convert_le<int16_t>(((*fin)[1] << 8) | (*fin)[0]); |
| 60 | *fin += 2; |
| 61 | return value; |
| 62 | } |
| 63 | |
| 64 | void mveaudio_process(char *buffer, unsigned char *data, int sample_size, bool is_compressed) { |
| 65 | if (is_compressed) { |
| 66 | int nCurOffsets[2]; |
| 67 | |
| 68 | data += 4; |
| 69 | int samples = getWord(&data) / sample_size; |
| 70 | // Fill predictors |
| 71 | nCurOffsets[0] = getWord(&data); |
| 72 | nCurOffsets[1] = getWord(&data); |
| 73 | |
| 74 | for (int i = 0; i < samples; i++) { |
| 75 | nCurOffsets[i & 1] = std::clamp(nCurOffsets[i & 1] + audio_exp_table[data[i]], -32768, 32767); |
| 76 | memcpy(buffer + i * sample_size, &nCurOffsets[i & 1], sample_size); |
| 77 | } |
| 78 | } else { |
| 79 | data += 2; |
| 80 | int size = getWord(&data); |
| 81 | memcpy(buffer, &data, size); |
| 82 | } |
| 83 | } |