| 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 <cstdint> |
| 20 | #include <cstdio> |
| 21 | |
| 22 | #include "aencode.h" |
| 23 | #include "audio_encode.h" |
| 24 | #include "mono.h" |
| 25 | |
| 26 | int32_t aenc_ReadSamp(void *data) { |
| 27 | FILE *f = (FILE *)data; |
| 28 | int a, b; |
| 29 | a = getc(f); |
| 30 | if (a == EOF) |
| 31 | return ReadSampleEof; |
| 32 | |
| 33 | b = getc(f); |
| 34 | if (b == EOF) |
| 35 | return ReadSampleEof; |
| 36 | return (b << 8) | a; |
| 37 | } |
| 38 | |
| 39 | bool aenc_Compress(char *input_filename, char *output_filename, const int *input_levels, const int *input_samples, |
| 40 | const int *input_rate, const int *input_channels, const float *input_factor, |
| 41 | const float *input_volscale) { |
| 42 | FILE *in, *out; |
| 43 | int32_t result; |
| 44 | |
| 45 | int levels = 0, samples_per_subband = 0; |
| 46 | unsigned sample_rate = 0, channels = 0; |
| 47 | float factor = 0, volume_scale = 0; |
| 48 | int levels_set = 0, samples_per_subband_set = 0, sample_rate_set = 0, channels_set = 0, factor_set = 0, |
| 49 | volume_scale_set = 0; |
| 50 | |
| 51 | in = fopen(input_filename, "rb"); |
| 52 | if (!in) { |
| 53 | mprintf(0, "AENC: Unable to open %s for input.\n", input_filename); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if (input_levels) { |
| 58 | levels = *input_levels; // Levels (default 7 or for 2k total) |
| 59 | levels_set = 1; |
| 60 | if (levels < 0 || levels > 16) { |
| 61 | mprintf(0, "AENC: Warning: levels outside of the range 0 to 16\n"); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (input_samples) { |
| 66 | samples_per_subband = *input_samples; // Samples per subband (default 16 or for 2k total) |
| 67 | samples_per_subband_set = 1; |
| 68 | if (samples_per_subband < 1 || samples_per_subband > 1024) { |
| 69 | mprintf(0, "AENC: Warning: samples per subband not in the range 1 to 1024\n"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (input_rate) { |
| 74 | sample_rate = *input_rate; // Sample rate (default 22K) |
| 75 | sample_rate_set = 1; |
| 76 | if (sample_rate != 11025 && sample_rate != 22050 && sample_rate != 44100) { |
| 77 | mprintf(0, "AENC: Warning: sample rate not 11025, 22050, or 44100\n"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (input_channels) { |
| 82 | channels = *input_channels; |
| 83 | channels_set = 1; |
| 84 | if (channels != 1 && channels != 2) { |
| 85 | mprintf(0, "AENC: Warning: /C channels not 1 or 2\n"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (input_factor) { |
| 90 | factor = *input_factor; // Factor of compression (default 4 for 22K, 8 for 44K) |
| 91 | factor_set = 1; |
| 92 | |
| 93 | if (factor != 0.0f && factor < 1.0f) |
| 94 | factor = 1.0f / factor; |
| 95 | |
| 96 | if (factor <= 0.0f) { |
| 97 | mprintf(0, "AENC: Warning: compression factor <= 0.0\n"); |
| 98 | factor = 1.0f; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (input_volscale) { |
| 103 | volume_scale = *input_volscale; // Volume scaling (slightly <= 1.0, default ,97) |
| 104 | volume_scale_set = 1; |
| 105 | } |
| 106 | |
| 107 | if (!levels_set && !samples_per_subband_set) { |
| 108 | levels = 7; |
| 109 | samples_per_subband = 16; |
| 110 | } else if (!samples_per_subband_set) { |
| 111 | samples_per_subband = 2048 / (1 << levels); |
| 112 | |
| 113 | if (samples_per_subband < 8) |
| 114 | samples_per_subband = 8; |
| 115 | } else if (!levels_set) { |
| 116 | unsigned subbands = (2048 / samples_per_subband) >> 1; |
| 117 | |
| 118 | for (levels = 0; subbands; subbands >>= 1, ++levels) { |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!sample_rate_set) |
| 123 | sample_rate = 22050; |
| 124 | if (!channels_set) |
| 125 | channels = 1; |
| 126 | if (!factor_set) |
| 127 | factor = sample_rate <= 22050 ? 4.0f : 8.0f; |
| 128 | if (!volume_scale_set) |
| 129 | volume_scale = .97f; |
| 130 | |
| 131 | out = fopen(output_filename, "wb"); |
| 132 | if (!out) { |
| 133 | mprintf(0, "AENC: Unable to open %s for output.\n", output_filename); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | result = AudioEncode(aenc_ReadSamp, in, channels, sample_rate, volume_scale, out, levels, samples_per_subband, |
| 138 | 1.0f / factor); |
| 139 | |
| 140 | fclose(out); |
| 141 | fclose(in); |
| 142 | |
| 143 | return result > 0; |
| 144 | } |