Repository navigation
Expand file tree
/
Copy pathaudio_callback.h
More file actions
1408 lines (1278 loc) · 48.6 KB
/
Copy pathaudio_callback.h
File metadata and controls
1408 lines (1278 loc) · 48.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023-2025 Zack Scholl, GPLv3.0
#include "audio_profile.h"
#include "audio_restart.h"
#if AUDIO_RESTART_WAKE_ENABLED
#include "audio_restart_queue.h"
// Owned only by the audio worker; the clock core communicates through the
// restart token and FIFO, never these fields.
static struct {
bool active;
bool published;
unsigned replaced_silence;
} audio_restart_wake;
static void audio_submit_buffer(audio_buffer_t *buffer, bool restart_block) {
if (audio_restart_wake.active || restart_block) {
unsigned removed = audio_restart_publish_buffer(ap, buffer);
if (audio_restart_wake.active) {
audio_restart_wake.published = true;
audio_restart_wake.replaced_silence += removed;
}
} else {
give_audio_buffer(ap, buffer);
}
}
#else
#define audio_submit_buffer(buffer, restart_block) give_audio_buffer(ap, buffer)
#endif
// previous output per channel (persistent state)
#ifdef INCLUDE_EZEPTOCORE
#define BASS_LP_A 64917 // alpha ≈ 0.9908
#define BASS_LP_B 619 // 1 - alpha
static int32_t smear_buf[32][2];
static uint8_t smear_pos = 0;
// auto-oscillation state
static uint8_t smear_amt = 1;
static int8_t smear_dir = 1;
static uint32_t smear_acc = 0;
static int32_t bass_lp[2] = {0, 0};
#define MICRO_DELAY_MAX 64 // ~1.45 ms @ 44.1kHz
static int32_t tremble_val[2] = {Q16_16_1, Q16_16_1};
static int32_t tremble_vel[2] = {0, 0};
#endif
uint8_t cpu_utilizations[64];
uint8_t cpu_utilizations_i = 0;
uint32_t reduce_cpu_usage = 0;
uint32_t cpu_usage_flag_total = 0;
uint8_t cpu_usage_flag = 0;
uint16_t cpu_flag_counter = 0;
const uint8_t cpu_usage_flag_limit = 60;
const uint8_t cpu_usage_limit_threshold = 150;
bool audio_was_muted = false;
bool do_open_file_ready = false;
bool first_loop_ever = true;
int32_t reverb_fade = 0;
int32_t amiga_previous_value[2];
bool reverb_activated = false;
bool mute_soft_activated = false;
#include "audio_silent_switch.h"
inline int32_t scale16to32_fixed_dither(int16_t val) {
return (((int32_t)val) << 16); // + ((rand() & 1) - 1);
}
void __not_in_flash_func(update_filter_from_envelope)(int32_t val) {
for (uint8_t channel = 0; channel < 2; channel++) {
ResonantFilter_setFilterType(resFilter[channel], global_filter_lphp);
ResonantFilter_setFc(resFilter[channel], val);
}
}
#define INTERPOLATE_VALUE 512
#include "audio_resample.h"
int16_t newArray[SAMPLES_PER_BUFFER];
// Core 1's stack is reserved for control flow and short-lived source buffers.
// This always-present 3.5 KiB render workspace is static so normal stereo
// playback fits within the RP2040 scratch-bank stack.
static int32_t audio_render_samples[SAMPLES_PER_BUFFER * 2];
void __not_in_flash_func(array_resample_linear441)(const int16_t *arr,
uint32_t arr_size,
uint32_t stride) {
AP_MEASURE(AP_RESAMPLE,audio_resample_linear(newArray, arr, arr_size, SAMPLES_PER_BUFFER, stride));
}
void __not_in_flash_func(process_source_fx)(int16_t *values,
uint32_t values_len) {
// beat repeat
AP_MEASURE(AP_BEAT,BeatRepeat_process(beatrepeat, values, values_len));
AP_START(ap_source);
// saturate before resampling?
if (sf->fx_active[FX_SATURATE]) {
for (uint16_t i = 0; i < values_len; i++) {
values[i] = values[i] * sf->fx_param[FX_SATURATE][0] / 128;
}
if (audio_variant_num > 0) {
set_audio_variant(sf->fx_param[FX_SATURATE][1] * audio_variant_num / 256);
}
Saturation_process(saturation, values, values_len);
}
// shaper
AP_END(AP_SATURATE,ap_source);AP_RESET(ap_source);
if (sf->fx_active[FX_SHAPER]) {
if (sf->fx_param[FX_SHAPER][0] > 128) {
Shaper_expandUnder_compressOver_process(
values, values_len, (sf->fx_param[FX_SHAPER][0] - 128) << 6,
sf->fx_param[FX_SHAPER][1]);
} else {
Shaper_expandOver_compressUnder_process(values, values_len,
sf->fx_param[FX_SHAPER][0] << 6,
sf->fx_param[FX_SHAPER][1]);
}
}
AP_END(AP_SHAPER,ap_source);AP_RESET(ap_source);
if (sf->fx_active[FX_FUZZ]) {
Fuzz_process(values, values_len, sf->fx_param[FX_FUZZ][0],
sf->fx_param[FX_FUZZ][1]);
}
// bitcrush
AP_END(AP_FUZZ,ap_source);AP_RESET(ap_source);
if (sf->fx_active[FX_BITCRUSH]) {
Bitcrush_process(values, values_len, sf->fx_param[FX_BITCRUSH][0],
sf->fx_param[FX_BITCRUSH][1]);
}
AP_END(AP_BITCRUSH,ap_source);
}
#ifdef DEBUG_AUDIO_WITH_SINE_WAVE
uint32_t sine_wave_counter = 0;
#endif
static void __not_in_flash_func(zeptocore_render_audio)() {
// A foreground control can update the pending selection during a render.
// Use one bounded snapshot for every lookup/open in this block.
const uint8_t audio_next_bank=sel_bank_next;
uint8_t audio_next_sample=sel_sample_next;
if(audio_next_bank<16 && banks[audio_next_bank] && banks[audio_next_bank]->num_samples)
audio_next_sample%=banks[audio_next_bank]->num_samples;
// void i2s_callback_func() {
uint32_t t0, t1;
uint32_t sd_card_total_time = 0;
#ifdef PRINT_SDCARD_TIMING
uint32_t give_audio_buffer_time = 0;
uint32_t take_audio_buffer_time = 0;
#endif
// flag for new phase
bool do_crossfade = false;
bool do_fade_out = false;
bool do_fade_in = false;
bool clock_restart_block = false;
clock_t startTime = time_us_64();
audio_buffer_t *buffer = take_audio_buffer(ap, false);
#ifdef PRINT_SDCARD_TIMING
take_audio_buffer_time = (time_us_64() - startTime);
#endif
if (buffer == NULL) {
ZD_CALL(zd_audio_counter(ZD_NO_BUFFER));
return;
}
int32_t *samples = audio_render_samples;
ZD_WITNESS_RENDERED();
int16_t *samples16 = (int16_t *)buffer->buffer->bytes;
#ifdef DEBUG_AUDIO_WITH_SINE_WAVE
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
int32_t value0 =
(int32_t)(sinf(2 * M_PI * 440 * sine_wave_counter / 44100) *
(0x7ffffff0));
sine_wave_counter++;
samples[i * 2 + 0] = value0;
samples[i * 2 + 1] = value0;
}
buffer->sample_count = buffer->max_sample_count;
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples16[i * 2 + 0] = (int16_t)(samples[i * 2 + 0] >> 16);
samples16[i * 2 + 1] = (int16_t)(samples[i * 2 + 1] >> 16);
}
ZD_CALL(zd_audio_output(buffer->sample_count, 0));
audio_submit_buffer(buffer, false);
return;
#endif
EnvelopeLinearInteger_update(envelope_filter, update_filter_from_envelope);
float envelope_volume_val = Envelope2_update(envelope_volume);
float envelope_pitch_val_new = Envelope2_update(envelope_pitch);
if (mute_because_of_playback_type || sync_using_sdcard || !fil_is_open ||
button_mute || reduce_cpu_usage > 0 ||
(envelope_pitch_val < ENVELOPE_PITCH_THRESHOLD) ||
envelope_volume_val < 0.001 || Gate_is_up(audio_gate) ||
clock_input_should_stop(time_us_32())) {
first_loop_ever = true;
audio_was_muted = true;
audio_callback_in_mute = true;
// File switches used to live below this early return, trapping a stopped
// one-shot in its old bank forever. Finish the switch while this block
// remains silent; the following block applies the normal mute conditions.
if(bank_transition_fading()) {
atomic_store_explicit(&bank_fade_silent,true,memory_order_relaxed);
} else if(!bank_transition_busy()) audio_switch_while_silent();
envelope_pitch_val = envelope_pitch_val_new;
// continue to update the gate
Gate_update(audio_gate, sf->bpm_tempo);
// check cpu usage
if (reduce_cpu_usage > 0) {
// printf("reduce_cpu_usage: %d\n", reduce_cpu_usage);
reduce_cpu_usage--;
}
int16_t values[buffer->max_sample_count];
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
values[i] = 0;
}
// saturate before resampling?
if (sf->fx_active[FX_SATURATE]) {
Saturation_process(saturation, values, buffer->max_sample_count);
}
// // fuzz
// if (sf->fx_active[FX_FUZZ]) {
// Fuzz_process(values, buffer->max_sample_count);
// }
// // bitcrush
// if (sf->fx_active[FX_BITCRUSH]) {
// Bitcrush_process(values, buffer->max_sample_count);
// }
int32_t vol_main =
round((float)volume_vals[sf->vol] * retrig_vol * envelope_volume_val);
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] =
q16_16_multiply(((int32_t)values[i]) << 16, vol_main);
samples[i * 2 + 1] = samples[i * 2 + 0]; // R = L
}
buffer->sample_count = buffer->max_sample_count;
#ifdef INCLUDE_SINEBASS
if (fil_is_open) {
// apply bass
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
int32_t v = WaveBass_next(wavebass);
samples[i * 2 + 0] += v;
samples[i * 2 + 1] += v;
}
}
#endif
// apply reverb
if (sf->fx_active[FX_EXPAND]) {
if (freeverb != NULL) {
FV_Reverb_process(freeverb, samples, buffer->max_sample_count);
}
} else {
// apply delay
Delay_process(delay, samples, buffer->max_sample_count, 0);
}
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples16[i * 2 + 0] = (int16_t)(samples[i * 2 + 0] >> 16);
samples16[i * 2 + 1] = (int16_t)(samples[i * 2 + 1] >> 16);
}
// A silent source may still have delay/reverb tails. Finish their fade
// before handing the buffer to the foreground metadata loader as well.
bool bank_silent_fading=bank_transition_fading();
if(bank_silent_fading) {
for(unsigned i=0;i<buffer->sample_count;++i) {
samples16[2*i]=crossfade3_out(samples16[2*i],i,CROSSFADE3_COS);
samples16[2*i+1]=crossfade3_out(samples16[2*i+1],i,CROSSFADE3_COS);
}
}
ZD_CALL(zd_audio_output(buffer->sample_count, 0));
#if AUDIO_RESTART_WAKE_ENABLED
if (audio_restart_pcm_silent(samples16, buffer->sample_count))
buffer->flags |= AUDIO_BUFFER_SILENCE;
#endif
audio_submit_buffer(buffer, false);
if(bank_silent_fading)
atomic_store_explicit(&bank_fade_done,true,memory_order_release);
ZD_CALL(zd_audio_counter(ZD_MUTED));
// audio muted flag to ensure a fade in occurs when
// unmuted
return;
}
audio_callback_in_mute = false;
if (playback_restarted) {
// printf("[audio_callback] playback_restarted\n");
playback_restarted = false;
audio_was_muted = false;
}
bool realtime_stretch_was_active = realtime_stretch_is_active();
realtime_stretch_update_state();
if (realtime_stretch_was_active && !realtime_stretch_is_active()) {
last_seeked = 1;
}
Gate_update(audio_gate, sf->bpm_tempo);
envelope_pitch_val = envelope_pitch_val_new;
if (trigger_button_mute || envelope_pitch_val < ENVELOPE_PITCH_THRESHOLD ||
Gate_is_up(audio_gate)) {
do_fade_out = true;
}
// mutex
sync_using_sdcard = true;
if(bank_transition_busy() || !metadata_ready(audio_next_bank)) do_open_file_ready=false;
bool do_open_file = do_open_file_ready;
// check if the file is the right one
if (do_open_file_ready) {
// printf("[audio_callback] next file: %s\n", banks[audio_next_bank]
// ->sample[audio_next_sample]
// ->name);
phases[0] = round(
((float)phases[0] *
(float)banks[audio_next_bank]
->sample[audio_next_sample]
.snd[FILEZERO]
->size) /
(float)banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->size);
// printf("[audio_callback] phase[0] -> phase_new: %d*%d/%d -> %d\n",
// phases[0],
// banks[audio_next_bank]
// ->sample[audio_next_sample]
// ->size,
// banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->size,
// phase_new);
// printf("[audio_callback] beat_current -> new beat_current: %d",
// beat_current);
beat_current = round(((float)beat_current * (float)banks[audio_next_bank]
->sample[audio_next_sample]
.snd[FILEZERO]
->slice_num)) /
(float)banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->slice_num;
// printf(" -> %d\n", beat_current);
do_open_file = true;
do_fade_in = true;
do_open_file_ready = false;
// printf("[audio_callback] do_fade_in from do_open_file_ready\n");
}
bool allow_file_change=!bank_transition_busy() && metadata_ready(audio_next_bank);
#if AUDIO_PREPARE_NEXT
if(fil_current_change||fil_current_change_force) {
char next_path[32];
format_sample_filename(next_path,audio_next_bank,
audio_next_sample%banks[audio_next_bank]->num_samples,
audio_variant * 2);
allow_file_change=allow_file_change && audio_prepare_ready(next_path);
}
#endif
if (allow_file_change && (fil_current_change || fil_current_change_force)) {
fil_current_change = sel_bank_next!=audio_next_bank || sel_sample_next!=audio_next_sample;
if (fil_current_change_force || sel_bank_cur != audio_next_bank ||
sel_sample_cur != audio_next_sample) {
do_open_file_ready = true;
do_fade_out = true;
fil_current_change_force = false;
// printf("[audio_callback] do_fade_out, readying do_open_file_ready\n");
}
}
// check if scratch is active
float scratch_pitch = 1.0;
bool change_phase_forward = false;
if (sf->fx_active[FX_SCRATCH] && sf->fx_param[FX_SCRATCH][0] > 20) {
scratch_lfo_val += scratch_lfo_inc;
scratch_pitch = q16_16_fp_to_float(q16_16_cos(scratch_lfo_val));
if (scratch_pitch < 0) {
scratch_pitch = -scratch_pitch;
change_phase_forward = true;
phase_forward = !phase_forward;
}
if (scratch_pitch < 0.05) {
scratch_pitch = 0.05;
}
}
// check if tempo matching is activated, if not then don't change
// based on bpm
uint32_t samples_to_read;
if (banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->tempo_match) {
samples_to_read =
round(buffer->max_sample_count * sf->bpm_tempo * envelope_pitch_val *
pitch_vals[sf->pitch_val_index] * scratch_pitch *
pitch_vals[retrig_pitch] *
(banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->oversampling +
1) /
banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->bpm);
} else {
samples_to_read =
round((float)buffer->max_sample_count * envelope_pitch_val *
pitch_vals[sf->pitch_val_index] * scratch_pitch *
pitch_vals[retrig_pitch]) *
(banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->oversampling +
1);
}
if (samples_to_read < 11) {
samples_to_read = 11;
}
// Normal and stretch rendering are mutually exclusive. Reuse the existing
// fixed stretch read workspace instead of pitch-sized stack arrays. Bound
// extreme combined rates to its capacity, including interpolation lookahead.
if (!realtime_stretch_is_active()) {
samples_to_read = audio_source_frame_limit(samples_to_read,
sizeof realtime_stretch_readbuf / sizeof realtime_stretch_readbuf[0],
banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->num_channels + 1);
}
uint64_t realtime_stretch_grain_phase_inc_q32 =
(((uint64_t)samples_to_read) << 32u) / buffer->max_sample_count;
uint32_t values_len =
(samples_to_read + 1) *
(banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->num_channels +
1);
uint32_t values_len_minus_peek =
(samples_to_read) *
(banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->num_channels +
1);
uint32_t values_to_read_minus_peek = values_len_minus_peek * 2;
uint32_t values_to_read = values_len * 2; // 16-bit = 2 x 1 byte reads
int32_t vol_main =
round((float)volume_vals[sf->vol] * retrig_vol * envelope_volume_val);
if (!phase_change && !realtime_stretch_is_active()) {
const int32_t next_phase = phases[0] + ((samples_to_read) *
(banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->num_channels +
1) *
2) *
(phase_forward * 2 - 1);
const int32_t splice_start = banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->slice_start[banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->slice_current];
const int32_t splice_stop = banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->slice_stop[banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->slice_current];
const int32_t sample_stop =
banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->size;
switch (
banks[sel_bank_cur]->sample[sel_sample_cur].snd[FILEZERO]->play_mode) {
case PLAY_NORMAL:
if (phase_forward && phases[0] > sample_stop) {
phase_change = true;
phase_new = phases[0] - sample_stop;
} else if (!phase_forward && phases[0] < 0) {
phase_change = true;
phase_new = phases[0] + sample_stop;
}
break;
case PLAY_SPLICE_STOP:
if ((phase_forward && (next_phase > splice_stop)) ||
(!phase_forward && (next_phase < splice_start))) {
do_fade_out = true;
mute_because_of_playback_type = true;
}
break;
case PLAY_SPLICE_LOOP:
if (phase_forward && (phases[0] > splice_stop)) {
phase_change = true;
phase_new = splice_start;
} else if (!phase_forward && (phases[0] < splice_stop)) {
phase_change = true;
phase_new = splice_stop;
}
break;
case PLAY_SAMPLE_STOP:
if ((phase_forward && (next_phase > sample_stop)) ||
(!phase_forward && (next_phase < 0))) {
do_fade_out = true;
mute_because_of_playback_type = true;
}
break;
case PLAY_SAMPLE_LOOP:
if (phase_forward && (phases[0] > sample_stop)) {
phase_change = true;
phase_new = splice_start;
} else if (!phase_forward && (phases[0] < 0)) {
phase_change = true;
phase_new = splice_stop;
}
break;
}
}
if (phase_change) {
do_crossfade = true;
phases[1] = phases[0]; // old phase
phases[0] = phase_new;
CL_CALL(cl_applied(phases[0]));
phase_change = false;
AR_CALL(if (audio_restart_take(phases[0])) {
clock_restart_block = true;
do_crossfade = false;
do_fade_in = false;
audio_was_muted = false;
});
if (realtime_stretch_is_active()) realtime_stretch_reset_from_playback_phase();
}
if (audio_was_muted) {
// printf("[audio_callback] audio_was_muted, fading in\n");
audio_was_muted = false;
do_fade_in = true;
// if fading in then do not crossfade
do_crossfade = false;
}
// cpu_usage_flag is written when cpu usage is consistently high
// in which case it will fade out audio and keep it muted for a little
// bit to reduce cpu usage
if (cpu_usage_flag == cpu_usage_flag_limit) {
do_fade_out = true;
}
bool first_loop = true;
if (realtime_stretch_is_active()) {
if (do_open_file) {
sel_bank_cur = audio_next_bank;
sel_sample_cur = audio_next_sample % banks[sel_bank_cur]->num_samples;
t0 = time_us_32();
format_sample_filename(fil_current_name, sel_bank_cur, sel_sample_cur,
audio_variant * 2);
audio_file_open(fil_current_name);
t1 = time_us_32();
sd_card_total_time += (t1 - t0);
do_open_file = false;
realtime_stretch_reset_from_playback_phase();
}
int16_t stretch_values[buffer->max_sample_count * 2];
AP_START(ap_stretch);
bool stretch_ok=realtime_stretch_render(stretch_values, buffer->max_sample_count,
realtime_stretch_grain_phase_inc_q32);
AP_END(AP_STRETCH,ap_stretch);
if (!stretch_ok) {
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] = 0;
samples[i * 2 + 1] = 0;
}
realtime_stretch_invalidate_grains();
} else {
ZD_CALL(buffer->user_data=zd_switch_render_tag());
process_source_fx(stretch_values, buffer->max_sample_count * 2);
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
for (uint8_t channel = 0; channel < 2; channel++) {
int16_t value = stretch_values[i * 2 + channel];
if (do_fade_out) {
value = crossfade3_out(value, i, CROSSFADE3_COS);
} else if (do_fade_in) {
value = crossfade3_in(value, i, CROSSFADE3_COS);
}
samples[i * 2 + channel] =
q16_16_multiply(((int32_t)value) << 16, vol_main);
}
}
}
last_seeked = 1;
goto AUDIO_SOURCE_RENDERED;
}
{
int16_t *values = realtime_stretch_readbuf;
for (int8_t head = 1; head >= 0; head--) {
if (head == 1 && (!do_crossfade || do_fade_in)) {
continue;
}
if (head == 0 && do_open_file) {
// setup the next
sel_bank_cur = audio_next_bank;
sel_sample_cur = audio_next_sample % banks[sel_bank_cur]->num_samples;
t0 = time_us_32();
format_sample_filename(fil_current_name, sel_bank_cur, sel_sample_cur,
audio_variant * 2);
audio_file_open(fil_current_name);
t1 = time_us_32();
sd_card_total_time += (t1 - t0);
}
// optimization here, only seek if the current position is not at the
// phases[head]
if (phases[head] != last_seeked || do_open_file) {
t0 = time_us_32();
int negative_latency = 0;
#ifdef INCLUDE_ECTOCORE
if (latency_factor > 0) {
negative_latency =
roundf((float)values_len_minus_peek * latency_factor) *
(phase_forward * 2 - 1);
if ((!AUDIO_CLOCK_RESTART_FIX || head == 0) && clock_input_present_first) {
clock_input_present_first = false;
negative_latency = 0;
}
if (clock_restart_block) negative_latency = 0;
}
#endif
CL_CALL(if(head == 0) cl_seek(negative_latency, samples_to_read,
values_to_read_minus_peek / samples_to_read,
(do_crossfade ? 16u : 0) | (do_fade_in ? 32u : 0) | (do_fade_out ? 64u : 0) |
(clock_restart_block ? 128u : 0)));
FRESULT seek_result=zd_f_lseek(&fil_current,
WAV_HEADER +
((banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->num_channels +
1) *
(banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->oversampling +
1) *
44100) +
((phases[head] + negative_latency) / PHASE_DIVISOR) *
PHASE_DIVISOR, ZD_SEEK);
if(seek_result!=FR_OK) {
audio_media_io_failed(seek_result);
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
int32_t value0 = 0;
samples[i * 2 + 0] = value0 + (value0 >> 16u); // L
samples[i * 2 + 1] = samples[i * 2 + 0]; // R = L
}
buffer->sample_count = buffer->max_sample_count;
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples16[i * 2 + 0] = (int16_t)(samples[i * 2 + 0] >> 16);
samples16[i * 2 + 1] = (int16_t)(samples[i * 2 + 1] >> 16);
}
ZD_CALL(zd_audio_output(buffer->sample_count, 0));
audio_submit_buffer(buffer, false);
sync_using_sdcard = false;
ZD_CALL(zd_audio_counter(ZD_ERROR_SILENCE));
// sdcard_startup();
return;
}
t1 = time_us_32();
sd_card_total_time += (t1 - t0);
} else {
ZD_CALL(zd_audio_counter(ZD_SEEK_SKIPPED));
}
t0 = time_us_32();
FRESULT read_result=zd_f_read(&fil_current,values,values_to_read,&fil_bytes_read,ZD_READ);
if(read_result!=FR_OK) {
audio_media_io_failed(read_result);
memset(values,0,values_to_read);fil_bytes_read=0;
}
if(head==0 && read_result==FR_OK && fil_bytes_read)
ZD_CALL(buffer->user_data=zd_switch_render_tag());
t1 = time_us_32();
sd_card_total_time += (t1 - t0);
last_seeked = phases[head] + fil_bytes_read;
if (!phase_forward) {
// reverse audio
for (int i = 0; i < values_len / 2; i++) {
int16_t temp = values[i];
values[i] = values[values_len - i - 1];
values[values_len - i - 1] = temp;
}
}
process_source_fx(values, values_len);
if (banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->num_channels == 0) {
// mono
array_resample_linear441(values, samples_to_read, 1);
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
if (do_crossfade && !do_fade_in) {
if (head == 0) {
newArray[i] = crossfade3_in(newArray[i], i, CROSSFADE3_COS);
} else {
newArray[i] = crossfade3_out(newArray[i], i, CROSSFADE3_COS);
}
} else if (do_fade_out) {
newArray[i] = crossfade3_out(newArray[i], i, CROSSFADE3_COS);
} else if (do_fade_in) {
newArray[i] = crossfade3_in(newArray[i], i, CROSSFADE3_COS);
}
if (first_loop) {
samples[i * 2 + 0] =
q16_16_multiply(((int32_t)newArray[i]) << 16, vol_main);
if (head == 0) {
samples[i * 2 + 1] = samples[i * 2 + 0]; // R = L
}
} else {
samples[i * 2 + 0] +=
q16_16_multiply(((int32_t)newArray[i]) << 16, vol_main);
samples[i * 2 + 1] = samples[i * 2 + 0]; // R = L
}
// int32_t value0 = (vol * newArray[i]) << 8u;
// samples[i * 2 + 0] =
// samples[i * 2 + 0] + value0 + (value0 >> 16u); // L
}
if (first_loop) {
first_loop = false;
}
} else if (banks[sel_bank_cur]
->sample[sel_sample_cur]
.snd[FILEZERO]
->num_channels == 1) {
// stereo
for (uint8_t channel = 0; channel < 2; channel++) {
array_resample_linear441(values + channel, samples_to_read, 2);
// TODO: function pointer for audio block here?
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
if (first_loop) {
samples[i * 2 + channel] = 0;
}
if (do_crossfade && !do_fade_in) {
if (head == 0) {
newArray[i] = crossfade3_in(newArray[i], i, CROSSFADE3_COS);
} else {
newArray[i] = crossfade3_out(newArray[i], i, CROSSFADE3_COS);
}
} else if (do_fade_out) {
newArray[i] = crossfade3_out(newArray[i], i, CROSSFADE3_COS);
} else if (do_fade_in) {
newArray[i] = crossfade3_in(newArray[i], i, CROSSFADE3_COS);
}
samples[i * 2 + channel] +=
q16_16_multiply(((int32_t)newArray[i]) << 16, vol_main);
}
}
first_loop = false;
}
phases[head] += (values_to_read_minus_peek * (phase_forward * 2 - 1));
}
}
AUDIO_SOURCE_RENDERED:
realtime_stretch_declick(samples, buffer->max_sample_count);
#ifdef INCLUDE_ECTOCORE
if (mute_soft) {
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] = 0;
samples[i * 2 + 1] = 0;
}
mute_soft_activated = true;
} else if (mute_soft_activated) {
// fade in
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] =
q16_16_multiply(samples[i * 2 + 0], crossfade3_cos_in[i]);
samples[i * 2 + 1] =
q16_16_multiply(samples[i * 2 + 1], crossfade3_cos_in[i]);
}
mute_soft_activated = false;
}
#endif
#ifdef INCLUDE_CUEDSOUNDS
#ifdef INCLUDE_ECTOCORE
bool cuedsounds_played =
cuedsounds_audio_update(samples, buffer->max_sample_count, vol_main);
if (cuedsounds_played) {
mute_soft = false;
}
#endif
#ifdef INCLUDE_ZEPTOCORE
cuedsounds_audio_update(samples, buffer->max_sample_count, vol_main);
#endif
#endif
// apply filter
AP_START(ap_filter);
#ifdef INCLUDE_FILTER
for (uint8_t channel = 0; channel < 2; channel++) {
ResonantFilter_update(resFilter[channel], samples, buffer->max_sample_count,
channel);
}
#endif
// apply other fx
AP_END(AP_FILTER,ap_filter);
AP_START(ap_pan);
// TODO: fade in/out these fx using the crossfade?
// TODO: LFO's move to main thread?
if (sf->fx_active[FX_PAN]) {
// int32_t u;
int32_t v;
int32_t w;
// if (sf->fx_active[FX_TREMELO]) {
// uint8_t vv = linlin(sf->fx_param[FX_TREMELO][1], 0, 255, 128, 255);
// u = q16_16_sin01(lfo_tremelo_val);
// u = u * vv / 255 + (Q16_16_1 * (255 - vv) / 255);
// }
if (sf->fx_active[FX_PAN]) {
uint8_t vv = linlin(sf->fx_param[FX_PAN][1], 0, 255, 128, 255);
v = q16_16_sin01(lfo_pan_val);
v = v * vv / 255 + (Q16_16_1 * (255 - vv) / 255);
w = Q16_16_1 - v;
}
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
for (uint8_t channel = 0; channel < 2; channel++) {
// if (sf->fx_active[FX_TREMELO]) {
// samples[i * 2 + channel] =
// q16_16_multiply(samples[i * 2 + channel], u);
// }
if (sf->fx_active[FX_PAN]) {
if (channel == 0) {
samples[i * 2 + channel] =
q16_16_multiply(samples[i * 2 + channel], v);
} else {
samples[i * 2 + channel] =
q16_16_multiply(samples[i * 2 + channel], w);
}
}
}
}
}
// apply reverb
AP_END(AP_PAN,ap_pan);
if (sf->fx_active[FX_EXPAND] || reverb_fade > 0 || reverb_activated) {
if (freeverb != NULL) {
if (first_loop_ever) {
// time this process
t0 = time_us_32();
}
if (reverb_activated && !sf->fx_active[FX_EXPAND]) {
reverb_activated = false;
if (reverb_fade <= 0) {
reverb_fade = Q16_16_0_85;
}
}
if (!reverb_activated && sf->fx_active[FX_EXPAND]) {
reverb_activated = true;
if (reverb_fade <= 0) {
reverb_fade = Q16_16_0_85;
}
}
if (reverb_fade > 0) {
reverb_fade -= 300;
if (reverb_fade < 0) {
reverb_fade = 0;
}
if (sf->fx_active[FX_EXPAND]) {
// fade in
FV_Reverb_set_wet(freeverb, Q16_16_0_85 - reverb_fade);
} else {
// fade out
FV_Reverb_set_wet(freeverb, reverb_fade);
}
}
AP_MEASURE(AP_REVERB,FV_Reverb_process(freeverb, samples, buffer->max_sample_count));
if (first_loop_ever) {
first_loop_ever = false;
}
}
} else {
#ifdef INCLUDE_ECTOCORE
if (sf->fx_active[FX_DELAY]) {
AP_START(ap_delay_setup);
Delay_setFeedbackf(delay,
Range(LFNoise2_period(noise_feedback, 1), 0.49, 0.99));
float v = Range(LFNoise2_period(noise_duration, 2), 100, 10000);
Delay_setDuration(delay, v);
AP_END(AP_DELAY_SETUP,ap_delay_setup);
// float v = Range(LFNoise2_period(noise_duration, 2), 6.64f, 13.28f);
// Delay_setDuration(delay, powf(2, v));
}
AP_MEASURE(AP_DELAY,Delay_process(delay, samples, buffer->max_sample_count, 0));
#else
if (sf->fx_active[FX_DELAY] && sf->fx_param[FX_DELAY][2] > 30) {
AP_START(ap_delay_setup);
Delay_setFeedbackf(delay,
Range(LFNoise2_period(noise_feedback, 2), 0.49, 0.99));
float v = Range(LFNoise2_period(noise_duration, 2), 100, 10000);
Delay_setDuration(delay, v);
// float v = Range(LFNoise2_period(noise_duration, 2), 6.64f, 13.28f);
// // raise v from the 10th power
// Delay_setDuration(delay, powf(2, v));
AP_END(AP_DELAY_SETUP,ap_delay_setup);
}
AP_MEASURE(AP_DELAY,Delay_process(delay, samples, buffer->max_sample_count, 0));
#endif
}
// apply comb
AP_MEASURE(AP_COMB,Comb_process(combfilter, samples, buffer->max_sample_count));
#ifdef INCLUDE_SINEBASS
// apply bass
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
int32_t v = WaveBass_next(wavebass);
samples[i * 2 + 0] += v;
samples[i * 2 + 1] += v;
}
#endif
#ifndef INCLUDE_ECTOCORE
if (clock_out_do) {
if (clock_out_ready) {
clock_out_ready = false;
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] = 2147483645;
}
} else {
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] = 0;
}
}
}
#endif
#ifdef INCLUDE_ECTOCORE
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
samples[i * 2 + 0] *= -1;
samples[i * 2 + 1] *= -1;
}
#endif
AP_START(ap_digital);
#ifdef INCLUDE_EZEPTOCORE
if (mode_amiga_index > 5) {
int32_t held[2] = {0, 0};
uint8_t hold_len = mode_amiga_index - 5;
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
if ((i % hold_len) == 0) {
// compute new filtered value
for (uint8_t j = 0; j < 2; j++) {
// convert 32-bit to 8-bit (Amiga-style)
held[j] = (int32_t)((int8_t)(samples[i * 2 + j] >> 24)) << 24;
}
}
// sample & hold
samples[i * 2 + 0] = held[0];
samples[i * 2 + 1] = held[1];
}
}
if (mode_digital_saturation > 0) {
uint8_t amt = mode_digital_saturation;
if (amt > 100)
amt = 100;
/*
Threshold mapping (Q16.16)
amt = 0 → threshold ≈ full scale
amt = 100 → threshold ≈ small → heavy folding
Use a nonlinear curve so low values are gentle
*/
int32_t thresh = 0x7FFF0000 >> (1 + (amt * 5) / 100); // shift 1..6
for (uint16_t i = 0; i < buffer->max_sample_count; i++) {
for (uint8_t j = 0; j < 2; j++) {
int32_t x = samples[i * 2 + j];