Debian Codegraph @

src/pt2_sample_loader.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include "pt2_textout.h"
11 #include "pt2_sampler.h"
12 #include "pt2_audio.h"
13 #include "pt2_visuals.h"
14 #include "pt2_helpers.h"
15 #include "pt2_config.h"
16 #include "pt2_sampling.h"
17 #include "pt2_downsample2x.h"
18 #include "pt2_askbox.h"
19 #include "pt2_replayer.h"
20
21 enum
22 {
23 SAMPLETYPE_RAW,
24 SAMPLETYPE_IFF,
25 SAMPLETYPE_AIFF,
26 SAMPLETYPE_WAV,
27 SAMPLETYPE_FLAC
28 };
29
30 bool loadRAWSample(FILE *f, uint32_t filesize, moduleSample_t *s);
31 bool loadIFFSample(FILE *f, uint32_t filesize, moduleSample_t *s);
32 bool loadAIFFSample(FILE *f, uint32_t filesize, moduleSample_t *s);
33 bool loadWAVSample(FILE *f, uint32_t filesize, moduleSample_t *s);
34 bool loadFLACSample(FILE *f, uint32_t filesize, moduleSample_t *s);
35
36 static void setSampleTextFromFilename(moduleSample_t *s, char *entryName, const char *ext)
37 {
38 int32_t extLen = (int32_t)strlen(ext);
39 int32_t nameLen = (int32_t)strlen(entryName);
40
41 // don't include file extension (if present)
42 if (nameLen >= extLen && !_strnicmp(&entryName[nameLen-extLen], ext, extLen))
43 nameLen -= extLen;
44
45 // copy over filename to sample name first
46 for (int32_t i = 0; i < 21; i++)
47 s->text[i] = (i < nameLen) ? (char)entryName[i] : '\0';
48
49 s->text[21] = '\0';
50 s->text[22] = '\0';
51 }
52
53 bool loadSample(UNICHAR *fileName, char *entryName)
54 {
55 if (editor.sampleZero)
56 {
57 statusNotSampleZero();
58 return false;
59 }
60
61 FILE *f = UNICHAR_FOPEN(fileName, "rb");
62 if (f == NULL)
63 {
64 displayErrorMsg("FILE I/O ERROR !");
65 return false;
66 }
67
68 uint32_t filesize;
69 fseek(f, 0, SEEK_END);
70 filesize = (uint32_t)ftell(f);
71 rewind(f);
72
73 // defaults to RAW if no format was identified
74 uint8_t sampleType = SAMPLETYPE_RAW;
75
76 // first, check heades before we eventually load as RAW
77 if (filesize > 16)
78 {
79 uint32_t ID;
80 fread(&ID, 4, 1, f);
81
82 if (ID == 0x43614C66) // "fLaC" (XXX: weak detection)
83 {
84 sampleType = SAMPLETYPE_FLAC;
85 }
86 else if (ID == 0x46464952) // "RIFF" (WAV)
87 {
88 fseek(f, 4, SEEK_CUR);
89 fread(&ID, 4, 1, f);
90
91 if (ID == 0x45564157) // "WAVE"
92 sampleType = SAMPLETYPE_WAV;
93 }
94 else if (ID == 0x4D524F46) // "FORM" (IFF/AIFF)
95 {
96 fseek(f, 4, SEEK_CUR);
97 fread(&ID, 4, 1, f);
98
99 // check if it's an Amiga IFF sample
100 if (ID == 0x58565338 || ID == 0x56533631) // "8SVX" (normal) and "16SV" (FT2 sample)
101 {
102 sampleType = SAMPLETYPE_IFF;
103 }
104
105 // check if it's an AIFF sample
106 else if (ID == 0x46464941) // "AIFF"
107 {
108 sampleType = SAMPLETYPE_AIFF;
109 }
110
111 else if (ID == 0x43464941) // "AIFC" (compressed AIFF)
112 {
113 fclose(f);
114 displayErrorMsg("UNSUPPORTED AIFF!");
115 return false;
116 }
117 }
118 }
119
120 moduleSample_t *s = &song->samples[editor.currSample];
121
122 // we're not ready to load the sample (error message is shown in the loaders)
123
124 rewind(f);
125
126 bool result = false;
127 switch (sampleType)
128 {
129 case SAMPLETYPE_RAW:
130 setSampleTextFromFilename(s, entryName, ".raw");
131 result = loadRAWSample(f, filesize, s);
132 break;
133
134 case SAMPLETYPE_IFF:
135 setSampleTextFromFilename(s, entryName, ".iff");
136 result = loadIFFSample(f, filesize, s);
137 break;
138
139 case SAMPLETYPE_AIFF:
140 setSampleTextFromFilename(s, entryName, ".aiff");
141 result = loadAIFFSample(f, filesize, s);
142 break;
143
144 case SAMPLETYPE_WAV:
145 setSampleTextFromFilename(s, entryName, ".wav");
146 result = loadWAVSample(f, filesize, s);
147 break;
148
149 case SAMPLETYPE_FLAC:
150 setSampleTextFromFilename(s, entryName, ".flac");
151 result = loadFLACSample(f, filesize, s);
152 break;
153
154 default: break;
155 }
156
157 fclose(f);
158
159 if (result == true)
160 {
161 // sample load was successful
162
163 if (s->length > config.maxSampleLength)
164 s->length = config.maxSampleLength;
165
166 if (s->loopStart+s->loopLength > s->length)
167 {
168 s->loopStart = 0;
169 s->loopLength = 2;
170 }
171
172 // zero out rest of sample (if not full length)
173 if (s->length < config.maxSampleLength)
174 memset(&song->sampleData[s->offset + s->length], 0, config.maxSampleLength - s->length);
175
176 editor.sampleZero = false;
177 editor.samplePos = 0;
178
179 fixSampleBeep(s);
180 fillSampleRedoBuffer(editor.currSample);
181
182 if (ui.samplingBoxShown)
183 {
184 removeSamplingBox();
185 ui.samplingBoxShown = false;
186 }
187
188 updateCurrSample();
189 updateWindowTitle(MOD_IS_MODIFIED);
190 }
191
192 return result;
193 }