Debian Codegraph @

src/pt2_chordmaker.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 <stdint.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <math.h>
11 #include "pt2_config.h"
12 #include "pt2_textout.h"
13 #include "pt2_visuals.h"
14 #include "pt2_helpers.h"
15 #include "pt2_tables.h"
16 #include "pt2_sampler.h"
17 #include "pt2_audio.h"
18 #include "pt2_blep.h"
19 #include "pt2_downsample2x.h"
20 #include "pt2_replayer.h"
21
22 #define MAX_NOTES 4
23
24 typedef struct sampleMixer_t
25 {
26 bool active;
27 int32_t length, pos;
28 float fDelta, fPhase, fBlepPhase;
29 } sampleMixer_t;
30
31 static void sortNotes(void)
32 {
33 for (int32_t i = 0; i < 3; i++)
34 {
35 if (editor.note2 == 36)
36 {
37 editor.note2 = editor.note3;
38 editor.note3 = editor.note4;
39 editor.note4 = 36;
40 }
41 }
42
43 for (int32_t i = 0; i < 3; i++)
44 {
45 if (editor.note3 == 36)
46 {
47 editor.note3 = editor.note4;
48 editor.note4 = 36;
49 }
50 }
51 }
52
53 static void removeDuplicateNotes(void)
54 {
55 if (editor.note4 == editor.note3) editor.note4 = 36;
56 if (editor.note4 == editor.note2) editor.note4 = 36;
57 if (editor.note3 == editor.note2) editor.note3 = 36;
58 }
59
60 static void setupMixVoice(sampleMixer_t *v, int32_t length, double dDelta)
61 {
62 v->fDelta = v->fBlepPhase = (float)dDelta;
63
64 v->length = (int32_t)ceil(length / dDelta);
65 if (v->length > 0)
66 v->active = true;
67 }
68
69 // this has 2x oversampling for BLEP to function properly with all pitches
70 void mixChordSample(void)
71 {
72 char smpText[22+1];
73 int32_t i;
74 sampleMixer_t mixCh[MAX_NOTES];
75 blep_t bleps[MAX_NOTES];
76
77 if (editor.sampleZero)
78 {
79 statusNotSampleZero();
80 return;
81 }
82
83 if (editor.note1 == 36)
84 {
85 displayErrorMsg("NO BASENOTE!");
86 return;
87 }
88
89 moduleSample_t *s = &song->samples[editor.currSample];
90 if (s->length == 0)
91 {
92 statusSampleIsEmpty();
93 return;
94 }
95
96 // check if all notes are the same (illegal)
97 bool sameNotes = true;
98 if (editor.note2 != 36 && editor.note2 != editor.note1) sameNotes = false; else editor.note2 = 36;
99 if (editor.note3 != 36 && editor.note3 != editor.note1) sameNotes = false; else editor.note3 = 36;
100 if (editor.note4 != 36 && editor.note4 != editor.note1) sameNotes = false; else editor.note4 = 36;
101
102 if (sameNotes)
103 {
104 displayErrorMsg("ONLY ONE NOTE!");
105 return;
106 }
107
108 sortNotes();
109 removeDuplicateNotes();
110
111 ui.updateChordNote1Text = true;
112 ui.updateChordNote2Text = true;
113 ui.updateChordNote3Text = true;
114 ui.updateChordNote4Text = true;
115
116 // setup some variables
117 int32_t smpLoopStart = s->loopStart;
118 int32_t smpLoopLength = s->loopLength;
119 bool smpLoopFlag = (smpLoopStart + smpLoopLength) > 2;
120 int32_t smpEnd = smpLoopFlag ? (smpLoopStart + smpLoopLength) : s->length;
121 int8_t *smpData = &song->sampleData[s->offset];
122
123 if (editor.newOldFlag == 0)
124 {
125 // find a free sample slot for the new sample
126
127 for (i = 0; i < MOD_SAMPLES; i++)
128 {
129 if (song->samples[i].length == 0)
130 break;
131 }
132
133 if (i == MOD_SAMPLES)
134 {
135 displayErrorMsg("NO EMPTY SAMPLE!");
136 return;
137 }
138
139 uint8_t smpFinetune = s->fineTune;
140 int8_t smpVolume = s->volume;
141 memcpy(smpText, s->text, sizeof (smpText));
142
143 s = &song->samples[i];
144 s->fineTune = smpFinetune;
145 s->volume = smpVolume;
146
147 memcpy(s->text, smpText, sizeof (smpText));
148 editor.currSample = (int8_t)i;
149 }
150
151 float *fMixData = (float *)calloc(config.maxSampleLength*2, sizeof (float));
152 if (fMixData == NULL)
153 {
154 statusOutOfMemory();
155 return;
156 }
157
158 s->length = smpLoopFlag ? config.maxSampleLength : editor.chordLength; // if sample loops, set max length
159 s->loopLength = 2;
160 s->loopStart = 0;
161 s->text[21] = '!'; // chord sample indicator
162 s->text[22] = '\0';
163
164 // clear mix voices (also clears position and frac etc.)
165 memset(mixCh, 0, sizeof (mixCh));
166
167 const int32_t outputNote = 24; // C-3 (16574.276Hz)
168 const double dOutputHz = (PAULA_PAL_CLK / (double)periodTable[outputNote]) * 2.0; // *2 for 2x oversampling
169
170 // set voice pitches
171 const double dBaseClk = PAULA_PAL_CLK / dOutputHz;
172 const int16_t *periods = &periodTable[(s->fineTune & 0xF) * 37];
173 if (editor.note1 < 36) setupMixVoice(&mixCh[0], smpEnd, dBaseClk / periods[editor.note1]);
174 if (editor.note2 < 36) setupMixVoice(&mixCh[1], smpEnd, dBaseClk / periods[editor.note2]);
175 if (editor.note3 < 36) setupMixVoice(&mixCh[2], smpEnd, dBaseClk / periods[editor.note3]);
176 if (editor.note4 < 36) setupMixVoice(&mixCh[3], smpEnd, dBaseClk / periods[editor.note4]);
177
178 // start mixing
179 memset(bleps, 0, sizeof (bleps));
180 turnOffVoices();
181
182 sampleMixer_t *v = mixCh;
183 blep_t *bSmp = bleps;
184
185 for (i = 0; i < MAX_NOTES; i++, v++, bSmp++)
186 {
187 if (!v->active || v->fDelta == 0.0f)
188 continue;
189
190 for (int32_t j = 0; j < config.maxSampleLength*2; j++) // *2 for 2x oversampling
191 {
192 float fSmp = smpData[v->pos] * (1.0f / 128.0f);
193
194 if (fSmp != bSmp->fLastValue)
195 {
196 if (v->fDelta > v->fBlepPhase)
197 {
198 const float fBlepOffset = v->fBlepPhase / v->fDelta;
199 blepAdd(bSmp, fBlepOffset, bSmp->fLastValue - fSmp);
200 }
201
202 bSmp->fLastValue = fSmp;
203 }
204
205 if (bSmp->samplesLeft > 0)
206 fSmp = blepRun(bSmp, fSmp);
207
208 fMixData[j] += fSmp;
209
210 v->fPhase += v->fDelta;
211 if (v->fPhase >= 1.0f)
212 {
213 v->fPhase -= 1.0f;
214 v->fBlepPhase = v->fPhase;
215
216 v->pos++;
217 if (v->pos >= smpEnd)
218 {
219 if (smpLoopFlag)
220 {
221 do
222 {
223 v->pos -= smpLoopLength;
224 }
225 while (v->pos >= smpEnd);
226 }
227 else
228 {
229 break;
230 }
231 }
232 }
233 }
234 }
235
236 // downsample oversampled buffer, normalize and quantize to 8-bit
237
238 downsample2xFloat(fMixData, s->length * 2);
239
240 const float fPeak = getFloatPeak(fMixData, s->length);
241
242 float fAmp = 0.0f;
243 if (fPeak > 0.0f)
244 fAmp = INT8_MAX / fPeak;
245
246 int8_t *smpPtr = &song->sampleData[s->offset];
247 for (i = 0; i < s->length; i++)
248 {
249 const int32_t smp = (const int32_t)roundf(fMixData[i] * fAmp);
250 ASSERT(smp >= -128 && smp <= 127); // shouldn't happen according to fAmp (but just in case)
251 smpPtr[i] = (int8_t)smp;
252 }
253
254 free(fMixData);
255
256 // clear unused sample data (if sample is not full already)
257 if (s->length < config.maxSampleLength)
258 memset(&song->sampleData[s->offset + s->length], 0, config.maxSampleLength - s->length);
259
260 // we're done
261
262 editor.samplePos = 0;
263 fixSampleBeep(s);
264 updateCurrSample();
265
266 updateWindowTitle(MOD_IS_MODIFIED);
267 }
268
269 static void backupChord(void)
270 {
271 editor.oldNote1 = editor.note1;
272 editor.oldNote2 = editor.note2;
273 editor.oldNote3 = editor.note3;
274 editor.oldNote4 = editor.note4;
275 }
276
277 void recalcChordLength(void)
278 {
279 int32_t note;
280
281 moduleSample_t *s = &song->samples[editor.currSample];
282
283 if (editor.chordLengthMin)
284 {
285 note = MAX(MAX((editor.note1 == 36) ? -1 : editor.note1,
286 (editor.note2 == 36) ? -1 : editor.note2),
287 MAX((editor.note3 == 36) ? -1 : editor.note3,
288 (editor.note4 == 36) ? -1 : editor.note4));
289 }
290 else
291 {
292 note = MIN(MIN(editor.note1, editor.note2), MIN(editor.note3, editor.note4));
293 }
294
295 if (note < 0 || note > 35)
296 {
297 editor.chordLength = 0;
298 }
299 else
300 {
301 int32_t len = (s->length * periodTable[(37 * s->fineTune) + note]) / periodTable[24];
302 if (len > config.maxSampleLength)
303 len = config.maxSampleLength;
304
305 editor.chordLength = len & config.maxSampleLength;
306 }
307
308 if (ui.editOpScreenShown && ui.editOpScreen == 3)
309 ui.updateChordLengthText = true;
310 }
311
312 void resetChord(void)
313 {
314 editor.note1 = 36;
315 editor.note2 = 36;
316 editor.note3 = 36;
317 editor.note4 = 36;
318
319 editor.chordLengthMin = false;
320
321 ui.updateChordNote1Text = true;
322 ui.updateChordNote2Text = true;
323 ui.updateChordNote3Text = true;
324 ui.updateChordNote4Text = true;
325
326 recalcChordLength();
327 }
328
329 void undoChord(void)
330 {
331 editor.note1 = editor.oldNote1;
332 editor.note2 = editor.oldNote2;
333 editor.note3 = editor.oldNote3;
334 editor.note4 = editor.oldNote4;
335
336 ui.updateChordNote1Text = true;
337 ui.updateChordNote2Text = true;
338 ui.updateChordNote3Text = true;
339 ui.updateChordNote4Text = true;
340
341 recalcChordLength();
342 }
343
344 void toggleChordLength(void)
345 {
346 if (song->samples[editor.currSample].loopLength == 2 && song->samples[editor.currSample].loopStart == 0)
347 {
348 editor.chordLengthMin = mouse.rightButtonPressed ? true : false;
349 recalcChordLength();
350 }
351 }
352
353 void setChordMajor(void)
354 {
355 if (editor.note1 == 36)
356 {
357 displayErrorMsg("NO BASENOTE!");
358 return;
359 }
360
361 backupChord();
362
363 editor.note2 = editor.note1 + 4;
364 editor.note3 = editor.note1 + 7;
365
366 if (editor.note2 >= 36) editor.note2 -= 12;
367 if (editor.note3 >= 36) editor.note3 -= 12;
368
369 editor.note4 = 36;
370
371 ui.updateChordNote2Text = true;
372 ui.updateChordNote3Text = true;
373 ui.updateChordNote4Text = true;
374
375 recalcChordLength();
376 }
377
378 void setChordMinor(void)
379 {
380 if (editor.note1 == 36)
381 {
382 displayErrorMsg("NO BASENOTE!");
383 return;
384 }
385
386 backupChord();
387
388 editor.note2 = editor.note1 + 3;
389 editor.note3 = editor.note1 + 7;
390
391 if (editor.note2 >= 36) editor.note2 -= 12;
392 if (editor.note3 >= 36) editor.note3 -= 12;
393
394 editor.note4 = 36;
395
396 ui.updateChordNote2Text = true;
397 ui.updateChordNote3Text = true;
398 ui.updateChordNote4Text = true;
399
400 recalcChordLength();
401 }
402
403 void setChordSus4(void)
404 {
405 if (editor.note1 == 36)
406 {
407 displayErrorMsg("NO BASENOTE!");
408 return;
409 }
410
411 backupChord();
412
413 editor.note2 = editor.note1 + 5;
414 editor.note3 = editor.note1 + 7;
415
416 if (editor.note2 >= 36) editor.note2 -= 12;
417 if (editor.note3 >= 36) editor.note3 -= 12;
418
419 editor.note4 = 36;
420
421 ui.updateChordNote2Text = true;
422 ui.updateChordNote3Text = true;
423 ui.updateChordNote4Text = true;
424
425 recalcChordLength();
426 }
427
428 void setChordMajor6(void)
429 {
430 if (editor.note1 == 36)
431 {
432 displayErrorMsg("NO BASENOTE!");
433 return;
434 }
435
436 backupChord();
437
438 editor.note2 = editor.note1 + 4;
439 editor.note3 = editor.note1 + 7;
440 editor.note4 = editor.note1 + 9;
441
442 if (editor.note2 >= 36) editor.note2 -= 12;
443 if (editor.note3 >= 36) editor.note3 -= 12;
444 if (editor.note4 >= 36) editor.note4 -= 12;
445
446 ui.updateChordNote2Text = true;
447 ui.updateChordNote3Text = true;
448 ui.updateChordNote4Text = true;
449
450 recalcChordLength();
451 }
452
453 void setChordMinor6(void)
454 {
455 if (editor.note1 == 36)
456 {
457 displayErrorMsg("NO BASENOTE!");
458 return;
459 }
460
461 backupChord();
462
463 editor.note2 = editor.note1 + 3;
464 editor.note3 = editor.note1 + 7;
465 editor.note4 = editor.note1 + 9;
466
467 if (editor.note2 >= 36) editor.note2 -= 12;
468 if (editor.note3 >= 36) editor.note3 -= 12;
469 if (editor.note4 >= 36) editor.note4 -= 12;
470
471 ui.updateChordNote2Text = true;
472 ui.updateChordNote3Text = true;
473 ui.updateChordNote4Text = true;
474
475 recalcChordLength();
476 }
477
478 void setChordMajor7(void)
479 {
480 if (editor.note1 == 36)
481 {
482 displayErrorMsg("NO BASENOTE!");
483 return;
484 }
485
486 backupChord();
487
488 editor.note2 = editor.note1 + 4;
489 editor.note3 = editor.note1 + 7;
490 editor.note4 = editor.note1 + 11;
491
492 if (editor.note2 >= 36) editor.note2 -= 12;
493 if (editor.note3 >= 36) editor.note3 -= 12;
494 if (editor.note4 >= 36) editor.note4 -= 12;
495
496 ui.updateChordNote2Text = true;
497 ui.updateChordNote3Text = true;
498 ui.updateChordNote4Text = true;
499
500 recalcChordLength();
501 }
502
503 void setChordMinor7(void)
504 {
505 if (editor.note1 == 36)
506 {
507 displayErrorMsg("NO BASENOTE!");
508 return;
509 }
510
511 backupChord();
512
513 editor.note2 = editor.note1 + 3;
514 editor.note3 = editor.note1 + 7;
515 editor.note4 = editor.note1 + 10;
516
517 if (editor.note2 >= 36) editor.note2 -= 12;
518 if (editor.note3 >= 36) editor.note3 -= 12;
519 if (editor.note4 >= 36) editor.note4 -= 12;
520
521 ui.updateChordNote2Text = true;
522 ui.updateChordNote3Text = true;
523 ui.updateChordNote4Text = true;
524
525 recalcChordLength();
526 }
527
528 void selectChordNote1(void)
529 {
530 if (mouse.rightButtonPressed)
531 {
532 editor.note1 = 36;
533 }
534 else
535 {
536 ui.changingChordNote = 1;
537 setStatusMessage("SELECT NOTE", NO_CARRY);
538 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
539 }
540
541 ui.updateChordNote1Text = true;
542 }
543
544 void selectChordNote2(void)
545 {
546 if (mouse.rightButtonPressed)
547 {
548 editor.note2 = 36;
549 }
550 else
551 {
552 ui.changingChordNote = 2;
553 setStatusMessage("SELECT NOTE", NO_CARRY);
554 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
555 }
556
557 ui.updateChordNote2Text = true;
558 }
559
560 void selectChordNote3(void)
561 {
562 if (mouse.rightButtonPressed)
563 {
564 editor.note3 = 36;
565 }
566 else
567 {
568 ui.changingChordNote = 3;
569 setStatusMessage("SELECT NOTE", NO_CARRY);
570 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
571 }
572
573 ui.updateChordNote3Text = true;
574 }
575
576 void selectChordNote4(void)
577 {
578 if (mouse.rightButtonPressed)
579 {
580 editor.note4 = 36;
581 }
582 else
583 {
584 ui.changingChordNote = 4;
585 setStatusMessage("SELECT NOTE", NO_CARRY);
586 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
587 }
588
589 ui.updateChordNote4Text = true;
590 }