Debian Codegraph @

src/pt2_sample_saver.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <ctype.h> // tolower()
6 #include <sys/stat.h>
7 #include "pt2_structs.h"
8 #include "pt2_textout.h"
9 #include "pt2_mouse.h"
10 #include "pt2_visuals.h"
11 #include "pt2_helpers.h"
12 #include "pt2_diskop.h"
13 #include "pt2_askbox.h"
14
15 #define PLAYBACK_FREQ 16574 /* C-3, period 214 */
16
17 static void removeSampleFileExt(char *text) // for sample saver
18 {
19 if (text == NULL || text[0] == '\0')
20 return;
21
22 uint32_t filenameLength = (uint32_t)strlen(text);
23 if (filenameLength < 5)
24 return;
25
26 // remove .wav/.iff/from end of sample name (if present)
27 uint32_t fileExtPos = filenameLength - 4;
28 if (fileExtPos > 0 && (!strncmp(&text[fileExtPos], ".wav", 4) || !strncmp(&text[fileExtPos], ".iff", 4)))
29 text[fileExtPos] = '\0';
30 }
31
32 static void iffWriteChunkHeader(FILE *f, char *chunkName, uint32_t chunkLen)
33 {
34 fwrite(chunkName, sizeof (int32_t), 1, f);
35 chunkLen = SWAP32(chunkLen);
36 fwrite(&chunkLen, sizeof (int32_t), 1, f);
37 }
38
39
40 static void iffWriteUint32(FILE *f, uint32_t value)
41 {
42 value = SWAP32(value);
43 fwrite(&value, sizeof (int32_t), 1, f);
44 }
45
46 static void iffWriteUint16(FILE *f, uint16_t value)
47 {
48 value = SWAP16(value);
49 fwrite(&value, sizeof (int16_t), 1, f);
50 }
51
52 static void iffWriteUint8(FILE *f, const uint8_t value)
53 {
54 fwrite(&value, sizeof (int8_t), 1, f);
55 }
56
57 static void iffWriteChunkData(FILE *f, const void *data, size_t length)
58 {
59 fwrite(data, sizeof (int8_t), length, f);
60 if (length & 1) fputc(0, f); // write pad byte if chunk size is uneven
61 }
62
63 bool saveSample(bool checkIfFileExist, bool giveNewFreeFilename)
64 {
65 char fileName[128], tmpBuffer[64];
66 struct stat statBuffer;
67 wavHeader_t wavHeader;
68 samplerChunk_t samplerChunk;
69 mptExtraChunk_t mptExtraChunk;
70
71 memset(tmpBuffer, 0, sizeof (tmpBuffer));
72 memset(fileName, 0, sizeof (fileName));
73
74 const moduleSample_t *s = &song->samples[editor.currSample];
75
76 if (s->length == 0)
77 {
78 statusSampleIsEmpty();
79 return false;
80 }
81
82 // get sample filename
83 if (s->text[0] == '\0')
84 {
85 strcpy(fileName, "untitled");
86 }
87 else
88 {
89 for (int32_t i = 0; i < 22; i++)
90 {
91 tmpBuffer[i] = (char)tolower(song->samples[editor.currSample].text[i]);
92 if (tmpBuffer[i] == '\0') break;
93 sanitizeFilenameChar(&tmpBuffer[i]);
94 }
95
96 strcpy(fileName, tmpBuffer);
97 }
98
99 removeSampleFileExt(fileName);
100 addSampleFileExt(fileName);
101
102 // if the user picked "no" to overwriting the file, generate a new filename
103 if (giveNewFreeFilename && stat(fileName, &statBuffer) == 0)
104 {
105 for (int32_t j = 1; j <= 999; j++)
106 {
107 if (s->text[0] == '\0')
108 {
109 sprintf(fileName, "untitled-%d", j);
110 }
111 else
112 {
113 for (int32_t i = 0; i < 22; i++)
114 {
115 tmpBuffer[i] = (char)tolower(song->samples[editor.currSample].text[i]);
116 if (tmpBuffer[i] == '\0') break;
117 sanitizeFilenameChar(&tmpBuffer[i]);
118 }
119
120 removeSampleFileExt(tmpBuffer);
121 sprintf(fileName, "%s-%d", tmpBuffer, j);
122 }
123
124 addSampleFileExt(fileName);
125
126 if (stat(fileName, &statBuffer) != 0)
127 break; // this filename can be used
128 }
129 }
130
131 if (checkIfFileExist && stat(fileName, &statBuffer) == 0)
132 {
133 if (!askBox(ASKBOX_YES_NO, "OVERWRITE FILE ?"))
134 return false;
135 }
136
137 FILE *f = fopen(fileName, "wb");
138 if (f == NULL)
139 {
140 displayErrorMsg("FILE I/O ERROR !");
141 return false;
142 }
143
144 const int8_t *sampleData = &song->sampleData[s->offset];
145 const uint32_t sampleLength = s->length;
146 const uint32_t loopStart = s->loopStart & ~1;
147 const uint32_t loopLength = s->loopLength & ~1;
148
149 switch (diskop.smpSaveType)
150 {
151 default:
152 case DISKOP_SMP_WAV:
153 {
154 wavHeader.format = 0x45564157; // "WAVE"
155 wavHeader.chunkID = 0x46464952; // "RIFF"
156 wavHeader.subchunk1ID = 0x20746D66; // "fmt "
157 wavHeader.subchunk2ID = 0x61746164; // "data"
158 wavHeader.subchunk1Size = 16;
159 wavHeader.subchunk2Size = sampleLength;
160 wavHeader.chunkSize = 36 + wavHeader.subchunk2Size;
161 wavHeader.audioFormat = 1;
162 wavHeader.numChannels = 1;
163 wavHeader.bitsPerSample = 8;
164 wavHeader.sampleRate = PLAYBACK_FREQ;
165 wavHeader.byteRate = wavHeader.sampleRate * wavHeader.numChannels * wavHeader.bitsPerSample / 8;
166 wavHeader.blockAlign = wavHeader.numChannels * wavHeader.bitsPerSample / 8;
167
168 // set "sampler" chunk if loop is enabled
169 if (loopStart+loopLength > 2) // loop enabled?
170 {
171 wavHeader.chunkSize += sizeof (samplerChunk_t);
172 memset(&samplerChunk, 0, sizeof (samplerChunk_t));
173 samplerChunk.chunkID = 0x6C706D73; // "smpl"
174 samplerChunk.chunkSize = 60;
175 samplerChunk.dwSamplePeriod = 1000000000 / PLAYBACK_FREQ;
176 samplerChunk.dwMIDIUnityNote = 60; // 60 = MIDI middle-C
177 samplerChunk.cSampleLoops = 1;
178 samplerChunk.loop.dwStart = loopStart;
179 samplerChunk.loop.dwEnd = (loopStart + loopLength) - 1;
180 }
181
182 // set ModPlug Tracker chunk (used for sample volume only in this case)
183 wavHeader.chunkSize += sizeof (mptExtraChunk);
184 memset(&mptExtraChunk, 0, sizeof (mptExtraChunk));
185 mptExtraChunk.chunkID = 0x61727478; // "xtra"
186 mptExtraChunk.chunkSize = sizeof (mptExtraChunk) - 4 - 4;
187 mptExtraChunk.defaultPan = 128; // 0..255
188 mptExtraChunk.defaultVolume = s->volume * 4; // 0..256
189 mptExtraChunk.globalVolume = 64; // 0..64
190
191 fwrite(&wavHeader, sizeof (wavHeader_t), 1, f);
192
193 for (uint32_t i = 0; i < sampleLength; i++)
194 fputc((uint8_t)(sampleData[i] + 128), f);
195
196 if (sampleLength & 1)
197 fputc(0, f); // pad byte needed for 8-bit samples
198
199 if (loopStart+loopLength > 2) // loop enabled?
200 fwrite(&samplerChunk, sizeof (samplerChunk), 1, f);
201
202 fwrite(&mptExtraChunk, sizeof (mptExtraChunk), 1, f);
203 }
204 break;
205
206 case DISKOP_SMP_IFF:
207 {
208 // "FORM" chunk
209 iffWriteChunkHeader(f, "FORM", 0); // "FORM" chunk size is overwritten later
210 iffWriteUint32(f, 0x38535658); // "8SVX"
211
212 // "VHDR" chunk
213 iffWriteChunkHeader(f, "VHDR", 20);
214
215 if (loopStart+loopLength > 2) // loop enabled?
216 {
217 iffWriteUint32(f, loopStart); // oneShotHiSamples (loop start when loop is enabled)
218 iffWriteUint32(f, loopLength); // repeatHiSamples
219 }
220 else
221 {
222 iffWriteUint32(f, sampleLength); // oneShotHiSamples (length of sample when no loop)
223 iffWriteUint32(f, 0); // repeatHiSamples
224 }
225
226 iffWriteUint32(f, 0); // samplesPerHiCycle
227 iffWriteUint16(f, PLAYBACK_FREQ); // samplesPerSec
228 iffWriteUint8(f, 1); // ctOctave (number of samples)
229 iffWriteUint8(f, 0); // sCompression
230 iffWriteUint32(f, s->volume * 1024); // volume (max: 65536/0x10000)
231
232 // "NAME" chunk
233 uint32_t chunkLen = (uint32_t)strlen(s->text);
234 if (chunkLen > 0)
235 {
236 iffWriteChunkHeader(f, "NAME", chunkLen);
237 iffWriteChunkData(f, s->text, chunkLen);
238 }
239
240 // "ANNO" chunk (we put the program name here)
241 const char annoStr[] = "ProTracker 2 clone";
242 chunkLen = sizeof (annoStr) - 1;
243 iffWriteChunkHeader(f, "ANNO", chunkLen);
244 iffWriteChunkData(f, annoStr, chunkLen);
245
246 // "BODY" chunk
247 chunkLen = sampleLength;
248 iffWriteChunkHeader(f, "BODY", chunkLen);
249 iffWriteChunkData(f, sampleData, chunkLen);
250
251 // go back and fill in "FORM" chunk size
252 chunkLen = ftell(f) - 8;
253 fseek(f, 4, SEEK_SET);
254 iffWriteUint32(f, chunkLen);
255 }
256 break;
257
258 case DISKOP_SMP_RAW:
259 fwrite(sampleData, 1, sampleLength, f);
260 break;
261 }
262
263 fclose(f);
264
265 displayMsg("SAMPLE SAVED !");
266 setMsgPointer();
267
268 diskop.cached = false;
269 if (ui.diskOpScreenShown)
270 ui.updateDiskOpFileList = true;
271
272 return true;
273 }