forked from pdrbsts/uv-k5-firmware-custom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.c
508 lines (416 loc) · 11.1 KB
/
audio.c
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
/* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef ENABLE_AIRCOPY
#include "app/aircopy.h"
#endif
#ifdef ENABLE_FMRADIO
#include "app/fm.h"
#endif
#include "audio.h"
#include "bsp/dp32g030/gpio.h"
#ifdef ENABLE_FMRADIO
#include "driver/bk1080.h"
#endif
#include "driver/bk4819.h"
#include "driver/gpio.h"
#include "driver/system.h"
#include "driver/systick.h"
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
#include "driver/uart.h"
#endif
#include "functions.h"
#include "misc.h"
#include "radio.h"
#include "settings.h"
#include "ui/ui.h"
#ifdef ENABLE_VOICE
static const uint8_t VoiceClipLengthChinese[58] =
{
0x32, 0x32, 0x32, 0x37, 0x37, 0x32, 0x32, 0x32,
0x32, 0x37, 0x37, 0x32, 0x64, 0x64, 0x64, 0x64,
0x64, 0x69, 0x64, 0x69, 0x5A, 0x5F, 0x5F, 0x64,
0x64, 0x69, 0x64, 0x64, 0x69, 0x69, 0x69, 0x64,
0x64, 0x6E, 0x69, 0x5F, 0x64, 0x64, 0x64, 0x69,
0x69, 0x69, 0x64, 0x69, 0x64, 0x64, 0x55, 0x5F,
0x5A, 0x4B, 0x4B, 0x46, 0x46, 0x69, 0x64, 0x6E,
0x5A, 0x64,
};
static const uint8_t VoiceClipLengthEnglish[76] =
{
0x50, 0x32, 0x2D, 0x2D, 0x2D, 0x37, 0x37, 0x37,
0x32, 0x32, 0x3C, 0x37, 0x46, 0x46, 0x4B, 0x82,
0x82, 0x6E, 0x82, 0x46, 0x96, 0x64, 0x46, 0x6E,
0x78, 0x6E, 0x87, 0x64, 0x96, 0x96, 0x46, 0x9B,
0x91, 0x82, 0x82, 0x73, 0x78, 0x64, 0x82, 0x6E,
0x78, 0x82, 0x87, 0x6E, 0x55, 0x78, 0x64, 0x69,
0x9B, 0x5A, 0x50, 0x3C, 0x32, 0x55, 0x64, 0x64,
0x50, 0x46, 0x46, 0x46, 0x4B, 0x4B, 0x50, 0x50,
0x55, 0x4B, 0x4B, 0x32, 0x32, 0x32, 0x32, 0x37,
0x41, 0x32, 0x3C, 0x37,
};
voice_id_t g_voice_id[8];
uint8_t g_voice_read_index;
uint8_t g_voice_write_index;
volatile uint16_t g_play_next_voice_tick_10ms;
volatile bool g_flag_play_queued_voice;
voice_id_t g_another_voice_id = VOICE_ID_INVALID;
#endif
beep_type_t g_beep_to_play = BEEP_NONE;
void AUDIO_set_mod_mode(const mod_mode_t mode)
{
BK4819_af_type_t af_mode;
switch (mode)
{
default:
case MOD_MODE_FM: af_mode = BK4819_AF_FM; break;
case MOD_MODE_AM: af_mode = BK4819_AF_AM; break;
case MOD_MODE_DSB: af_mode = BK4819_AF_BASEBAND1; break;
}
BK4819_SetAF(af_mode);
}
void AUDIO_PlayBeep(beep_type_t Beep)
{
const uint16_t tone_val = BK4819_read_reg(0x71);
// const uint16_t af_val = BK4819_read_reg(0x47);
uint16_t ToneFrequency;
uint16_t Duration;
if (g_eeprom.config.setting.beep_control == 0)
{ // beep not enabled
if (Beep != BEEP_880HZ_60MS_TRIPLE_BEEP &&
Beep != BEEP_500HZ_60MS_DOUBLE_BEEP &&
Beep != BEEP_440HZ_500MS &&
Beep != BEEP_880HZ_200MS &&
Beep != BEEP_880HZ_500MS)
{
return;
}
}
if (g_flash_light_state == FLASHLIGHT_SOS ||
g_current_function == FUNCTION_RECEIVE ||
g_monitor_enabled ||
g_squelch_open ||
GPIO_CheckBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER))
{
return;
}
#ifdef ENABLE_AIRCOPY
// if (g_current_display_screen == DISPLAY_AIRCOPY || g_aircopy_state != AIRCOPY_READY)
// return;
#endif
// GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
if (g_current_function == FUNCTION_POWER_SAVE && g_rx_idle_mode)
BK4819_RX_TurnOn();
#ifdef ENABLE_FMRADIO
#ifdef MUTE_AUDIO_FOR_VOICE
if (g_fm_radio_mode)
BK1080_Mute(true);
#endif
#endif
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
// UART_printf("beep %u\r\n", (unsigned int)Beep);
#endif
// whats this for ?
SYSTEM_DelayMs(20);
// SYSTEM_DelayMs(2);
switch (Beep)
{
default:
case BEEP_NONE:
ToneFrequency = 220;
break;
case BEEP_1KHZ_60MS_OPTIONAL:
ToneFrequency = 1000;
break;
case BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL:
case BEEP_500HZ_60MS_DOUBLE_BEEP:
ToneFrequency = 500;
break;
case BEEP_440HZ_40MS_OPTIONAL:
case BEEP_440HZ_500MS:
ToneFrequency = 440;
break;
case BEEP_880HZ_40MS_OPTIONAL:
case BEEP_880HZ_60MS_TRIPLE_BEEP:
case BEEP_880HZ_200MS:
case BEEP_880HZ_500MS:
ToneFrequency = 880;
break;
}
BK4819_start_tone(ToneFrequency, 10, false, true);
SYSTEM_DelayMs(2);
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
SYSTEM_DelayMs(60);
switch (Beep)
{
case BEEP_880HZ_60MS_TRIPLE_BEEP:
BK4819_ExitTxMute();
SYSTEM_DelayMs(60);
BK4819_EnterTxMute();
SYSTEM_DelayMs(20);
// Fallthrough
case BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL:
case BEEP_500HZ_60MS_DOUBLE_BEEP:
BK4819_ExitTxMute();
SYSTEM_DelayMs(60);
BK4819_EnterTxMute();
SYSTEM_DelayMs(20);
// Fallthrough
case BEEP_1KHZ_60MS_OPTIONAL:
BK4819_ExitTxMute();
Duration = 60;
break;
case BEEP_880HZ_40MS_OPTIONAL:
case BEEP_440HZ_40MS_OPTIONAL:
BK4819_ExitTxMute();
Duration = 40;
break;
case BEEP_880HZ_200MS:
BK4819_ExitTxMute();
Duration = 200;
break;
case BEEP_440HZ_500MS:
case BEEP_880HZ_500MS:
default:
BK4819_ExitTxMute();
Duration = 500;
break;
}
SYSTEM_DelayMs(Duration);
BK4819_EnterTxMute();
SYSTEM_DelayMs(2);
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
#ifdef ENABLE_VOX
g_vox_resume_tick_10ms = 80; // 800ms
#endif
SYSTEM_DelayMs(2);
BK4819_TurnsOffTones_TurnsOnRX();
SYSTEM_DelayMs(2);
BK4819_write_reg(0x71, tone_val);
#ifdef ENABLE_FMRADIO
if (g_fm_radio_mode)
BK1080_Mute(false);
#endif
if (g_current_function == FUNCTION_POWER_SAVE && g_rx_idle_mode)
{
BK4819_Sleep();
}
else
if (g_squelch_open || g_monitor_enabled)
{
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
}
}
#ifdef ENABLE_VOICE
void AUDIO_PlayVoice(uint8_t VoiceID)
{
unsigned int i;
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_OFF)
return;
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_VOICE_0);
SYSTEM_DelayMs(20);
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_VOICE_0);
for (i = 0; i < 8; i++)
{
if ((VoiceID & 0x80U) == 0)
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_VOICE_1);
else
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_VOICE_1);
SYSTICK_Delay250us(4000);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_VOICE_0);
SYSTICK_Delay250us(4800);
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_VOICE_0);
VoiceID <<= 1;
SYSTICK_Delay250us(800);
}
}
void AUDIO_PlaySingleVoice(bool flag)
{
uint8_t Delay;
uint8_t VoiceID = g_voice_id[0];
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_OFF || g_voice_write_index == 0)
goto Bailout;
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_CHINESE)
{ // Chinese
if (VoiceID >= ARRAY_SIZE(VoiceClipLengthChinese))
goto Bailout;
Delay = VoiceClipLengthChinese[VoiceID];
VoiceID += VOICE_ID_CHI_BASE;
}
else
{ // English
if (VoiceID >= ARRAY_SIZE(VoiceClipLengthEnglish))
goto Bailout;
Delay = VoiceClipLengthEnglish[VoiceID];
VoiceID += VOICE_ID_ENG_BASE;
}
#ifdef MUTE_AUDIO_FOR_VOICE
if (g_current_function == FUNCTION_RECEIVE)
BK4819_SetAF(BK4819_AF_MUTE);
#endif
#ifdef ENABLE_FMRADIO
#ifdef MUTE_AUDIO_FOR_VOICE
if (g_fm_radio_mode)
BK1080_Mute(true);
#endif
#endif
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
#ifdef ENABLE_VOX
g_vox_resume_tick_10ms = 2000;
#endif
SYSTEM_DelayMs(5);
AUDIO_PlayVoice(VoiceID);
if (g_voice_write_index == 1)
Delay += 3;
if (flag)
{
SYSTEM_DelayMs(Delay * 10);
if (g_current_function == FUNCTION_RECEIVE)
AUDIO_set_mod_mode(g_rx_vfo->channel.mod_mode);
#ifdef ENABLE_FMRADIO
if (g_fm_radio_mode)
BK1080_Mute(false);
#endif
if (!g_squelch_open && !g_monitor_enabled)
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
g_voice_write_index = 0;
g_voice_read_index = 0;
#ifdef ENABLE_VOX
g_vox_resume_tick_10ms = 80;
#endif
return;
}
g_voice_read_index = 1;
g_play_next_voice_tick_10ms = Delay;
g_flag_play_queued_voice = false;
return;
Bailout:
g_voice_read_index = 0;
g_voice_write_index = 0;
}
void AUDIO_SetVoiceID(uint8_t Index, voice_id_t VoiceID)
{
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_OFF || Index == 0)
{
g_voice_write_index = 0;
g_voice_read_index = 0;
}
if (g_eeprom.config.setting.voice_prompt != VOICE_PROMPT_OFF && Index < ARRAY_SIZE(g_voice_id))
{
g_voice_id[Index] = VoiceID;
g_voice_write_index++;
}
}
uint8_t AUDIO_SetDigitVoice(uint8_t Index, uint16_t Value)
{
uint16_t Remainder;
uint8_t Result;
uint8_t Count;
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_OFF || Index == 0)
{
g_voice_write_index = 0;
g_voice_read_index = 0;
}
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_OFF)
return 0;
Count = 0;
Result = Value / 1000U;
Remainder = Value % 1000U;
if (Remainder < 100U)
{
if (Remainder < 10U)
goto Skip;
}
else
{
Result = Remainder / 100U;
g_voice_id[g_voice_write_index++] = (voice_id_t)Result;
Count++;
Remainder -= Result * 100U;
}
Result = Remainder / 10U;
g_voice_id[g_voice_write_index++] = (voice_id_t)Result;
Count++;
Remainder -= Result * 10U;
Skip:
g_voice_id[g_voice_write_index++] = (voice_id_t)Remainder;
return Count + 1U;
}
void AUDIO_PlayQueuedVoice(void)
{
uint8_t VoiceID;
uint8_t Delay;
bool Skip = false;
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_OFF)
{
g_voice_write_index = 0;
g_voice_read_index = 0;
return;
}
if (g_voice_read_index != g_voice_write_index && g_eeprom.config.setting.voice_prompt != VOICE_PROMPT_OFF)
{
VoiceID = g_voice_id[g_voice_read_index];
if (g_eeprom.config.setting.voice_prompt == VOICE_PROMPT_CHINESE)
{
if (VoiceID < ARRAY_SIZE(VoiceClipLengthChinese))
{
Delay = VoiceClipLengthChinese[VoiceID];
VoiceID += VOICE_ID_CHI_BASE;
}
else
Skip = true;
}
else
{
if (VoiceID < ARRAY_SIZE(VoiceClipLengthEnglish))
{
Delay = VoiceClipLengthEnglish[VoiceID];
VoiceID += VOICE_ID_ENG_BASE;
}
else
Skip = true;
}
g_voice_read_index++;
if (!Skip)
{
if (g_voice_read_index == g_voice_write_index)
Delay += 3;
AUDIO_PlayVoice(VoiceID);
g_play_next_voice_tick_10ms = Delay;
g_flag_play_queued_voice = false;
#ifdef ENABLE_VOX
g_vox_resume_tick_10ms = 2000;
#endif
return;
}
}
// ***********************
// unmute the radios audio
if (g_current_function == FUNCTION_RECEIVE)
AUDIO_set_mod_mode(g_rx_vfo->channel.mod_mode);
#ifdef ENABLE_FMRADIO
if (g_fm_radio_mode)
BK1080_Mute(false);
#endif
if (!g_squelch_open && !g_monitor_enabled)
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
// **********************
#ifdef ENABLE_VOX
g_vox_resume_tick_10ms = 80;
#endif
g_voice_write_index = 0;
g_voice_read_index = 0;
}
#endif