| 1 | #cxx . . $ `<file>/src/pt2_sample_saver.c`/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"cxx . . $ `<file>/src/pt2_structs.h`/ |
| 8 | #include "pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/ |
| 9 | #include "pt2_mouse.h"cxx . . $ `<file>/src/pt2_mouse.h`/ |
| 10 | #include "pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/ |
| 11 | #include "pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/ |
| 12 | #include "pt2_diskop.h"cxx . . $ `<file>/src/pt2_diskop.h`/ |
| 13 | #include "pt2_askbox.h"cxx . . $ `<file>/src/pt2_askbox.h`/ |
| 14 | |
| 15 | #define PLAYBACK_FREQcxx . . $ `src/pt2_sample_saver.c:15:9`! 16574 /* C-3, period 214 */ |
| 16 | |
| 17 | static void removeSampleFileExtcxx . . $ removeSampleFileExt(fda57c8427c60377).(char *text) // for sample saver |
| 18 | { |
| 19 | if (text == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || text[0] == '\0') |
| 20 | return; |
| 21 | |
| 22 | uint32_tcxx . . $ uint32_t# filenameLength = (uint32_tcxx . . $ uint32_t#)strlencxx . . $ strlen(751346f8406fd082).(text); |
| 23 | if (filenameLength < 5) |
| 24 | return; |
| 25 | |
| 26 | // remove .wav/.iff/from end of sample name (if present) |
| 27 | uint32_tcxx . . $ uint32_t# fileExtPos = filenameLength - 4; |
| 28 | if (fileExtPos > 0 && (!strncmpcxx . . $ strncmp(fd278af2f295fa2e).(&text[fileExtPos], ".wav", 4) || !strncmpcxx . . $ strncmp(fd278af2f295fa2e).(&text[fileExtPos], ".iff", 4))) |
| 29 | text[fileExtPos] = '\0'; |
| 30 | } |
| 31 | |
| 32 | static void iffWriteChunkHeadercxx . . $ iffWriteChunkHeader(2a882a974ae25a53).(FILEcxx . . $ FILE# *f, char *chunkName, uint32_tcxx . . $ uint32_t# chunkLen) |
| 33 | { |
| 34 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(chunkName, sizeof (int32_tcxx . . $ int32_t#), 1, f); |
| 35 | chunkLen = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(chunkLen); |
| 36 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&chunkLen, sizeof (int32_tcxx . . $ int32_t#), 1, f); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | static void iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(FILEcxx . . $ FILE# *f, uint32_tcxx . . $ uint32_t# value) |
| 41 | { |
| 42 | value = SWAP32cxx . . $ `src/pt2_helpers.h:12:9`!(value); |
| 43 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&value, sizeof (int32_tcxx . . $ int32_t#), 1, f); |
| 44 | } |
| 45 | |
| 46 | static void iffWriteUint16cxx . . $ iffWriteUint16(b1b189e7e9b2c26e).(FILEcxx . . $ FILE# *f, uint16_tcxx . . $ uint16_t# value) |
| 47 | { |
| 48 | value = SWAP16cxx . . $ `src/pt2_helpers.h:6:9`!(value); |
| 49 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&value, sizeof (int16_tcxx . . $ int16_t#), 1, f); |
| 50 | } |
| 51 | |
| 52 | static void iffWriteUint8cxx . . $ iffWriteUint8(aa9ab397cc85c69b).(FILEcxx . . $ FILE# *f, const uint8_tcxx . . $ uint8_t# value) |
| 53 | { |
| 54 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&value, sizeof (int8_tcxx . . $ int8_t#), 1, f); |
| 55 | } |
| 56 | |
| 57 | static void iffWriteChunkDatacxx . . $ iffWriteChunkData(4a34f311aa103ecc).(FILEcxx . . $ FILE# *f, const void *data, size_t length) |
| 58 | { |
| 59 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(data, sizeof (int8_tcxx . . $ int8_t#), length, f); |
| 60 | if (length & 1) fputccxx . . $ fputc(b1cd03d6c470d1bd).(0, f); // write pad byte if chunk size is uneven |
| 61 | } |
| 62 | |
| 63 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! saveSamplecxx . . $ saveSample(f2cb034b043a1ce9).(boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! checkIfFileExist, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! giveNewFreeFilename) |
| 64 | { |
| 65 | char fileName[128], tmpBuffer[64]; |
| 66 | struct statcxx . . $ stat# statBuffer; |
| 67 | wavHeader_tcxx . . $ wavHeader_t# wavHeader; |
| 68 | samplerChunk_tcxx . . $ samplerChunk_t# samplerChunk; |
| 69 | mptExtraChunk_tcxx . . $ mptExtraChunk_t# mptExtraChunk; |
| 70 | |
| 71 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(tmpBuffer, 0, sizeof (tmpBuffer)); |
| 72 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(fileName, 0, sizeof (fileName)); |
| 73 | |
| 74 | const moduleSample_tcxx . . $ moduleSample_t# *s = &songcxx . . $ song.->samplescxx . . $ module_t#samples.[editorcxx . . $ editor..currSamplecxx . . $ editor_t#currSample.]; |
| 75 | |
| 76 | if (s->lengthcxx . . $ moduleSample_t#length. == 0) |
| 77 | { |
| 78 | statusSampleIsEmptycxx . . $ statusSampleIsEmpty(49f6e7a06ebc5aa8).(); |
| 79 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 80 | } |
| 81 | |
| 82 | // get sample filename |
| 83 | if (s->textcxx . . $ moduleSample_t#text.[0] == '\0') |
| 84 | { |
| 85 | strcpycxx . . $ strcpy(b7b3a83273496486).(fileName, "untitled"); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | for (int32_tcxx . . $ int32_t# i = 0; i < 22; i++) |
| 90 | { |
| 91 | tmpBuffer[i] = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(song->samples[editor.currSample].text[i]); |
| 92 | if (tmpBuffer[i] == '\0') break; |
| 93 | sanitizeFilenameCharcxx . . $ sanitizeFilenameChar(fda57c8427c60377).(&tmpBuffer[i]); |
| 94 | } |
| 95 | |
| 96 | strcpycxx . . $ strcpy(b7b3a83273496486).(fileName, tmpBuffer); |
| 97 | } |
| 98 | |
| 99 | removeSampleFileExtcxx . . $ removeSampleFileExt(fda57c8427c60377).(fileName); |
| 100 | addSampleFileExtcxx . . $ addSampleFileExt(fda57c8427c60377).(fileName); |
| 101 | |
| 102 | // if the user picked "no" to overwriting the file, generate a new filename |
| 103 | if (giveNewFreeFilename && statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) == 0) |
| 104 | { |
| 105 | for (int32_tcxx . . $ int32_t# j = 1; j <= 999; j++) |
| 106 | { |
| 107 | if (s->textcxx . . $ moduleSample_t#text.[0] == '\0') |
| 108 | { |
| 109 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "untitled-%d", j); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | for (int32_tcxx . . $ int32_t# i = 0; i < 22; i++) |
| 114 | { |
| 115 | tmpBuffer[i] = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(song->samples[editor.currSample].text[i]); |
| 116 | if (tmpBuffer[i] == '\0') break; |
| 117 | sanitizeFilenameCharcxx . . $ sanitizeFilenameChar(fda57c8427c60377).(&tmpBuffer[i]); |
| 118 | } |
| 119 | |
| 120 | removeSampleFileExtcxx . . $ removeSampleFileExt(fda57c8427c60377).(tmpBuffer); |
| 121 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "%s-%d", tmpBuffer, j); |
| 122 | } |
| 123 | |
| 124 | addSampleFileExtcxx . . $ addSampleFileExt(fda57c8427c60377).(fileName); |
| 125 | |
| 126 | if (statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) != 0) |
| 127 | break; // this filename can be used |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (checkIfFileExist && statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) == 0) |
| 132 | { |
| 133 | if (!askBoxcxx . . $ askBox(7371ee129cebb502).(ASKBOX_YES_NOcxx . . $ ASKBOX_YES_NO., "OVERWRITE FILE ?")) |
| 134 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 135 | } |
| 136 | |
| 137 | FILEcxx . . $ FILE# *f = fopencxx . . $ fopen(82ec0787cd37e0a3).(fileName, "wb"); |
| 138 | if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 139 | { |
| 140 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("FILE I/O ERROR !"); |
| 141 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 142 | } |
| 143 | |
| 144 | const int8_tcxx . . $ int8_t# *sampleData = &songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.]; |
| 145 | const uint32_tcxx . . $ uint32_t# sampleLength = s->lengthcxx . . $ moduleSample_t#length.; |
| 146 | const uint32_tcxx . . $ uint32_t# loopStart = s->loopStartcxx . . $ moduleSample_t#loopStart. & ~1; |
| 147 | const uint32_tcxx . . $ uint32_t# loopLength = s->loopLengthcxx . . $ moduleSample_t#loopLength. & ~1; |
| 148 | |
| 149 | switch (diskopcxx . . $ diskop..smpSaveTypecxx . . $ diskop_t#smpSaveType.) |
| 150 | { |
| 151 | default: |
| 152 | case DISKOP_SMP_WAVcxx . . $ DISKOP_SMP_WAV.: |
| 153 | { |
| 154 | wavHeader.formatcxx . . $ wavHeader_t#format. = 0x45564157; // "WAVE" |
| 155 | wavHeader.chunkIDcxx . . $ wavHeader_t#chunkID. = 0x46464952; // "RIFF" |
| 156 | wavHeader.subchunk1IDcxx . . $ wavHeader_t#subchunk1ID. = 0x20746D66; // "fmt " |
| 157 | wavHeader.subchunk2IDcxx . . $ wavHeader_t#subchunk2ID. = 0x61746164; // "data" |
| 158 | wavHeader.subchunk1Sizecxx . . $ wavHeader_t#subchunk1Size. = 16; |
| 159 | wavHeader.subchunk2Sizecxx . . $ wavHeader_t#subchunk2Size. = sampleLength; |
| 160 | wavHeader.chunkSizecxx . . $ wavHeader_t#chunkSize. = 36 + wavHeader.subchunk2Sizecxx . . $ wavHeader_t#subchunk2Size.; |
| 161 | wavHeader.audioFormatcxx . . $ wavHeader_t#audioFormat. = 1; |
| 162 | wavHeader.numChannelscxx . . $ wavHeader_t#numChannels. = 1; |
| 163 | wavHeader.bitsPerSamplecxx . . $ wavHeader_t#bitsPerSample. = 8; |
| 164 | wavHeader.sampleRatecxx . . $ wavHeader_t#sampleRate. = PLAYBACK_FREQcxx . . $ `src/pt2_sample_saver.c:15:9`!; |
| 165 | wavHeader.byteRatecxx . . $ wavHeader_t#byteRate. = wavHeader.sampleRatecxx . . $ wavHeader_t#sampleRate. * wavHeader.numChannelscxx . . $ wavHeader_t#numChannels. * wavHeader.bitsPerSamplecxx . . $ wavHeader_t#bitsPerSample. / 8; |
| 166 | wavHeader.blockAligncxx . . $ wavHeader_t#blockAlign. = wavHeader.numChannelscxx . . $ wavHeader_t#numChannels. * wavHeader.bitsPerSamplecxx . . $ wavHeader_t#bitsPerSample. / 8; |
| 167 | |
| 168 | // set "sampler" chunk if loop is enabled |
| 169 | if (loopStart+loopLength > 2) // loop enabled? |
| 170 | { |
| 171 | wavHeader.chunkSizecxx . . $ wavHeader_t#chunkSize. += sizeof (samplerChunk_tcxx . . $ samplerChunk_t#); |
| 172 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&samplerChunk, 0, sizeof (samplerChunk_tcxx . . $ samplerChunk_t#)); |
| 173 | samplerChunk.chunkIDcxx . . $ samplerChunk_t#chunkID. = 0x6C706D73; // "smpl" |
| 174 | samplerChunk.chunkSizecxx . . $ samplerChunk_t#chunkSize. = 60; |
| 175 | samplerChunk.dwSamplePeriodcxx . . $ samplerChunk_t#dwSamplePeriod. = 1000000000 / PLAYBACK_FREQcxx . . $ `src/pt2_sample_saver.c:15:9`!; |
| 176 | samplerChunk.dwMIDIUnityNotecxx . . $ samplerChunk_t#dwMIDIUnityNote. = 60; // 60 = MIDI middle-C |
| 177 | samplerChunk.cSampleLoopscxx . . $ samplerChunk_t#cSampleLoops. = 1; |
| 178 | samplerChunk.loopcxx . . $ samplerChunk_t#loop..dwStartcxx . . $ sampleLoop_t#dwStart. = loopStart; |
| 179 | samplerChunk.loopcxx . . $ samplerChunk_t#loop..dwEndcxx . . $ sampleLoop_t#dwEnd. = (loopStart + loopLength) - 1; |
| 180 | } |
| 181 | |
| 182 | // set ModPlug Tracker chunk (used for sample volume only in this case) |
| 183 | wavHeader.chunkSizecxx . . $ wavHeader_t#chunkSize. += sizeof (mptExtraChunk); |
| 184 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&mptExtraChunk, 0, sizeof (mptExtraChunk)); |
| 185 | mptExtraChunk.chunkIDcxx . . $ mptExtraChunk_t#chunkID. = 0x61727478; // "xtra" |
| 186 | mptExtraChunk.chunkSizecxx . . $ mptExtraChunk_t#chunkSize. = sizeof (mptExtraChunk) - 4 - 4; |
| 187 | mptExtraChunk.defaultPancxx . . $ mptExtraChunk_t#defaultPan. = 128; // 0..255 |
| 188 | mptExtraChunk.defaultVolumecxx . . $ mptExtraChunk_t#defaultVolume. = s->volumecxx . . $ moduleSample_t#volume. * 4; // 0..256 |
| 189 | mptExtraChunk.globalVolumecxx . . $ mptExtraChunk_t#globalVolume. = 64; // 0..64 |
| 190 | |
| 191 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&wavHeader, sizeof (wavHeader_tcxx . . $ wavHeader_t#), 1, f); |
| 192 | |
| 193 | for (uint32_tcxx . . $ uint32_t# i = 0; i < sampleLength; i++) |
| 194 | fputccxx . . $ fputc(b1cd03d6c470d1bd).((uint8_tcxx . . $ uint8_t#)(sampleData[i] + 128), f); |
| 195 | |
| 196 | if (sampleLength & 1) |
| 197 | fputccxx . . $ fputc(b1cd03d6c470d1bd).(0, f); // pad byte needed for 8-bit samples |
| 198 | |
| 199 | if (loopStart+loopLength > 2) // loop enabled? |
| 200 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&samplerChunk, sizeof (samplerChunk), 1, f); |
| 201 | |
| 202 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(&mptExtraChunk, sizeof (mptExtraChunk), 1, f); |
| 203 | } |
| 204 | break; |
| 205 | |
| 206 | case DISKOP_SMP_IFFcxx . . $ DISKOP_SMP_IFF.: |
| 207 | { |
| 208 | // "FORM" chunk |
| 209 | iffWriteChunkHeadercxx . . $ iffWriteChunkHeader(2a882a974ae25a53).(f, "FORM", 0); // "FORM" chunk size is overwritten later |
| 210 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, 0x38535658); // "8SVX" |
| 211 | |
| 212 | // "VHDR" chunk |
| 213 | iffWriteChunkHeadercxx . . $ iffWriteChunkHeader(2a882a974ae25a53).(f, "VHDR", 20); |
| 214 | |
| 215 | if (loopStart+loopLength > 2) // loop enabled? |
| 216 | { |
| 217 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, loopStart); // oneShotHiSamples (loop start when loop is enabled) |
| 218 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, loopLength); // repeatHiSamples |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, sampleLength); // oneShotHiSamples (length of sample when no loop) |
| 223 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, 0); // repeatHiSamples |
| 224 | } |
| 225 | |
| 226 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, 0); // samplesPerHiCycle |
| 227 | iffWriteUint16cxx . . $ iffWriteUint16(b1b189e7e9b2c26e).(f, PLAYBACK_FREQcxx . . $ `src/pt2_sample_saver.c:15:9`!); // samplesPerSec |
| 228 | iffWriteUint8cxx . . $ iffWriteUint8(aa9ab397cc85c69b).(f, 1); // ctOctave (number of samples) |
| 229 | iffWriteUint8cxx . . $ iffWriteUint8(aa9ab397cc85c69b).(f, 0); // sCompression |
| 230 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, s->volumecxx . . $ moduleSample_t#volume. * 1024); // volume (max: 65536/0x10000) |
| 231 | |
| 232 | // "NAME" chunk |
| 233 | uint32_tcxx . . $ uint32_t# chunkLen = (uint32_tcxx . . $ uint32_t#)strlencxx . . $ strlen(751346f8406fd082).(s->textcxx . . $ moduleSample_t#text.); |
| 234 | if (chunkLen > 0) |
| 235 | { |
| 236 | iffWriteChunkHeadercxx . . $ iffWriteChunkHeader(2a882a974ae25a53).(f, "NAME", chunkLen); |
| 237 | iffWriteChunkDatacxx . . $ iffWriteChunkData(4a34f311aa103ecc).(f, s->textcxx . . $ moduleSample_t#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 | iffWriteChunkHeadercxx . . $ iffWriteChunkHeader(2a882a974ae25a53).(f, "ANNO", chunkLen); |
| 244 | iffWriteChunkDatacxx . . $ iffWriteChunkData(4a34f311aa103ecc).(f, annoStr, chunkLen); |
| 245 | |
| 246 | // "BODY" chunk |
| 247 | chunkLen = sampleLength; |
| 248 | iffWriteChunkHeadercxx . . $ iffWriteChunkHeader(2a882a974ae25a53).(f, "BODY", chunkLen); |
| 249 | iffWriteChunkDatacxx . . $ iffWriteChunkData(4a34f311aa103ecc).(f, sampleData, chunkLen); |
| 250 | |
| 251 | // go back and fill in "FORM" chunk size |
| 252 | chunkLen = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(f) - 8; |
| 253 | fseekcxx . . $ fseek(543fae30d249f6b1).(f, 4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 254 | iffWriteUint32cxx . . $ iffWriteUint32(601809f6b723505a).(f, chunkLen); |
| 255 | } |
| 256 | break; |
| 257 | |
| 258 | case DISKOP_SMP_RAWcxx . . $ DISKOP_SMP_RAW.: |
| 259 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(sampleData, 1, sampleLength, f); |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 264 | |
| 265 | displayMsgcxx . . $ displayMsg(85c52e162fed56f9).("SAMPLE SAVED !"); |
| 266 | setMsgPointercxx . . $ setMsgPointer(49f6e7a06ebc5aa8).(); |
| 267 | |
| 268 | diskopcxx . . $ diskop..cachedcxx . . $ diskop_t#cached. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 269 | if (uicxx . . $ ui..diskOpScreenShowncxx . . $ ui_t#diskOpScreenShown.) |
| 270 | uicxx . . $ ui..updateDiskOpFileListcxx . . $ ui_t#updateDiskOpFileList. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 271 | |
| 272 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 273 | } |