-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathViperContext.cpp
More file actions
875 lines (821 loc) · 43 KB
/
ViperContext.cpp
File metadata and controls
875 lines (821 loc) · 43 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
#include <cerrno>
#include <cstring>
#include <cmath>
#include <chrono>
#include "ViperContext.h"
#include "log.h"
#include "viper/constants.h"
#ifdef __hexagon__
#define SET(type, ptr, value) \
do { \
type v = value; \
memcpy(ptr, &v, sizeof(type)); \
} while (false)
#else
#define SET(type, ptr, value) (*(type *) (ptr) = (value))
#endif
ViperContext::ViperContext() :
config({}),
disableReason(DisableReason::UNKNOWN),
buffer(std::vector<float>()),
bufferFrameCount(0),
enabled(false) {
VIPER_LOGI("ViperContext created");
}
ViperContext::~ViperContext() {}
void ViperContext::copyBufferConfig(buffer_config_t *dest, buffer_config_t *src) {
if (src->mask & EFFECT_CONFIG_BUFFER) {
dest->buffer = src->buffer;
}
if (src->mask & EFFECT_CONFIG_SMP_RATE) {
dest->samplingRate = src->samplingRate;
}
if (src->mask & EFFECT_CONFIG_CHANNELS) {
dest->channels = src->channels;
}
if (src->mask & EFFECT_CONFIG_FORMAT) {
dest->format = src->format;
}
if (src->mask & EFFECT_CONFIG_ACC_MODE) {
dest->accessMode = src->accessMode;
}
if (src->mask & EFFECT_CONFIG_PROVIDER) {
dest->bufferProvider = src->bufferProvider;
}
dest->mask |= src->mask;
}
void ViperContext::handleSetConfig(effect_config_t *newConfig) {
VIPER_LOGI("Checking input and output configuration ...");
VIPER_LOGI("Input mask: 0x%04X", newConfig->inputCfg.mask);
VIPER_LOGI("Input buffer frame count: %zu", newConfig->inputCfg.buffer.frameCount);
VIPER_LOGI("Input sampling rate: %d", newConfig->inputCfg.samplingRate);
VIPER_LOGI("Input channels: %d", newConfig->inputCfg.channels);
VIPER_LOGI("Input format: %d", newConfig->inputCfg.format);
VIPER_LOGI("Input access mode: %d", newConfig->inputCfg.accessMode);
VIPER_LOGI("Output mask: 0x%04X", newConfig->outputCfg.mask);
VIPER_LOGI("Output buffer frame count: %zu", newConfig->outputCfg.buffer.frameCount);
VIPER_LOGI("Output sampling rate: %d", newConfig->outputCfg.samplingRate);
VIPER_LOGI("Output channels: %d", newConfig->outputCfg.channels);
VIPER_LOGI("Output format: %d", newConfig->outputCfg.format);
VIPER_LOGI("Output access mode: %d", newConfig->outputCfg.accessMode);
setDisableReason(DisableReason::UNKNOWN);
copyBufferConfig(&config.inputCfg, &newConfig->inputCfg);
copyBufferConfig(&config.outputCfg, &newConfig->outputCfg);
if (config.inputCfg.buffer.frameCount != config.outputCfg.buffer.frameCount) {
VIPER_LOGE("ViPER4Android disabled, reason [in.FC = %zu, out.FC = %zu]",
config.inputCfg.buffer.frameCount, config.outputCfg.buffer.frameCount);
setDisableReason(DisableReason::INVALID_FRAME_COUNT);
return;
}
if (config.inputCfg.samplingRate != config.outputCfg.samplingRate) {
VIPER_LOGE("ViPER4Android disabled, reason [in.SR = %d, out.SR = %d]",
config.inputCfg.samplingRate, config.outputCfg.samplingRate);
setDisableReason(DisableReason::INVALID_SAMPLING_RATE);
return;
}
// if (config.inputCfg.samplingRate > 48000) {
// VIPER_LOGE("ViPER4Android disabled, reason [SR out of range]");
// setDisableReason(DisableReason::INVALID_SAMPLING_RATE, "Sampling rate out of range: " + std::to_string(config.inputCfg.samplingRate));
// return;
// }
if (config.inputCfg.channels != config.outputCfg.channels) {
VIPER_LOGE("ViPER4Android disabled, reason [in.CH = %d, out.CH = %d]",
config.inputCfg.channels, config.outputCfg.channels);
setDisableReason(DisableReason::INVALID_CHANNEL_COUNT);
return;
}
if (config.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
VIPER_LOGE("ViPER4Android disabled, reason [CH != 2]");
setDisableReason(DisableReason::INVALID_CHANNEL_COUNT);
return;
}
if (config.inputCfg.format != AUDIO_FORMAT_PCM_16_BIT &&
config.inputCfg.format != AUDIO_FORMAT_PCM_32_BIT &&
config.inputCfg.format != AUDIO_FORMAT_PCM_FLOAT) {
VIPER_LOGE("ViPER4Android disabled, reason [in.FMT = %d]", config.inputCfg.format);
VIPER_LOGE("We only accept AUDIO_FORMAT_PCM_16_BIT, AUDIO_FORMAT_PCM_32_BIT and AUDIO_FORMAT_PCM_FLOAT input format!");
setDisableReason(DisableReason::INVALID_FORMAT);
return;
}
if (config.outputCfg.format != AUDIO_FORMAT_PCM_16_BIT &&
config.outputCfg.format != AUDIO_FORMAT_PCM_32_BIT &&
config.outputCfg.format != AUDIO_FORMAT_PCM_FLOAT) {
VIPER_LOGE("ViPER4Android disabled, reason [out.FMT = %d]", config.outputCfg.format);
VIPER_LOGE("We only accept AUDIO_FORMAT_PCM_16_BIT, AUDIO_FORMAT_PCM_32_BIT and AUDIO_FORMAT_PCM_FLOAT output format!");
setDisableReason(DisableReason::INVALID_FORMAT);
return;
}
VIPER_LOGI("Input and output configuration checked.");
setDisableReason(DisableReason::NONE);
// Processing buffer
buffer.resize(config.inputCfg.buffer.frameCount * 2);
bufferFrameCount = config.inputCfg.buffer.frameCount;
// ViPER
viper.setSamplingRate(config.inputCfg.samplingRate);
viper.reset();
}
int32_t ViperContext::handleSetParam(effect_param_t *pCmdParam, void *pReplyData) {
// The value offset of an effect parameter is computed by rounding up
// the parameter size to the next 32 bit alignment.
uint32_t vOffset = ((pCmdParam->psize + sizeof(int32_t) - 1) / sizeof(int32_t)) * sizeof(int32_t);
if (pCmdParam->psize != sizeof(uint32_t)) {
VIPER_LOGE("handleSetParam: EFFECT_CMD_SET_PARAM called with invalid psize = %d, expected psize = %zu", pCmdParam->vsize, sizeof(uint32_t));
return -EINVAL;
}
*(int32_t *) pReplyData = 0;
uint32_t key = *(uint32_t *) (pCmdParam->data);
switch (key) {
case PARAM_SET_RESET: {
VIPER_LOGD("handleSetParam: PARAM_SET_RESET called");
viper.reset();
return 0;
}
case PARAM_SET_VIPER_DDC_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_DDC_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_DDC_ENABLE called with enable = %d", enable);
viper.viperDdc.SetEnable(enable);
return 0;
}
case PARAM_SET_VIPER_DDC_COEFFICIENTS: {
if (pCmdParam->vsize < sizeof(uint32_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_DDC_COEFFICIENTS called with invalid vsize = %d, expected vsize >= %zu", pCmdParam->vsize, sizeof(uint32_t));
return -EINVAL;
}
uint32_t size = *(uint32_t *) (pCmdParam->data + vOffset);
float *coeffs44100 = (float *) (pCmdParam->data + vOffset + sizeof(uint32_t));
float *coeffs48000 = (float *) (pCmdParam->data + vOffset + sizeof(uint32_t) + sizeof(float) * size);
if (pCmdParam->vsize != sizeof(uint32_t) + sizeof(float) * 2 * size) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_DDC_COEFFICIENTS called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint32_t) + sizeof(float) * 2 * size);
return -EINVAL;
}
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_DDC_COEFFICIENTS called with size = %d", size);
viper.viperDdc.SetCoeffs(size, coeffs44100, coeffs48000);
return 0;
}
case PARAM_SET_VIPER_BASS_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_BASS_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_BASS_ENABLE called with enable = %d", enable);
viper.viperBass.SetEnable(enable);
return 0;
}
case PARAM_SET_VIPER_BASS_MODE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_BASS_MODE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t mode = *(uint8_t *) (pCmdParam->data + vOffset);
if (mode > 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_BASS_MODE called with invalid mode = %d, expected mode = 0, 1 or 2", mode);
*(int32_t *) pReplyData = -EINVAL;
return 0;
}
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_BASS_MODE called with mode = %d", mode);
viper.viperBass.SetProcessMode(static_cast<ViPERBass::ProcessMode>(mode));
return 0;
}
case PARAM_SET_VIPER_BASS_FREQUENCY: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_BASS_FREQUENCY called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t frequency = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_BASS_FREQUENCY called with frequency = %d", frequency);
viper.viperBass.SetSpeaker(frequency);
return 0;
}
case PARAM_SET_VIPER_BASS_GAIN: {
if (pCmdParam->vsize != sizeof(uint16_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_BASS_GAIN called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t));
return -EINVAL;
}
uint16_t gain = *(uint16_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_BASS_GAIN called with gain = %d", gain);
viper.viperBass.SetBassFactor(static_cast<float>(gain) / 100.0f);
return 0;
}
case PARAM_SET_VIPER_CLARITY_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_CLARITY_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_CLARITY_ENABLE called with enable = %d", enable);
viper.viperClarity.SetEnable(enable);
return 0;
}
case PARAM_SET_VIPER_CLARITY_MODE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_CLARITY_MODE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t mode = *(uint8_t *) (pCmdParam->data + vOffset);
if (mode > 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_CLARITY_MODE called with invalid mode = %d, expected mode = 0, 1 or 2", mode);
*(int32_t *) pReplyData = -EINVAL;
return 0;
}
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_CLARITY_MODE called with mode = %d", mode);
viper.viperClarity.SetProcessMode(static_cast<ViPERClarity::ClarityMode>(mode));
return 0;
}
case PARAM_SET_VIPER_CLARITY_GAIN: {
if (pCmdParam->vsize != sizeof(uint16_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_VIPER_CLARITY_GAIN called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t));
return -EINVAL;
}
uint16_t gain = *(uint16_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_VIPER_CLARITY_GAIN called with gain = %d", gain);
viper.viperClarity.SetClarity(static_cast<float>(gain) / 100.0f);
return 0;
}
case PARAM_SET_OUTPUT_GAIN: {
// 0 - 255
if (pCmdParam->vsize != sizeof(uint8_t) * 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_OUTPUT_GAIN called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t) * 2);
return -EINVAL;
}
uint8_t gainL = *(uint8_t *) (pCmdParam->data + vOffset);
uint8_t gainR = *(uint8_t *) (pCmdParam->data + vOffset + sizeof(uint8_t));
VIPER_LOGD("handleSetParam: PARAM_SET_OUTPUT_GAIN called with gainL = %d, gainR = %d", gainL, gainR);
viper.setGain(static_cast<float>(gainL) / 100.0f, static_cast<float>(gainR) / 100.0f);
return 0;
}
case PARAM_SET_THRESHOLD_LIMIT: {
// 0 - 100 (TODO: Check range)
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_THRESHOLD_LIMIT called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t limit = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_THRESHOLD_LIMIT called with limit = %d", limit);
viper.setThresholdLimit(static_cast<float>(limit) / 100.0f);
return 0;
}
case PARAM_SET_SPEAKER_OPTIMIZATION_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_SPEAKER_OPTIMIZATION_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_SPEAKER_OPTIMIZATION_ENABLE called with enable = %d", enable);
viper.speakerCorrection.SetEnable(enable);
return 0;
}
case PARAM_SET_ANALOGX_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_ANALOGX_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_ANALOGX_ENABLE called with enable = %d", enable);
viper.analogX.SetEnable(enable);
return 0;
}
case PARAM_SET_ANALOGX_LEVEL: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_ANALOGX_LEVEL called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t level = *(uint8_t *) (pCmdParam->data + vOffset);
if (level > 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_ANALOGX_LEVEL called with invalid level = %d, expected level = 0, 1 or 2", level);
*(int32_t *) pReplyData = -EINVAL;
return 0;
}
VIPER_LOGD("handleSetParam: PARAM_SET_ANALOGX_LEVEL called with level = %d", level);
viper.analogX.SetProcessingModel(level);
return 0;
}
case PARAM_SET_TUBE_SIMULATOR_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_TUBE_SIMULATOR_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_TUBE_SIMULATOR_ENABLE called with enable = %d", enable);
viper.tubeSimulator.SetEnable(enable);
return 0;
}
case PARAM_SET_CURE_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_CURE_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_CURE_ENABLE called with enable = %d", enable);
viper.cure.SetEnable(enable);
return 0;
}
case PARAM_SET_CURE_LEVEL: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_CURE_LEVEL called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t level = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_CURE_LEVEL called with level = %d", level);
switch (level) {
case 0: {
struct Crossfeed::Preset preset = {
.cutoff = 650,
.feedback = 95,
};
viper.cure.SetPreset(preset);
break;
}
case 1: {
struct Crossfeed::Preset preset = {
.cutoff = 700,
.feedback = 60,
};
viper.cure.SetPreset(preset);
break;
}
case 2: {
struct Crossfeed::Preset preset = {
.cutoff = 700,
.feedback = 45,
};
viper.cure.SetPreset(preset);
break;
}
default:
VIPER_LOGE("handleSetParam: PARAM_SET_CURE_LEVEL called with invalid level = %d, expected level = 0, 1 or 2", level);
*(int32_t *) pReplyData = -EINVAL;
break;
}
return 0;
}
case PARAM_SET_REVERBERATION_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_REVERBERATION_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_REVERBERATION_ENABLE called with enable = %d", enable);
viper.reverberation.SetEnable(enable);
return 0;
}
case PARAM_SET_REVERBERATION_ROOM_SIZE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_REVERBERATION_ROOM_SIZE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t roomSize = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_REVERBERATION_ROOM_SIZE called with roomSize = %d", roomSize);
viper.reverberation.SetRoomSize(static_cast<float>(roomSize) / 100.0f);
return 0;
}
case PARAM_SET_REVERBERATION_SOUND_FIELD: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_REVERBERATION_SOUND_FIELD called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t soundField = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_REVERBERATION_SOUND_FIELD called with soundField = %d", soundField);
viper.reverberation.SetWidth(static_cast<float>(soundField) / 100.0f);
return 0;
}
case PARAM_SET_REVERBERATION_DAMPING: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_REVERBERATION_DAMPING called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t damping = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_REVERBERATION_DAMPING called with damping = %d", damping);
viper.reverberation.SetDamp(static_cast<float>(damping) / 100.0f);
return 0;
}
case PARAM_SET_REVERBERATION_WET_SIGNAL: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_REVERBERATION_WET_SIGNAL called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t wetSignal = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_REVERBERATION_WET_SIGNAL called with wetSignal = %d", wetSignal);
viper.reverberation.SetWet(static_cast<float>(wetSignal) / 100.0f);
return 0;
}
case PARAM_SET_REVERBERATION_DRY_SIGNAL: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_REVERBERATION_DRY_SIGNAL called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t drySignal = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_REVERBERATION_DRY_SIGNAL called with drySignal = %d", drySignal);
viper.reverberation.SetDry(static_cast<float>(drySignal) / 100.0f);
return 0;
}
case PARAM_SET_DIFFERENTIAL_SURROUND_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_DIFFERENTIAL_SURROUND_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_DIFFERENTIAL_SURROUND_ENABLE called with enable = %d", enable);
viper.diffSurround.SetEnable(enable);
return 0;
}
case PARAM_SET_DIFFERENTIAL_SURROUND_DELAY: {
if (pCmdParam->vsize != sizeof(uint16_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_DIFFERENTIAL_SURROUND_DELAY called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t));
return -EINVAL;
}
uint16_t delay = *(uint16_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_DIFFERENTIAL_SURROUND_DELAY called with delay = %d", delay);
viper.diffSurround.SetDelayTime(static_cast<float>(delay) / 100.0f);
return 0;
}
case PARAM_SET_FIELD_SURROUND_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_FIELD_SURROUND_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_FIELD_SURROUND_ENABLE called with enable = %d", enable);
viper.colorfulMusic.SetEnable(enable);
return 0;
}
case PARAM_SET_FIELD_SURROUND_DEPTH: {
if (pCmdParam->vsize != sizeof(uint16_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_FIELD_SURROUND_DEPTH called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t));
return -EINVAL;
}
uint16_t depth = *(uint16_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_FIELD_SURROUND_DEPTH called with depth = %d", depth);
viper.colorfulMusic.SetDepthValue(depth);
return 0;
}
case PARAM_SET_FIELD_SURROUND_MID_IMAGE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_FIELD_SURROUND_MID_IMAGE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t midImage = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_FIELD_SURROUND_MID_IMAGE called with midImage = %d", midImage);
viper.colorfulMusic.SetMidImageValue(static_cast<float>(midImage) / 100.0f);
return 0;
}
case PARAM_SET_IIR_EQUALIZER_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_IIR_EQUALIZER_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_IIR_EQUALIZER_ENABLE called with enable = %d", enable);
viper.iirFilter.SetEnable(enable);
return 0;
}
case PARAM_SET_IIR_EQUALIZER_BAND_LEVEL: {
if (pCmdParam->vsize != sizeof(uint8_t) + sizeof(int16_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_IIR_EQUALIZER_BAND_LEVEL called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t) + sizeof(int16_t));
return -EINVAL;
}
uint8_t band = *(uint8_t *) (pCmdParam->data + vOffset);
int16_t level = *(int16_t *) (pCmdParam->data + vOffset + sizeof(uint8_t));
VIPER_LOGD("handleSetParam: PARAM_SET_IIR_EQUALIZER_BAND_LEVEL called with band = %d, level = %d", band, level);
viper.iirFilter.SetBandLevel(band, static_cast<float>(level) / 100.0f);
return 0;
}
case PARAM_SET_SPECTRUM_EXTENSION_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_SPECTRUM_EXTENSION_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_SPECTRUM_EXTENSION_ENABLE called with enable = %d", enable);
viper.spectrumExtend.SetEnable(enable);
return 0;
}
case PARAM_SET_SPECTRUM_EXTENSION_STRENGTH: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_SPECTRUM_EXTENSION_STRENGTH called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t strength = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_SPECTRUM_EXTENSION_STRENGTH called with strength = %d", strength);
viper.spectrumExtend.SetExciter(static_cast<float>(strength) / 100.0f);
return 0;
}
case PARAM_SET_HEADPHONE_SURROUND_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_HEADPHONE_SURROUND_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_HEADPHONE_SURROUND_ENABLE called with enable = %d", enable);
viper.vhe.SetEnable(enable);
return 0;
}
case PARAM_SET_HEADPHONE_SURROUND_LEVEL: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_HEADPHONE_SURROUND_LEVEL called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
uint8_t level = *(uint8_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_HEADPHONE_SURROUND_LEVEL called with level = %d", level);
viper.vhe.SetEffectLevel(level);
return 0;
}
case PARAM_SET_DYNAMIC_SYSTEM_ENABLE: {
if (pCmdParam->vsize != sizeof(uint8_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_ENABLE called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t));
return -EINVAL;
}
bool enable = *(uint8_t *) (pCmdParam->data + vOffset) != 0;
VIPER_LOGD("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_ENABLE called with enable = %d", enable);
viper.dynamicSystem.SetEnable(enable);
return 0;
}
case PARAM_SET_DYNAMIC_SYSTEM_X_COEFFICIENTS: {
if (pCmdParam->vsize != sizeof(uint16_t) * 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_X_COEFFICIENTS called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t) * 2);
return -EINVAL;
}
uint16_t low = *(uint16_t *) (pCmdParam->data + vOffset);
uint16_t high = *(uint16_t *) (pCmdParam->data + vOffset + sizeof(uint16_t));
VIPER_LOGD("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_X_COEFFICIENTS called with low = %d, high = %d", low, high);
viper.dynamicSystem.SetXCoeffs(low, high);
return 0;
}
case PARAM_SET_DYNAMIC_SYSTEM_Y_COEFFICIENTS: {
if (pCmdParam->vsize != sizeof(uint16_t) * 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_Y_COEFFICIENTS called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t) * 2);
return -EINVAL;
}
uint16_t low = *(uint16_t *) (pCmdParam->data + vOffset);
uint16_t high = *(uint16_t *) (pCmdParam->data + vOffset + sizeof(uint16_t));
VIPER_LOGD("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_Y_COEFFICIENTS called with low = %d, high = %d", low, high);
viper.dynamicSystem.SetYCoeffs(low, high);
return 0;
}
case PARAM_SET_DYNAMIC_SYSTEM_SIDE_GAIN: {
if (pCmdParam->vsize != sizeof(uint8_t) * 2) {
VIPER_LOGE("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_SIDE_GAIN called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint8_t) * 2);
return -EINVAL;
}
uint8_t gainX = *(uint8_t *) (pCmdParam->data + vOffset);
uint8_t gainY = *(uint8_t *) (pCmdParam->data + vOffset + sizeof(uint8_t));
VIPER_LOGD("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_SIDE_GAIN called with gainX = %d, gainY = %d", gainX, gainY);
viper.dynamicSystem.SetSideGain(static_cast<float>(gainX) / 100.0f, static_cast<float>(gainY) / 100.0f);
return 0;
}
case PARAM_SET_DYNAMIC_SYSTEM_STRENGTH: {
if (pCmdParam->vsize != sizeof(uint16_t)) {
VIPER_LOGE("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_STRENGTH called with invalid vsize = %d, expected vsize = %zu", pCmdParam->vsize, sizeof(uint16_t));
return -EINVAL;
}
uint16_t strength = *(uint16_t *) (pCmdParam->data + vOffset);
VIPER_LOGD("handleSetParam: PARAM_SET_DYNAMIC_SYSTEM_STRENGTH called with strength = %d", strength);
viper.dynamicSystem.SetBassGain(static_cast<float>(strength) / 100.0f);
return 0;
}
default: {
VIPER_LOGE("handleSetParam: called with unknown key: %d", key);
return -EINVAL;
}
}
}
int32_t ViperContext::handleGetParam(effect_param_t *pCmdParam, effect_param_t *pReplyParam, uint32_t *pReplySize) {
// The value offset of an effect parameter is computed by rounding up
// the parameter size to the next 32 bit alignment.
uint32_t vOffset = ((pCmdParam->psize + sizeof(int32_t) - 1) / sizeof(int32_t)) * sizeof(int32_t);
if (pCmdParam->psize != sizeof(uint32_t)) {
VIPER_LOGE("handleGetParam() EFFECT_CMD_GET_PARAM called with invalid psize = %d, expected psize = %zu", pCmdParam->vsize, sizeof(uint32_t));
return -EINVAL;
}
memcpy(pReplyParam, pCmdParam, sizeof(effect_param_t) + pCmdParam->psize);
uint32_t key = *(uint32_t *) (pCmdParam->data);
switch (key) {
case PARAM_GET_ENABLED: {
pReplyParam->status = 0;
pReplyParam->vsize = sizeof(uint8_t);
*(uint8_t *) (pReplyParam->data + vOffset) = enabled;
*pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize;
return 0;
}
case PARAM_GET_FRAME_COUNT: {
pReplyParam->status = 0;
pReplyParam->vsize = sizeof(uint64_t);
*(uint64_t *) (pReplyParam->data + vOffset) = viper.getFrameCount();
*pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize;
return 0;
}
case PARAM_GET_VERSION: {
pReplyParam->status = 0;
pReplyParam->vsize = sizeof(uint32_t);
*(uint32_t *) (pReplyParam->data + vOffset) = VIPER_VERSION;
*pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize;
return 0;
}
case PARAM_GET_DISABLE_REASON: {
pReplyParam->status = 0;
pReplyParam->vsize = sizeof(int32_t);
*(int32_t *) (pReplyParam->data + vOffset) = static_cast<int32_t>(disableReason);
*pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize;
return 0;
}
case PARAM_GET_CONFIG: {
pReplyParam->status = 0;
pReplyParam->vsize = 40;
// Input
SET(uint64_t, pReplyParam->data + vOffset, config.inputCfg.buffer.frameCount);
SET(uint32_t, pReplyParam->data + vOffset + 8, config.inputCfg.samplingRate);
SET(uint32_t, pReplyParam->data + vOffset + 12, config.inputCfg.channels);
SET(uint8_t, pReplyParam->data + vOffset + 16, config.inputCfg.format);
SET(uint8_t, pReplyParam->data + vOffset + 17, config.inputCfg.accessMode);
SET(uint16_t, pReplyParam->data + vOffset + 18, config.inputCfg.mask);
// Output
SET(uint64_t, pReplyParam->data + vOffset + 20, config.outputCfg.buffer.frameCount);
SET(uint32_t, pReplyParam->data + vOffset + 28, config.outputCfg.samplingRate);
SET(uint32_t, pReplyParam->data + vOffset + 32, config.outputCfg.channels);
SET(uint8_t, pReplyParam->data + vOffset + 36, config.outputCfg.format);
SET(uint8_t, pReplyParam->data + vOffset + 37, config.outputCfg.accessMode);
SET(uint16_t, pReplyParam->data + vOffset + 38, config.outputCfg.mask);
*pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize;
return 0;
}
case PARAM_GET_ARCHITECTURE: {
pReplyParam->status = 0;
pReplyParam->vsize = sizeof(uint8_t);
*(uint8_t *) (pReplyParam->data + vOffset) = static_cast<uint8_t>(VIPER_ARCHITECTURE);
*pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize;
return 0;
}
default: {
VIPER_LOGE("handleGetParam() called with unknown key: %d", key);
return -EINVAL;
}
}
}
int32_t ViperContext::handleCommand(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, uint32_t *pReplySize, void *pReplyData) {
uint32_t replySize = pReplySize == nullptr ? 0 : *pReplySize;
switch (cmdCode) {
case EFFECT_CMD_INIT: {
if (replySize != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_INIT called with invalid replySize = %d, pReplyData = %p, expected replySize = %zu", replySize, pReplyData, sizeof(int32_t));
return -EINVAL;
}
SET(int32_t, pReplyData, 0);
return 0;
}
case EFFECT_CMD_SET_CONFIG: {
if (cmdSize != sizeof(effect_config_t) || pCmdData == nullptr || replySize != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_SET_CONFIG called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %zu, expected replySize = %zu", cmdSize, pCmdData, replySize, pReplyData, sizeof(effect_config_t), sizeof(int32_t));
return -EINVAL;
}
handleSetConfig((effect_config_t *) pCmdData);
SET(int32_t, pReplyData, 0);
return 0;
}
case EFFECT_CMD_RESET: {
viper.reset();
return 0;
}
case EFFECT_CMD_ENABLE: {
if (replySize != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_ENABLE called with invalid replySize = %d, pReplyData = %p, expected replySize = %zu", replySize, pReplyData, sizeof(int32_t));
return -EINVAL;
}
enabled = true;
SET(int32_t, pReplyData, 0);
return 0;
}
case EFFECT_CMD_DISABLE: {
if (replySize != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_DISABLE called with invalid replySize = %d, pReplyData = %p, expected replySize = %zu", replySize, pReplyData, sizeof(int32_t));
return -EINVAL;
}
enabled = false;
SET(int32_t, pReplyData, 0);
return 0;
}
case EFFECT_CMD_SET_PARAM: {
if (cmdSize < sizeof(effect_param_t) || pCmdData == nullptr || replySize != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_SET_PARAM called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize >= %zu, expected replySize >= %zu", cmdSize, pCmdData, replySize, pReplyData, sizeof(effect_param_t), sizeof(int32_t));
return -EINVAL;
}
return handleSetParam((effect_param_t *) pCmdData, pReplyData);
}
case EFFECT_CMD_GET_PARAM: {
if (cmdSize < sizeof(effect_param_t) || pCmdData == nullptr || replySize < sizeof(effect_param_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_GET_PARAM called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize >= %zu, expected replySize >= %zu", cmdSize, pCmdData, replySize, pReplyData, sizeof(effect_param_t), sizeof(effect_param_t));
return -EINVAL;
}
return handleGetParam((effect_param_t *) pCmdData, (effect_param_t *) pReplyData, pReplySize);
}
case EFFECT_CMD_GET_CONFIG: {
if (replySize != sizeof(effect_config_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_GET_CONFIG called with invalid replySize = %d, pReplyData = %p, expected replySize = %zu", replySize, pReplyData, sizeof(effect_config_t));
return -EINVAL;
}
*(effect_config_t *) pReplyData = config;
return 0;
}
default: {
VIPER_LOGE("handleCommand called with unknown command: %d", cmdCode);
return -EINVAL;
}
}
}
template <typename T>
void pcmToFloat(float* dst, const T* src, size_t frameCount) {
constexpr float max_val = static_cast<float>(std::numeric_limits<T>::max());
for (size_t i = 0; i < frameCount * 2; i++) {
dst[i] = static_cast<float>(src[i]) / max_val;
}
}
template <typename T>
static const T& clamp(const T& v, const T& lo, const T& hi) {
return std::min(std::max(v, lo), hi);
}
static void floatToFloat(float *dst, const float *src, size_t frameCount, bool accumulate) {
if (accumulate) {
for (size_t i = 0; i < frameCount * 2; i++) {
dst[i] = clamp(dst[i] + src[i], -1.0f, 1.0f);
}
} else {
memcpy(dst, src, frameCount * 2 * sizeof(float));
}
}
template <typename T, typename U>
void floatToPcm(T *dst, const float *src, size_t frameCount, bool accumulate) {
constexpr T max_val = std::numeric_limits<T>::max();
constexpr T min_val = std::numeric_limits<T>::min();
for (size_t i = 0; i < frameCount * 2; i++) {
T pcm = static_cast<T>(src[i] * static_cast<float>(max_val));
if (accumulate) {
U temp = static_cast<U>(dst[i]) + pcm;
dst[i] = static_cast<T>(clamp(temp, static_cast<U>(min_val), static_cast<U>(max_val)));
} else {
dst[i] = pcm;
}
}
}
static audio_buffer_t *getBuffer(buffer_config_s *config, audio_buffer_t *buffer) {
if (buffer != nullptr) return buffer;
if (config->mask & EFFECT_CONFIG_BUFFER) return &config->buffer;
// EFFECT_CONFIG_PROVIDER not implemented, it's not used by any known effect
return nullptr;
}
int32_t ViperContext::process(audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
if (disableReason != DisableReason::NONE) {
return -EINVAL;
}
if (!enabled) {
return -ENODATA;
}
inBuffer = getBuffer(&config.inputCfg, inBuffer);
outBuffer = getBuffer(&config.outputCfg, outBuffer);
if (inBuffer == nullptr || outBuffer == nullptr ||
inBuffer->raw == nullptr || outBuffer->raw == nullptr ||
inBuffer->frameCount != outBuffer->frameCount ||
inBuffer->frameCount == 0) {
return -EINVAL;
}
size_t frameCount = inBuffer->frameCount;
if (frameCount > bufferFrameCount) {
// This should never happen, but just in case
buffer.resize(frameCount * 2);
bufferFrameCount = frameCount;
}
switch (config.inputCfg.format) {
case AUDIO_FORMAT_PCM_16_BIT:
pcmToFloat<int16_t>(buffer.data(), inBuffer->s16, frameCount);
break;
case AUDIO_FORMAT_PCM_32_BIT:
pcmToFloat<int32_t>(buffer.data(), inBuffer->s32, frameCount);
break;
case AUDIO_FORMAT_PCM_FLOAT:
floatToFloat(buffer.data(), inBuffer->f32, frameCount, false);
break;
default:
return -EINVAL;
}
viper.process(buffer, frameCount);
const bool accumulate = config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE;
switch (config.outputCfg.format) {
case AUDIO_FORMAT_PCM_16_BIT:
floatToPcm<int16_t, int32_t>(outBuffer->s16, buffer.data(), frameCount, accumulate);
break;
case AUDIO_FORMAT_PCM_32_BIT:
floatToPcm<int32_t, int64_t>(outBuffer->s32, buffer.data(), frameCount, accumulate);
break;
case AUDIO_FORMAT_PCM_FLOAT:
floatToFloat(outBuffer->f32, buffer.data(), frameCount, accumulate);
break;
default:
return -EINVAL;
}
return 0;
}
void ViperContext::setDisableReason(DisableReason reason) {
this->disableReason = reason;
}