| 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdbool.h> |
| 4 | |
| 5 | // hide miniflac compiler warnings |
| 6 | #ifdef _MSC_VER |
| 7 | #pragma warning(disable: 4146) |
| 8 | #pragma warning(disable: 4201) |
| 9 | #pragma warning(disable: 4244) |
| 10 | #pragma warning(disable: 4245) |
| 11 | #pragma warning(disable: 4267) |
| 12 | #pragma warning(disable: 4334) |
| 13 | #define MINIFLAC_IMPLEMENTATION |
| 14 | #include "miniflac.h" |
| 15 | #else |
| 16 | #pragma GCC diagnostic push |
| 17 | #pragma GCC diagnostic ignored "-Wpragmas" |
| 18 | #pragma GCC diagnostic ignored "-Wunknown-warning-option" |
| 19 | #pragma GCC diagnostic ignored "-Wshadow" |
| 20 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 21 | #define MINIFLAC_IMPLEMENTATION |
| 22 | #include "miniflac.h" |
| 23 | #pragma GCC diagnostic pop |
| 24 | #endif |
| 25 | |
| 26 | #include "../pt2_header.h" |
| 27 | #include "../pt2_config.h" |
| 28 | #include "../pt2_structs.h" |
| 29 | #include "../pt2_textout.h" |
| 30 | #include "../pt2_visuals.h" |
| 31 | #include "../pt2_helpers.h" |
| 32 | #include "../pt2_replayer.h" |
| 33 | #include "../pt2_askbox.h" |
| 34 | #include "../pt2_downsample2x.h" |
| 35 | #include "../pt2_audio.h" |
| 36 | |
| 37 | #define MAX_FLAC_BLOCK_SIZE 65535 |
| 38 | |
| 39 | #define INC_BUFFER \ |
| 40 | bytesLeft -= bytesHandled; \ |
| 41 | bufferPtr += bytesHandled; |
| 42 | |
| 43 | static double *dSmpBuf; |
| 44 | static uint8_t numChannels, bitDepth; |
| 45 | static uint64_t totalSamples; |
| 46 | |
| 47 | static bool writeSamples(uint64_t sampleIndex, int32_t **samples, uint32_t numSamples); |
| 48 | |
| 49 | bool loadFLACSample(FILE *f, uint32_t filesize, moduleSample_t *s) |
| 50 | { |
| 51 | int32_t *samples[2] = { NULL }; |
| 52 | uint32_t sampleRate = 44100; |
| 53 | miniflac_t decoder; |
| 54 | |
| 55 | dSmpBuf = NULL; |
| 56 | |
| 57 | uint8_t *fileBuffer = (uint8_t *)malloc(filesize); |
| 58 | if (fileBuffer == NULL) |
| 59 | goto oomError; |
| 60 | |
| 61 | samples[0] = (int32_t *)malloc(sizeof (int32_t) * MAX_FLAC_BLOCK_SIZE); |
| 62 | samples[1] = (int32_t *)malloc(sizeof (int32_t) * MAX_FLAC_BLOCK_SIZE); |
| 63 | if (samples[0] == NULL || samples[1] == NULL) |
| 64 | goto oomError; |
| 65 | |
| 66 | if (fread(fileBuffer, 1, filesize, f) != filesize) |
| 67 | goto decodeError; |
| 68 | |
| 69 | uint8_t *bufferPtr = fileBuffer; |
| 70 | uint32_t bytesHandled, bytesLeft = filesize; |
| 71 | |
| 72 | miniflac_init(&decoder, MINIFLAC_CONTAINER_NATIVE); |
| 73 | if (miniflac_sync(&decoder, bufferPtr, bytesLeft, &bytesHandled) != MINIFLAC_OK) goto decodeError; |
| 74 | INC_BUFFER |
| 75 | |
| 76 | int32_t loopStart = 0, loopLength = 2; |
| 77 | s->volume = 64; |
| 78 | |
| 79 | while (decoder.state == MINIFLAC_METADATA) |
| 80 | { |
| 81 | if (decoder.metadata.header.type == MINIFLAC_METADATA_STREAMINFO) |
| 82 | { |
| 83 | if (miniflac_streaminfo_sample_rate(&decoder, bufferPtr, bytesLeft, &bytesHandled, &sampleRate) != MINIFLAC_OK) goto decodeError; |
| 84 | INC_BUFFER |
| 85 | if (miniflac_streaminfo_channels(&decoder, bufferPtr, bytesLeft, &bytesHandled, &numChannels) != MINIFLAC_OK) goto decodeError; |
| 86 | INC_BUFFER |
| 87 | if (miniflac_streaminfo_bps(&decoder, bufferPtr, bytesLeft, &bytesHandled, &bitDepth) != MINIFLAC_OK) goto decodeError; |
| 88 | INC_BUFFER |
| 89 | if (miniflac_streaminfo_total_samples(&decoder, bufferPtr, bytesLeft, &bytesHandled, &totalSamples) != MINIFLAC_OK) goto decodeError; |
| 90 | INC_BUFFER |
| 91 | } |
| 92 | else if (decoder.metadata.header.type == MINIFLAC_METADATA_APPLICATION && !memcmp(bufferPtr, "riff", 4)) |
| 93 | { |
| 94 | const uint8_t *data = bufferPtr + 4; |
| 95 | |
| 96 | uint32_t chunkID = *(uint32_t *)data; data += 4; |
| 97 | uint32_t chunkLen = *(uint32_t *)data; data += 4; |
| 98 | |
| 99 | if (chunkID == 0x61727478 && chunkLen >= 8) // "xtra" |
| 100 | { |
| 101 | data += 6; |
| 102 | |
| 103 | // volume (0..256) |
| 104 | uint16_t tmpVol = *(uint16_t *)data; |
| 105 | if (tmpVol > 256) |
| 106 | tmpVol = 256; |
| 107 | |
| 108 | s->volume = (uint8_t)((tmpVol + 2) / 4); // 0..256 -> 0..64 (rounded) |
| 109 | } |
| 110 | |
| 111 | if (chunkID == 0x6C706D73 && chunkLen > 52) // "smpl" |
| 112 | { |
| 113 | data += 28; // seek to first wanted byte |
| 114 | |
| 115 | uint32_t numLoops = *(uint32_t *)data; data += 4; |
| 116 | if (numLoops == 1) |
| 117 | { |
| 118 | data += 4+4+4; // skip "samplerData", "identifier" and "loopType" |
| 119 | |
| 120 | loopStart = *(uint32_t *)data; data += 4; |
| 121 | uint32_t loopEnd = *(uint32_t *)data; data += 4; |
| 122 | |
| 123 | loopLength = (loopEnd+1) - loopStart; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (miniflac_sync_native(&decoder, bufferPtr, bytesLeft, &bytesHandled) != MINIFLAC_OK) break; |
| 129 | INC_BUFFER |
| 130 | } |
| 131 | |
| 132 | if (totalSamples == 0 || numChannels == 0) |
| 133 | goto decodeError; |
| 134 | |
| 135 | if (numChannels > 2 || (bitDepth != 8 && bitDepth != 16 && bitDepth != 24)) |
| 136 | { |
| 137 | displayErrorMsg("UNSUPPORTED FLAC !"); |
| 138 | goto error; |
| 139 | } |
| 140 | |
| 141 | bool downSample = false; |
| 142 | if (sampleRate > 22050 && !config.noDownsampleOnSmpLoad) |
| 143 | { |
| 144 | if (askBox(ASKBOX_DOWNSAMPLE, "DOWNSAMPLE ?")) |
| 145 | downSample = true; |
| 146 | } |
| 147 | |
| 148 | if (totalSamples > (uint64_t)(config.maxSampleLength*2)) |
| 149 | totalSamples = config.maxSampleLength*2; |
| 150 | |
| 151 | dSmpBuf = (double *)calloc(totalSamples, sizeof (double)); |
| 152 | if (dSmpBuf == NULL) |
| 153 | goto oomError; |
| 154 | |
| 155 | int64_t sampleIndex = 0; |
| 156 | while (true) |
| 157 | { |
| 158 | if (miniflac_decode(&decoder, bufferPtr, bytesLeft, &bytesHandled, samples) != MINIFLAC_OK) break; |
| 159 | INC_BUFFER |
| 160 | |
| 161 | const int32_t numSamples = decoder.frame.header.block_size; |
| 162 | if (!writeSamples(sampleIndex, samples, numSamples)) break; |
| 163 | sampleIndex += numSamples; |
| 164 | |
| 165 | if (miniflac_sync_native(&decoder, bufferPtr, bytesLeft, &bytesHandled) != MINIFLAC_OK) break; |
| 166 | INC_BUFFER |
| 167 | } |
| 168 | |
| 169 | int32_t sampleLength = (int32_t)totalSamples; |
| 170 | |
| 171 | if (downSample) |
| 172 | { |
| 173 | downsample2xDouble(dSmpBuf, sampleLength); |
| 174 | sampleLength /= 2; |
| 175 | } |
| 176 | |
| 177 | double dAmp = 0.0; |
| 178 | const double dPeak = getDoublePeak(dSmpBuf, sampleLength); |
| 179 | if (dPeak > 0.0) |
| 180 | dAmp = INT8_MAX / dPeak; |
| 181 | |
| 182 | int8_t *smpDataPtr = &song->sampleData[s->offset]; |
| 183 | |
| 184 | turnOffVoices(); |
| 185 | for (int32_t i = 0; i < sampleLength; i++) |
| 186 | smpDataPtr[i] = (int8_t)round(dSmpBuf[i] * dAmp); |
| 187 | |
| 188 | if (sampleLength & 1) |
| 189 | { |
| 190 | if (++sampleLength > config.maxSampleLength) |
| 191 | sampleLength = config.maxSampleLength; |
| 192 | else |
| 193 | smpDataPtr[sampleLength-1] = 0; |
| 194 | } |
| 195 | |
| 196 | if (downSample) |
| 197 | { |
| 198 | // we already downsampled 2x, so we're half the original length |
| 199 | loopStart >>= 1; |
| 200 | loopLength >>= 1; |
| 201 | } |
| 202 | |
| 203 | loopStart &= ~1; |
| 204 | loopLength &= ~1; |
| 205 | |
| 206 | if (loopLength < 2 || loopStart+loopLength > sampleLength) |
| 207 | { |
| 208 | loopStart = 0; |
| 209 | loopLength = 2; |
| 210 | } |
| 211 | |
| 212 | s->fineTune = 0; |
| 213 | s->loopStart = loopStart; |
| 214 | s->loopLength = loopLength; |
| 215 | s->length = sampleLength; |
| 216 | |
| 217 | if (dSmpBuf != NULL) { free(dSmpBuf); dSmpBuf = NULL; } |
| 218 | if (fileBuffer != NULL) free(fileBuffer); |
| 219 | if (samples[0] != NULL) free(samples[0]); |
| 220 | if (samples[1] != NULL) free(samples[1]); |
| 221 | |
| 222 | return true; |
| 223 | |
| 224 | decodeError: |
| 225 | displayErrorMsg("FLAC LOAD ERROR !"); |
| 226 | goto error; |
| 227 | oomError: |
| 228 | statusOutOfMemory(); |
| 229 | error: |
| 230 | if (dSmpBuf != NULL) { free(dSmpBuf); dSmpBuf = NULL; } |
| 231 | if (fileBuffer != NULL) free(fileBuffer); |
| 232 | if (samples[0] != NULL) free(samples[0]); |
| 233 | if (samples[1] != NULL) free(samples[1]); |
| 234 | |
| 235 | return false; |
| 236 | |
| 237 | (void)filesize; |
| 238 | } |
| 239 | |
| 240 | static bool writeSamples(uint64_t sampleIndex, int32_t **samples, uint32_t numSamples) |
| 241 | { |
| 242 | if (sampleIndex >= totalSamples) |
| 243 | return false; |
| 244 | |
| 245 | uint32_t samplesTodo = numSamples; |
| 246 | if (sampleIndex+samplesTodo > totalSamples) |
| 247 | samplesTodo = totalSamples - sampleIndex; |
| 248 | |
| 249 | double dMul = 1.0 / (1 << (bitDepth-1)); |
| 250 | double *dPtr = (double *)dSmpBuf + sampleIndex; |
| 251 | |
| 252 | if (numChannels == 1) |
| 253 | { |
| 254 | int32_t *src32 = samples[0]; |
| 255 | for (uint32_t i = 0; i < samplesTodo; i++) |
| 256 | dPtr[i] = (double)(src32[i] * dMul); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | dMul *= 0.5; |
| 261 | |
| 262 | int32_t *src32L = samples[0], *src32R = samples[1]; |
| 263 | for (uint32_t i = 0; i < samplesTodo; i++) |
| 264 | dPtr[i] = (double)((src32L[i] + src32R[i]) * dMul); |
| 265 | } |
| 266 | |
| 267 | return true; |
| 268 | } |