Debian Codegraph @

src/smploaders/pt2_load_flac.c

raw symbols SCIP
1 #cxx . . $ `<file>/src/smploaders/pt2_load_flac.c`/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_IMPLEMENTATIONcxx . . $ `src/smploaders/pt2_load_flac.c:21:9`!
22 #include "miniflac.h"cxx . . $ `<file>/src/smploaders/miniflac.h`/
23 #pragma GCC diagnostic pop
24 #endif
25
26 #include "../pt2_header.h"cxx . . $ `<file>/src/pt2_header.h`/
27 #include "../pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/
28 #include "../pt2_structs.h"cxx . . $ `<file>/src/pt2_structs.h`/
29 #include "../pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/
30 #include "../pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/
31 #include "../pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/
32 #include "../pt2_replayer.h"cxx . . $ `<file>/src/pt2_replayer.h`/
33 #include "../pt2_askbox.h"cxx . . $ `<file>/src/pt2_askbox.h`/
34 #include "../pt2_downsample2x.h"cxx . . $ `<file>/src/pt2_downsample2x.h`/
35 #include "../pt2_audio.h"cxx . . $ `<file>/src/pt2_audio.h`/
36
37 #define MAX_FLAC_BLOCK_SIZEcxx . . $ `src/smploaders/pt2_load_flac.c:37:9`! 65535
38
39 #define INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`! \
40 bytesLeft -= bytesHandled; \
41 bufferPtr += bytesHandled;
42
43 static double *dSmpBufcxx . . $ dSmpBuf.;
44 static uint8_tcxx . . $ uint8_t# numChannelscxx . . $ numChannels., bitDepthcxx . . $ bitDepth.;
45 static uint64_tcxx . . $ uint64_t# totalSamplescxx . . $ totalSamples.;
46
47 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! writeSamplescxx . . $ writeSamples(dbca5d4557f57443).(uint64_tcxx . . $ uint64_t# sampleIndex, int32_tcxx . . $ int32_t# **samples, uint32_tcxx . . $ uint32_t# numSamples);
48
49 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! loadFLACSamplecxx . . $ loadFLACSample(85a5b89df4a007d2).(FILEcxx . . $ FILE# *f, uint32_tcxx . . $ uint32_t# filesize, moduleSample_tcxx . . $ moduleSample_t# *s)
50 {
51 int32_tcxx . . $ int32_t# *samples[2] = { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! };
52 uint32_tcxx . . $ uint32_t# sampleRate = 44100;
53 miniflac_tcxx . . $ miniflac_t# decoder;
54
55 dSmpBufcxx . . $ dSmpBuf. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
56
57 uint8_tcxx . . $ uint8_t# *fileBuffer = (uint8_tcxx . . $ uint8_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(filesize);
58 if (fileBuffer == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
59 goto oomError;
60
61 samples[0] = (int32_tcxx . . $ int32_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof (int32_tcxx . . $ int32_t#) * MAX_FLAC_BLOCK_SIZEcxx . . $ `src/smploaders/pt2_load_flac.c:37:9`!);
62 samples[1] = (int32_tcxx . . $ int32_t# *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof (int32_tcxx . . $ int32_t#) * MAX_FLAC_BLOCK_SIZEcxx . . $ `src/smploaders/pt2_load_flac.c:37:9`!);
63 if (samples[0] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || samples[1] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
64 goto oomError;
65
66 if (freadcxx . . $ fread(738fc8ac1d711e58).(fileBuffer, 1, filesize, f) != filesize)
67 goto decodeError;
68
69 uint8_tcxx . . $ uint8_t# *bufferPtr = fileBuffer;
70 uint32_tcxx . . $ uint32_t# bytesHandled, bytesLeft = filesize;
71
72 miniflac_initcxx . . $ miniflac_init(ef6ba273102204bd).(&decoder, MINIFLAC_CONTAINER_NATIVEcxx . . $ MINIFLAC_CONTAINER_NATIVE.);
73 if (miniflac_synccxx . . $ miniflac_sync(f0528a776fc83f12).(&decoder, bufferPtr, bytesLeft, &bytesHandled) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) goto decodeError;
74 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
75
76 int32_tcxx . . $ int32_t# loopStart = 0, loopLength = 2;
77 s->volumecxx . . $ moduleSample_t#volume. = 64;
78
79 while (decoder.statecxx . . $ miniflac_s#state. == MINIFLAC_METADATAcxx . . $ MINIFLAC_METADATA.)
80 {
81 if (decoder.metadatacxx . . $ miniflac_s#metadata..headercxx . . $ miniflac_metadata_s#header..typecxx . . $ miniflac_metadata_header_s#type. == MINIFLAC_METADATA_STREAMINFOcxx . . $ MINIFLAC_METADATA_STREAMINFO.)
82 {
83 if (miniflac_streaminfo_sample_ratecxx . . $ miniflac_streaminfo_sample_rate(a37880e9eb81ec66).(&decoder, bufferPtr, bytesLeft, &bytesHandled, &sampleRate) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) goto decodeError;
84 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
85 if (miniflac_streaminfo_channelscxx . . $ miniflac_streaminfo_channels(26c669ce245d484c).(&decoder, bufferPtr, bytesLeft, &bytesHandled, &numChannelscxx . . $ numChannels.) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) goto decodeError;
86 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
87 if (miniflac_streaminfo_bpscxx . . $ miniflac_streaminfo_bps(26c669ce245d484c).(&decoder, bufferPtr, bytesLeft, &bytesHandled, &bitDepthcxx . . $ bitDepth.) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) goto decodeError;
88 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
89 if (miniflac_streaminfo_total_samplescxx . . $ miniflac_streaminfo_total_samples(477ff51243addb12).(&decoder, bufferPtr, bytesLeft, &bytesHandled, &totalSamplescxx . . $ totalSamples.) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) goto decodeError;
90 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
91 }
92 else if (decoder.metadatacxx . . $ miniflac_s#metadata..headercxx . . $ miniflac_metadata_s#header..typecxx . . $ miniflac_metadata_header_s#type. == MINIFLAC_METADATA_APPLICATIONcxx . . $ MINIFLAC_METADATA_APPLICATION. && !memcmpcxx . . $ memcmp(36f949851efa3055).(bufferPtr, "riff", 4))
93 {
94 const uint8_tcxx . . $ uint8_t# *data = bufferPtr + 4;
95
96 uint32_tcxx . . $ uint32_t# chunkID = *(uint32_tcxx . . $ uint32_t# *)data; data += 4;
97 uint32_tcxx . . $ uint32_t# chunkLen = *(uint32_tcxx . . $ uint32_t# *)data; data += 4;
98
99 if (chunkID == 0x61727478 && chunkLen >= 8) // "xtra"
100 {
101 data += 6;
102
103 // volume (0..256)
104 uint16_tcxx . . $ uint16_t# tmpVol = *(uint16_tcxx . . $ uint16_t# *)data;
105 if (tmpVol > 256)
106 tmpVol = 256;
107
108 s->volumecxx . . $ moduleSample_t#volume. = (uint8_tcxx . . $ 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_tcxx . . $ uint32_t# numLoops = *(uint32_tcxx . . $ uint32_t# *)data; data += 4;
116 if (numLoops == 1)
117 {
118 data += 4+4+4; // skip "samplerData", "identifier" and "loopType"
119
120 loopStart = *(uint32_tcxx . . $ uint32_t# *)data; data += 4;
121 uint32_tcxx . . $ uint32_t# loopEnd = *(uint32_tcxx . . $ uint32_t# *)data; data += 4;
122
123 loopLength = (loopEnd+1) - loopStart;
124 }
125 }
126 }
127
128 if (miniflac_sync_nativecxx . . $ miniflac_sync_native(f0528a776fc83f12).(&decoder, bufferPtr, bytesLeft, &bytesHandled) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) break;
129 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
130 }
131
132 if (totalSamplescxx . . $ totalSamples. == 0 || numChannelscxx . . $ numChannels. == 0)
133 goto decodeError;
134
135 if (numChannelscxx . . $ numChannels. > 2 || (bitDepthcxx . . $ bitDepth. != 8 && bitDepthcxx . . $ bitDepth. != 16 && bitDepthcxx . . $ bitDepth. != 24))
136 {
137 displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("UNSUPPORTED FLAC !");
138 goto error;
139 }
140
141 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! downSample = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
142 if (sampleRate > 22050 && !configcxx . . $ config..noDownsampleOnSmpLoadcxx . . $ config_t#noDownsampleOnSmpLoad.)
143 {
144 if (askBoxcxx . . $ askBox(7371ee129cebb502).(ASKBOX_DOWNSAMPLEcxx . . $ ASKBOX_DOWNSAMPLE., "DOWNSAMPLE ?"))
145 downSample = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
146 }
147
148 if (totalSamplescxx . . $ totalSamples. > (uint64_tcxx . . $ uint64_t#)(configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*2))
149 totalSamplescxx . . $ totalSamples. = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.*2;
150
151 dSmpBufcxx . . $ dSmpBuf. = (double *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(totalSamplescxx . . $ totalSamples., sizeof (double));
152 if (dSmpBufcxx . . $ dSmpBuf. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
153 goto oomError;
154
155 int64_tcxx . . $ int64_t# sampleIndex = 0;
156 while (truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!)
157 {
158 if (miniflac_decodecxx . . $ miniflac_decode(349bb1a01608c6da).(&decoder, bufferPtr, bytesLeft, &bytesHandled, samples) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) break;
159 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
160
161 const int32_tcxx . . $ int32_t# numSamples = decoder.framecxx . . $ miniflac_s#frame..headercxx . . $ miniflac_frame_s#header..block_sizecxx . . $ miniflac_frame_header_s#block_size.;
162 if (!writeSamplescxx . . $ writeSamples(dbca5d4557f57443).(sampleIndex, samples, numSamples)) break;
163 sampleIndex += numSamples;
164
165 if (miniflac_sync_nativecxx . . $ miniflac_sync_native(f0528a776fc83f12).(&decoder, bufferPtr, bytesLeft, &bytesHandled) != MINIFLAC_OKcxx . . $ MINIFLAC_OK.) break;
166 INC_BUFFERcxx . . $ `src/smploaders/pt2_load_flac.c:39:9`!
167 }
168
169 int32_tcxx . . $ int32_t# sampleLength = (int32_tcxx . . $ int32_t#)totalSamplescxx . . $ totalSamples.;
170
171 if (downSample)
172 {
173 downsample2xDoublecxx . . $ downsample2xDouble(b832380d3925cf6).(dSmpBufcxx . . $ dSmpBuf., sampleLength);
174 sampleLength /= 2;
175 }
176
177 double dAmp = 0.0;
178 const double dPeak = getDoublePeakcxx . . $ getDoublePeak(fe75d5ffdcd76c3).(dSmpBufcxx . . $ dSmpBuf., sampleLength);
179 if (dPeak > 0.0)
180 dAmp = INT8_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:110:10`! / dPeak;
181
182 int8_tcxx . . $ int8_t# *smpDataPtr = &songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.];
183
184 turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).();
185 for (int32_tcxx . . $ int32_t# i = 0; i < sampleLength; i++)
186 smpDataPtr[i] = (int8_tcxx . . $ int8_t#)roundcxx . . $ round(447c67c6676f75c3).(dSmpBufcxx . . $ dSmpBuf.[i] * dAmp);
187
188 if (sampleLength & 1)
189 {
190 if (++sampleLength > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.)
191 sampleLength = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#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->fineTunecxx . . $ moduleSample_t#fineTune. = 0;
213 s->loopStartcxx . . $ moduleSample_t#loopStart. = loopStart;
214 s->loopLengthcxx . . $ moduleSample_t#loopLength. = loopLength;
215 s->lengthcxx . . $ moduleSample_t#length. = sampleLength;
216
217 if (dSmpBufcxx . . $ dSmpBuf. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { freecxx . . $ free(5d22bdacc48458e8).(dSmpBufcxx . . $ dSmpBuf.); dSmpBufcxx . . $ dSmpBuf. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; }
218 if (fileBuffer != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(fileBuffer);
219 if (samples[0] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(samples[0]);
220 if (samples[1] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(samples[1]);
221
222 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
223
224 decodeError:
225 displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("FLAC LOAD ERROR !");
226 goto error;
227 oomError:
228 statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).();
229 error:
230 if (dSmpBufcxx . . $ dSmpBuf. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { freecxx . . $ free(5d22bdacc48458e8).(dSmpBufcxx . . $ dSmpBuf.); dSmpBufcxx . . $ dSmpBuf. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; }
231 if (fileBuffer != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(fileBuffer);
232 if (samples[0] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(samples[0]);
233 if (samples[1] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(samples[1]);
234
235 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
236
237 (void)filesize;
238 }
239
240 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! writeSamplescxx . . $ writeSamples(dbca5d4557f57443).(uint64_tcxx . . $ uint64_t# sampleIndex, int32_tcxx . . $ int32_t# **samples, uint32_tcxx . . $ uint32_t# numSamples)
241 {
242 if (sampleIndex >= totalSamplescxx . . $ totalSamples.)
243 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
244
245 uint32_tcxx . . $ uint32_t# samplesTodo = numSamples;
246 if (sampleIndex+samplesTodo > totalSamplescxx . . $ totalSamples.)
247 samplesTodo = totalSamplescxx . . $ totalSamples. - sampleIndex;
248
249 double dMul = 1.0 / (1 << (bitDepthcxx . . $ bitDepth.-1));
250 double *dPtr = (double *)dSmpBufcxx . . $ dSmpBuf. + sampleIndex;
251
252 if (numChannelscxx . . $ numChannels. == 1)
253 {
254 int32_tcxx . . $ int32_t# *src32 = samples[0];
255 for (uint32_tcxx . . $ 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_tcxx . . $ int32_t# *src32L = samples[0], *src32R = samples[1];
263 for (uint32_tcxx . . $ uint32_t# i = 0; i < samplesTodo; i++)
264 dPtr[i] = (double)((src32L[i] + src32R[i]) * dMul);
265 }
266
267 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
268 }