-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathextended_audio.c
More file actions
257 lines (232 loc) · 7.47 KB
/
extended_audio.c
File metadata and controls
257 lines (232 loc) · 7.47 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
/*
This project is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Deviation 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.
You should have received a copy of the GNU General Public License
along with Deviation. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "music.h"
#include "config/tx.h"
#include "config/model.h"
#include "config/voice.h"
#include "extended_audio.h"
#include "stdlib.h"
#ifndef EMULATOR
#include <libopencm3/stm32/usart.h>
#endif // EMULATOR
#if HAS_EXTENDED_AUDIO
struct QueueEntry current_voice_mapping;
struct QueueEntry audio_queue[AUDIO_QUEUE_LENGTH];
u8 next_audio;
u8 num_audio;
u32 audio_queue_time;
#ifndef EMULATOR
static u8 player_buffer[] = {0x7E, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF};
#endif
// Initialize UART for extended-audio
void AUDIO_Init() {
if (Transmitter.audio_player == AUDIO_DISABLED) {
// Reload hardware.ini if audio_player had been temporarily disabled
CONFIG_LoadHardware();
}
printf("Voice: Initializing UART for extended-audio\n");
#if HAS_AUDIO_UART
if (Transmitter.audio_uart) {
printf("Voice: UART5 already initialized\n");
return;
}
else
#endif
{
#if defined BUILDTYPE_DEV
printf("Voice: Dev mode enabled, no audio output\n");
return;
#endif
}
#ifndef _DEVO12_TARGET_H_
if ( PPMin_Mode() || Model.protocol == PROTOCOL_PPM ) {
printf("Voice: Cannot initialize UART for extended-audio, PPM in use\n");
UART_SetDataRate(0);
}
else {
#endif // _DEVO12_TARGET_H_
printf("Voice: Setting up UART for extended-audio\n");
UART_SetDataRate(9600);
#ifndef _DEVO12_TARGET_H_
}
#endif // _DEVO12_TARGET_H_
AUDIO_SetVolume();
}
#ifndef EMULATOR
// Send a block of len bytes to the Audio device.
static void AUDIO_Send(u8 *data, int len) {
void AUDIO_send_char(char c);
for (u16 iter = 0; iter < len; iter += 1) {
AUDIO_send_char(data[iter]);
}
}
// Send a string to the Audio device.
static void AUDIO_Print(char *string) {
AUDIO_Send((u8 *)string, strlen(string));
}
#endif // EMULATOR
static void u16ToArray(u16 value, u8 *array) {
*array = (u8)(value>>8);
*(array+1) = (u8)value;
}
// generate Checksum for DFPlyer commands
static u16 AUDIO_CalculateChecksum(u8 *buffer) {
u16 sum = 0;
for (int i=1; i < 7; i += 1)
sum += buffer[i];
return -sum;
}
// Generate a string to play.
static int AUDIO_Play(u16 id) {
// If we are just playing beeps....
if (id == MUSIC_KEY_PRESSING || id == MUSIC_MAXLEN) {
printf("Voice: beep only\n");
return 0;
}
printf("Voice: Playing mp3 #%d\n", id);
#ifdef EMULATOR // On emulators call mpg123 to play mp3s
char cmd[70];
u16 vol_val = Transmitter.audio_vol * 32786/10;
#ifdef _WIN32
snprintf(cmd, sizeof(cmd), "start /B ..\\..\\mpg123 -f %d -q ..\\..\\mp3\\%04d*.mp3 > nul 2>&1", vol_val, id);
#else
snprintf(cmd, sizeof(cmd), "mpg123 -f %d -q ../../mp3/%04d*.mp3 > /dev/null 2>&1 &", vol_val, id);
#endif // _WIN32
system(cmd);
return 1;
#endif // EMULATOR
#ifndef EMULATOR
switch (Transmitter.audio_player) {
case AUDIO_DISABLED:
case AUDIO_LAST: // Sigh. Shut up the warnings
case AUDIO_NONE: return 0; // Play beeps...
case AUDIO_AUDIOFX: {
char buffer[5];
snprintf(buffer, sizeof(buffer), "#%d\n", id);
AUDIO_Print(buffer);
break;
}
case AUDIO_DF_PLAYER:
// Fill in track number and checksum
player_buffer[3] = 0x12;
u16ToArray(id, player_buffer+5);
u16ToArray(AUDIO_CalculateChecksum(player_buffer), player_buffer+7);
AUDIO_Send(player_buffer, sizeof(player_buffer));
break;
}
return 1;
#endif // EMULATOR
}
void AUDIO_SetVolume() {
#if defined BUILDTYPE_DEV
#if HAS_AUDIO_UART
if (!Transmitter.audio_uart)
#endif
{
printf("Voice: Dev mode enabled, cannot set volume\n");
return;
}
#endif // BUILDTYPE_DEV
#ifndef _DEVO12_TARGET_H_
#if HAS_AUDIO_UART
if ( !Transmitter.audio_uart && (PPMin_Mode() || Model.protocol == PROTOCOL_PPM) ) { // don't send volume command when using PPM port
#else
if ( PPMin_Mode() || Model.protocol == PROTOCOL_PPM ) { // don't send volume command when using PPM port
#endif
printf("Voice: PPM port in use, cannot set volume\n");
return;
}
#endif //_DEVO12_TARGET_H_
printf("Voice: Setting external audio volume to %d\n", Transmitter.audio_vol);
#ifndef EMULATOR
switch (Transmitter.audio_player) {
case AUDIO_DISABLED:
case AUDIO_LAST: // Sigh. Shut up the warnings
case AUDIO_AUDIOFX: // AUDIOFX only allows up down selection of volume...not implemented
case AUDIO_NONE:
break; // Play beeps...
case AUDIO_DF_PLAYER:
// Fill in volume and checksum
player_buffer[3] = 0x06;
u16ToArray(Transmitter.audio_vol * 3, player_buffer+5); // DFPlayer has volume range 0-30
u16ToArray(AUDIO_CalculateChecksum(player_buffer), player_buffer+7);
AUDIO_Send(player_buffer, sizeof(player_buffer));
break;
}
#endif
}
static void AUDIO_ResetQueue() {
num_audio = 0;
next_audio = 0;
printf("Voice: Resetting queue.\n");
}
void AUDIO_CheckQueue() {
u32 t = CLOCK_getms();
if (next_audio < num_audio) {
if (t > audio_queue_time) {
AUDIO_Play(audio_queue[next_audio].id);
audio_queue_time = CLOCK_getms() + audio_queue[next_audio].duration;
next_audio++;
}
} else if (num_audio && t > audio_queue_time) {
printf("Voice: Queue finished.\n");
AUDIO_ResetQueue();
AUDIO_SetVolume();
}
}
static int AUDIO_VoiceAvailable() {
#if defined BUILDTYPE_DEV
#if HAS_AUDIO_UART
if (!Transmitter.audio_uart)
#endif
{
printf("Voice: Dev mode enabled, cannot set volume\n");
AUDIO_ResetQueue();
return 0;
}
#endif // BUILDTYPE_DEV
#ifndef _DEVO12_TARGET_H_
#if HAS_AUDIO_UART
if ( !Transmitter.audio_uart && (PPMin_Mode() || Model.protocol == PROTOCOL_PPM) ) { // don't send play command when using PPM port
#else
if ( PPMin_Mode() || Model.protocol == PROTOCOL_PPM ) { // don't send play command when using PPM port
#endif
printf("Voice: PPM port in use\n");
AUDIO_ResetQueue();
return 0;
}
#endif // _DEVO12_TARGET_H_
if ( (Transmitter.audio_player == AUDIO_NONE) || (Transmitter.audio_player == AUDIO_DISABLED) || !Transmitter.audio_vol ) {
AUDIO_ResetQueue();
return 0;
}
return 1;
}
int AUDIO_AddQueue(u16 music) {
if (!AUDIO_VoiceAvailable() || num_audio == AUDIO_QUEUE_LENGTH) {
printf("Voice: queue full or voice not available, cannot add new mp3 #%d\n", music);
return 0;
}
CONFIG_VoiceParse(music);
if (!current_voice_mapping.duration) {
printf("Voice: mp3 length is zero\n");
return 0;
}
audio_queue[num_audio].duration = current_voice_mapping.duration;
audio_queue[num_audio].id = music;
printf("Voice: added ID %d with duration %d to queue position %d.\n", music, current_voice_mapping.duration, num_audio);
num_audio++;
return 1;
}
#endif