Debian Codegraph @

src/pt2_sampler.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 <math.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <string.h>
12 #include "pt2_helpers.h"
13 #include "pt2_textout.h"
14 #include "pt2_audio.h"
15 #include "pt2_tables.h"
16 #include "pt2_visuals.h"
17 #include "pt2_scopes.h"
18 #include "pt2_sampler.h"
19 #include "pt2_config.h"
20 #include "pt2_bmp.h"
21 #include "pt2_rcfilters.h"
22 #include "pt2_chordmaker.h"
23 #include "pt2_replayer.h"
24 #include "pt2_visuals_sync.h"
25 #include "pt2_askbox.h"
26
27 #define CENTER_LINE_COLOR 0x303030
28 #define MARK_COLOR_1 0x666666 /* inverted background */
29 #define MARK_COLOR_2 0xCCCCCC /* inverted waveform */
30 #define MARK_COLOR_3 0x7D7D7D /* inverted center line */
31
32 #define SAMPLE_AREA_Y_CENTER 169
33 #define SAMPLE_AREA_HEIGHT 64
34
35 static int32_t samOffsetScaled, lastDrawX, lastDrawY;
36 static uint16_t TToneBit;
37 static uint32_t waveInvertTable[8];
38
39 sampler_t sampler; // globalized
40
41 static const int8_t tuneToneData[32] = // Tuning Tone (sine, regenerated with 127-scale instead of 128)
42 {
43 0, 25, 49, 71, 90, 106, 117, 125,
44 127, 125, 117, 106, 90, 71, 49, 25,
45 0, -25, -49, -71, -90, -106, -117, -125,
46 -127, -125, -117, -106, -90, -71, -49, -25
47 };
48
49 void killSample(void)
50 {
51 if (editor.sampleZero)
52 {
53 statusNotSampleZero();
54 return;
55 }
56
57 turnOffVoices();
58 moduleSample_t *s = &song->samples[editor.currSample];
59
60 s->fineTune = 0;
61 s->volume = 0;
62 s->length = 0;
63 s->loopStart = 0;
64 s->loopLength = 2;
65
66 memset(s->text, 0, sizeof (s->text));
67 memset(&song->sampleData[(editor.currSample * config.maxSampleLength)], 0, config.maxSampleLength);
68
69 editor.samplePos = 0;
70 updateCurrSample();
71
72 ui.updateSongSize = true;
73 updateWindowTitle(MOD_IS_MODIFIED);
74 }
75
76 void upSample(void)
77 {
78 if (editor.sampleZero)
79 {
80 statusNotSampleZero();
81 return;
82 }
83
84 moduleSample_t *s = &song->samples[editor.currSample];
85
86 int32_t newLength = (s->length >> 1) & config.maxSampleLength;
87 if (newLength < 2)
88 return;
89
90 turnOffVoices();
91
92 // upsample
93 int8_t *ptr8 = &song->sampleData[s->offset];
94 for (int32_t i = 0; i < newLength; i++)
95 ptr8[i] = ptr8[i << 1];
96
97 // clear junk after shrunk sample
98 if (newLength < config.maxSampleLength)
99 memset(&ptr8[newLength], 0, config.maxSampleLength - newLength);
100
101 s->length = newLength;
102 s->loopStart = (s->loopStart >> 1) & ~1;
103 s->loopLength = (s->loopLength >> 1) & ~1;
104
105 if (s->loopLength < 2)
106 {
107 s->loopStart = 0;
108 s->loopLength = 2;
109 }
110
111 fixSampleBeep(s);
112 updateCurrSample();
113
114 ui.updateSongSize = true;
115 updateWindowTitle(MOD_IS_MODIFIED);
116 }
117
118 void downSample(void)
119 {
120 if (editor.sampleZero)
121 {
122 statusNotSampleZero();
123 return;
124 }
125
126 moduleSample_t *s = &song->samples[editor.currSample];
127
128 int32_t newLength = s->length << 1;
129 if (newLength > config.maxSampleLength)
130 newLength = config.maxSampleLength;
131
132 turnOffVoices();
133
134 // downsample
135
136 int8_t *ptr8 = &song->sampleData[s->offset];
137 int8_t *ptr8_2 = ptr8 - 1;
138 for (int32_t i = s->length-1; i > 0; i--)
139 {
140 ptr8[i<<1] = ptr8[i];
141 ptr8_2[i<<1] = ptr8_2[i];
142 }
143
144 s->length = newLength;
145
146 if (s->loopLength > 2)
147 {
148 int32_t loopStart = s->loopStart << 1;
149 int32_t loopLength = s->loopLength << 1;
150
151 if (loopStart+loopLength > s->length)
152 {
153 loopStart = 0;
154 loopLength = 2;
155 }
156
157 s->loopStart = loopStart;
158 s->loopLength = loopLength;
159 }
160
161 fixSampleBeep(s);
162 updateCurrSample();
163
164 ui.updateSongSize = true;
165 updateWindowTitle(MOD_IS_MODIFIED);
166 }
167
168 void createSampleMarkTable(void)
169 {
170 // used for invertRange() (sample data marking)
171
172 waveInvertTable[0] = 0x00000000 | video.palette[PAL_BACKGRD];
173 waveInvertTable[1] = 0x01000000 | video.palette[PAL_QADSCP];
174 waveInvertTable[2] = 0x02000000 | CENTER_LINE_COLOR;
175 waveInvertTable[3] = 0x03000000; // spacer, not used
176 waveInvertTable[4] = 0x04000000 | MARK_COLOR_1;
177 waveInvertTable[5] = 0x05000000 | MARK_COLOR_2;
178 waveInvertTable[6] = 0x06000000 | MARK_COLOR_3;
179 waveInvertTable[7] = 0x07000000; // spacer, not used
180 }
181
182 static void updateSamOffset(void)
183 {
184 if (sampler.samDisplay == 0)
185 samOffsetScaled = 0;
186 else
187 samOffsetScaled = (sampler.samOffset * SAMPLE_AREA_WIDTH) / sampler.samDisplay; // truncate here
188 }
189
190 void fixSampleBeep(moduleSample_t *s)
191 {
192 if (s->length >= 2 && s->loopStart+s->loopLength <= 2)
193 {
194 song->sampleData[s->offset+0] = 0;
195 song->sampleData[s->offset+1] = 0;
196 }
197 }
198
199 void updateSamplePos(void)
200 {
201 moduleSample_t *s;
202
203 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
204 if (editor.currSample >= 0 && editor.currSample <= 30)
205 {
206 s = &song->samples[editor.currSample];
207 if (editor.samplePos > s->length)
208 editor.samplePos = s->length;
209
210 if (ui.editOpScreenShown && ui.editOpScreen == 2)
211 ui.updatePosText = true;
212 }
213 }
214
215 void fillSampleFilterUndoBuffer(void)
216 {
217 moduleSample_t *s;
218
219 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
220 if (editor.currSample >= 0 && editor.currSample <= 30)
221 {
222 s = &song->samples[editor.currSample];
223 memcpy(sampler.sampleUndoCopy, &song->sampleData[s->offset], s->length);
224 }
225 }
226
227 void sampleLine(int32_t line_x1, int32_t line_x2, int32_t line_y1, int32_t line_y2)
228 {
229 const uint32_t color = 0x01000000 | video.palette[PAL_QADSCP]; // set alpha to 0x10 ( used for invertRange() as a hack )
230
231 ASSERT(line_x1 >= 0 || line_x2 >= 0 || line_x1 < SCREEN_W || line_x2 < SCREEN_W);
232 ASSERT(line_y1 >= 0 || line_y2 >= 0 || line_y1 < SCREEN_H || line_y2 < SCREEN_H);
233
234 int32_t dx = line_x2 - line_x1;
235 int32_t ax = ABS(dx) * 2;
236 int32_t sx = SGN(dx);
237 int32_t dy = line_y2 - line_y1;
238 int32_t ay = ABS(dy) * 2;
239 int32_t sy = SGN(dy);
240 int32_t x = line_x1;
241 int32_t y = line_y1;
242
243 if (ax > ay)
244 {
245 int32_t d = ay - ((uint16_t)ax >> 1);
246 while (true)
247 {
248 ASSERT(y >= 0 || x >= 0 || y < SCREEN_H || x < SCREEN_W);
249
250 video.frameBuffer[(y * SCREEN_W) + x] = color;
251
252 if (x == line_x2)
253 break;
254
255 if (d >= 0)
256 {
257 y += sy;
258 d -= ax;
259 }
260
261 x += sx;
262 d += ay;
263 }
264 }
265 else
266 {
267 int32_t d = ax - ((uint16_t)ay >> 1);
268 while (true)
269 {
270 ASSERT(y >= 0 || x >= 0 || y < SCREEN_H || x < SCREEN_W);
271
272 video.frameBuffer[(y * SCREEN_W) + x] = color;
273
274 if (y == line_y2)
275 break;
276
277 if (d >= 0)
278 {
279 x += sx;
280 d -= ay;
281 }
282
283 y += sy;
284 d += ax;
285 }
286 }
287 }
288
289 static void setDragBar(void)
290 {
291 // clear drag bar background
292 fillRect(4, 206, 312, 4, video.palette[PAL_BACKGRD]);
293
294 if (sampler.samLength > 0 && sampler.samDisplay != sampler.samLength)
295 {
296 const int32_t roundingBias = sampler.samLength >> 1;
297
298 // update drag bar coordinates
299 int32_t pos = 4 + (((sampler.samOffset * 311) + roundingBias) / sampler.samLength);
300 sampler.dragStart = (uint16_t)CLAMP(pos, 4, 315);
301
302 pos = 5 + ((((sampler.samDisplay + sampler.samOffset) * 311) + roundingBias) / sampler.samLength);
303 sampler.dragEnd = (uint16_t)CLAMP(pos, 5, 316);
304
305 if (sampler.dragStart > sampler.dragEnd-1)
306 sampler.dragStart = sampler.dragEnd-1;
307
308 // draw drag bar
309
310 const uint32_t dragWidth = sampler.dragEnd - sampler.dragStart;
311 if (dragWidth > 0)
312 fillRect(sampler.dragStart, 206, dragWidth, 4, video.palette[PAL_QADSCP]);
313 }
314 }
315
316 static int8_t getScaledSample(int32_t index)
317 {
318 if (sampler.samLength <= 0 || index < 0 || index > sampler.samLength)
319 return 0;
320
321 const int8_t *ptr8 = sampler.samStart;
322 if (ptr8 == NULL)
323 return 0;
324
325 return ptr8[index] >> 2;
326 }
327
328 int32_t smpPos2Scr(int32_t pos) // sample pos -> screen x pos
329 {
330 if (sampler.samDisplay == 0)
331 return 0;
332
333 const uint32_t roundingBias = (const uint32_t)sampler.samDisplay >> 1;
334
335 pos = (((uint32_t)pos * SAMPLE_AREA_WIDTH) + roundingBias) / (uint32_t)sampler.samDisplay; // rounded
336 pos -= samOffsetScaled;
337
338 return pos;
339 }
340
341 int32_t scr2SmpPos(int32_t x) // screen x pos -> sample pos
342 {
343 if (sampler.samDisplay == 0)
344 return 0;
345
346 if (x < 0)
347 x = 0;
348
349 x += samOffsetScaled;
350 x = (uint32_t)(x * sampler.samDisplay) / SAMPLE_AREA_WIDTH; // truncate here
351
352 if (x > sampler.samLength)
353 x = sampler.samLength;
354
355 return x;
356 }
357
358 static void getSampleDataPeak(int8_t *smpPtr, int32_t numBytes, int16_t *outMin, int16_t *outMax)
359 {
360 int8_t smpMin = 127;
361 int8_t smpMax = -128;
362
363 for (int32_t i = 0; i < numBytes; i++)
364 {
365 int8_t smp = smpPtr[i];
366 if (smp < smpMin) smpMin = smp;
367 if (smp > smpMax) smpMax = smp;
368 }
369
370 *outMin = SAMPLE_AREA_Y_CENTER - (smpMin >> 2);
371 *outMax = SAMPLE_AREA_Y_CENTER - (smpMax >> 2);
372 }
373
374 void renderSampleData(void)
375 {
376 moduleSample_t *s = &song->samples[editor.currSample];
377
378 // clear sample data background
379 fillRect(3, 138, SAMPLE_AREA_WIDTH, SAMPLE_VIEW_HEIGHT, video.palette[PAL_BACKGRD]);
380
381 // display center line (if enabled)
382 if (config.waveformCenterLine)
383 {
384 uint32_t *dstPtr = &video.frameBuffer[(SAMPLE_AREA_Y_CENTER * SCREEN_W) + 3];
385 for (int32_t x = 0; x < SAMPLE_AREA_WIDTH; x++)
386 dstPtr[x] = 0x02000000 | CENTER_LINE_COLOR;
387 }
388
389 // render sample data
390 if (sampler.samDisplay >= 0 && sampler.samDisplay <= config.maxSampleLength)
391 {
392 int16_t y1 = SAMPLE_AREA_Y_CENTER - getScaledSample(scr2SmpPos(0));
393
394 if (sampler.samDisplay <= SAMPLE_AREA_WIDTH)
395 {
396 // 1:1 or zoomed in
397 for (int32_t x = 1; x < SAMPLE_AREA_WIDTH; x++)
398 {
399 int16_t y2 = SAMPLE_AREA_Y_CENTER - getScaledSample(scr2SmpPos(x));
400 sampleLine(x + 2, x + 3, y1, y2);
401 y1 = y2;
402 }
403 }
404 else
405 {
406 // zoomed out
407
408 int16_t min, max;
409
410 int16_t oldMin = y1;
411 int16_t oldMax = y1;
412
413 int8_t *smpPtr = &song->sampleData[s->offset];
414 for (int32_t x = 0; x < SAMPLE_AREA_WIDTH; x++)
415 {
416 int32_t smpIdx = scr2SmpPos(x);
417 int32_t smpNum = scr2SmpPos(x+1) - smpIdx;
418
419 // prevent look-up overflow (yes, this can happen near the end of the sample)
420 if (smpIdx+smpNum > sampler.samLength)
421 smpNum = sampler.samLength - smpNum;
422
423 if (smpNum < 1)
424 smpNum = 1;
425
426 getSampleDataPeak(&smpPtr[smpIdx], smpNum, &min, &max);
427
428 if (x > 0)
429 {
430 if (min > oldMax) sampleLine(x + 2, x + 3, oldMax, min);
431 if (max < oldMin) sampleLine(x + 2, x + 3, oldMin, max);
432 }
433
434 sampleLine(x + 3, x + 3, max, min);
435
436 oldMin = min;
437 oldMax = max;
438 }
439 }
440 }
441
442 if (ui.samplingBoxShown)
443 return;
444
445 // render "sample display" text
446
447 if (config.maxSampleLength == 0xFFFE)
448 {
449 if (sampler.samStart == sampler.blankSample)
450 printFiveDecimalsBg(272, 214, 0, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
451 else
452 printFiveDecimalsBg(272, 214, sampler.samDisplay, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
453 }
454 else
455 {
456 if (sampler.samStart == sampler.blankSample)
457 printSixDecimalsBg(270, 214, 0, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
458 else
459 printSixDecimalsBg(270, 214, sampler.samDisplay, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
460 }
461
462 setDragBar();
463 setLoopSprites();
464 }
465
466 void invertRange(void)
467 {
468 if (editor.markStartOfs == -1)
469 return; // no marking
470
471 int32_t start = smpPos2Scr(editor.markStartOfs);
472 int32_t end = smpPos2Scr(editor.markEndOfs);
473
474 if (sampler.samDisplay < sampler.samLength && (start >= SAMPLE_AREA_WIDTH || end < 0))
475 return; // range is outside of view
476
477 start = CLAMP(start, 0, SAMPLE_AREA_WIDTH-1);
478 end = CLAMP(end, 0, SAMPLE_AREA_WIDTH-1);
479
480 int32_t rangeLen = (end + 1) - start;
481 if (rangeLen < 1)
482 rangeLen = 1;
483
484 uint32_t *dstPtr = &video.frameBuffer[(138 * SCREEN_W) + (start + 3)];
485 for (int32_t y = 0; y < 64; y++)
486 {
487 for (int32_t x = 0; x < rangeLen; x++)
488 dstPtr[x] = waveInvertTable[((dstPtr[x] >> 24) & 7) ^ 4]; // ptr[x]>>24 = wave/invert color number
489
490 dstPtr += SCREEN_W;
491 }
492 }
493
494 void displaySample(void)
495 {
496 if (!ui.samplerScreenShown)
497 return;
498
499 renderSampleData();
500 if (editor.markStartOfs != -1)
501 invertRange();
502
503 ui.update9xxPos = true;
504 }
505
506 void redrawSample(void)
507 {
508 if (!ui.samplerScreenShown)
509 return;
510
511 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
512 if (editor.currSample >= 0 && editor.currSample <= 30)
513 {
514 editor.markStartOfs = -1;
515
516 sampler.samOffset = 0;
517 updateSamOffset();
518
519 moduleSample_t *s = &song->samples[editor.currSample];
520 if (s->length > 0)
521 {
522 sampler.samStart = &song->sampleData[s->offset];
523 sampler.samDisplay = s->length;
524 sampler.samLength = s->length;
525 }
526 else
527 {
528 // "blank sample" template
529 sampler.samStart = sampler.blankSample;
530 sampler.samLength = SAMPLE_AREA_WIDTH;
531 sampler.samDisplay = SAMPLE_AREA_WIDTH;
532 }
533
534 renderSampleData();
535 updateSamplePos();
536
537 ui.update9xxPos = true;
538 ui.lastSampleOffset = 0x900;
539 }
540 }
541
542 void highPassSample(int32_t cutOff)
543 {
544 onePoleFilter_t filterHi;
545
546 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
547
548 if (editor.sampleZero)
549 {
550 statusNotSampleZero();
551 return;
552 }
553
554 if (cutOff == 0)
555 {
556 displayErrorMsg("CUTOFF CAN'T BE 0");
557 return;
558 }
559
560 moduleSample_t *s = &song->samples[editor.currSample];
561 if (s->length == 0)
562 {
563 statusSampleIsEmpty();
564 return;
565 }
566
567 int32_t from = 0;
568 int32_t to = s->length;
569
570 if (editor.markStartOfs != -1)
571 {
572 from = editor.markStartOfs;
573 to = editor.markEndOfs;
574
575 if (to > s->length)
576 to = s->length;
577
578 if (from == to || from >= s->length || to < from)
579 {
580 from = 0;
581 to = s->length;
582 }
583 }
584
585 float *fSampleData = (float *)malloc(s->length * sizeof (float));
586 if (fSampleData == NULL)
587 {
588 statusOutOfMemory();
589 return;
590 }
591
592 fillSampleFilterUndoBuffer();
593
594 // setup filter coefficients
595
596 double dBaseFreq = FILTERS_BASE_FREQ;
597
598 double dCutOff = (double)cutOff;
599 if (dCutOff >= dBaseFreq/2.0)
600 {
601 dCutOff = dBaseFreq/2.0;
602 editor.hpCutOff = (uint16_t)dCutOff;
603 }
604
605 setupOnePoleFilter(dBaseFreq, dCutOff, &filterHi);
606
607 clearOnePoleFilterState(&filterHi);
608 if (to <= s->length)
609 {
610 const int8_t *smpPtr = &song->sampleData[s->offset];
611 for (int32_t i = from; i < to; i++)
612 {
613 float fSmp = smpPtr[i];
614 onePoleHPFilter(&filterHi, fSmp, &fSampleData[i]);
615 }
616 }
617
618 float fAmp = 1.0f;
619 if (editor.normalizeFiltersFlag)
620 {
621 const float fPeak = getFloatPeak(fSampleData, s->length);
622 if (fPeak > 0.0f)
623 fAmp = INT8_MAX / fPeak;
624 }
625
626 int8_t *smpPtr = &song->sampleData[s->offset];
627 for (int32_t i = from; i < to; i++)
628 {
629 int16_t smp16 = (int16_t)roundf(fSampleData[i] * fAmp);
630 smpPtr[i] = (int8_t)(CLAMP(smp16, INT8_MIN, INT8_MAX));
631 }
632
633 free(fSampleData);
634
635 fixSampleBeep(s);
636 displaySample();
637 updateWindowTitle(MOD_IS_MODIFIED);
638 }
639
640 void lowPassSample(int32_t cutOff)
641 {
642 onePoleFilter_t filterLo;
643
644 if (editor.sampleZero)
645 {
646 statusNotSampleZero();
647 return;
648 }
649
650 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
651
652 if (cutOff == 0)
653 {
654 displayErrorMsg("CUTOFF CAN'T BE 0");
655 return;
656 }
657
658 moduleSample_t *s = &song->samples[editor.currSample];
659 if (s->length == 0)
660 {
661 statusSampleIsEmpty();
662 return;
663 }
664
665 int32_t from = 0;
666 int32_t to = s->length;
667
668 if (editor.markStartOfs != -1)
669 {
670 from = editor.markStartOfs;
671 to = editor.markEndOfs;
672
673 if (to > s->length)
674 to = s->length;
675
676 if (from == to || from >= s->length || to < from)
677 {
678 from = 0;
679 to = s->length;
680 }
681 }
682
683 float *fSampleData = (float *)malloc(s->length * sizeof (float));
684 if (fSampleData == NULL)
685 {
686 statusOutOfMemory();
687 return;
688 }
689
690 fillSampleFilterUndoBuffer();
691
692 // setup filter coefficients
693
694 double dBaseFreq = FILTERS_BASE_FREQ;
695
696 double dCutOff = (double)cutOff;
697 if (dCutOff >= dBaseFreq/2.0)
698 {
699 dCutOff = dBaseFreq/2.0;
700 editor.lpCutOff = (uint16_t)dCutOff;
701 }
702
703 setupOnePoleFilter(dBaseFreq, dCutOff, &filterLo);
704
705 // copy over sample data to double buffer
706 for (int32_t i = 0; i < s->length; i++)
707 fSampleData[i] = song->sampleData[s->offset+i];
708
709 clearOnePoleFilterState(&filterLo);
710 if (to <= s->length)
711 {
712 const int8_t *smpPtr = &song->sampleData[s->offset];
713 for (int32_t i = from; i < to; i++)
714 {
715 float fSmp = smpPtr[i];
716 onePoleLPFilter(&filterLo, fSmp, &fSampleData[i]);
717 }
718 }
719
720 float fAmp = 1.0f;
721
722 if (editor.normalizeFiltersFlag)
723 {
724 const float fPeak = getFloatPeak(fSampleData, s->length);
725 if (fPeak > 0.0f)
726 fAmp = INT8_MAX / fPeak;
727 }
728
729 int8_t *smpPtr = &song->sampleData[s->offset];
730 for (int32_t i = from; i < to; i++)
731 {
732 int16_t smp16 = (int16_t)roundf(fSampleData[i] * fAmp);
733 smpPtr[i] = (int8_t)(CLAMP(smp16, INT8_MIN, INT8_MAX));
734 }
735
736 free(fSampleData);
737
738 fixSampleBeep(s);
739 displaySample();
740 updateWindowTitle(MOD_IS_MODIFIED);
741 }
742
743 void redoSampleData(int8_t sample)
744 {
745 if (editor.sampleZero)
746 {
747 statusNotSampleZero();
748 return;
749 }
750
751 ASSERT(sample >= 0 && sample <= 30);
752 moduleSample_t *s = &song->samples[sample];
753
754 turnOffVoices();
755
756 if (editor.smpRedoBuffer[sample] != NULL && editor.smpRedoLengths[sample] > 0)
757 {
758 memcpy(&song->sampleData[s->offset], editor.smpRedoBuffer[sample], editor.smpRedoLengths[sample]);
759
760 if (editor.smpRedoLengths[sample] < config.maxSampleLength)
761 memset(&song->sampleData[s->offset + editor.smpRedoLengths[sample]], 0, config.maxSampleLength - editor.smpRedoLengths[sample]);
762 }
763 else
764 {
765 memset(&song->sampleData[s->offset], 0, config.maxSampleLength);
766 }
767
768 s->fineTune = editor.smpRedoFinetunes[sample];
769 s->volume = editor.smpRedoVolumes[sample];
770 s->length = editor.smpRedoLengths[sample];
771 s->loopStart = editor.smpRedoLoopStarts[sample];
772 s->loopLength = (editor.smpRedoLoopLengths[sample] < 2) ? 2 : editor.smpRedoLoopLengths[sample];
773
774 displayMsg("SAMPLE RESTORED !");
775
776 editor.samplePos = 0;
777 updateCurrSample();
778
779 // this routine can be called while the sampler toolboxes are open, so redraw them
780 if (ui.samplerScreenShown)
781 {
782 if (ui.samplerVolBoxShown)
783 renderSamplerVolBox();
784 else if (ui.samplerFiltersBoxShown)
785 renderSamplerFiltersBox();
786 }
787 }
788
789 void fillSampleRedoBuffer(int8_t sample)
790 {
791 ASSERT(sample >= 0 && sample <= 30);
792 moduleSample_t *s = &song->samples[sample];
793
794 if (editor.smpRedoBuffer[sample] != NULL)
795 {
796 free(editor.smpRedoBuffer[sample]);
797 editor.smpRedoBuffer[sample] = NULL;
798 }
799
800 editor.smpRedoFinetunes[sample] = s->fineTune;
801 editor.smpRedoVolumes[sample] = s->volume;
802 editor.smpRedoLengths[sample] = s->length;
803 editor.smpRedoLoopStarts[sample] = s->loopStart;
804 editor.smpRedoLoopLengths[sample] = s->loopLength;
805
806 if (s->length > 0)
807 {
808 editor.smpRedoBuffer[sample] = (int8_t *)malloc(s->length);
809 if (editor.smpRedoBuffer[sample] != NULL)
810 memcpy(editor.smpRedoBuffer[sample], &song->sampleData[s->offset], s->length);
811 }
812 }
813
814 bool allocSamplerVars(void) // must be called after config is loaded
815 {
816 sampler.copyBuf = (int8_t *)malloc(config.maxSampleLength);
817 sampler.blankSample = (int8_t *)calloc(config.maxSampleLength, 1);
818 sampler.sampleUndoCopy = (int8_t *)calloc(config.maxSampleLength, 1);
819
820 if (sampler.copyBuf == NULL || sampler.blankSample == NULL || sampler.sampleUndoCopy == NULL)
821 return false;
822
823 return true;
824 }
825
826 void deAllocSamplerVars(void)
827 {
828 if (sampler.copyBuf != NULL)
829 {
830 free(sampler.copyBuf);
831 sampler.copyBuf = NULL;
832 }
833
834 if (sampler.blankSample != NULL)
835 {
836 free(sampler.blankSample);
837 sampler.blankSample = NULL;
838 }
839
840 if (sampler.sampleUndoCopy != NULL)
841 {
842 free(sampler.sampleUndoCopy);
843 sampler.sampleUndoCopy = NULL;
844 }
845
846 for (int32_t i = 0; i < MOD_SAMPLES; i++)
847 {
848 if (editor.smpRedoBuffer[i] != NULL)
849 {
850 free(editor.smpRedoBuffer[i]);
851 editor.smpRedoBuffer[i] = NULL;
852 }
853 }
854 }
855
856 void samplerRemoveDcOffset(void)
857 {
858 if (editor.sampleZero)
859 {
860 statusNotSampleZero();
861 return;
862 }
863
864 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
865
866 moduleSample_t *s = &song->samples[editor.currSample];
867 if (s->length == 0)
868 {
869 statusSampleIsEmpty();
870 return;
871 }
872
873 int8_t *smpDat = &song->sampleData[s->offset];
874
875 int32_t from = 0;
876 int32_t to = s->length;
877
878 if (editor.markStartOfs != -1)
879 {
880 from = editor.markStartOfs;
881 to = editor.markEndOfs;
882
883 if (to > s->length)
884 to = s->length;
885
886 if (from == to || from >= s->length || to < from)
887 {
888 from = 0;
889 to = s->length;
890 }
891 }
892
893 if (to <= 0)
894 return;
895
896 // calculate offset value
897 int32_t offset = 0;
898 for (int32_t i = from; i < to; i++)
899 offset += smpDat[i];
900 offset /= to;
901
902 // remove DC offset
903 for (int32_t i = from; i < to; i++)
904 {
905 int32_t smp32 = smpDat[i] - offset;
906 smpDat[i] = (int8_t)(CLAMP(smp32, INT8_MIN, INT8_MAX));
907 }
908
909 fixSampleBeep(s);
910 displaySample();
911 updateWindowTitle(MOD_IS_MODIFIED);
912 }
913
914 #define INTRP_LINEAR_TAPS 2
915 #define INTRP8_LINEAR(s1, s2, f) /* output: -127..128 */ \
916 s2 -= s1; \
917 s2 *= (int32_t)(f >> 16); \
918 s1 <<= 8; \
919 s2 >>= 16-8; \
920 s1 += s2; \
921 s1 >>= 8; \
922
923 void samplerResample(void)
924 {
925 int32_t samples[INTRP_LINEAR_TAPS];
926
927 if (editor.sampleZero)
928 {
929 statusNotSampleZero();
930 return;
931 }
932
933 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
934 ASSERT(editor.tuningNote <= 35 && editor.resampleNote <= 35);
935
936 moduleSample_t *s = &song->samples[editor.currSample];
937 if (s->length == 0)
938 {
939 statusSampleIsEmpty();
940 return;
941 }
942
943 // setup resampling variables
944 int32_t readPos = 0;
945 int32_t writePos = 0;
946 int8_t *writeData = &song->sampleData[s->offset];
947 int16_t refPeriod = periodTable[editor.tuningNote];
948 int16_t newPeriod = periodTable[(37 * (s->fineTune & 0xF)) + editor.resampleNote];
949 int32_t readLength = s->length;
950 int32_t writeLength = (readLength * newPeriod) / refPeriod;
951
952 if (readLength == writeLength)
953 return; // no resampling needed
954
955 // allocate memory for our sample duplicate
956 int8_t *readData = (int8_t *)malloc(s->length);
957 if (readData == NULL)
958 {
959 statusOutOfMemory();
960 return;
961 }
962
963 if (writeLength <= 0)
964 {
965 free(readData);
966 displayErrorMsg("RESAMPLE ERROR !");
967 return;
968 }
969
970 uint64_t delta64 = ((uint64_t)readLength << 32) / writeLength;
971 ASSERT(delta64 != 0);
972
973 writeLength = writeLength & ~1;
974 if (writeLength > config.maxSampleLength)
975 writeLength = config.maxSampleLength;
976
977 memcpy(readData, writeData, readLength);
978
979 // resample
980
981 turnOffVoices();
982
983 uint64_t frac64 = 0;
984 while (writePos < writeLength)
985 {
986 // collect samples for interpolation
987 for (int32_t i = 0; i < INTRP_LINEAR_TAPS; i++)
988 {
989 int32_t pos = readPos + i;
990 if (pos >= readLength)
991 samples[i] = 0;
992 else
993 samples[i] = readData[pos];
994 }
995
996 INTRP8_LINEAR(samples[0], samples[1], frac64);
997 writeData[writePos++] = (int8_t)samples[0];
998
999 frac64 += delta64;
1000 readPos += frac64 >> 32;
1001 frac64 &= UINT32_MAX;
1002 }
1003 free(readData);
1004
1005 // wipe non-used data in new sample
1006 if (writeLength < config.maxSampleLength)
1007 memset(&writeData[writePos], 0, config.maxSampleLength - writeLength);
1008
1009 // update sample attributes
1010 s->length = writeLength;
1011 s->fineTune = 0;
1012
1013 // scale loop points (and deactivate if overflowing)
1014 if ((s->loopStart + s->loopLength) > 2)
1015 {
1016 int32_t loopStart = (int32_t)(((uint64_t)s->loopStart << 32) / delta64) & ~1;
1017 int32_t loopLength = (int32_t)(((uint64_t)s->loopLength << 32) / delta64) & ~1;
1018
1019 if (loopStart+loopLength > s->length)
1020 {
1021 s->loopStart = 0;
1022 s->loopLength = 2;
1023 }
1024 else
1025 {
1026 s->loopStart = loopStart;
1027 s->loopLength = loopLength;
1028 }
1029 }
1030
1031 fixSampleBeep(s);
1032 updateCurrSample();
1033 updateWindowTitle(MOD_IS_MODIFIED);
1034 }
1035
1036 // reads two hex chars from pointer and converts them to one byte
1037 static uint8_t hexToInteger2(char *ptr)
1038 {
1039 char hi = ptr[0];
1040 char lo = ptr[1];
1041
1042 if (hi >= '0' && hi <= '9')
1043 hi -= '0';
1044 else if (hi >= 'A' && hi <= 'F')
1045 hi -= 'A'-10;
1046 else if (hi >= 'a' && hi <= 'f')
1047 hi -= 'a'-10;
1048 else
1049 hi = 0;
1050
1051 if (lo >= '0' && lo <= '9')
1052 lo -= '0';
1053 else if (lo >= 'A' && lo <= 'F')
1054 lo -= 'A'-10;
1055 else if (lo >= 'a' && lo <= 'f')
1056 lo -= 'a'-10;
1057 else
1058 lo = 0;
1059
1060 return (hi << 4) | lo;
1061 }
1062
1063 void doMix(void)
1064 {
1065 int8_t *fromPtr1, *fromPtr2;
1066 int32_t mixLength;
1067
1068 uint8_t smpFrom1 = hexToInteger2(&editor.mixText[4]);
1069 uint8_t smpFrom2 = hexToInteger2(&editor.mixText[7]);
1070 uint8_t smpTo = hexToInteger2(&editor.mixText[13]);
1071
1072 if (smpFrom1 == 0 || smpFrom1 > 0x1F || smpFrom2 == 0 || smpFrom2 > 0x1F || smpTo == 0 || smpTo > 0x1F)
1073 {
1074 displayErrorMsg("NOT RANGE 01-1F !");
1075 return;
1076 }
1077
1078 smpFrom1--;
1079 smpFrom2--;
1080 smpTo--;
1081
1082 moduleSample_t *s1 = &song->samples[smpFrom1];
1083 moduleSample_t *s2 = &song->samples[smpFrom2];
1084 moduleSample_t *s3 = &song->samples[smpTo];
1085
1086 if (s1->length == 0 || s2->length == 0)
1087 {
1088 displayErrorMsg("EMPTY SAMPLES !!!");
1089 return;
1090 }
1091
1092 if (s1->length > s2->length)
1093 {
1094 fromPtr1 = &song->sampleData[s1->offset];
1095 fromPtr2 = &song->sampleData[s2->offset];
1096 mixLength = s1->length;
1097 }
1098 else
1099 {
1100 fromPtr1 = &song->sampleData[s2->offset];
1101 fromPtr2 = &song->sampleData[s1->offset];
1102 mixLength = s2->length;
1103 }
1104
1105 int8_t *mixPtr = (int8_t *)malloc(mixLength);
1106 if (mixPtr == NULL)
1107 {
1108 statusOutOfMemory();
1109 return;
1110 }
1111
1112 turnOffVoices();
1113
1114 for (int32_t i = 0; i < mixLength; i++)
1115 {
1116 int16_t tmp16 = (i < s2->length) ? (fromPtr1[i] + fromPtr2[i]) : fromPtr1[i];
1117 if (editor.halveSampleFlag)
1118 tmp16 >>= 1;
1119
1120 mixPtr[i] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
1121 }
1122
1123 memcpy(&song->sampleData[s3->offset], mixPtr, mixLength);
1124 if (mixLength < config.maxSampleLength) // clear unused part of sample
1125 memset(&song->sampleData[s3->offset + mixLength], 0, config.maxSampleLength - mixLength);
1126
1127 free(mixPtr);
1128
1129 s3->length = mixLength;
1130 s3->volume = 64;
1131 s3->fineTune = 0;
1132 s3->loopStart = 0;
1133 s3->loopLength = 2;
1134
1135 editor.currSample = smpTo;
1136 editor.samplePos = 0;
1137
1138 fixSampleBeep(s3);
1139 updateCurrSample();
1140 updateWindowTitle(MOD_IS_MODIFIED);
1141 }
1142
1143 // this is actually treble increase
1144 void boostSample(int32_t sample, bool ignoreMark)
1145 {
1146 ASSERT(sample >= 0 && sample <= 30);
1147
1148 moduleSample_t *s = &song->samples[sample];
1149 if (s->length == 0)
1150 return; // don't display warning/show warning pointer, it is done elsewhere
1151
1152 int8_t *smpDat = &song->sampleData[s->offset];
1153
1154 int32_t from = 0;
1155 int32_t to = s->length;
1156
1157 if (!ignoreMark)
1158 {
1159 if (editor.markStartOfs != -1)
1160 {
1161 from = editor.markStartOfs;
1162 to = editor.markEndOfs;
1163
1164 if (to > s->length)
1165 to = s->length;
1166
1167 if (from == to || from >= s->length || to < from)
1168 {
1169 from = 0;
1170 to = s->length;
1171 }
1172 }
1173 }
1174
1175 int8_t prevSmp = 0;
1176 for (int32_t i = from; i < to; i++)
1177 {
1178 int16_t tmp16 = smpDat[i] - prevSmp;
1179
1180 // hackery to make it ProTracker accurate
1181 int16_t smp16;
1182 if (tmp16 < 0)
1183 smp16 = smpDat[i] - (-tmp16 >> 2);
1184 else
1185 smp16 = smpDat[i] + ( tmp16 >> 2);
1186
1187 prevSmp = smpDat[i];
1188
1189 smpDat[i] = (int8_t)(CLAMP(smp16, INT8_MIN, INT8_MAX));
1190 }
1191
1192 fixSampleBeep(s);
1193
1194 // don't redraw sample here, it is done elsewhere
1195 }
1196
1197 // this is actually treble decrease
1198 void filterSample(int32_t sample, bool ignoreMark)
1199 {
1200 ASSERT(sample >= 0 && sample <= 30);
1201
1202 moduleSample_t *s = &song->samples[sample];
1203 if (s->length == 0)
1204 return; // don't display warning/show warning pointer, it is done elsewhere
1205
1206 int8_t *smpDat = &song->sampleData[s->offset];
1207
1208 int32_t from = 1;
1209 int32_t to = s->length;
1210
1211 if (!ignoreMark)
1212 {
1213 if (editor.markStartOfs != -1)
1214 {
1215 from = editor.markStartOfs;
1216 to = editor.markEndOfs;
1217
1218 if (to > s->length)
1219 to = s->length;
1220
1221 if (from == to || from >= s->length || to < from)
1222 {
1223 from = 0;
1224 to = s->length;
1225 }
1226 }
1227 }
1228
1229 if (to < 1)
1230 return;
1231
1232 to--;
1233 for (int32_t i = from; i < to; i++)
1234 smpDat[i] = (smpDat[i+0] + smpDat[i+1]) >> 1;
1235
1236 fixSampleBeep(s);
1237 // don't redraw sample here, it is done elsewhere
1238 }
1239
1240 void toggleTuningTone(void)
1241 {
1242 // bugfix: don't allow tuning tone during play (it was very bugged anyway)
1243 if (editor.currMode == MODE_PLAY || editor.currMode == MODE_RECORD)
1244 return;
1245
1246 editor.tuningToneFlag ^= 1;
1247 if (editor.tuningToneFlag)
1248 {
1249 // turn tuning tone on
1250
1251 const int32_t chNum = (cursor.channel + 1) & 3;
1252 TToneBit = 1 << chNum;
1253
1254 lockAudio();
1255
1256 const uint32_t voiceAddr = 0xDFF0A0 + (chNum * 16);
1257
1258 paulaWriteWord(0xDFF096, TToneBit); // voice DMA off
1259
1260 paulaWriteWord(voiceAddr + 6, periodTable[editor.tuningNote]);
1261 paulaWriteWord(voiceAddr + 8, 64); // volume
1262 paulaWritePtr(voiceAddr + 0, tuneToneData);
1263 paulaWriteWord(voiceAddr + 4, sizeof (tuneToneData) / 2); // length
1264
1265 paulaWriteWord(0xDFF096, 0x8000 | TToneBit); // voice DMA on
1266
1267 // update tracker visuals
1268 setVisualsDMACON(TToneBit);
1269 setVisualsPeriod(chNum, periodTable[editor.tuningNote]);
1270 setVisualsVolume(chNum, 64);
1271 setVisualsDataPtr(chNum, tuneToneData);
1272 setVisualsLength(chNum, sizeof (tuneToneData) / 2);
1273 setVisualsDMACON(0x8000 | TToneBit);
1274
1275 unlockAudio();
1276 }
1277 else
1278 {
1279 // turn tuning tone off
1280
1281 lockAudio();
1282 paulaWriteWord(0xDFF096, TToneBit); // voice DMA off
1283 setVisualsDMACON(TToneBit);
1284 unlockAudio();
1285 }
1286 }
1287
1288 void sampleMarkerToBeg(void)
1289 {
1290 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1291
1292 moduleSample_t *s = &song->samples[editor.currSample];
1293 if (s->length == 0)
1294 {
1295 invertRange();
1296 editor.markStartOfs = -1;
1297 editor.samplePos = 0;
1298 }
1299 else
1300 {
1301 invertRange();
1302 if (keyb.shiftPressed && editor.markStartOfs != -1)
1303 {
1304 editor.markStartOfs = sampler.samOffset;
1305 }
1306 else
1307 {
1308 editor.markStartOfs = sampler.samOffset;
1309 editor.markEndOfs = editor.markStartOfs;
1310 }
1311 invertRange();
1312
1313 editor.samplePos = editor.markEndOfs;
1314 }
1315
1316 updateSamplePos();
1317 }
1318
1319 void sampleMarkerToCenter(void)
1320 {
1321 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1322
1323 moduleSample_t *s = &song->samples[editor.currSample];
1324 if (s->length == 0)
1325 {
1326 invertRange();
1327 editor.markStartOfs = -1;
1328 editor.samplePos = 0;
1329 }
1330 else
1331 {
1332 int32_t middlePos = sampler.samOffset + ((sampler.samDisplay + 1) / 2);
1333
1334 invertRange();
1335 if (keyb.shiftPressed && editor.markStartOfs != -1)
1336 {
1337 if (editor.markStartOfs < middlePos)
1338 editor.markEndOfs = middlePos;
1339 else if (editor.markEndOfs > middlePos)
1340 editor.markStartOfs = middlePos;
1341 }
1342 else
1343 {
1344 editor.markStartOfs = middlePos;
1345 editor.markEndOfs = editor.markStartOfs;
1346 }
1347 invertRange();
1348
1349 editor.samplePos = editor.markEndOfs;
1350 }
1351
1352 updateSamplePos();
1353 }
1354
1355 void sampleMarkerToEnd(void)
1356 {
1357 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1358
1359 moduleSample_t *s = &song->samples[editor.currSample];
1360 if (s->length == 0)
1361 {
1362 invertRange();
1363 editor.markStartOfs = -1;
1364 editor.samplePos = 0;
1365 }
1366 else
1367 {
1368 invertRange();
1369 if (keyb.shiftPressed && editor.markStartOfs != -1)
1370 {
1371 editor.markEndOfs = s->length;
1372 }
1373 else
1374 {
1375 editor.markStartOfs = s->length;
1376 editor.markEndOfs = editor.markStartOfs;
1377 }
1378 invertRange();
1379
1380 editor.samplePos = editor.markEndOfs;
1381 }
1382
1383 updateSamplePos();
1384 }
1385
1386 void samplerSamCopy(void)
1387 {
1388 if (editor.sampleZero)
1389 {
1390 statusNotSampleZero();
1391 return;
1392 }
1393
1394 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1395 moduleSample_t *s = &song->samples[editor.currSample];
1396
1397 if (s->length == 0)
1398 {
1399 statusSampleIsEmpty();
1400 return;
1401 }
1402
1403 // copy sample/slice to new sample slot if shift is held down (new PT feature)
1404 if (keyb.shiftPressed)
1405 {
1406 // find free sample slot
1407 int8_t newSmpNum = 0;
1408 moduleSample_t *dstSmp = song->samples;
1409 for (; newSmpNum < MOD_SAMPLES; newSmpNum++, dstSmp++)
1410 {
1411 if (dstSmp->length == 0)
1412 break;
1413 }
1414
1415 if (newSmpNum == MOD_SAMPLES)
1416 {
1417 displayErrorMsg("NO FREE SMP SLOT!");
1418 return;
1419 }
1420
1421 const int32_t markLength = editor.markEndOfs - editor.markStartOfs;
1422 if (editor.markStartOfs == -1 || markLength <= 0)
1423 {
1424 // no sample data marked, copy whole sample
1425
1426 if (!askBox(ASKBOX_YES_NO, "CLONE SAMPLE ?"))
1427 return;
1428
1429 dstSmp->fineTune = s->fineTune;
1430 dstSmp->length = s->length;
1431 dstSmp->loopLength = s->loopLength;
1432 dstSmp->loopStart = s->loopStart;
1433 dstSmp->volume = s->volume;
1434
1435 memset(dstSmp->text, '\0', sizeof (dstSmp->text));
1436 strcpy(dstSmp->text, "[CLONED SAMPLE]");
1437
1438 // copy over sample data
1439 memcpy(&song->sampleData[dstSmp->offset], &song->sampleData[s->offset], s->length);
1440 }
1441 else
1442 {
1443 // sample data marked, copy slice only
1444
1445 if (!askBox(ASKBOX_YES_NO, "COPY SMP SLICE ?"))
1446 return;
1447
1448 dstSmp->fineTune = s->fineTune;
1449 dstSmp->length = markLength;
1450 dstSmp->loopLength = 2;
1451 dstSmp->loopStart = 0;
1452 dstSmp->volume = s->volume;
1453
1454 memset(dstSmp->text, '\0', sizeof (dstSmp->text));
1455 strcpy(dstSmp->text, "[COPIED SAMPLE SLICE]");
1456
1457 // copy over sample data slice
1458 memcpy(&song->sampleData[dstSmp->offset], &song->sampleData[s->offset+editor.markStartOfs],markLength);
1459
1460 // new data must have its first two bytes zeroed to prevent beep syndrome
1461 fixSampleBeep(dstSmp);
1462 }
1463
1464 editor.samplePos = 0;
1465 editor.currSample = newSmpNum;
1466 updateCurrSample();
1467
1468 return;
1469 }
1470
1471 if (editor.markStartOfs == -1)
1472 {
1473 displayErrorMsg("NO RANGE SELECTED");
1474 return;
1475 }
1476
1477 if (editor.markEndOfs-editor.markStartOfs <= 0)
1478 {
1479 displayErrorMsg("SET LARGER RANGE");
1480 return;
1481 }
1482
1483 sampler.copyBufSize = editor.markEndOfs - editor.markStartOfs;
1484
1485 if ((int32_t)(editor.markStartOfs + sampler.copyBufSize) > config.maxSampleLength)
1486 {
1487 displayErrorMsg("COPY ERROR !");
1488 return;
1489 }
1490
1491 memcpy(sampler.copyBuf, &song->sampleData[s->offset+editor.markStartOfs], sampler.copyBufSize);
1492 }
1493
1494 void samplerSamDelete(uint8_t cut)
1495 {
1496 if (editor.sampleZero)
1497 {
1498 statusNotSampleZero();
1499 return;
1500 }
1501
1502 if (editor.markStartOfs == -1)
1503 {
1504 displayErrorMsg("NO RANGE SELECTED");
1505 return;
1506 }
1507
1508 if (editor.markEndOfs-editor.markStartOfs <= 0)
1509 {
1510 displayErrorMsg("SET LARGER RANGE");
1511 return;
1512 }
1513
1514 if (cut)
1515 samplerSamCopy();
1516
1517 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1518 moduleSample_t *s = &song->samples[editor.currSample];
1519
1520 int32_t sampleLength = s->length;
1521 if (sampleLength == 0)
1522 {
1523 statusSampleIsEmpty();
1524 return;
1525 }
1526
1527 turnOffVoices();
1528
1529 // if whole sample is marked, wipe it
1530 if (editor.markEndOfs-editor.markStartOfs >= sampleLength)
1531 {
1532 memset(&song->sampleData[s->offset], 0, config.maxSampleLength);
1533
1534 invertRange();
1535 editor.markStartOfs = -1;
1536
1537 sampler.samStart = sampler.blankSample;
1538 sampler.samDisplay = SAMPLE_AREA_WIDTH;
1539 sampler.samLength = SAMPLE_AREA_WIDTH;
1540
1541 s->length = 0;
1542 s->loopStart = 0;
1543 s->loopLength = 2;
1544 s->volume = 0;
1545 s->fineTune = 0;
1546
1547 editor.samplePos = 0;
1548 updateCurrSample();
1549
1550 updateWindowTitle(MOD_IS_MODIFIED);
1551 return;
1552 }
1553
1554 int32_t markEnd = (editor.markEndOfs > sampleLength) ? sampleLength : editor.markEndOfs;
1555 int32_t markStart = editor.markStartOfs;
1556
1557 int32_t copyLength = (editor.markStartOfs + sampleLength) - markEnd;
1558 if (copyLength < 2 || copyLength > config.maxSampleLength)
1559 {
1560 displayErrorMsg("SAMPLE CUT FAIL !");
1561 return;
1562 }
1563
1564 int8_t *tmpBuf = (int8_t *)malloc(copyLength);
1565 if (tmpBuf == NULL)
1566 {
1567 statusOutOfMemory();
1568 return;
1569 }
1570
1571 // copy start part
1572 memcpy(tmpBuf, &song->sampleData[s->offset], editor.markStartOfs);
1573
1574 // copy end part
1575 if (sampleLength-markEnd > 0)
1576 memcpy(&tmpBuf[editor.markStartOfs], &song->sampleData[s->offset+markEnd], sampleLength - markEnd);
1577
1578 // wipe sample data and copy over the result
1579 memcpy(&song->sampleData[s->offset], tmpBuf, copyLength);
1580
1581 if (copyLength < config.maxSampleLength)
1582 memset(&song->sampleData[s->offset+copyLength], 0, config.maxSampleLength - copyLength);
1583
1584 free(tmpBuf);
1585
1586 sampler.samLength = copyLength;
1587 if (sampler.samOffset+sampler.samDisplay >= sampler.samLength)
1588 {
1589 if (sampler.samDisplay < sampler.samLength)
1590 {
1591 if (sampler.samLength-sampler.samDisplay < 0)
1592 {
1593 sampler.samOffset = 0;
1594 sampler.samDisplay = sampler.samLength;
1595 }
1596 else
1597 {
1598 sampler.samOffset = sampler.samLength - sampler.samDisplay;
1599 }
1600 }
1601 else
1602 {
1603 sampler.samOffset = 0;
1604 sampler.samDisplay = sampler.samLength;
1605 }
1606
1607 updateSamOffset();
1608 }
1609
1610 if (s->loopStart+s->loopLength > 2) // loop enabled?
1611 {
1612 if (markEnd > s->loopStart)
1613 {
1614 if (markStart < s->loopStart+s->loopLength)
1615 {
1616 // we cut data inside the loop, increase loop length
1617 int32_t val32 = (s->loopLength - (markEnd - markStart)) & ~1;
1618 if (val32 < 2)
1619 val32 = 2;
1620
1621 s->loopLength = val32;
1622 }
1623
1624 // we cut data after the loop, don't modify loop points
1625 }
1626 else
1627 {
1628 // we cut data before the loop, adjust loop start point
1629 int32_t val32 = (s->loopStart - (markEnd - markStart)) & ~1;
1630 if (val32 < 0)
1631 {
1632 s->loopStart = 0;
1633 s->loopLength = 2;
1634 }
1635 else
1636 {
1637 s->loopStart = val32;
1638 }
1639 }
1640 }
1641
1642 s->length = copyLength & ~1;
1643
1644 // disable loop if invalid
1645 if (s->loopStart+s->loopLength > s->length)
1646 {
1647 s->loopStart = 0;
1648 s->loopLength = 2;
1649 }
1650
1651 if (sampler.samDisplay <= 2)
1652 {
1653 sampler.samStart = sampler.blankSample;
1654 sampler.samLength = SAMPLE_AREA_WIDTH;
1655 sampler.samDisplay = SAMPLE_AREA_WIDTH;
1656 }
1657
1658 invertRange();
1659 if (sampler.samDisplay == 0)
1660 {
1661 editor.markStartOfs = -1; // clear marking
1662 }
1663 else
1664 {
1665 if (editor.markStartOfs >= s->length)
1666 editor.markStartOfs = s->length - 1;
1667
1668 editor.markEndOfs = editor.markStartOfs;
1669 invertRange();
1670 }
1671
1672 editor.samplePos = editor.markStartOfs;
1673 fixSampleBeep(s);
1674 updateSamplePos();
1675 recalcChordLength();
1676 displaySample();
1677
1678 ui.updateCurrSampleLength = true;
1679 ui.updateCurrSampleRepeat = true;
1680 ui.updateCurrSampleReplen = true;
1681 ui.updateSongSize = true;
1682
1683 updateWindowTitle(MOD_IS_MODIFIED);
1684 }
1685
1686 void samplerSamPaste(void)
1687 {
1688 if (editor.sampleZero)
1689 {
1690 statusNotSampleZero();
1691 return;
1692 }
1693
1694 if (sampler.copyBuf == NULL || sampler.copyBufSize == 0)
1695 {
1696 displayErrorMsg("BUFFER IS EMPTY");
1697 return;
1698 }
1699
1700 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1701
1702 moduleSample_t *s = &song->samples[editor.currSample];
1703 if (s->length > 0 && editor.markStartOfs == -1)
1704 {
1705 displayErrorMsg("SET CURSOR POS");
1706 return;
1707 }
1708
1709 bool sampleWasEmpty = (s->length == 0);
1710 int32_t markStart = sampleWasEmpty ? 0 : editor.markStartOfs;
1711
1712 if (s->length+sampler.copyBufSize > config.maxSampleLength)
1713 {
1714 displayErrorMsg("NOT ENOUGH ROOM");
1715 return;
1716 }
1717
1718 int8_t *tmpBuf = (int8_t *)malloc(config.maxSampleLength);
1719 if (tmpBuf == NULL)
1720 {
1721 statusOutOfMemory();
1722 return;
1723 }
1724
1725 uint32_t readPos = 0;
1726 turnOffVoices();
1727 bool wasZooming = (sampler.samDisplay != sampler.samLength);
1728
1729 // copy start part
1730 if (markStart > 0)
1731 {
1732 memcpy(&tmpBuf[readPos], &song->sampleData[s->offset], markStart);
1733 readPos += markStart;
1734 }
1735
1736 // copy actual buffer
1737 memcpy(&tmpBuf[readPos], sampler.copyBuf, sampler.copyBufSize);
1738
1739 // copy end part
1740 if (markStart >= 0)
1741 {
1742 readPos += sampler.copyBufSize;
1743
1744 if (s->length-markStart > 0)
1745 memcpy(&tmpBuf[readPos], &song->sampleData[s->offset+markStart], s->length - markStart);
1746 }
1747
1748 int32_t newLength = (s->length + sampler.copyBufSize) & ~1;
1749 if (newLength > config.maxSampleLength)
1750 newLength = config.maxSampleLength;
1751
1752 sampler.samLength = s->length = newLength;
1753
1754 if (s->loopLength > 2) // loop enabled?
1755 {
1756 if (markStart > s->loopStart)
1757 {
1758 if (markStart < s->loopStart+s->loopLength)
1759 {
1760 // we pasted data inside the loop, increase loop length
1761
1762 if (s->loopLength+sampler.copyBufSize > config.maxSampleLength)
1763 {
1764 s->loopStart = 0;
1765 s->loopLength = 2;
1766 }
1767 else
1768 {
1769 s->loopLength = (s->loopLength + sampler.copyBufSize) & config.maxSampleLength;
1770 if (s->loopStart+s->loopLength > s->length)
1771 {
1772 s->loopStart = 0;
1773 s->loopLength = 2;
1774 }
1775 }
1776 }
1777
1778 // we pasted data after the loop, don't modify loop points
1779 }
1780 else
1781 {
1782 // we pasted data before the loop, adjust loop start point
1783 if (s->loopStart+sampler.copyBufSize > config.maxSampleLength)
1784 {
1785 s->loopStart = 0;
1786 s->loopLength = 2;
1787 }
1788 else
1789 {
1790 s->loopStart = (s->loopStart + sampler.copyBufSize) & config.maxSampleLength;
1791 if (s->loopStart+s->loopLength > s->length)
1792 {
1793 s->loopStart = 0;
1794 s->loopLength = 2;
1795 }
1796 }
1797 }
1798 }
1799
1800 memcpy(&song->sampleData[s->offset], tmpBuf, s->length);
1801
1802 // clear data after sample's length (if present)
1803 if (s->length < config.maxSampleLength)
1804 memset(&song->sampleData[s->offset+s->length], 0, config.maxSampleLength - s->length);
1805
1806 free(tmpBuf);
1807
1808 invertRange();
1809 editor.markStartOfs = -1;
1810
1811 fixSampleBeep(s);
1812 updateSamplePos();
1813 recalcChordLength();
1814
1815 if (wasZooming)
1816 displaySample();
1817 else
1818 redrawSample();
1819
1820 // if we pasted into an empty sample, force its volume to max (64)
1821 if (sampleWasEmpty)
1822 {
1823 s->volume = 64;
1824 ui.updateCurrSampleVolume = true;
1825 }
1826
1827 ui.updateCurrSampleLength = true;
1828 ui.updateSongSize = true;
1829
1830 updateWindowTitle(MOD_IS_MODIFIED);
1831 }
1832
1833 static void playCurrSample(uint8_t chn, int32_t startOffset, int32_t endOffset, bool playWaveformFlag)
1834 {
1835 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1836 ASSERT(chn < PAULA_VOICES);
1837 ASSERT(editor.currPlayNote <= 35);
1838
1839 moduleSample_t *s = &song->samples[editor.currSample];
1840 moduleChannel_t *ch = &song->channels[chn];
1841
1842 lockAudio();
1843
1844 ch->n_samplenum = editor.currSample;
1845 ch->n_volume = s->volume;
1846 ch->n_period = periodTable[(37 * (s->fineTune & 0xF)) + editor.currPlayNote];
1847
1848 if (playWaveformFlag)
1849 {
1850 ch->n_start = &song->sampleData[s->offset];
1851 ch->n_length = (uint16_t)((s->loopStart > 0) ? (s->loopStart + s->loopLength) >> 1 : s->length >> 1);
1852 ch->n_loopstart = &song->sampleData[s->offset + s->loopStart];
1853 ch->n_replen = (uint16_t)(s->loopLength >> 1);
1854 }
1855 else
1856 {
1857 ch->n_start = &song->sampleData[s->offset + startOffset];
1858 ch->n_length = (uint16_t)((uint32_t)(endOffset - startOffset) >> 1);
1859 ch->n_loopstart = &song->sampleData[s->offset];
1860 ch->n_replen = 1;
1861 }
1862
1863 if (ch->n_length == 0)
1864 ch->n_length = 1;
1865
1866 const uint32_t voiceAddr = 0xDFF0A0 + (chn * 16);
1867
1868 paulaWriteWord(voiceAddr + 8, ch->n_volume);
1869 paulaWriteWord(voiceAddr + 6, ch->n_period);
1870 paulaWritePtr(voiceAddr + 0, ch->n_start);
1871 paulaWriteWord(voiceAddr + 4, ch->n_length);
1872
1873 if (!editor.muted[chn])
1874 paulaWriteWord(0xDFF096, 0x8000 | ch->n_dmabit); // voice DMA on
1875 else
1876 paulaWriteWord(0xDFF096, ch->n_dmabit); // voice DMA off
1877
1878 // these take effect after the current DMA cycle is done
1879 if (playWaveformFlag)
1880 {
1881 paulaWritePtr(voiceAddr + 0, ch->n_loopstart);
1882 paulaWriteWord(voiceAddr + 4, ch->n_replen);
1883 }
1884 else
1885 {
1886 paulaWritePtr(voiceAddr + 0, NULL); // data
1887 paulaWriteWord(voiceAddr + 4, 1); // length
1888 }
1889
1890 // update tracker visuals
1891
1892 setVisualsVolume(chn, ch->n_volume);
1893 setVisualsPeriod(chn, ch->n_period);
1894 setVisualsDataPtr(chn, ch->n_start);
1895 setVisualsLength(chn, ch->n_length);
1896
1897 if (!editor.muted[chn])
1898 setVisualsDMACON(0x8000 | ch->n_dmabit);
1899 else
1900 setVisualsDMACON(ch->n_dmabit);
1901
1902 if (playWaveformFlag)
1903 {
1904 setVisualsDataPtr(chn, ch->n_loopstart);
1905 setVisualsLength(chn, ch->n_replen);
1906 }
1907 else
1908 {
1909 setVisualsDataPtr(chn, NULL);
1910 setVisualsLength(chn, 1);
1911 }
1912
1913 unlockAudio();
1914
1915 // PT quirk: spectrum analyzer is still handled here even if channel is muted
1916 updateSpectrumAnalyzer(ch->n_volume, ch->n_period);
1917 }
1918
1919 void samplerPlayWaveform(void)
1920 {
1921 playCurrSample(cursor.channel, 0, 0, true);
1922 }
1923
1924 void samplerPlayDisplay(void)
1925 {
1926 int32_t start = sampler.samOffset;
1927 int32_t end = sampler.samOffset + sampler.samDisplay;
1928
1929 playCurrSample(cursor.channel, start, end, false);
1930 }
1931
1932 void samplerPlayRange(void)
1933 {
1934 if (editor.markStartOfs == -1)
1935 {
1936 displayErrorMsg("NO RANGE SELECTED");
1937 return;
1938 }
1939
1940 if (editor.markEndOfs-editor.markStartOfs < 2)
1941 {
1942 displayErrorMsg("SET LARGER RANGE");
1943 return;
1944 }
1945
1946 playCurrSample(cursor.channel, editor.markStartOfs, editor.markEndOfs, false);
1947 }
1948
1949 void setLoopSprites(void)
1950 {
1951 if (!ui.samplerScreenShown)
1952 {
1953 hideSprite(SPRITE_LOOP_PIN_LEFT);
1954 hideSprite(SPRITE_LOOP_PIN_RIGHT);
1955 return;
1956 }
1957
1958 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
1959
1960 moduleSample_t *s = &song->samples[editor.currSample];
1961 if (s->loopStart+s->loopLength > 2)
1962 {
1963 if (sampler.samDisplay > 0)
1964 {
1965 sampler.loopStartPos = (int16_t)smpPos2Scr(s->loopStart);
1966 if (sampler.loopStartPos >= 0 && sampler.loopStartPos <= SAMPLE_AREA_WIDTH)
1967 setSpritePos(SPRITE_LOOP_PIN_LEFT, sampler.loopStartPos, 138);
1968 else
1969 hideSprite(SPRITE_LOOP_PIN_LEFT);
1970
1971 sampler.loopEndPos = (int16_t)smpPos2Scr(s->loopStart + s->loopLength);
1972
1973 /* Nasty kludge for where the right loop pin would sometimes disappear
1974 ** when zoomed in and scrolled all the way to the right.
1975 */
1976 if (sampler.loopEndPos == SAMPLE_AREA_WIDTH+1)
1977 sampler.loopEndPos = SAMPLE_AREA_WIDTH;
1978
1979 if (sampler.loopEndPos >= 0 && sampler.loopEndPos <= SAMPLE_AREA_WIDTH)
1980 setSpritePos(SPRITE_LOOP_PIN_RIGHT, sampler.loopEndPos + 3, 138);
1981 else
1982 hideSprite(SPRITE_LOOP_PIN_RIGHT);
1983 }
1984 }
1985 else
1986 {
1987 sampler.loopStartPos = 0;
1988 sampler.loopEndPos = 0;
1989
1990 hideSprite(SPRITE_LOOP_PIN_LEFT);
1991 hideSprite(SPRITE_LOOP_PIN_RIGHT);
1992 }
1993
1994 textOutBg(288, 225, (s->loopStart+s->loopLength > 2) ? "ON " : "OFF", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
1995 }
1996
1997 void samplerShowAll(void)
1998 {
1999 if (sampler.samDisplay == sampler.samLength)
2000 return; // don't attempt to show all if already showing all!
2001
2002 sampler.samOffset = 0;
2003 sampler.samDisplay = sampler.samLength;
2004
2005 updateSamOffset();
2006 displaySample();
2007 }
2008
2009 static void samplerZoomIn(int32_t step, int32_t x)
2010 {
2011 if (song->samples[editor.currSample].length == 0 || sampler.samDisplay <= 2)
2012 return;
2013
2014 if (step < 1)
2015 step = 1;
2016
2017 int32_t tmpDisplay = sampler.samDisplay - (step << 1);
2018 if (tmpDisplay < 2)
2019 tmpDisplay = 2;
2020
2021 const int32_t roundingBias = SCREEN_W / 4;
2022
2023 step += (((x - (SCREEN_W / 2)) * step) + roundingBias) / (SCREEN_W / 2);
2024
2025 int32_t tmpOffset = sampler.samOffset + step;
2026 if (tmpOffset < 0)
2027 tmpOffset = 0;
2028
2029 if (tmpOffset+tmpDisplay > sampler.samLength)
2030 tmpOffset = sampler.samLength-tmpDisplay;
2031
2032 sampler.samOffset = tmpOffset;
2033 sampler.samDisplay = tmpDisplay;
2034
2035 updateSamOffset();
2036 displaySample();
2037 }
2038
2039 static void samplerZoomOut(int32_t step, int32_t x)
2040 {
2041 int32_t tmpOffset;
2042
2043 if (song->samples[editor.currSample].length == 0 || sampler.samDisplay == sampler.samLength)
2044 return;
2045
2046 if (step < 1)
2047 step = 1;
2048
2049 int32_t tmpDisplay = sampler.samDisplay + (step << 1);
2050 if (tmpDisplay > sampler.samLength)
2051 {
2052 tmpOffset = 0;
2053 tmpDisplay = sampler.samLength;
2054 }
2055 else
2056 {
2057 const int32_t roundingBias = SCREEN_W / 4;
2058
2059 step += (((x - (SCREEN_W / 2)) * step) + roundingBias) / (SCREEN_W / 2);
2060
2061 tmpOffset = sampler.samOffset - step;
2062 if (tmpOffset < 0)
2063 tmpOffset = 0;
2064
2065 if (tmpOffset+tmpDisplay > sampler.samLength)
2066 tmpOffset = sampler.samLength-tmpDisplay;
2067 }
2068
2069 sampler.samOffset = tmpOffset;
2070 sampler.samDisplay = tmpDisplay;
2071
2072 updateSamOffset();
2073 displaySample();
2074 }
2075
2076 void samplerZoomInMouseWheel(void)
2077 {
2078 samplerZoomIn((sampler.samDisplay + 5) / 10, mouse.x);
2079 }
2080
2081 void samplerZoomOutMouseWheel(void)
2082 {
2083 samplerZoomOut((sampler.samDisplay + 5) / 10, mouse.x);
2084 }
2085
2086 void samplerZoomOut2x(void)
2087 {
2088 samplerZoomOut((sampler.samDisplay + 1) / 2, SCREEN_W / 2);
2089 }
2090
2091 void samplerRangeAll(void)
2092 {
2093 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
2094
2095 moduleSample_t *s = &song->samples[editor.currSample];
2096 if (s->length == 0)
2097 {
2098 invertRange();
2099 editor.markStartOfs = -1;
2100 }
2101 else
2102 {
2103 invertRange();
2104 editor.markStartOfs = sampler.samOffset;
2105 editor.markEndOfs = sampler.samOffset + sampler.samDisplay;
2106 invertRange();
2107 }
2108 }
2109
2110 void samplerShowRange(void)
2111 {
2112 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
2113
2114 moduleSample_t *s = &song->samples[editor.currSample];
2115 if (s->length == 0)
2116 {
2117 statusSampleIsEmpty();
2118 return;
2119 }
2120
2121 if (editor.markStartOfs == -1)
2122 {
2123 displayErrorMsg("NO RANGE SELECTED");
2124 return;
2125 }
2126
2127 if (editor.markEndOfs-editor.markStartOfs < 2)
2128 {
2129 displayErrorMsg("SET LARGER RANGE");
2130 return;
2131 }
2132
2133 sampler.samDisplay = editor.markEndOfs - editor.markStartOfs;
2134 sampler.samOffset = editor.markStartOfs;
2135
2136 if (sampler.samDisplay+sampler.samOffset > sampler.samLength)
2137 sampler.samOffset = sampler.samLength-sampler.samDisplay;
2138
2139 updateSamOffset();
2140
2141 invertRange();
2142 editor.markStartOfs = -1;
2143
2144 displaySample();
2145 }
2146
2147 void volBoxBarPressed(bool mouseButtonHeld)
2148 {
2149 if (!mouseButtonHeld)
2150 {
2151 if (mouse.x >= 72 && mouse.x <= 173)
2152 {
2153 if (mouse.y >= 154 && mouse.y <= 174) ui.forceVolDrag = 1;
2154 if (mouse.y >= 165 && mouse.y <= 175) ui.forceVolDrag = 2;
2155 }
2156 }
2157 else
2158 {
2159 if (sampler.lastMouseX != mouse.x)
2160 {
2161 sampler.lastMouseX = mouse.x;
2162 int32_t mouseX = CLAMP(sampler.lastMouseX - 107, 0, 60);
2163
2164 if (ui.forceVolDrag == 1)
2165 {
2166 editor.vol1 = (int16_t)(((mouseX * 200) + (60/2)) / 60); // rounded
2167 ui.updateVolFromText = true;
2168 showVolFromSlider();
2169 }
2170 else if (ui.forceVolDrag == 2)
2171 {
2172 editor.vol2 = (int16_t)(((mouseX * 200) + (60/2)) / 60); // rounded
2173 ui.updateVolToText = true;
2174 showVolToSlider();
2175 }
2176 }
2177 }
2178 }
2179
2180 void samplerBarPressed(bool mouseButtonHeld)
2181 {
2182 if (!mouseButtonHeld)
2183 {
2184 if (mouse.x >= 4 && mouse.x <= 315)
2185 {
2186 if (mouse.x < sampler.dragStart)
2187 {
2188 int32_t tmp32 = sampler.samOffset - sampler.samDisplay;
2189 if (tmp32 < 0)
2190 tmp32 = 0;
2191
2192 if (tmp32 == sampler.samOffset)
2193 return;
2194
2195 sampler.samOffset = tmp32;
2196
2197 updateSamOffset();
2198 displaySample();
2199 return;
2200 }
2201
2202 if (mouse.x > sampler.dragEnd)
2203 {
2204 int32_t tmp32 = sampler.samOffset + sampler.samDisplay;
2205 if (tmp32+sampler.samDisplay <= sampler.samLength)
2206 {
2207 if (tmp32 == sampler.samOffset)
2208 return;
2209
2210 sampler.samOffset = tmp32;
2211 }
2212 else
2213 {
2214 tmp32 = sampler.samLength - sampler.samDisplay;
2215 if (tmp32 == sampler.samOffset)
2216 return;
2217
2218 sampler.samOffset = tmp32;
2219 }
2220
2221 updateSamOffset();
2222 displaySample();
2223 return;
2224 }
2225
2226 sampler.lastSamPos = mouse.x;
2227 sampler.saveMouseX = sampler.lastSamPos - sampler.dragStart;
2228
2229 ui.forceSampleDrag = true;
2230 }
2231 }
2232
2233 if (mouse.x != sampler.lastSamPos)
2234 {
2235 sampler.lastSamPos = mouse.x;
2236
2237 int32_t tmp32 = sampler.lastSamPos - sampler.saveMouseX - 4;
2238 tmp32 = CLAMP(tmp32, 0, SAMPLE_AREA_WIDTH);
2239
2240 tmp32 = ((tmp32 * sampler.samLength) + (311/2)) / 311; // rounded
2241 if (tmp32+sampler.samDisplay <= sampler.samLength)
2242 {
2243 if (tmp32 == sampler.samOffset)
2244 return;
2245
2246 sampler.samOffset = tmp32;
2247 }
2248 else
2249 {
2250 tmp32 = sampler.samLength - sampler.samDisplay;
2251 if (tmp32 == sampler.samOffset)
2252 return;
2253
2254 sampler.samOffset = tmp32;
2255 }
2256
2257 updateSamOffset();
2258 displaySample();
2259 }
2260 }
2261
2262 static int32_t mouseYToSampleY(int32_t my)
2263 {
2264 if (my == SAMPLE_AREA_Y_CENTER) // center
2265 {
2266 return 128;
2267 }
2268 else
2269 {
2270 int32_t tmp32 = my - 138;
2271 tmp32 = ((tmp32 << 8) + (SAMPLE_AREA_HEIGHT/2)) / SAMPLE_AREA_HEIGHT;
2272 tmp32 = CLAMP(tmp32, 0, 255);
2273 tmp32 ^= 0xFF;
2274
2275 return tmp32;
2276 }
2277 }
2278
2279 void samplerEditSample(bool mouseButtonHeld)
2280 {
2281 int32_t p, vl;
2282
2283 if (editor.sampleZero)
2284 {
2285 statusNotSampleZero();
2286 return;
2287 }
2288
2289 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
2290 moduleSample_t *s = &song->samples[editor.currSample];
2291
2292 if (s->length == 0)
2293 {
2294 displayErrorMsg("SAMPLE LENGTH = 0");
2295 return;
2296 }
2297
2298 int32_t mx = mouse.x;
2299 if (mx > 4+SAMPLE_AREA_WIDTH)
2300 mx = 4+SAMPLE_AREA_WIDTH;
2301
2302 int32_t my = mouse.y;
2303
2304 if (!mouseButtonHeld)
2305 {
2306 lastDrawX = scr2SmpPos(mx);
2307 lastDrawY = mouseYToSampleY(my);
2308
2309 ui.forceSampleEdit = true;
2310 updateWindowTitle(MOD_IS_MODIFIED);
2311 }
2312 else if (mx == sampler.lastMouseX && my == sampler.lastMouseY)
2313 {
2314 return; // don't continue if we didn't move the mouse
2315 }
2316
2317 if (mx != sampler.lastMouseX)
2318 p = scr2SmpPos(mx);
2319 else
2320 p = lastDrawX;
2321
2322 if (!keyb.shiftPressed && my != sampler.lastMouseY)
2323 vl = mouseYToSampleY(my);
2324 else
2325 vl = lastDrawY;
2326
2327 sampler.lastMouseX = mx;
2328 sampler.lastMouseY = my;
2329
2330 int32_t r = p;
2331 int32_t rvl = vl;
2332
2333 // swap x/y if needed
2334 if (p > lastDrawX)
2335 {
2336 // swap x
2337 int32_t tmp32 = p;
2338 p = lastDrawX;
2339 lastDrawX = tmp32;
2340
2341 // swap y
2342 tmp32 = lastDrawY;
2343 lastDrawY = vl;
2344 vl = tmp32;
2345 }
2346
2347 int8_t *ptr8 = &song->sampleData[s->offset];
2348
2349 int32_t start = p;
2350 if (start < 0)
2351 start = 0;
2352
2353 int32_t end = lastDrawX+1;
2354 if (end > s->length)
2355 end = s->length;
2356
2357 if (p == lastDrawX)
2358 {
2359 const int8_t smpVal = (int8_t)(vl ^ 0x80);
2360 for (int32_t i = start; i < end; i++)
2361 ptr8[i] = smpVal;
2362 }
2363 else
2364 {
2365 int32_t y = lastDrawY - vl;
2366 int32_t x = lastDrawX - p;
2367
2368 if (x != 0)
2369 {
2370 double dMul = 1.0 / x;
2371 int32_t i = 0;
2372
2373 for (int32_t rl = start; rl < end; rl++)
2374 {
2375 int32_t tvl = y * i;
2376 tvl = (int32_t)(tvl * dMul); // tvl /= x
2377 tvl += vl;
2378 tvl ^= 0x80;
2379
2380 ptr8[rl] = (int8_t)tvl;
2381 i++;
2382 }
2383 }
2384 }
2385
2386 lastDrawY = rvl;
2387 lastDrawX = r;
2388
2389 displaySample();
2390 }
2391
2392 void samplerSamplePressed(bool mouseButtonHeld)
2393 {
2394 if (!mouseButtonHeld)
2395 {
2396 if (!editor.sampleZero && mouse.y < 142)
2397 {
2398 if (mouse.x >= sampler.loopStartPos && mouse.x <= sampler.loopStartPos+3)
2399 {
2400 ui.leftLoopPinMoving = true;
2401 ui.rightLoopPinMoving = false;
2402 ui.sampleMarkingPos = 1;
2403 sampler.lastMouseX = mouse.x;
2404 return;
2405 }
2406 else if (mouse.x >= sampler.loopEndPos+3 && mouse.x <= sampler.loopEndPos+6)
2407 {
2408 ui.rightLoopPinMoving = true;
2409 ui.leftLoopPinMoving = false;
2410 ui.sampleMarkingPos = 1;
2411 sampler.lastMouseX = mouse.x;
2412 return;
2413 }
2414 }
2415 }
2416
2417 int32_t mouseX = CLAMP(mouse.x, 0, SCREEN_W+8); // allow some extra pixels outside of the screen
2418
2419 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
2420 moduleSample_t *s = &song->samples[editor.currSample];
2421
2422 if (ui.leftLoopPinMoving)
2423 {
2424 if (sampler.lastMouseX != mouseX)
2425 {
2426 sampler.lastMouseX = mouseX;
2427
2428 int32_t tmpPos = (scr2SmpPos(mouseX - 1) - s->loopStart) & ~1;
2429 if (tmpPos > config.maxSampleLength)
2430 tmpPos = config.maxSampleLength;
2431
2432 if (s->loopStart+tmpPos >= (s->loopStart+s->loopLength)-2)
2433 {
2434 s->loopStart = (s->loopStart + s->loopLength) - 2;
2435 s->loopLength = 2;
2436 }
2437 else
2438 {
2439 s->loopStart = s->loopStart + tmpPos;
2440
2441 if (s->loopLength-tmpPos > 2)
2442 s->loopLength -= tmpPos;
2443 else
2444 s->loopLength = 2;
2445 }
2446
2447 ui.updateCurrSampleRepeat = true;
2448 ui.updateCurrSampleReplen = true;
2449
2450 setLoopSprites();
2451 updatePaulaLoops();
2452 updateWindowTitle(MOD_IS_MODIFIED);
2453 }
2454
2455 return;
2456 }
2457
2458 if (ui.rightLoopPinMoving)
2459 {
2460 if (sampler.lastMouseX != mouseX)
2461 {
2462 sampler.lastMouseX = mouseX;
2463
2464 s = &song->samples[editor.currSample];
2465
2466 int32_t tmpPos = (scr2SmpPos(mouseX - 4) - s->loopStart) & ~1;
2467 tmpPos = CLAMP(tmpPos, 2, config.maxSampleLength);
2468
2469 s->loopLength = tmpPos;
2470
2471 ui.updateCurrSampleRepeat = true;
2472 ui.updateCurrSampleReplen = true;
2473
2474 setLoopSprites();
2475 updatePaulaLoops();
2476 updateWindowTitle(MOD_IS_MODIFIED);
2477 }
2478
2479 return;
2480 }
2481
2482 if (!mouseButtonHeld)
2483 {
2484 if (mouseX < 0 || mouseX >= SCREEN_W)
2485 return;
2486
2487 ui.sampleMarkingPos = (int16_t)mouseX;
2488 sampler.lastSamPos = ui.sampleMarkingPos;
2489
2490 invertRange();
2491 if (s->length == 0)
2492 {
2493 editor.markStartOfs = -1; // clear marking
2494 }
2495 else
2496 {
2497 editor.markStartOfs = scr2SmpPos(ui.sampleMarkingPos - 3);
2498 editor.markEndOfs = scr2SmpPos(ui.sampleMarkingPos - 3);
2499
2500 if (editor.markEndOfs > s->length)
2501 editor.markEndOfs = s->length;
2502
2503 invertRange();
2504 }
2505
2506 if (s->length == 0)
2507 {
2508 editor.samplePos = 0;
2509 }
2510 else
2511 {
2512 int32_t tmpPos = scr2SmpPos(mouseX - 3);
2513 if (tmpPos > s->length)
2514 tmpPos = s->length;
2515
2516 editor.samplePos = tmpPos;
2517 }
2518
2519 updateSamplePos();
2520
2521 return;
2522 }
2523
2524 mouseX = CLAMP(mouseX, 3, SCREEN_W);
2525
2526 if (mouseX != sampler.lastSamPos)
2527 {
2528 sampler.lastSamPos = mouseX;
2529
2530 invertRange();
2531 if (s->length == 0)
2532 {
2533 editor.markStartOfs = -1; // clear marking
2534 }
2535 else
2536 {
2537 if (sampler.lastSamPos > ui.sampleMarkingPos)
2538 {
2539 editor.markStartOfs = scr2SmpPos(ui.sampleMarkingPos - 3);
2540 editor.markEndOfs = scr2SmpPos(sampler.lastSamPos - 3);
2541 }
2542 else
2543 {
2544 editor.markStartOfs = scr2SmpPos(sampler.lastSamPos - 3);
2545 editor.markEndOfs = scr2SmpPos(ui.sampleMarkingPos - 3);
2546 }
2547
2548 if (editor.markEndOfs > s->length)
2549 editor.markEndOfs = s->length;
2550
2551 invertRange();
2552 }
2553 }
2554
2555 if (s->length == 0)
2556 {
2557 editor.samplePos = 0;
2558 }
2559 else
2560 {
2561 int32_t tmpPos = scr2SmpPos(mouseX - 3);
2562 if (tmpPos > s->length)
2563 tmpPos = s->length;
2564
2565 editor.samplePos = tmpPos;
2566 }
2567
2568 updateSamplePos();
2569 }
2570
2571 void samplerLoopToggle(void)
2572 {
2573 if (editor.sampleZero)
2574 {
2575 statusNotSampleZero();
2576 return;
2577 }
2578
2579 ASSERT(editor.currSample >= 0 && editor.currSample <= 30);
2580
2581 moduleSample_t *s = &song->samples[editor.currSample];
2582 if (s->length < 2)
2583 return;
2584
2585 turnOffVoices();
2586
2587 if (s->loopStart+s->loopLength > 2)
2588 {
2589 // disable loop
2590
2591 sampler.tmpLoopStart = s->loopStart;
2592 sampler.tmpLoopLength = s->loopLength;
2593
2594 s->loopStart = 0;
2595 s->loopLength = 2;
2596 }
2597 else
2598 {
2599 // enable loop
2600
2601 if (sampler.tmpLoopStart == 0 && sampler.tmpLoopLength == 0)
2602 {
2603 s->loopStart = 0;
2604 s->loopLength = s->length;
2605 }
2606 else
2607 {
2608 s->loopStart = sampler.tmpLoopStart;
2609 s->loopLength = sampler.tmpLoopLength;
2610
2611 if (s->loopStart+s->loopLength > s->length)
2612 {
2613 s->loopStart = 0;
2614 s->loopLength = s->length;
2615 }
2616 }
2617 }
2618
2619 ui.updateCurrSampleRepeat = true;
2620 ui.updateCurrSampleReplen = true;
2621
2622 displaySample();
2623 updatePaulaLoops();
2624 recalcChordLength();
2625 updateWindowTitle(MOD_IS_MODIFIED);
2626 }
2627
2628 void exitFromSam(void)
2629 {
2630 ui.samplerScreenShown = false;
2631 memcpy(&video.frameBuffer[121 * SCREEN_W], &trackerFrameBMP[121 * SCREEN_W], 320 * 134 * sizeof (int32_t));
2632
2633 updateCursorPos();
2634 setLoopSprites();
2635
2636 ui.updateStatusText = true;
2637 ui.updateSongSize = true;
2638 ui.updateSongTiming = true;
2639 ui.updateSongBPM = true;
2640 ui.updateCurrPattText = true;
2641 ui.updatePatternData = true;
2642
2643 editor.markStartOfs = -1;
2644 }
2645
2646 void samplerScreen(void)
2647 {
2648 if (ui.samplerScreenShown)
2649 {
2650 exitFromSam();
2651 return;
2652 }
2653
2654 ui.samplerScreenShown = true;
2655 memcpy(&video.frameBuffer[(121 * SCREEN_W)], samplerScreenBMP, 320 * 134 * sizeof (int32_t));
2656 hideSprite(SPRITE_PATTERN_CURSOR);
2657
2658 ui.updateStatusText = true;
2659 ui.updateSongSize = true;
2660 ui.updateSongTiming = true;
2661 ui.updateResampleNote = true;
2662 ui.update9xxPos = true;
2663
2664 redrawSample();
2665 }
2666
2667 void drawSamplerLine(void)
2668 {
2669 hideSprite(SPRITE_SAMPLING_POS_LINE);
2670 if (!ui.samplerScreenShown || ui.samplerVolBoxShown || ui.samplerFiltersBoxShown)
2671 return;
2672
2673 for (int32_t ch = 0; ch < PAULA_VOICES; ch++)
2674 {
2675 int32_t pos = getSampleReadPos(ch);
2676 if (pos >= 0)
2677 {
2678 pos = 3 + smpPos2Scr(pos);
2679 if (pos >= 3 && pos <= 316)
2680 setSpritePos(SPRITE_SAMPLING_POS_LINE, pos, 138);
2681 }
2682 }
2683 }