-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
audio.cpp
1254 lines (1133 loc) · 38.8 KB
/
audio.cpp
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 (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "opentx.h"
#include <math.h>
#if defined(LIBOPENUI)
#include "libopenui.h"
#endif
extern RTOS_MUTEX_HANDLE audioMutex;
const int16_t sineValues[] =
{
0, 196, 392, 588, 784, 980, 1175, 1370, 1564, 1758,
1951, 2143, 2335, 2525, 2715, 2904, 3091, 3278, 3463, 3647,
3829, 4011, 4190, 4369, 4545, 4720, 4894, 5065, 5235, 5403,
5569, 5733, 5895, 6055, 6213, 6369, 6522, 6673, 6822, 6969,
7113, 7255, 7395, 7532, 7667, 7799, 7929, 8056, 8180, 8302,
8422, 8539, 8653, 8765, 8874, 8980, 9084, 9185, 9283, 9379,
9472, 9562, 9650, 9735, 9818, 9898, 9975, 10050, 10123, 10192,
10260, 10324, 10387, 10447, 10504, 10559, 10612, 10663, 10711, 10757,
10801, 10843, 10882, 10920, 10955, 10989, 11020, 11050, 11078, 11104,
11128, 11151, 11172, 11191, 11209, 11225, 11240, 11254, 11266, 11277,
11287, 11296, 11303, 11310, 11316, 11320, 11324, 11327, 11330, 11331,
11332, 11333, 11333, 11333, 11332, 11331, 11329, 11328, 11326, 11324,
11323, 11321, 11319, 11318, 11316, 11315, 11314, 11313, 11313, 11313,
11314, 11315, 11317, 11319, 11323, 11326, 11331, 11336, 11342, 11349,
11356, 11365, 11375, 11385, 11397, 11409, 11423, 11437, 11453, 11470,
11488, 11507, 11527, 11548, 11571, 11595, 11620, 11646, 11673, 11702,
11732, 11763, 11795, 11828, 11863, 11899, 11936, 11974, 12013, 12054,
12095, 12138, 12182, 12227, 12273, 12320, 12368, 12417, 12467, 12518,
12570, 12623, 12676, 12731, 12786, 12842, 12898, 12956, 13014, 13072,
13131, 13191, 13251, 13311, 13372, 13433, 13495, 13556, 13618, 13680,
13743, 13805, 13867, 13929, 13991, 14053, 14115, 14177, 14238, 14299,
14359, 14419, 14479, 14538, 14597, 14655, 14712, 14768, 14824, 14879,
14933, 14986, 15039, 15090, 15140, 15189, 15237, 15284, 15330, 15375,
15418, 15460, 15500, 15539, 15577, 15614, 15648, 15682, 15714, 15744,
15772, 15799, 15825, 15849, 15871, 15891, 15910, 15927, 15942, 15955,
15967, 15977, 15985, 15991, 15996, 15999, 16000, 15999, 15996, 15991,
15985, 15977, 15967, 15955, 15942, 15927, 15910, 15891, 15871, 15849,
15825, 15799, 15772, 15744, 15714, 15682, 15648, 15614, 15577, 15539,
15500, 15460, 15418, 15375, 15330, 15284, 15237, 15189, 15140, 15090,
15039, 14986, 14933, 14879, 14824, 14768, 14712, 14655, 14597, 14538,
14479, 14419, 14359, 14299, 14238, 14177, 14115, 14053, 13991, 13929,
13867, 13805, 13743, 13680, 13618, 13556, 13495, 13433, 13372, 13311,
13251, 13191, 13131, 13072, 13014, 12956, 12898, 12842, 12786, 12731,
12676, 12623, 12570, 12518, 12467, 12417, 12368, 12320, 12273, 12227,
12182, 12138, 12095, 12054, 12013, 11974, 11936, 11899, 11863, 11828,
11795, 11763, 11732, 11702, 11673, 11646, 11620, 11595, 11571, 11548,
11527, 11507, 11488, 11470, 11453, 11437, 11423, 11409, 11397, 11385,
11375, 11365, 11356, 11349, 11342, 11336, 11331, 11326, 11323, 11319,
11317, 11315, 11314, 11313, 11313, 11313, 11314, 11315, 11316, 11318,
11319, 11321, 11323, 11324, 11326, 11328, 11329, 11331, 11332, 11333,
11333, 11333, 11332, 11331, 11330, 11327, 11324, 11320, 11316, 11310,
11303, 11296, 11287, 11277, 11266, 11254, 11240, 11225, 11209, 11191,
11172, 11151, 11128, 11104, 11078, 11050, 11020, 10989, 10955, 10920,
10882, 10843, 10801, 10757, 10711, 10663, 10612, 10559, 10504, 10447,
10387, 10324, 10260, 10192, 10123, 10050, 9975, 9898, 9818, 9735,
9650, 9562, 9472, 9379, 9283, 9185, 9084, 8980, 8874, 8765,
8653, 8539, 8422, 8302, 8180, 8056, 7929, 7799, 7667, 7532,
7395, 7255, 7113, 6969, 6822, 6673, 6522, 6369, 6213, 6055,
5895, 5733, 5569, 5403, 5235, 5065, 4894, 4720, 4545, 4369,
4190, 4011, 3829, 3647, 3463, 3278, 3091, 2904, 2715, 2525,
2335, 2143, 1951, 1758, 1564, 1370, 1175, 980, 784, 588,
392, 196, 0, -196, -392, -588, -784, -980, -1175, -1370,
-1564, -1758, -1951, -2143, -2335, -2525, -2715, -2904, -3091, -3278,
-3463, -3647, -3829, -4011, -4190, -4369, -4545, -4720, -4894, -5065,
-5235, -5403, -5569, -5733, -5895, -6055, -6213, -6369, -6522, -6673,
-6822, -6969, -7113, -7255, -7395, -7532, -7667, -7799, -7929, -8056,
-8180, -8302, -8422, -8539, -8653, -8765, -8874, -8980, -9084, -9185,
-9283, -9379, -9472, -9562, -9650, -9735, -9818, -9898, -9975, -10050,
-10123, -10192, -10260, -10324, -10387, -10447, -10504, -10559, -10612, -10663,
-10711, -10757, -10801, -10843, -10882, -10920, -10955, -10989, -11020, -11050,
-11078, -11104, -11128, -11151, -11172, -11191, -11209, -11225, -11240, -11254,
-11266, -11277, -11287, -11296, -11303, -11310, -11316, -11320, -11324, -11327,
-11330, -11331, -11332, -11333, -11333, -11333, -11332, -11331, -11329, -11328,
-11326, -11324, -11323, -11321, -11319, -11318, -11316, -11315, -11314, -11313,
-11313, -11313, -11314, -11315, -11317, -11319, -11323, -11326, -11331, -11336,
-11342, -11349, -11356, -11365, -11375, -11385, -11397, -11409, -11423, -11437,
-11453, -11470, -11488, -11507, -11527, -11548, -11571, -11595, -11620, -11646,
-11673, -11702, -11732, -11763, -11795, -11828, -11863, -11899, -11936, -11974,
-12013, -12054, -12095, -12138, -12182, -12227, -12273, -12320, -12368, -12417,
-12467, -12518, -12570, -12623, -12676, -12731, -12786, -12842, -12898, -12956,
-13014, -13072, -13131, -13191, -13251, -13311, -13372, -13433, -13495, -13556,
-13618, -13680, -13743, -13805, -13867, -13929, -13991, -14053, -14115, -14177,
-14238, -14299, -14359, -14419, -14479, -14538, -14597, -14655, -14712, -14768,
-14824, -14879, -14933, -14986, -15039, -15090, -15140, -15189, -15237, -15284,
-15330, -15375, -15418, -15460, -15500, -15539, -15577, -15614, -15648, -15682,
-15714, -15744, -15772, -15799, -15825, -15849, -15871, -15891, -15910, -15927,
-15942, -15955, -15967, -15977, -15985, -15991, -15996, -15999, -16000, -15999,
-15996, -15991, -15985, -15977, -15967, -15955, -15942, -15927, -15910, -15891,
-15871, -15849, -15825, -15799, -15772, -15744, -15714, -15682, -15648, -15614,
-15577, -15539, -15500, -15460, -15418, -15375, -15330, -15284, -15237, -15189,
-15140, -15090, -15039, -14986, -14933, -14879, -14824, -14768, -14712, -14655,
-14597, -14538, -14479, -14419, -14359, -14299, -14238, -14177, -14115, -14053,
-13991, -13929, -13867, -13805, -13743, -13680, -13618, -13556, -13495, -13433,
-13372, -13311, -13251, -13191, -13131, -13072, -13014, -12956, -12898, -12842,
-12786, -12731, -12676, -12623, -12570, -12518, -12467, -12417, -12368, -12320,
-12273, -12227, -12182, -12138, -12095, -12054, -12013, -11974, -11936, -11899,
-11863, -11828, -11795, -11763, -11732, -11702, -11673, -11646, -11620, -11595,
-11571, -11548, -11527, -11507, -11488, -11470, -11453, -11437, -11423, -11409,
-11397, -11385, -11375, -11365, -11356, -11349, -11342, -11336, -11331, -11326,
-11323, -11319, -11317, -11315, -11314, -11313, -11313, -11313, -11314, -11315,
-11316, -11318, -11319, -11321, -11323, -11324, -11326, -11328, -11329, -11331,
-11332, -11333, -11333, -11333, -11332, -11331, -11330, -11327, -11324, -11320,
-11316, -11310, -11303, -11296, -11287, -11277, -11266, -11254, -11240, -11225,
-11209, -11191, -11172, -11151, -11128, -11104, -11078, -11050, -11020, -10989,
-10955, -10920, -10882, -10843, -10801, -10757, -10711, -10663, -10612, -10559,
-10504, -10447, -10387, -10324, -10260, -10192, -10123, -10050, -9975, -9898,
-9818, -9735, -9650, -9562, -9472, -9379, -9283, -9185, -9084, -8980,
-8874, -8765, -8653, -8539, -8422, -8302, -8180, -8056, -7929, -7799,
-7667, -7532, -7395, -7255, -7113, -6969, -6822, -6673, -6522, -6369,
-6213, -6055, -5895, -5733, -5569, -5403, -5235, -5065, -4894, -4720,
-4545, -4369, -4190, -4011, -3829, -3647, -3463, -3278, -3091, -2904,
-2715, -2525, -2335, -2143, -1951, -1758, -1564, -1370, -1175, -980,
-784, -588, -392, -196,
};
#if defined(SDCARD)
const char * const unitsFilenames[] = {
"",
"volt",
"amp",
"mamp",
"knot",
"mps",
"fps",
"kph",
"mph",
"meter",
"foot",
"celsius",
"fahr",
"percent",
"mamph",
"watt",
"mwatt",
"db",
"rpm",
"g",
"degree",
"radian",
"ml",
"founce",
"mlpm",
"hertz",
"ms",
"us",
"km",
"dbm",
"spare6",
"spare7",
"spare8",
"spare9",
"spare10",
"hour",
"minute",
"second",
};
const char * const audioFilenames[] = {
"hello",
"bye",
"thralert",
"swalert",
"baddata",
"lowbatt",
"inactiv",
"rssi_org",
"rssi_red",
"swr_red",
"telemko",
"telemok",
"trainco",
"trainko",
"trainok",
"sensorko",
"servoko",
"rxko",
"modelpwr",
"error",
"warning1",
"warning2",
"warning3",
"midtrim",
"mintrim",
"maxtrim",
"midstck1",
"midstck2",
"midstck3",
"midstck4",
#if defined(PCBFRSKY)
"midpot1",
"midpot2",
#if defined(PCBX9E)
"midpot3",
"midpot4",
#endif
#if defined(PCBX10)
"midpot4",
"midpot5",
"midpot6",
"midpot7",
#endif
"midslid1",
"midslid2",
#if defined(PCBX9E)
"midslid3",
"midslid4",
#endif
#else
"midpot1",
"midpot2",
"midpot3",
#endif
"mixwarn1",
"mixwarn2",
"mixwarn3",
"timovr1",
"timovr2",
"timovr3"
};
BitField<(AU_SPECIAL_SOUND_FIRST)> sdAvailableSystemAudioFiles;
BitField<(MAX_FLIGHT_MODES * 2/*on, off*/)> sdAvailableFlightmodeAudioFiles;
BitField<(SWSRC_LAST_SWITCH+NUM_XPOTS*XPOTS_MULTIPOS_COUNT)> sdAvailableSwitchAudioFiles;
BitField<(MAX_LOGICAL_SWITCHES * 2/*on, off*/)> sdAvailableLogicalSwitchAudioFiles;
char * getAudioPath(char * path)
{
strcpy(path, SOUNDS_PATH "/");
strncpy(path+SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
return path + sizeof(SOUNDS_PATH);
}
char * strAppendSystemAudioPath(char * path)
{
char * str = getAudioPath(path);
strcpy(str, SYSTEM_SUBDIR "/");
return str + sizeof(SYSTEM_SUBDIR);
}
void getSystemAudioFile(char * filename, int index)
{
char * str = strAppendSystemAudioPath(filename);
strcpy(str, audioFilenames[index]);
strcat(str, SOUNDS_EXT);
}
void referenceSystemAudioFiles()
{
static_assert(sizeof(audioFilenames)==AU_SPECIAL_SOUND_FIRST*sizeof(char *), "Invalid audioFilenames size");
char path[AUDIO_FILENAME_MAXLEN+1];
FILINFO fno;
DIR dir;
sdAvailableSystemAudioFiles.reset();
char * filename = strAppendSystemAudioPath(path);
*(filename-1) = '\0';
FRESULT res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
uint8_t len = strlen(fno.fname);
// Eliminates directories / non wav files
if (len < 5 || strcasecmp(fno.fname+len-4, SOUNDS_EXT) || (fno.fattrib & AM_DIR)) continue;
for (int i=0; i<AU_SPECIAL_SOUND_FIRST; i++) {
getSystemAudioFile(path, i);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableSystemAudioFiles.setBit(i);
break;
}
}
}
f_closedir(&dir);
}
}
const char * const suffixes[] = { "-off", "-on" };
char *getModelAudioPath(char *path)
{
strcpy(path, SOUNDS_PATH "/");
strncpy(path + SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2);
char *buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), ' ');
if (!isFileAvailable(path)) {
buf = strcat_currentmodelname(path + sizeof(SOUNDS_PATH), 0);
}
*buf++ = '/';
*buf = '\0';
return buf;
}
void getFlightmodeAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);
char * tmp = strcatFlightmodeName(str, index);
strcpy(tmp, suffixes[event]);
strcat(tmp, SOUNDS_EXT);
}
void getSwitchAudioFile(char * filename, swsrc_t index)
{
char * str = getModelAudioPath(filename);
if (index <= SWSRC_LAST_SWITCH) {
div_t swinfo = switchInfo(index);
*str++ = 'S';
*str++ = getRawSwitchFromIdx(swinfo.quot);
const char * positions[] = { "-up", "-mid", "-down" };
strcpy(str, positions[swinfo.rem]);
}
#if NUM_XPOTS > 0
else {
div_t swinfo = div(int(index - SWSRC_FIRST_MULTIPOS_SWITCH), XPOTS_MULTIPOS_COUNT);
*str++ = 'S';
*str++ = '1' + swinfo.quot;
*str++ = '1' + swinfo.rem;
*str = '\0';
}
#endif
strcat(str, SOUNDS_EXT);
}
void getLogicalSwitchAudioFile(char * filename, int index, unsigned int event)
{
char * str = getModelAudioPath(filename);
*str++ = 'L';
if (index >= 9) {
div_t qr = div(index+1, 10);
*str++ = '0' + qr.quot;
*str++ = '0' + qr.rem;
}
else {
*str++ = '1' + index;
}
strcpy(str, suffixes[event]);
strcat(str, SOUNDS_EXT);
}
void referenceModelAudioFiles()
{
char path[AUDIO_FILENAME_MAXLEN+1];
FILINFO fno;
DIR dir;
sdAvailableFlightmodeAudioFiles.reset();
sdAvailableSwitchAudioFiles.reset();
sdAvailableLogicalSwitchAudioFiles.reset();
char * filename = getModelAudioPath(path);
*(filename-1) = '\0';
FRESULT res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
uint8_t len = strlen(fno.fname);
bool found = false;
// Eliminates directories / non wav files
if (len < 5 || strcasecmp(fno.fname+len-4, SOUNDS_EXT) || (fno.fattrib & AM_DIR)) continue;
TRACE("referenceModelAudioFiles(): using file: %s", fno.fname);
// Flight modes Audio Files <flightmodename>-[on|off].wav
for (int i=0; i<MAX_FLIGHT_MODES && !found; i++) {
for (int event=0; event<2; event++) {
getFlightmodeAudioFile(path, i, event);
// TRACE("referenceModelAudioFiles(): searching for %s in %s", filename, fno.fname);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableFlightmodeAudioFiles.setBit(INDEX_PHASE_AUDIO_FILE(i, event));
found = true;
TRACE("\tfound: %s", filename);
break;
}
}
}
// Switches Audio Files <switchname>-[up|mid|down].wav
for (int i=SWSRC_FIRST_SWITCH; i<=SWSRC_LAST_SWITCH+NUM_XPOTS*XPOTS_MULTIPOS_COUNT && !found; i++) {
getSwitchAudioFile(path, i);
// TRACE("referenceModelAudioFiles(): searching for %s in %s (%d)", path, fno.fname, i);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableSwitchAudioFiles.setBit(i-SWSRC_FIRST_SWITCH);
found = true;
TRACE("\tfound: %s", filename);
}
}
// Logical Switches Audio Files <switchname>-[on|off].wav
for (int i=0; i<MAX_LOGICAL_SWITCHES && !found; i++) {
for (int event=0; event<2; event++) {
getLogicalSwitchAudioFile(path, i, event);
// TRACE("referenceModelAudioFiles(): searching for %s in %s", filename, fno.fname);
if (!strcasecmp(filename, fno.fname)) {
sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(i, event));
found = true;
TRACE("\tfound: %s", filename);
break;
}
}
}
}
f_closedir(&dir);
}
}
bool isAudioFileReferenced(uint32_t i, char * filename)
{
uint8_t category = (i >> 24);
uint8_t index = (i >> 16) & 0xFF;
event_t event = i & 0xFF;
// TRACE("isAudioFileReferenced(%08x)", i);
if (category == SYSTEM_AUDIO_CATEGORY) {
if (sdAvailableSystemAudioFiles.getBit(event)) {
getSystemAudioFile(filename, event);
return true;
}
}
else if (category == PHASE_AUDIO_CATEGORY) {
if (sdAvailableFlightmodeAudioFiles.getBit(INDEX_PHASE_AUDIO_FILE(index, event))) {
getFlightmodeAudioFile(filename, index, event);
return true;
}
}
else if (category == SWITCH_AUDIO_CATEGORY) {
if (sdAvailableSwitchAudioFiles.getBit(index)) {
getSwitchAudioFile(filename, SWSRC_FIRST_SWITCH+index);
return true;
}
}
else if (category == LOGICAL_SWITCH_AUDIO_CATEGORY) {
if (sdAvailableLogicalSwitchAudioFiles.getBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(index, event))) {
getLogicalSwitchAudioFile(filename, index, event);
return true;
}
}
return false;
}
tmr10ms_t timeAutomaticPromptsSilence = 0;
void playModelEvent(uint8_t category, uint8_t index, event_t event)
{
char filename[AUDIO_FILENAME_MAXLEN+1];
// TRACE("playModelEvent(): cat: %u, idx: %u, evt:%u", (uint32_t)category, (uint32_t)index, (uint32_t)event);
if (IS_SILENCE_PERIOD_ELAPSED() && isAudioFileReferenced((category << 24) + (index << 16) + event, filename)) {
audioQueue.playFile(filename);
}
}
void playModelName()
{
char filename[AUDIO_FILENAME_MAXLEN + 1];
char *str = getModelAudioPath(filename);
strcpy(str, "name.wav");
audioQueue.playFile(filename);
}
#else // defined(SDCARD)
#define isAudioFileReferenced(i, f) false
#endif // defined(SDCARD)
AudioQueue audioQueue __DMA; // to place it in the RAM section on Horus, to have file buffers in RAM for DMA access
AudioBuffer audioBuffers[AUDIO_BUFFER_COUNT] __DMA;
AudioQueue::AudioQueue()
: buffersFifo(),
_started(false),
normalContext(),
backgroundContext(),
priorityContext(),
varioContext(),
fragmentsFifo()
{
}
#define CODEC_ID_PCM_S16LE 1
#if !defined(SIMU)
void audioTask(void * pdata)
{
while (!audioQueue.started()) {
RTOS_WAIT_TICKS(1);
}
setSampleRate(AUDIO_SAMPLE_RATE);
#if defined(PCBX12S) || defined(RADIO_TX16S)
// The audio amp needs ~2s to start
RTOS_WAIT_MS(1000); // 1s
#endif
while (true) {
DEBUG_TIMER_SAMPLE(debugTimerAudioIterval);
DEBUG_TIMER_START(debugTimerAudioDuration);
audioQueue.wakeup();
DEBUG_TIMER_STOP(debugTimerAudioDuration);
RTOS_WAIT_MS(4);
}
}
#endif
inline void mixSample(audio_data_t * result, int sample, unsigned int fade)
{
*result = limit(AUDIO_DATA_MIN, *result + ((sample >> fade) >> (16-AUDIO_BITS_PER_SAMPLE)), AUDIO_DATA_MAX);
}
#if defined(SDCARD)
#define RIFF_CHUNK_SIZE 12
uint8_t wavBuffer[AUDIO_BUFFER_SIZE*2] __DMA;
int WavContext::mixBuffer(AudioBuffer *buffer, int volume, unsigned int fade)
{
FRESULT result = FR_OK;
UINT read = 0;
if (fragment.file[1]) {
result = f_open(&state.file, fragment.file, FA_OPEN_EXISTING | FA_READ);
fragment.file[1] = 0;
if (result == FR_OK) {
result = f_read(&state.file, wavBuffer, RIFF_CHUNK_SIZE+8, &read);
if (result == FR_OK && read == RIFF_CHUNK_SIZE+8 && !memcmp(wavBuffer, "RIFF", 4) && !memcmp(wavBuffer+8, "WAVEfmt ", 8)) {
uint32_t size = *((uint32_t *)(wavBuffer+16));
result = (size < 256 ? f_read(&state.file, wavBuffer, size+8, &read) : FR_DENIED);
if (result == FR_OK && read == size+8) {
state.codec = ((uint16_t *)wavBuffer)[0];
state.freq = ((uint16_t *)wavBuffer)[2];
uint32_t *wavSamplesPtr = (uint32_t *)(wavBuffer + size);
uint32_t size = wavSamplesPtr[1];
if (state.freq != 0 && state.freq * (AUDIO_SAMPLE_RATE / state.freq) == AUDIO_SAMPLE_RATE) {
state.resampleRatio = (AUDIO_SAMPLE_RATE / state.freq);
state.readSize = (state.codec == CODEC_ID_PCM_S16LE ? 2*AUDIO_BUFFER_SIZE : AUDIO_BUFFER_SIZE) / state.resampleRatio;
}
else {
result = FR_DENIED;
}
while (result == FR_OK && memcmp(wavSamplesPtr, "data", 4) != 0) {
result = f_lseek(&state.file, f_tell(&state.file)+size);
if (result == FR_OK) {
result = f_read(&state.file, wavBuffer, 8, &read);
if (read != 8) result = FR_DENIED;
wavSamplesPtr = (uint32_t *)wavBuffer;
size = wavSamplesPtr[1];
}
}
state.size = size;
}
else {
result = FR_DENIED;
}
}
else {
result = FR_DENIED;
}
}
}
if (result == FR_OK) {
read = 0;
result = f_read(&state.file, wavBuffer, state.readSize, &read);
if (result == FR_OK) {
if (read > state.size) {
read = state.size;
}
state.size -= read;
if (read != state.readSize) {
f_close(&state.file);
fragment.clear();
}
audio_data_t * samples = buffer->data;
if (state.codec == CODEC_ID_PCM_S16LE) {
read /= 2;
for (uint32_t i=0; i<read; i++) {
for (uint8_t j=0; j<state.resampleRatio; j++) {
mixSample(samples++, ((int16_t *)wavBuffer)[i], fade+2-volume);
}
}
}
return samples - buffer->data;
}
}
if (result != FR_OK) {
clear();
}
return 0;
}
#else
int WavContext::mixBuffer(AudioBuffer *buffer, int volume, unsigned int fade)
{
return 0;
}
#endif
const unsigned int toneVolumes[] = { 10, 8, 6, 4, 2 };
inline float evalVolumeRatio(int freq, int volume)
{
float result = toneVolumes[2+volume];
if (freq < 330) {
result = (result * freq * freq) / (330 * 330);
}
return result;
}
int ToneContext::mixBuffer(AudioBuffer * buffer, int volume, unsigned int fade)
{
int duration = 0;
int result = 0;
int remainingDuration = fragment.tone.duration - state.duration;
if (remainingDuration > 0) {
int points;
float toneIdx = state.idx;
if (fragment.tone.reset) {
fragment.tone.reset = 0;
state.duration = 0;
state.pause = 0;
}
if (fragment.tone.freq != state.freq) {
state.freq = fragment.tone.freq;
state.step = limit<float>(1, float(fragment.tone.freq) * (float(DIM(sineValues))/float(AUDIO_SAMPLE_RATE)), 512);
state.volume = 1.0f / evalVolumeRatio(fragment.tone.freq, volume);
}
if (fragment.tone.freqIncr) {
int freqChange = AUDIO_BUFFER_DURATION * fragment.tone.freqIncr;
if (freqChange > 0) {
fragment.tone.freq += freqChange;
if (fragment.tone.freq > BEEP_MAX_FREQ) {
fragment.tone.freq = BEEP_MAX_FREQ;
}
}
else {
if (fragment.tone.freq > BEEP_MIN_FREQ - freqChange) {
fragment.tone.freq += freqChange;
}
else {
fragment.tone.freq = BEEP_MIN_FREQ;
}
}
}
if (remainingDuration > AUDIO_BUFFER_DURATION) {
duration = AUDIO_BUFFER_DURATION;
points = AUDIO_BUFFER_SIZE;
}
else {
duration = remainingDuration;
points = (duration * AUDIO_BUFFER_SIZE) / AUDIO_BUFFER_DURATION;
unsigned int end = toneIdx + (state.step * points);
if (end > DIM(sineValues))
end -= (end % DIM(sineValues));
else
end = DIM(sineValues);
points = (float(end) - toneIdx) / state.step;
}
for (int i=0; i<points; i++) {
int16_t sample = sineValues[int(toneIdx)] * state.volume;
mixSample(&buffer->data[i], sample, fade);
toneIdx += state.step;
if ((unsigned int)toneIdx >= DIM(sineValues))
toneIdx -= DIM(sineValues);
}
if (remainingDuration > AUDIO_BUFFER_DURATION) {
state.duration += AUDIO_BUFFER_DURATION;
state.idx = toneIdx;
return AUDIO_BUFFER_SIZE;
}
else {
state.duration = 32000; // once the tone is finished, it's not possible to update its frequency and duration
}
}
remainingDuration = fragment.tone.pause - state.pause;
if (remainingDuration > 0) {
result = AUDIO_BUFFER_SIZE;
state.pause += min<unsigned int>(AUDIO_BUFFER_DURATION-duration, fragment.tone.pause);
if (fragment.tone.pause > state.pause)
return result;
}
clear();
return result;
}
void AudioQueue::wakeup()
{
DEBUG_TIMER_START(debugTimerAudioConsume);
audioConsumeCurrentBuffer();
DEBUG_TIMER_STOP(debugTimerAudioConsume);
AudioBuffer * buffer;
while ((buffer = buffersFifo.getEmptyBuffer()) != nullptr) {
int result;
unsigned int fade = 0;
int size = 0;
// write silence in the buffer
for (uint32_t i=0; i<AUDIO_BUFFER_SIZE; i++) {
buffer->data[i] = AUDIO_DATA_SILENCE; /* silence */
}
// mix the priority context (only tones)
result = priorityContext.mixBuffer(buffer, g_eeGeneral.beepVolume, fade);
if (result > 0) {
size = result;
fade += 1;
}
// mix the normal context (tones and wavs)
if (normalContext.isEmpty() && !fragmentsFifo.empty()) {
RTOS_LOCK_MUTEX(audioMutex);
normalContext.setFragment(fragmentsFifo.get());
RTOS_UNLOCK_MUTEX(audioMutex);
}
result = normalContext.mixBuffer(buffer, g_eeGeneral.beepVolume, g_eeGeneral.wavVolume, fade);
if (result > 0) {
size = max(size, result);
fade += 1;
}
// mix the vario context
result = varioContext.mixBuffer(buffer, g_eeGeneral.varioVolume, fade);
if (result > 0) {
size = max(size, result);
fade += 1;
}
// mix the background context
if (isFunctionActive(FUNCTION_BACKGND_MUSIC) && !isFunctionActive(FUNCTION_BACKGND_MUSIC_PAUSE)) {
result = backgroundContext.mixBuffer(buffer, g_eeGeneral.backgroundVolume, fade);
if (result > 0) {
size = max(size, result);
}
}
// push the buffer if needed
if (size > 0) {
// TRACE("pushing buffer %p", buffer);
buffer->size = size;
#if defined(SOFTWARE_VOLUME)
if (currentSpeakerVolume > 0) {
for (uint32_t i=0; i<buffer->size; ++i) {
int32_t tmpSample = (int32_t) ((uint32_t) (buffer->data[i]) - AUDIO_DATA_SILENCE); // conversion from uint16_t
buffer->data[i] = (int16_t) (((tmpSample * currentSpeakerVolume) / VOLUME_LEVEL_MAX) + AUDIO_DATA_SILENCE);
}
buffersFifo.audioPushBuffer();
}
else {
break;
}
#else
buffersFifo.audioPushBuffer();
#endif
}
else {
// break the endless loop
break;
}
DEBUG_TIMER_START(debugTimerAudioConsume);
audioConsumeCurrentBuffer();
DEBUG_TIMER_STOP(debugTimerAudioConsume);
}
}
inline unsigned int getToneLength(uint16_t len)
{
unsigned int result = len; // default
if (g_eeGeneral.beepLength < 0) {
result /= (1-g_eeGeneral.beepLength);
}
else if (g_eeGeneral.beepLength > 0) {
result *= (1+g_eeGeneral.beepLength);
}
return result;
}
void AudioQueue::pause(uint16_t len)
{
playTone(0, 0, len);
}
bool AudioQueue::isPlaying(uint8_t id)
{
return normalContext.hasPromptId(id) ||
(isFunctionActive(FUNCTION_BACKGND_MUSIC) && backgroundContext.hasPromptId(id)) ||
fragmentsFifo.hasPromptId(id);
}
void AudioQueue::playTone(uint16_t freq, uint16_t len, uint16_t pause, uint8_t flags, int8_t freqIncr)
{
#if defined(SIMU) && !defined(SIMU_AUDIO)
return;
#endif
RTOS_LOCK_MUTEX(audioMutex);
freq = limit<uint16_t>(BEEP_MIN_FREQ, freq, BEEP_MAX_FREQ);
if (flags & PLAY_BACKGROUND) {
varioContext.setFragment(freq, len, pause, 0, 0, (flags & PLAY_NOW));
}
else {
// adjust frequency and length according to the user preferences
freq += g_eeGeneral.speakerPitch * 15;
len = getToneLength(len);
if (flags & PLAY_NOW) {
if (priorityContext.isFree()) {
priorityContext.clear();
priorityContext.setFragment(freq, len, pause, flags & 0x0f, freqIncr, false);
}
}
else {
fragmentsFifo.push(AudioFragment(freq, len, pause, flags & 0x0f, freqIncr, false));
}
}
RTOS_UNLOCK_MUTEX(audioMutex);
}
#if defined(SDCARD)
void AudioQueue::playFile(const char * filename, uint8_t flags, uint8_t id)
{
#if defined(SIMU)
TRACE("playFile(\"%s\", flags=%x, id=%d)", filename, flags, id);
if (strlen(filename) > AUDIO_FILENAME_MAXLEN) {
TRACE("file name too long! maximum length is %d characters", AUDIO_FILENAME_MAXLEN);
return;
}
#if !defined(SIMU_AUDIO)
return;
#endif
#endif
if (!sdMounted())
return;
if (g_eeGeneral.beepMode == e_mode_quiet)
return;
if (strlen(filename) > AUDIO_FILENAME_MAXLEN) {
POPUP_WARNING(STR_PATH_TOO_LONG);
return;
}
RTOS_LOCK_MUTEX(audioMutex);
if (flags & PLAY_BACKGROUND) {
backgroundContext.clear();
backgroundContext.setFragment(filename, 0, id);
}
else {
fragmentsFifo.push(AudioFragment(filename, flags & 0x0f, id));
}
RTOS_UNLOCK_MUTEX(audioMutex);
}
void AudioQueue::stopPlay(uint8_t id)
{
#if defined(SIMU)
TRACE("stopPlay(id=%d)", id);
#endif
#if defined(SIMU) && !defined(SIMU_AUDIO)
return;
#endif
RTOS_LOCK_MUTEX(audioMutex);
fragmentsFifo.removePromptById(id);
backgroundContext.stop(id);
RTOS_UNLOCK_MUTEX(audioMutex);
}
void AudioQueue::stopSD()
{
sdAvailableSystemAudioFiles.reset();
stopAll();
playTone(0, 0, 100, PLAY_NOW); // insert a 100ms pause
}
#endif
void AudioQueue::stopAll()
{
flush();
RTOS_LOCK_MUTEX(audioMutex);
priorityContext.clear();
normalContext.clear();
RTOS_UNLOCK_MUTEX(audioMutex);
}
void AudioQueue::flush()
{
RTOS_LOCK_MUTEX(audioMutex);
fragmentsFifo.clear();
varioContext.clear();
backgroundContext.clear();
RTOS_UNLOCK_MUTEX(audioMutex);
}
void audioPlay(unsigned int index, uint8_t id)
{
if (g_eeGeneral.beepMode >= -1) {
char filename[AUDIO_FILENAME_MAXLEN+1];
if (isAudioFileReferenced(index, filename)) {
audioQueue.playFile(filename, 0, id);
}
}
}
void audioKeyPress()
{
if (g_eeGeneral.beepMode == e_mode_all) {
audioQueue.playTone(BEEP_DEFAULT_FREQ, 40, 20, PLAY_NOW);
}
#if defined(HAPTIC)
if (g_eeGeneral.hapticMode == e_mode_all) {
haptic.play(5, 0, PLAY_NOW);
}
#endif
}
void audioKeyError()
{
if (g_eeGeneral.beepMode >= e_mode_nokeys) {
audioQueue.playTone(BEEP_DEFAULT_FREQ, 160, 20, PLAY_NOW);
}
#if defined(HAPTIC)
if (g_eeGeneral.hapticMode >= e_mode_nokeys) {
haptic.play(15, 3, PLAY_NOW);
}
#endif
}
void audioTrimPress(int value)
{
if (g_eeGeneral.beepMode >= e_mode_nokeys) {
value = limit(TRIM_MIN, value, TRIM_MAX) * 8 + 120*16;
audioQueue.playTone(value, 40, 20, PLAY_NOW);
}
}
void audioTimerCountdown(uint8_t timer, int value)
{
if (g_model.timers[timer].countdownBeep == COUNTDOWN_VOICE) {
int announceValue = value;
if (g_model.timers[timer].showElapsed) {
announceValue = g_model.timers[timer].start - value;
}