-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilePlayer.c
246 lines (208 loc) · 5.19 KB
/
filePlayer.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
/** @file
* @brief Source file for player of sound files
*/
#include <string.h>
#include "filePlayer.h"
#include "card.h"
#include "player.h"
#include "pcmPlayer.h"
#include "midiPlayer.h"
#include "assert.h"
/** Maximum size of the file extension */
#define MAX_EXT_SIZE 4U
const char rootDir[] ="/";
typedef enum
{
FILE_NOT_SUPPORTED,
FILE_PCM,
FILE_MIDI,
FILE_WAV,
FILE_AMOUNT
} fileType_t;
typedef struct
{
char ext[MAX_EXT_SIZE + 1];
fileType_t type;
} fileExstRow_t;
typedef enum
{
FILE_PLAYER_START,
FILE_PLAYER_PLAY,
FILE_PLAYER_PAUSE,
FILE_PLAYER_STOP,
FILE_PLAYER_NO_DIR,
} filePlayer_State_t;
typedef struct
{
DIR dir;
FILINFO fno_temp;
filePlayer_State_t state;
} filePlayer_t;
static const fileExstRow_t extDB[] =
{
{ .ext = ".WAV", .type = FILE_WAV, },
{ .ext = ".PCM", .type = FILE_PCM, },
{ .ext = ".MID", .type = FILE_MIDI,},
};
static const uint32_t extDB_size = sizeof(extDB) / sizeof(fileExstRow_t); /* Will be calculated at compile time */
static bool filePlayer_Init(void);
static bool filePlayer_GetNextFile(void);
static fileType_t filePlayer_GetFileType(void);
static filePlayer_t filePlayer = {.state = FILE_PLAYER_START,};
void filePlayer_Pause(void)
{
if (FILE_PLAYER_PLAY == filePlayer.state)
{
player_Pause();
filePlayer.state = FILE_PLAYER_PAUSE;
}
}
void filePlayer_Stop(void)
{
if (FILE_PLAYER_START != filePlayer.state)
{
filePlayer.state = FILE_PLAYER_STOP;
player_Stop();
}
}
void filePlayer_Play(void)
{
if (FILE_PLAYER_PAUSE == filePlayer.state)
{
filePlayer.state = FILE_PLAYER_PLAY;
player_Play();
} else
{
filePlayer_Next();
}
}
bool filePlayer_isPlay(void)
{
return (filePlayer.state == FILE_PLAYER_PLAY);
}
void filePlayer_Next(void)
{
fileType_t fileType;
bool ret = false;
if (FILE_PLAYER_START != filePlayer.state)
{
filePlayer_Stop();
do
{
ret = filePlayer_GetNextFile();
if (!ret)
{
uartPrintf("FPLAYER: No files, rewind to the begining\r\n");
filePlayer.state = FILE_PLAYER_START;
break;
}
fileType = filePlayer_GetFileType();
uartPrintf("FPLAYER: %s Type: %d\r\n", filePlayer.fno_temp.fname, fileType);
switch(fileType)
{
case FILE_WAV:
ret = pcmPlayerPlayWav(filePlayer.fno_temp.fname);
break;
case FILE_PCM:
audio_SetSampleRate(AUDIO_SAMPLE_RATE_32K);
ret = pcmPlayerPlay(filePlayer.fno_temp.fname);
break;
case FILE_MIDI:
ret = midiPlayer_Play(filePlayer.fno_temp.fname);
break;
default:
ret = false;
break;
}
} while(!ret);
if (ret)
{
filePlayer.state = FILE_PLAYER_PLAY;
}
}
}
void filePlayer_Run(void)
{
player_Run();
switch (filePlayer.state)
{
case FILE_PLAYER_START:
if (filePlayer_Init())
{
uartPrintf("FPLAYER: Started!\r\n");
filePlayer.state = FILE_PLAYER_STOP;
}
break;
case FILE_PLAYER_STOP:
case FILE_PLAYER_PAUSE:
break;
case FILE_PLAYER_PLAY:
if (!player_IsPlaying())
{
filePlayer_Next();
}
break;
default:
uartPrintf("FPLAYER: Wrong state %d\r\n", filePlayer.state);
ASSERT(false);
break;
}
}
static fileType_t filePlayer_GetFileType(void)
{
uint32_t i;
fileType_t ret = FILE_NOT_SUPPORTED;
for (i = 0; i < extDB_size; i++)
{
if (NULL != strstr(filePlayer.fno_temp.fname, extDB[i].ext))
{
ret = extDB[i].type;
break;
}
}
return ret;
}
static bool filePlayer_GetNextFile(void)
{
FRESULT res;
bool ret = false;
do
{
res = f_readdir(&filePlayer.dir, &filePlayer.fno_temp);
if (res != FR_OK)
{
uartPrintf("FPLAYER: Error readdir\r\n");
break;
}
if (0 == filePlayer.fno_temp.fname[0])
{
uartPrintf("FPLAYER: Dir end, restart!\r\n");
break;
} else
{
if (!(filePlayer.fno_temp.fattrib & AM_DIR))
{
ret = true;
break;
}
}
} while (true);
if (FR_OK != res)
{
cardErrorPrint(res);
}
return ret;
}
static bool filePlayer_Init(void)
{
bool res = false;
FRESULT resF = f_opendir(&filePlayer.dir, rootDir);
if (resF == FR_OK) {
res = true;
} else
{
uartPrintf("FPLAYER: Error openDir\r\n");
cardErrorPrint(resF);
}
return res;
}